blob: 7bcfa9578a73d8e1b2644504e8fa87520ac677ff [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 Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020039#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020040#include <haproxy/server-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020042#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020043#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020044#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020045#include <haproxy/task.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020046#include <haproxy/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreau918ff602011-07-25 16:33:49 +020049int listeners; /* # of proxy listeners, set by cfgparse */
Olivier Houchardfbc74e82017-11-24 16:54:05 +010050struct proxy *proxies_list = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020051struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010052struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010053unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020054
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010055/* proxy->options */
56const struct cfg_opt cfg_opts[] =
57{
58 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0, 0 },
59 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0, 0 },
60 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0, PR_MODE_HTTP },
61 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0, 0 },
62 { "contstats", PR_O_CONTSTATS, PR_CAP_FE, 0, 0 },
63 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0, 0 },
64 { "http_proxy", PR_O_HTTP_PROXY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
65 { "http-buffer-request", PR_O_WREQ_BODY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
66 { "http-ignore-probes", PR_O_IGNORE_PRB, PR_CAP_FE, 0, PR_MODE_HTTP },
67 { "prefer-last-server", PR_O_PREF_LAST, PR_CAP_BE, 0, PR_MODE_HTTP },
68 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0, 0 },
69 { "nolinger", PR_O_TCP_NOLING, PR_CAP_FE | PR_CAP_BE, 0, 0 },
70 { "persist", PR_O_PERSIST, PR_CAP_BE, 0, 0 },
71 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0, 0 },
Willy Tarreaue5733232019-05-22 19:24:06 +020072#ifdef USE_TPROXY
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010073 { "transparent", PR_O_TRANSP, PR_CAP_BE, 0, 0 },
74#else
75 { "transparent", 0, 0, 0, 0 },
76#endif
77
78 { NULL, 0, 0, 0, 0 }
79};
80
81/* proxy->options2 */
82const struct cfg_opt cfg_opts2[] =
83{
Willy Tarreaue5733232019-05-22 19:24:06 +020084#ifdef USE_LINUX_SPLICE
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010085 { "splice-request", PR_O2_SPLIC_REQ, PR_CAP_FE|PR_CAP_BE, 0, 0 },
86 { "splice-response", PR_O2_SPLIC_RTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
87 { "splice-auto", PR_O2_SPLIC_AUT, PR_CAP_FE|PR_CAP_BE, 0, 0 },
88#else
89 { "splice-request", 0, 0, 0, 0 },
90 { "splice-response", 0, 0, 0, 0 },
91 { "splice-auto", 0, 0, 0, 0 },
92#endif
93 { "accept-invalid-http-request", PR_O2_REQBUG_OK, PR_CAP_FE, 0, PR_MODE_HTTP },
94 { "accept-invalid-http-response", PR_O2_RSPBUG_OK, PR_CAP_BE, 0, PR_MODE_HTTP },
95 { "dontlog-normal", PR_O2_NOLOGNORM, PR_CAP_FE, 0, 0 },
96 { "log-separate-errors", PR_O2_LOGERRORS, PR_CAP_FE, 0, 0 },
97 { "log-health-checks", PR_O2_LOGHCHKS, PR_CAP_BE, 0, 0 },
98 { "socket-stats", PR_O2_SOCKSTAT, PR_CAP_FE, 0, 0 },
99 { "tcp-smart-accept", PR_O2_SMARTACC, PR_CAP_FE, 0, 0 },
100 { "tcp-smart-connect", PR_O2_SMARTCON, PR_CAP_BE, 0, 0 },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100101 { "independent-streams", PR_O2_INDEPSTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
102 { "http-use-proxy-header", PR_O2_USE_PXHDR, PR_CAP_FE, 0, PR_MODE_HTTP },
103 { "http-pretend-keepalive", PR_O2_FAKE_KA, PR_CAP_BE, 0, PR_MODE_HTTP },
104 { "http-no-delay", PR_O2_NODELAY, PR_CAP_FE|PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet31930372019-07-15 10:16:58 +0200105 { "http-use-htx", 0, PR_CAP_FE|PR_CAP_BE, 0, 0 }, // deprecated
Christopher Faulet98fbe952019-07-22 16:18:24 +0200106
107 {"h1-case-adjust-bogus-client", PR_O2_H1_ADJ_BUGCLI, PR_CAP_FE, 0, PR_MODE_HTTP },
108 {"h1-case-adjust-bogus-server", PR_O2_H1_ADJ_BUGSRV, PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet89aed322020-06-02 17:33:56 +0200109 {"disable-h2-upgrade", PR_O2_NO_H2_UPGRADE, PR_CAP_FE, 0, PR_MODE_HTTP },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100110 { NULL, 0, 0, 0 }
111};
112
Willy Tarreau977b8e42006-12-29 14:19:17 +0100113/*
Willy Tarreau816eb542007-11-04 07:04:43 +0100114 * This function returns a string containing a name describing capabilities to
115 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +0200116 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +0100117 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +0100118 */
Willy Tarreau816eb542007-11-04 07:04:43 +0100119const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +0100120{
Willy Tarreau816eb542007-11-04 07:04:43 +0100121 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
122 if (cap & PR_CAP_FE)
123 return "frontend";
124 else if (cap & PR_CAP_BE)
125 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +0100126 }
127 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +0100128}
129
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100130/*
131 * This function returns a string containing the mode of the proxy in a format
132 * suitable for error messages.
133 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100134const char *proxy_mode_str(int mode) {
135
136 if (mode == PR_MODE_TCP)
137 return "tcp";
138 else if (mode == PR_MODE_HTTP)
139 return "http";
140 else if (mode == PR_MODE_HEALTH)
141 return "health";
William Lallemandcf62f7e2018-10-26 14:47:40 +0200142 else if (mode == PR_MODE_CLI)
143 return "cli";
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100144 else
145 return "unknown";
146}
147
Willy Tarreauf3950172009-10-10 18:35:51 +0200148/*
149 * This function scans the list of backends and servers to retrieve the first
150 * backend and the first server with the given names, and sets them in both
151 * parameters. It returns zero if either is not found, or non-zero and sets
152 * the ones it did not found to NULL. If a NULL pointer is passed for the
153 * backend, only the pointer to the server will be updated.
154 */
155int get_backend_server(const char *bk_name, const char *sv_name,
156 struct proxy **bk, struct server **sv)
157{
158 struct proxy *p;
159 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100160 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200161
162 *sv = NULL;
163
Willy Tarreau050536d2012-10-04 08:47:34 +0200164 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200165 if (*sv_name == '#')
166 sid = atoi(sv_name + 1);
167
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200168 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200169 if (bk)
170 *bk = p;
171 if (!p)
172 return 0;
173
174 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100175 if ((sid >= 0 && s->puid == sid) ||
176 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200177 break;
178 *sv = s;
179 if (!s)
180 return 0;
181 return 1;
182}
183
Willy Tarreaue219db72007-12-03 01:30:13 +0100184/* This function parses a "timeout" statement in a proxy section. It returns
185 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200186 * return zero, it will write an error or warning message into a preallocated
187 * buffer returned at <err>. The trailing is not be written. The function must
188 * be called with <args> pointing to the first command line word, with <proxy>
189 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
190 * As a special case for compatibility with older configs, it also accepts
191 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100192 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200193static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200194 struct proxy *defpx, const char *file, int line,
195 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100196{
197 unsigned timeout;
198 int retval, cap;
199 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200200 int *tv = NULL;
201 int *td = NULL;
Willy Tarreaue219db72007-12-03 01:30:13 +0100202
203 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200204
205 /* simply skip "timeout" but remain compatible with old form */
206 if (strcmp(args[0], "timeout") == 0)
207 args++;
208
Willy Tarreaue219db72007-12-03 01:30:13 +0100209 name = args[0];
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200210 if (!strcmp(args[0], "client")) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100211 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100212 tv = &proxy->timeout.client;
213 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100214 cap = PR_CAP_FE;
215 } else if (!strcmp(args[0], "tarpit")) {
216 tv = &proxy->timeout.tarpit;
217 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100218 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreaub16a5742010-01-10 14:46:16 +0100219 } else if (!strcmp(args[0], "http-keep-alive")) {
220 tv = &proxy->timeout.httpka;
221 td = &defpx->timeout.httpka;
222 cap = PR_CAP_FE | PR_CAP_BE;
Willy Tarreau036fae02008-01-06 13:24:40 +0100223 } else if (!strcmp(args[0], "http-request")) {
224 tv = &proxy->timeout.httpreq;
225 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200226 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200227 } else if (!strcmp(args[0], "server")) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100228 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100229 tv = &proxy->timeout.server;
230 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100231 cap = PR_CAP_BE;
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200232 } else if (!strcmp(args[0], "connect")) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100233 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100234 tv = &proxy->timeout.connect;
235 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100236 cap = PR_CAP_BE;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100237 } else if (!strcmp(args[0], "check")) {
238 tv = &proxy->timeout.check;
239 td = &defpx->timeout.check;
240 cap = PR_CAP_BE;
Willy Tarreaue219db72007-12-03 01:30:13 +0100241 } else if (!strcmp(args[0], "queue")) {
242 tv = &proxy->timeout.queue;
243 td = &defpx->timeout.queue;
244 cap = PR_CAP_BE;
Willy Tarreauce887fd2012-05-12 12:50:00 +0200245 } else if (!strcmp(args[0], "tunnel")) {
246 tv = &proxy->timeout.tunnel;
247 td = &defpx->timeout.tunnel;
248 cap = PR_CAP_BE;
Willy Tarreau05cdd962014-05-10 14:30:07 +0200249 } else if (!strcmp(args[0], "client-fin")) {
250 tv = &proxy->timeout.clientfin;
251 td = &defpx->timeout.clientfin;
252 cap = PR_CAP_FE;
253 } else if (!strcmp(args[0], "server-fin")) {
254 tv = &proxy->timeout.serverfin;
255 td = &defpx->timeout.serverfin;
256 cap = PR_CAP_BE;
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200257 } else if (!strcmp(args[0], "clitimeout")) {
258 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout client'.", args[0]);
259 return -1;
260 } else if (!strcmp(args[0], "srvtimeout")) {
261 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout server'.", args[0]);
262 return -1;
263 } else if (!strcmp(args[0], "contimeout")) {
264 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout connect'.", args[0]);
265 return -1;
Willy Tarreaue219db72007-12-03 01:30:13 +0100266 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200267 memprintf(err,
268 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200269 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
270 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200271 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100272 return -1;
273 }
274
275 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200276 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100277 return -1;
278 }
279
280 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200281 if (res == PARSE_TIME_OVER) {
282 memprintf(err, "timer overflow in argument '%s' to 'timeout %s' (maximum value is 2147483647 ms or ~24.8 days)",
283 args[1], name);
284 return -1;
285 }
286 else if (res == PARSE_TIME_UNDER) {
287 memprintf(err, "timer underflow in argument '%s' to 'timeout %s' (minimum non-null value is 1 ms)",
288 args[1], name);
289 return -1;
290 }
291 else if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200292 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100293 return -1;
294 }
295
296 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200297 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
298 name, proxy_type_str(proxy), proxy->id,
299 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100300 retval = 1;
301 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200302 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200303 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100304 retval = 1;
305 }
306
Willy Tarreaufac5b592014-05-22 08:24:46 +0200307 if (*args[2] != 0) {
308 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
309 retval = -1;
310 }
311
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200312 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100313 return retval;
314}
315
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100316/* This function parses a "rate-limit" statement in a proxy section. It returns
317 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200318 * return zero, it will write an error or warning message into a preallocated
319 * buffer returned at <err>. The function must be called with <args> pointing
320 * to the first command line word, with <proxy> pointing to the proxy being
321 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100322 */
323static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200324 struct proxy *defpx, const char *file, int line,
325 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100326{
William Dauchybb9da0b2020-01-16 01:34:27 +0100327 int retval;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200328 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100329 unsigned int *tv = NULL;
330 unsigned int *td = NULL;
331 unsigned int val;
332
333 retval = 0;
334
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200335 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200336 tv = &proxy->fe_sps_lim;
337 td = &defpx->fe_sps_lim;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200338 }
339 else {
340 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100341 return -1;
342 }
343
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200344 if (*args[2] == 0) {
345 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100346 return -1;
347 }
348
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200349 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100350 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200351 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 +0100352 return -1;
353 }
354
William Dauchybb9da0b2020-01-16 01:34:27 +0100355 if (!(proxy->cap & PR_CAP_FE)) {
356 memprintf(err, "%s %s will be ignored because %s '%s' has no frontend capability",
357 args[0], args[1], proxy_type_str(proxy), proxy->id);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100358 retval = 1;
359 }
360 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200361 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100362 retval = 1;
363 }
364
365 *tv = val;
366 return retval;
367}
368
Willy Tarreauc35362a2014-04-25 13:58:37 +0200369/* This function parses a "max-keep-alive-queue" statement in a proxy section.
370 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
371 * does not return zero, it will write an error or warning message into a
372 * preallocated buffer returned at <err>. The function must be called with
373 * <args> pointing to the first command line word, with <proxy> pointing to
374 * the proxy being parsed, and <defpx> to the default proxy or NULL.
375 */
376static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
377 struct proxy *defpx, const char *file, int line,
378 char **err)
379{
380 int retval;
381 char *res;
382 unsigned int val;
383
384 retval = 0;
385
386 if (*args[1] == 0) {
387 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
388 return -1;
389 }
390
391 val = strtol(args[1], &res, 0);
392 if (*res) {
393 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
394 return -1;
395 }
396
397 if (!(proxy->cap & PR_CAP_BE)) {
398 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
399 args[0], proxy_type_str(proxy), proxy->id);
400 retval = 1;
401 }
402
403 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
404 proxy->max_ka_queue = val + 1;
405 return retval;
406}
407
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200408/* This function parses a "declare" statement in a proxy section. It returns -1
409 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
410 * it will write an error or warning message into a preallocated buffer returned
411 * at <err>. The function must be called with <args> pointing to the first command
412 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
413 * default proxy or NULL.
414 */
415static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
416 struct proxy *defpx, const char *file, int line,
417 char **err)
418{
419 /* Capture keyword wannot be declared in a default proxy. */
420 if (curpx == defpx) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800421 memprintf(err, "'%s' not available in default section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200422 return -1;
423 }
424
Joseph Herlant59dd2952018-11-15 11:46:55 -0800425 /* Capture keywork is only available in frontend. */
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200426 if (!(curpx->cap & PR_CAP_FE)) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800427 memprintf(err, "'%s' only available in frontend or listen section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200428 return -1;
429 }
430
431 /* Check mandatory second keyword. */
432 if (!args[1] || !*args[1]) {
433 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
434 return -1;
435 }
436
Joseph Herlant59dd2952018-11-15 11:46:55 -0800437 /* Actually, declare is only available for declaring capture
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200438 * slot, but in the future it can declare maps or variables.
Joseph Herlant59dd2952018-11-15 11:46:55 -0800439 * So, this section permits to check and switch according with
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200440 * the second keyword.
441 */
442 if (strcmp(args[1], "capture") == 0) {
443 char *error = NULL;
444 long len;
445 struct cap_hdr *hdr;
446
447 /* Check the next keyword. */
448 if (!args[2] || !*args[2] ||
449 (strcmp(args[2], "response") != 0 &&
450 strcmp(args[2], "request") != 0)) {
451 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
452 return -1;
453 }
454
455 /* Check the 'len' keyword. */
456 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
457 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
458 return -1;
459 }
460
461 /* Check the length value. */
462 if (!args[4] || !*args[4]) {
463 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
464 "capture length",
465 args[0], args[1]);
466 return -1;
467 }
468
469 /* convert the length value. */
470 len = strtol(args[4], &error, 10);
471 if (*error != '\0') {
472 memprintf(err, "'%s %s': cannot parse the length '%s'.",
473 args[0], args[1], args[3]);
474 return -1;
475 }
476
477 /* check length. */
478 if (len <= 0) {
479 memprintf(err, "length must be > 0");
480 return -1;
481 }
482
483 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200484 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200485 hdr->name = NULL; /* not a header capture */
486 hdr->namelen = 0;
487 hdr->len = len;
488 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
489
490 if (strcmp(args[2], "request") == 0) {
491 hdr->next = curpx->req_cap;
492 hdr->index = curpx->nb_req_cap++;
493 curpx->req_cap = hdr;
494 }
495 if (strcmp(args[2], "response") == 0) {
496 hdr->next = curpx->rsp_cap;
497 hdr->index = curpx->nb_rsp_cap++;
498 curpx->rsp_cap = hdr;
499 }
500 return 0;
501 }
502 else {
503 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
504 return -1;
505 }
506}
507
Olivier Houcharda254a372019-04-05 15:30:12 +0200508/* This function parses a "retry-on" statement */
509static int
510proxy_parse_retry_on(char **args, int section, struct proxy *curpx,
511 struct proxy *defpx, const char *file, int line,
512 char **err)
513{
514 int i;
515
516 if (!(*args[1])) {
517 memprintf(err, "'%s' needs at least one keyword to specify when to retry", args[0]);
518 return -1;
519 }
520 if (!(curpx->cap & PR_CAP_BE)) {
521 memprintf(err, "'%s' only available in backend or listen section", args[0]);
522 return -1;
523 }
524 curpx->retry_type = 0;
525 for (i = 1; *(args[i]); i++) {
526 if (!strcmp(args[i], "conn-failure"))
527 curpx->retry_type |= PR_RE_CONN_FAILED;
528 else if (!strcmp(args[i], "empty-response"))
529 curpx->retry_type |= PR_RE_DISCONNECTED;
530 else if (!strcmp(args[i], "response-timeout"))
531 curpx->retry_type |= PR_RE_TIMEOUT;
532 else if (!strcmp(args[i], "404"))
533 curpx->retry_type |= PR_RE_404;
534 else if (!strcmp(args[i], "408"))
535 curpx->retry_type |= PR_RE_408;
536 else if (!strcmp(args[i], "425"))
537 curpx->retry_type |= PR_RE_425;
538 else if (!strcmp(args[i], "500"))
539 curpx->retry_type |= PR_RE_500;
540 else if (!strcmp(args[i], "501"))
541 curpx->retry_type |= PR_RE_501;
542 else if (!strcmp(args[i], "502"))
543 curpx->retry_type |= PR_RE_502;
544 else if (!strcmp(args[i], "503"))
545 curpx->retry_type |= PR_RE_503;
546 else if (!strcmp(args[i], "504"))
547 curpx->retry_type |= PR_RE_504;
Olivier Houchard865d8392019-05-03 22:46:27 +0200548 else if (!strcmp(args[i], "0rtt-rejected"))
549 curpx->retry_type |= PR_RE_EARLY_ERROR;
Olivier Houcharde3249a92019-05-03 23:01:47 +0200550 else if (!strcmp(args[i], "junk-response"))
551 curpx->retry_type |= PR_RE_JUNK_REQUEST;
Olivier Houchardddf0e032019-05-10 18:05:40 +0200552 else if (!(strcmp(args[i], "all-retryable-errors")))
553 curpx->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED |
554 PR_RE_TIMEOUT | PR_RE_500 | PR_RE_502 |
555 PR_RE_503 | PR_RE_504 | PR_RE_EARLY_ERROR |
556 PR_RE_JUNK_REQUEST;
Olivier Houcharda254a372019-04-05 15:30:12 +0200557 else if (!strcmp(args[i], "none")) {
558 if (i != 1 || *args[i + 1]) {
559 memprintf(err, "'%s' 'none' keyworld only usable alone", args[0]);
560 return -1;
561 }
562 } else {
563 memprintf(err, "'%s': unknown keyword '%s'", args[0], args[i]);
564 return -1;
565 }
566
567 }
568
569
570 return 0;
571}
572
Willy Tarreau52543212020-07-09 05:58:51 +0200573#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900574/* This function parses "{cli|srv}tcpka-cnt" statements */
575static int proxy_parse_tcpka_cnt(char **args, int section, struct proxy *proxy,
576 struct proxy *defpx, const char *file, int line,
577 char **err)
578{
579 int retval;
580 char *res;
581 unsigned int tcpka_cnt;
582
583 retval = 0;
584
585 if (*args[1] == 0) {
586 memprintf(err, "'%s' expects an integer value", args[0]);
587 return -1;
588 }
589
590 tcpka_cnt = strtol(args[1], &res, 0);
591 if (*res) {
592 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
593 return -1;
594 }
595
596 if (!strcmp(args[0], "clitcpka-cnt")) {
597 if (!(proxy->cap & PR_CAP_FE)) {
598 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
599 args[0], proxy_type_str(proxy), proxy->id);
600 retval = 1;
601 }
602 proxy->clitcpka_cnt = tcpka_cnt;
603 } else if (!strcmp(args[0], "srvtcpka-cnt")) {
604 if (!(proxy->cap & PR_CAP_BE)) {
605 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
606 args[0], proxy_type_str(proxy), proxy->id);
607 retval = 1;
608 }
609 proxy->srvtcpka_cnt = tcpka_cnt;
610 } else {
611 /* unreachable */
612 memprintf(err, "'%s': unknown keyword", args[0]);
613 return -1;
614 }
615
616 return retval;
617}
Willy Tarreau52543212020-07-09 05:58:51 +0200618#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900619
Willy Tarreau52543212020-07-09 05:58:51 +0200620#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900621/* This function parses "{cli|srv}tcpka-idle" statements */
622static int proxy_parse_tcpka_idle(char **args, int section, struct proxy *proxy,
623 struct proxy *defpx, const char *file, int line,
624 char **err)
625{
626 int retval;
627 const char *res;
628 unsigned int tcpka_idle;
629
630 retval = 0;
631
632 if (*args[1] == 0) {
633 memprintf(err, "'%s' expects an integer value", args[0]);
634 return -1;
635 }
636 res = parse_time_err(args[1], &tcpka_idle, TIME_UNIT_S);
637 if (res == PARSE_TIME_OVER) {
638 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
639 args[1], args[0]);
640 return -1;
641 }
642 else if (res == PARSE_TIME_UNDER) {
643 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
644 args[1], args[0]);
645 return -1;
646 }
647 else if (res) {
648 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
649 return -1;
650 }
651
652 if (!strcmp(args[0], "clitcpka-idle")) {
653 if (!(proxy->cap & PR_CAP_FE)) {
654 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
655 args[0], proxy_type_str(proxy), proxy->id);
656 retval = 1;
657 }
658 proxy->clitcpka_idle = tcpka_idle;
659 } else if (!strcmp(args[0], "srvtcpka-idle")) {
660 if (!(proxy->cap & PR_CAP_BE)) {
661 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
662 args[0], proxy_type_str(proxy), proxy->id);
663 retval = 1;
664 }
665 proxy->srvtcpka_idle = tcpka_idle;
666 } else {
667 /* unreachable */
668 memprintf(err, "'%s': unknown keyword", args[0]);
669 return -1;
670 }
671
672 return retval;
673}
Willy Tarreau52543212020-07-09 05:58:51 +0200674#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900675
Willy Tarreau52543212020-07-09 05:58:51 +0200676#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900677/* This function parses "{cli|srv}tcpka-intvl" statements */
678static int proxy_parse_tcpka_intvl(char **args, int section, struct proxy *proxy,
679 struct proxy *defpx, const char *file, int line,
680 char **err)
681{
682 int retval;
683 const char *res;
684 unsigned int tcpka_intvl;
685
686 retval = 0;
687
688 if (*args[1] == 0) {
689 memprintf(err, "'%s' expects an integer value", args[0]);
690 return -1;
691 }
692 res = parse_time_err(args[1], &tcpka_intvl, TIME_UNIT_S);
693 if (res == PARSE_TIME_OVER) {
694 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
695 args[1], args[0]);
696 return -1;
697 }
698 else if (res == PARSE_TIME_UNDER) {
699 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
700 args[1], args[0]);
701 return -1;
702 }
703 else if (res) {
704 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
705 return -1;
706 }
707
708 if (!strcmp(args[0], "clitcpka-intvl")) {
709 if (!(proxy->cap & PR_CAP_FE)) {
710 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
711 args[0], proxy_type_str(proxy), proxy->id);
712 retval = 1;
713 }
714 proxy->clitcpka_intvl = tcpka_intvl;
715 } else if (!strcmp(args[0], "srvtcpka-intvl")) {
716 if (!(proxy->cap & PR_CAP_BE)) {
717 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
718 args[0], proxy_type_str(proxy), proxy->id);
719 retval = 1;
720 }
721 proxy->srvtcpka_intvl = tcpka_intvl;
722 } else {
723 /* unreachable */
724 memprintf(err, "'%s': unknown keyword", args[0]);
725 return -1;
726 }
727
728 return retval;
729}
Willy Tarreau52543212020-07-09 05:58:51 +0200730#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900731
Willy Tarreauf79d9502014-03-15 07:22:35 +0100732/* This function inserts proxy <px> into the tree of known proxies. The proxy's
733 * name is used as the storing key so it must already have been initialized.
734 */
735void proxy_store_name(struct proxy *px)
736{
737 px->conf.by_name.key = px->id;
738 ebis_insert(&proxy_by_name, &px->conf.by_name);
739}
740
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200741/* Returns a pointer to the first proxy matching capabilities <cap> and id
742 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
743 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200744 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200745struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200746{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200747 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200748
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200749 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
750 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100751
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200752 if (px->uuid != id)
753 break;
Alex Williams96532db2009-11-01 21:27:13 -0500754
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200755 if ((px->cap & cap) != cap)
756 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500757
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100758 if (table && (!px->table || !px->table->size))
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200759 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100760
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200761 return px;
762 }
763 return NULL;
764}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100765
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200766/* Returns a pointer to the first proxy matching either name <name>, or id
767 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
768 * If <table> is non-zero, it only considers proxies having a table.
769 */
770struct proxy *proxy_find_by_name(const char *name, int cap, int table)
771{
772 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200773
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200774 if (*name == '#') {
775 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
776 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200777 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500778 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100779 else {
780 struct ebpt_node *node;
781
782 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
783 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500784
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100785 if (strcmp(curproxy->id, name) != 0)
786 break;
787
788 if ((curproxy->cap & cap) != cap)
789 continue;
790
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100791 if (table && (!curproxy->table || !curproxy->table->size))
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200792 continue;
793
Willy Tarreauc739aa82015-05-26 11:35:41 +0200794 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100795 }
796 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200797 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500798}
799
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200800/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
801 * <id>. At most one of <id> or <name> may be different provided that <cap> is
802 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
803 * find a proxy based on some information from a previous configuration, across
804 * reloads or during information exchange between peers.
805 *
806 * Names are looked up first if present, then IDs are compared if present. In
807 * case of an inexact match whatever is forced in the configuration has
808 * precedence in the following order :
809 * - 1) forced ID (proves a renaming / change of proxy type)
810 * - 2) proxy name+type (may indicate a move if ID differs)
811 * - 3) automatic ID+type (may indicate a renaming)
812 *
813 * Depending on what is found, we can end up in the following situations :
814 *
815 * name id cap | possible causes
816 * -------------+-----------------
817 * -- -- -- | nothing found
818 * -- -- ok | nothing found
819 * -- ok -- | proxy deleted, ID points to next one
820 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
821 * ok -- -- | proxy deleted, but other half with same name still here (before)
822 * ok -- ok | proxy's ID changed (proxy moved in the config file)
823 * ok ok -- | proxy deleted, but other half with same name still here (after)
824 * ok ok ok | perfect match
825 *
826 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200827 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
828 * (and ID was not zero)
829 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
830 * (and name was not NULL)
831 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
832 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200833 *
834 * Only a valid proxy is returned. If capabilities do not match, NULL is
835 * returned. The caller can check <diff> to report detailed warnings / errors,
836 * and decide whether or not to use what was found.
837 */
838struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
839{
840 struct proxy *byname;
841 struct proxy *byid;
842
843 if (!name && !id)
844 return NULL;
845
846 if (diff)
847 *diff = 0;
848
849 byname = byid = NULL;
850
851 if (name) {
852 byname = proxy_find_by_name(name, cap, 0);
853 if (byname && (!id || byname->uuid == id))
854 return byname;
855 }
856
Joseph Herlant59dd2952018-11-15 11:46:55 -0800857 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200858 * - name not set
859 * - name set but not found
860 * - name found, but ID doesn't match.
861 */
862 if (id) {
863 byid = proxy_find_by_id(id, cap, 0);
864 if (byid) {
865 if (byname) {
866 /* id+type found, name+type found, but not all 3.
867 * ID wins only if forced, otherwise name wins.
868 */
869 if (byid->options & PR_O_FORCED_ID) {
870 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200871 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200872 return byid;
873 }
874 else {
875 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200876 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200877 return byname;
878 }
879 }
880
Joseph Herlant59dd2952018-11-15 11:46:55 -0800881 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200882 * - name not set
883 * - name set but not found
884 */
885 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200886 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200887 return byid;
888 }
889
890 /* ID not found */
891 if (byname) {
892 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200893 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200894 return byname;
895 }
896 }
897
Joseph Herlant59dd2952018-11-15 11:46:55 -0800898 /* All remaining possibilities will lead to NULL. If we can report more
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200899 * detailed information to the caller about changed types and/or name,
900 * we'll do it. For example, we could detect that "listen foo" was
901 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
902 * - name not set, ID not found
903 * - name not found, ID not set
904 * - name not found, ID not found
905 */
906 if (!diff)
907 return NULL;
908
909 if (name) {
910 byname = proxy_find_by_name(name, 0, 0);
911 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200912 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200913 }
914
915 if (id) {
916 byid = proxy_find_by_id(id, 0, 0);
917 if (byid) {
918 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200919 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200920 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200921 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200922 /* otherwise it's a different proxy that was returned */
923 }
924 }
925 return NULL;
926}
927
Willy Tarreaubaaee002006-06-26 02:48:02 +0200928/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100929 * This function finds a server with matching name within selected proxy.
930 * It also checks if there are more matching servers with
931 * requested name as this often leads into unexpected situations.
932 */
933
934struct server *findserver(const struct proxy *px, const char *name) {
935
936 struct server *cursrv, *target = NULL;
937
938 if (!px)
939 return NULL;
940
941 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
942 if (strcmp(cursrv->id, name))
943 continue;
944
945 if (!target) {
946 target = cursrv;
947 continue;
948 }
949
Christopher Faulet767a84b2017-11-24 16:50:31 +0100950 ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
951 name, px->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100952
953 return NULL;
954 }
955
956 return target;
957}
958
Willy Tarreauff01a212009-03-15 13:46:16 +0100959/* This function checks that the designated proxy has no http directives
960 * enabled. It will output a warning if there are, and will fix some of them.
961 * It returns the number of fatal errors encountered. This should be called
962 * at the end of the configuration parsing if the proxy is not in http mode.
963 * The <file> argument is used to construct the error message.
964 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200965int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100966{
967 if (curproxy->cookie_name != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100968 ha_warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
969 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100970 }
Willy Tarreauff01a212009-03-15 13:46:16 +0100971 if (curproxy->monitor_uri != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100972 ha_warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
973 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100974 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200975 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100976 curproxy->lbprm.algo &= ~BE_LB_ALGO;
977 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100978 ha_warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
979 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100980 }
Willy Tarreau17804162009-11-09 21:27:51 +0100981 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
982 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100983 ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
984 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
985 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau17804162009-11-09 21:27:51 +0100986 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200987 if (curproxy->conf.logformat_string == default_http_log_format ||
988 curproxy->conf.logformat_string == clf_http_log_format) {
989 /* Note: we don't change the directive's file:line number */
990 curproxy->conf.logformat_string = default_tcp_log_format;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100991 ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
992 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
993 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau196729e2012-05-31 19:30:26 +0200994 }
995
Willy Tarreauff01a212009-03-15 13:46:16 +0100996 return 0;
997}
998
Willy Tarreau237250c2011-07-29 01:49:03 +0200999/* Perform the most basic initialization of a proxy :
1000 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +02001001 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +02001002 */
1003void init_new_proxy(struct proxy *p)
1004{
1005 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001006 p->obj_type = OBJ_TYPE_PROXY;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001007 p->pendconns = EB_ROOT;
Willy Tarreau237250c2011-07-29 01:49:03 +02001008 LIST_INIT(&p->acl);
1009 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001010 LIST_INIT(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001011 LIST_INIT(&p->http_after_res_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001012 LIST_INIT(&p->redirect_rules);
1013 LIST_INIT(&p->mon_fail_cond);
1014 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001015 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001016 LIST_INIT(&p->persist_rules);
1017 LIST_INIT(&p->sticking_rules);
1018 LIST_INIT(&p->storersp_rules);
1019 LIST_INIT(&p->tcp_req.inspect_rules);
1020 LIST_INIT(&p->tcp_rep.inspect_rules);
1021 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +02001022 LIST_INIT(&p->tcp_req.l5_rules);
Olivier Houchard859dc802019-08-08 15:47:21 +02001023 MT_LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +02001024 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +01001025 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001026 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +01001027 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001028 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001029 LIST_INIT(&p->conf.listeners);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001030 LIST_INIT(&p->conf.errors);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001031 LIST_INIT(&p->conf.args.list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001032 LIST_INIT(&p->filter_configs);
Christopher Faulet7a1e2e12020-04-02 18:05:11 +02001033 LIST_INIT(&p->tcpcheck_rules.preset_vars);
Willy Tarreau237250c2011-07-29 01:49:03 +02001034
1035 /* Timeouts are defined as -1 */
1036 proxy_reset_timeouts(p);
1037 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +02001038
1039 /* initial uuid is unassigned (-1) */
1040 p->uuid = -1;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001041
Olivier Houcharda254a372019-04-05 15:30:12 +02001042 /* Default to only allow L4 retries */
1043 p->retry_type = PR_RE_CONN_FAILED;
1044
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001045 HA_SPIN_INIT(&p->lock);
Willy Tarreau237250c2011-07-29 01:49:03 +02001046}
1047
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001048/*
Willy Tarreaue91bff22020-09-02 11:11:43 +02001049 * This function finishes the startup of proxies by marking them ready. */
1050void start_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001051{
1052 struct proxy *curproxy;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001053
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001054 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001055 if (curproxy->disabled)
1056 continue;
Willy Tarreaue91bff22020-09-02 11:11:43 +02001057 send_log(curproxy, LOG_NOTICE, "Proxy %s started.\n", curproxy->id);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001058 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001059}
1060
1061
1062/*
Willy Tarreau918ff602011-07-25 16:33:49 +02001063 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +02001064 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +02001065 * called as a task which is woken up upon stopping or when rate limiting must
1066 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001067 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001068struct task *manage_proxy(struct task *t, void *context, unsigned short state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001070 struct proxy *p = context;
Willy Tarreau918ff602011-07-25 16:33:49 +02001071 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +01001072 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001073
Willy Tarreau918ff602011-07-25 16:33:49 +02001074 /* We should periodically try to enable listeners waiting for a
1075 * global resource here.
1076 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001077
Willy Tarreau918ff602011-07-25 16:33:49 +02001078 /* first, let's check if we need to stop the proxy */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001079 if (unlikely(stopping && !p->disabled)) {
Willy Tarreau918ff602011-07-25 16:33:49 +02001080 int t;
1081 t = tick_remain(now_ms, p->stop_time);
1082 if (t == 0) {
William Dauchy1e2256d2020-01-25 23:45:18 +01001083 ha_warning("Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001084 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
William Dauchy1e2256d2020-01-25 23:45:18 +01001085 send_log(p, LOG_WARNING, "Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
Willy Tarreau918ff602011-07-25 16:33:49 +02001086 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
1087 stop_proxy(p);
1088 /* try to free more memory */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001089 pool_gc(NULL);
Willy Tarreau918ff602011-07-25 16:33:49 +02001090 }
1091 else {
1092 next = tick_first(next, p->stop_time);
1093 }
1094 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +02001095
Willy Tarreau3a925c12013-09-04 17:54:01 +02001096 /* If the proxy holds a stick table, we need to purge all unused
1097 * entries. These are all the ones in the table with ref_cnt == 0
1098 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +02001099 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +02001100 * be in neither list. Any entry being dumped will have ref_cnt > 0.
1101 * However we protect tables that are being synced to peers.
1102 */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001103 if (unlikely(stopping && p->disabled && p->table && p->table->current)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001104 if (!p->table->syncing) {
1105 stktable_trash_oldest(p->table, p->table->current);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001106 pool_gc(NULL);
Willy Tarreau3a925c12013-09-04 17:54:01 +02001107 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001108 if (p->table->current) {
Willy Tarreau3a925c12013-09-04 17:54:01 +02001109 /* some entries still remain, let's recheck in one second */
1110 next = tick_first(next, tick_add(now_ms, 1000));
1111 }
1112 }
1113
Willy Tarreau918ff602011-07-25 16:33:49 +02001114 /* the rest below is just for frontends */
1115 if (!(p->cap & PR_CAP_FE))
1116 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +02001117
Willy Tarreau918ff602011-07-25 16:33:49 +02001118 /* check the various reasons we may find to block the frontend */
Willy Tarreau6b3bf732020-09-24 07:35:46 +02001119 if (unlikely(p->feconn >= p->maxconn))
Willy Tarreau918ff602011-07-25 16:33:49 +02001120 goto out;
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001121
Willy Tarreau918ff602011-07-25 16:33:49 +02001122 if (p->fe_sps_lim &&
1123 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
1124 /* we're blocking because a limit was reached on the number of
1125 * requests/s on the frontend. We want to re-check ASAP, which
1126 * means in 1 ms before estimated expiration date, because the
1127 * timer will have settled down.
1128 */
1129 next = tick_first(next, tick_add(now_ms, wait));
1130 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001131 }
Willy Tarreau918ff602011-07-25 16:33:49 +02001132
1133 /* The proxy is not limited so we can re-enable any waiting listener */
Willy Tarreau241797a2019-12-10 14:10:52 +01001134 dequeue_proxy_listeners(p);
Willy Tarreau918ff602011-07-25 16:33:49 +02001135 out:
1136 t->expire = next;
1137 task_queue(t);
1138 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001139}
1140
1141
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001142static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx,
1143 struct proxy *defpx, const char *file, int line,
1144 char **err)
1145{
1146 const char *res;
1147
1148 if (!*args[1]) {
1149 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
1150 return -1;
1151 }
1152 res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001153 if (res == PARSE_TIME_OVER) {
1154 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1155 args[1], args[0]);
1156 return -1;
1157 }
1158 else if (res == PARSE_TIME_UNDER) {
1159 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1160 args[1], args[0]);
1161 return -1;
1162 }
1163 else if (res) {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001164 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
1165 return -1;
1166 }
1167 return 0;
1168}
1169
Olivier Houchard9f6af332018-05-25 14:04:04 +02001170struct task *hard_stop(struct task *t, void *context, unsigned short state)
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001171{
1172 struct proxy *p;
1173 struct stream *s;
1174
1175 if (killed) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001176 ha_warning("Some tasks resisted to hard-stop, exiting now.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001177 send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n");
Willy Tarreau7067b3a2019-06-02 11:11:29 +02001178 killed = 2;
1179 t->expire = TICK_ETERNITY;
1180 return t;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001181 }
1182
Christopher Faulet767a84b2017-11-24 16:50:31 +01001183 ha_warning("soft-stop running for too long, performing a hard-stop.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001184 send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n");
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001185 p = proxies_list;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001186 while (p) {
1187 if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001188 ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1189 p->id, p->feconn);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001190 send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1191 p->id, p->feconn);
1192 }
1193 p = p->next;
1194 }
1195 list_for_each_entry(s, &streams, list) {
1196 stream_shutdown(s, SF_ERR_KILLED);
1197 }
1198
1199 killed = 1;
1200 t->expire = tick_add(now_ms, MS_TO_TICKS(1000));
1201 return t;
1202}
1203
Willy Tarreaubaaee002006-06-26 02:48:02 +02001204/*
1205 * this function disables health-check servers so that the process will quickly be ignored
1206 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
1207 * time will not be used since it would already not listen anymore to the socket.
1208 */
1209void soft_stop(void)
1210{
1211 struct proxy *p;
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001212 struct peers *prs;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001213 struct task *task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001214
1215 stopping = 1;
William Dauchy3894d972019-12-28 15:36:02 +01001216 /* disable busy polling to avoid cpu eating for the new process */
1217 global.tune.options &= ~GTUNE_BUSY_POLLING;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001218 if (tick_isset(global.hard_stop_after)) {
Emeric Brunc60def82017-09-27 14:59:38 +02001219 task = task_new(MAX_THREADS_MASK);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001220 if (task) {
1221 task->process = hard_stop;
1222 task_schedule(task, tick_add(now_ms, global.hard_stop_after));
1223 }
1224 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001225 ha_alert("out of memory trying to allocate the hard-stop task.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001226 }
1227 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001228 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001229 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001230 while (p) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001231 if (!p->disabled) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001232 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 +02001233 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 +02001234 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001235
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001236 /* Note: do not wake up stopped proxies' task nor their tables'
1237 * tasks as these ones might point to already released entries.
1238 */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001239 if (p->table && p->table->size && p->table->sync_task)
1240 task_wakeup(p->table->sync_task, TASK_WOKEN_MSG);
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001241
1242 if (p->task)
1243 task_wakeup(p->task, TASK_WOKEN_MSG);
1244 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001245 p = p->next;
1246 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001247
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001248 prs = cfg_peers;
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001249 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001250 if (prs->peers_fe)
1251 stop_proxy(prs->peers_fe);
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001252 prs = prs->next;
1253 }
Emeric Brunc47ba592020-10-07 10:13:10 +02001254
1255 p = cfg_log_forward;
1256 while (p) {
1257 /* Zombie proxy, let's close the file descriptors */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001258 if (p->disabled &&
Emeric Brunc47ba592020-10-07 10:13:10 +02001259 !LIST_ISEMPTY(&p->conf.listeners) &&
1260 LIST_ELEM(p->conf.listeners.n,
1261 struct listener *, by_fe)->state > LI_ASSIGNED) {
1262 struct listener *l;
1263 list_for_each_entry(l, &p->conf.listeners, by_fe) {
1264 if (l->state > LI_ASSIGNED)
1265 close(l->rx.fd);
1266 l->state = LI_INIT;
1267 }
1268 }
1269
Willy Tarreauc3914d42020-09-24 08:39:22 +02001270 if (!p->disabled) {
Emeric Brunc47ba592020-10-07 10:13:10 +02001271 stop_proxy(p);
1272 }
1273 p = p->next;
1274 }
1275
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
Christopher Faulet767a84b2017-11-24 16:50:31 +01001292 ha_warning("Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001293 send_log(p, LOG_WARNING, "Pausing %s %s.\n", proxy_cap_str(p->cap), p->id);
1294
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001295 list_for_each_entry(l, &p->conf.listeners, by_fe)
1296 pause_listener(l);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001297
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001298 if (p->li_ready) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001299 ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001300 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
1301 return 0;
1302 }
Willy Tarreauce8fe252011-09-07 19:14:57 +02001303 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +02001304}
1305
Olivier Houchard1fc05162017-04-06 01:05:05 +02001306/* This function makes the proxy unusable, but keeps the listening sockets
1307 * opened, so that if any process requests them, we are able to serve them.
1308 * This should only be called early, before we started accepting requests.
1309 */
1310void zombify_proxy(struct proxy *p)
1311{
1312 struct listener *l;
Olivier Houchard1fc05162017-04-06 01:05:05 +02001313
1314 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Olivier Houchard1fc05162017-04-06 01:05:05 +02001315 unbind_listener_no_close(l);
Willy Tarreaubec7ab02020-09-23 18:16:03 +02001316 if (l->state >= LI_ASSIGNED)
Olivier Houchard1fc05162017-04-06 01:05:05 +02001317 delete_listener(l);
Olivier Houchard1fc05162017-04-06 01:05:05 +02001318 }
Willy Tarreauc3914d42020-09-24 08:39:22 +02001319 p->disabled = 1;
Olivier Houchard1fc05162017-04-06 01:05:05 +02001320}
Willy Tarreauda250db2008-10-12 12:07:48 +02001321
1322/*
1323 * This function completely stops a proxy and releases its listeners. It has
1324 * to be called when going down in order to release the ports so that another
1325 * process may bind to them. It must also be called on disabled proxies at the
William Lallemandc59f9882018-11-16 16:57:21 +01001326 * end of start-up. If all listeners are closed, the proxy is set to the
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001327 * PR_STSTOPPED state. The function takes the proxy's lock so it's safe to
1328 * call from multiple places.
Willy Tarreauda250db2008-10-12 12:07:48 +02001329 */
1330void stop_proxy(struct proxy *p)
1331{
1332 struct listener *l;
William Lallemandc59f9882018-11-16 16:57:21 +01001333 int nostop = 0;
Willy Tarreauda250db2008-10-12 12:07:48 +02001334
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001335 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
1336
Willy Tarreau4348fad2012-09-20 16:48:07 +02001337 list_for_each_entry(l, &p->conf.listeners, by_fe) {
William Lallemandc59f9882018-11-16 16:57:21 +01001338 if (l->options & LI_O_NOSTOP) {
1339 HA_ATOMIC_ADD(&unstoppable_jobs, 1);
1340 nostop = 1;
1341 continue;
1342 }
William Lallemandf7f488d2019-10-18 21:16:39 +02001343 /* The master should not close an inherited FD */
Willy Tarreau43046fa2020-09-01 15:41:59 +02001344 if (master && (l->rx.flags & RX_F_INHERITED))
William Lallemandf7f488d2019-10-18 21:16:39 +02001345 unbind_listener_no_close(l);
1346 else
1347 unbind_listener(l);
Willy Tarreauda250db2008-10-12 12:07:48 +02001348 if (l->state >= LI_ASSIGNED) {
1349 delete_listener(l);
Willy Tarreauda250db2008-10-12 12:07:48 +02001350 }
1351 }
William Lallemandc59f9882018-11-16 16:57:21 +01001352 if (!nostop)
Willy Tarreauc3914d42020-09-24 08:39:22 +02001353 p->disabled = 1;
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001354
1355 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001356}
1357
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001358/* This function resumes listening on the specified proxy. It scans all of its
1359 * listeners and tries to enable them all. If any of them fails, the proxy is
1360 * put back to the paused state. It returns 1 upon success, or zero if an error
1361 * is encountered.
1362 */
1363int resume_proxy(struct proxy *p)
1364{
1365 struct listener *l;
1366 int fail;
1367
Willy Tarreauc3914d42020-09-24 08:39:22 +02001368 if (p->disabled || !p->li_paused)
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001369 return 1;
1370
Christopher Faulet767a84b2017-11-24 16:50:31 +01001371 ha_warning("Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001372 send_log(p, LOG_WARNING, "Enabling %s %s.\n", proxy_cap_str(p->cap), p->id);
1373
1374 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001375 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001376 if (!resume_listener(l)) {
1377 int port;
1378
Willy Tarreau37159062020-08-27 07:48:42 +02001379 port = get_host_port(&l->rx.addr);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001380 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001381 ha_warning("Port %d busy while trying to enable %s %s.\n",
1382 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001383 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1384 port, proxy_cap_str(p->cap), p->id);
1385 }
1386 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001387 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
1388 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001389 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1390 l->luid, proxy_cap_str(p->cap), p->id);
1391 }
1392
1393 /* Another port might have been enabled. Let's stop everything. */
1394 fail = 1;
1395 break;
1396 }
1397 }
1398
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001399 if (fail) {
1400 pause_proxy(p);
1401 return 0;
1402 }
1403 return 1;
1404}
1405
Willy Tarreaubaaee002006-06-26 02:48:02 +02001406/*
1407 * This function temporarily disables listening so that another new instance
1408 * can start listening. It is designed to be called upon reception of a
1409 * SIGTTOU, after which either a SIGUSR1 can be sent to completely stop
1410 * the proxy, or a SIGTTIN can be sent to listen again.
1411 */
1412void pause_proxies(void)
1413{
1414 int err;
1415 struct proxy *p;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001416 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001417
1418 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001419 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001420 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001421 while (p) {
Willy Tarreauce8fe252011-09-07 19:14:57 +02001422 err |= !pause_proxy(p);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001423 p = p->next;
1424 }
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001425
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001426 prs = cfg_peers;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001427 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001428 if (prs->peers_fe)
1429 err |= !pause_proxy(prs->peers_fe);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001430 prs = prs->next;
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001431 }
1432
Willy Tarreaubaaee002006-06-26 02:48:02 +02001433 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001434 ha_warning("Some proxies refused to pause, performing soft stop now.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001435 send_log(p, LOG_WARNING, "Some proxies refused to pause, performing soft stop now.\n");
1436 soft_stop();
1437 }
1438}
1439
1440
1441/*
1442 * This function reactivates listening. This can be used after a call to
1443 * sig_pause(), for example when a new instance has failed starting up.
1444 * It is designed to be called upon reception of a SIGTTIN.
1445 */
Willy Tarreaube58c382011-07-24 18:28:10 +02001446void resume_proxies(void)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001447{
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001448 int err;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001449 struct proxy *p;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001450 struct peers *prs;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001451
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001452 err = 0;
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001453 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001454 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001455 while (p) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001456 err |= !resume_proxy(p);
1457 p = p->next;
1458 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001459
Frédéric Lécailleed2b4a62017-07-13 09:07:09 +02001460 prs = cfg_peers;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001461 while (prs) {
Willy Tarreau337a6662015-09-28 16:27:44 +02001462 if (prs->peers_fe)
1463 err |= !resume_proxy(prs->peers_fe);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001464 prs = prs->next;
1465 }
Willy Tarreaube58c382011-07-24 18:28:10 +02001466
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001467 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001468 ha_warning("Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001469 send_log(p, LOG_WARNING, "Some proxies refused to resume, a restart is probably needed to resume safe operations.\n");
Willy Tarreaubaaee002006-06-26 02:48:02 +02001470 }
1471}
1472
Willy Tarreau87b09662015-04-03 00:22:06 +02001473/* Set current stream's backend to <be>. Nothing is done if the
1474 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001475 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001476 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001477 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001478 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001479int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001480{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001481 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001482 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001483
1484 if (flt_set_stream_backend(s, be) < 0)
1485 return 0;
1486
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001487 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001488 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
1489 HA_ATOMIC_ADD(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001490 proxy_inc_be_ctr(be);
1491
Willy Tarreau87b09662015-04-03 00:22:06 +02001492 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001493 s->si[1].flags &= ~SI_FL_INDEP_STR;
1494 if (be->options2 & PR_O2_INDEPSTR)
1495 s->si[1].flags |= SI_FL_INDEP_STR;
1496
Hongbo Longe39683c2017-03-10 18:41:51 +01001497 if (tick_isset(be->timeout.serverfin))
1498 s->si[1].hcto = be->timeout.serverfin;
1499
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001500 /* We want to enable the backend-specific analysers except those which
1501 * were already run as part of the frontend/listener. Note that it would
1502 * be more reliable to store the list of analysers that have been run,
1503 * but what we do here is OK for now.
1504 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001505 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001506
Willy Tarreau51aecc72009-07-12 09:47:04 +02001507 /* If the target backend requires HTTP processing, we have to allocate
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001508 * the HTTP transaction if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001509 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001510 if (unlikely(!s->txn && be->http_needed)) {
1511 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001512 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001513
1514 /* and now initialize the HTTP transaction state */
1515 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001516 }
1517
Christopher Faulet309c6412015-12-02 09:57:32 +01001518 /* Be sure to filter request headers if the backend is an HTTP proxy and
1519 * if there are filters attached to the stream. */
1520 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001521 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001522
Willy Tarreaueee5b512015-04-03 23:46:31 +02001523 if (s->txn) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02001524 /* If we chain a TCP frontend to an HTX backend, we must upgrade
1525 * the client mux */
Christopher Faulet60d29b32019-07-15 15:00:25 +02001526 if (!IS_HTX_STRM(s) && be->mode == PR_MODE_HTTP) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02001527 struct connection *conn = objt_conn(strm_sess(s)->origin);
1528 struct conn_stream *cs = objt_cs(s->si[0].end);
1529
1530 if (conn && cs) {
1531 si_rx_endp_more(&s->si[0]);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +02001532 /* Make sure we're unsubscribed, the the new
1533 * mux will probably want to subscribe to
1534 * the underlying XPRT
1535 */
1536 if (s->si[0].wait_event.events)
1537 conn->mux->unsubscribe(cs, s->si[0].wait_event.events,
1538 &s->si[0].wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001539 if (conn_upgrade_mux_fe(conn, cs, &s->req.buf, ist(""), PROTO_MODE_HTTP) == -1)
Christopher Fauletbbe68542019-04-08 10:53:51 +02001540 return 0;
Olivier Houchard4c18f942019-07-31 18:05:26 +02001541 if (!strcmp(conn->mux->name, "H2")) {
1542 /* For HTTP/2, destroy the conn_stream,
1543 * disable logging, and pretend that we
1544 * failed, to that the stream is
1545 * silently destroyed. The new mux
1546 * will create new streams.
1547 */
Olivier Houchard59dd06d2019-08-09 18:01:15 +02001548 cs_free(cs);
Olivier Houchard4c18f942019-07-31 18:05:26 +02001549 si_detach_endpoint(&s->si[0]);
1550 s->logs.logwait = 0;
1551 s->logs.level = 0;
1552 s->flags |= SF_IGNORE;
1553 return 0;
1554 }
Olivier Houchard71b20c22019-08-09 17:50:05 +02001555 s->flags |= SF_HTX;
Christopher Fauletbbe68542019-04-08 10:53:51 +02001556 }
1557 }
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01001558 else if (IS_HTX_STRM(s) && be->mode != PR_MODE_HTTP) {
1559 /* If a TCP backend is assgiend to an HTX stream, return
1560 * an error. It may happens for a new stream on a
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001561 * previously upgraded connections. */
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01001562 if (!(s->flags & SF_ERR_MASK))
1563 s->flags |= SF_ERR_INTERNAL;
1564 return 0;
1565 }
Christopher Fauletbbe68542019-04-08 10:53:51 +02001566
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001567 /* we may request to parse a request body */
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001568 if (be->options & PR_O_WREQ_BODY)
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001569 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001570 }
1571
1572 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001573 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001574 s->req.flags |= CF_NEVER_WAIT;
1575 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001576 }
1577
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001578 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001579}
1580
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001581/* Capture a bad request or response and archive it in the proxy's structure.
1582 * It is relatively protocol-agnostic so it requires that a number of elements
1583 * are passed :
1584 * - <proxy> is the proxy where the error was detected and where the snapshot
1585 * needs to be stored
Joseph Herlant59dd2952018-11-15 11:46:55 -08001586 * - <is_back> indicates that the error happened when receiving the response
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001587 * - <other_end> is a pointer to the proxy on the other side when known
1588 * - <target> is the target of the connection, usually a server or a proxy
1589 * - <sess> is the session which experienced the error
1590 * - <ctx> may be NULL or should contain any info relevant to the protocol
1591 * - <buf> is the buffer containing the offending data
1592 * - <buf_ofs> is the position of this buffer's input data in the input
1593 * stream, starting at zero. It may be passed as zero if unknown.
1594 * - <buf_out> is the portion of <buf->data> which was already forwarded and
1595 * which precedes the buffer's input. The buffer's input starts at
1596 * buf->head + buf_out.
1597 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
1598 * - <show> is the callback to use to display <ctx>. It may be NULL.
1599 */
1600void proxy_capture_error(struct proxy *proxy, int is_back,
1601 struct proxy *other_end, enum obj_type *target,
1602 const struct session *sess,
1603 const struct buffer *buf, long buf_ofs,
1604 unsigned int buf_out, unsigned int err_pos,
1605 const union error_snapshot_ctx *ctx,
1606 void (*show)(struct buffer *, const struct error_snapshot *))
1607{
1608 struct error_snapshot *es;
1609 unsigned int buf_len;
1610 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001611 unsigned int ev_id;
1612
1613 ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
1614
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001615 buf_len = b_data(buf) - buf_out;
1616
1617 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001618 if (!es)
1619 return;
1620
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001621 es->buf_len = buf_len;
1622 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001623
Christopher Faulet47a72102020-01-06 11:37:00 +01001624 len1 = b_size(buf) - b_peek_ofs(buf, buf_out);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001625 if (len1 > buf_len)
1626 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001627
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001628 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001629 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001630 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001631 if (len2)
1632 memcpy(es->buf + len1, b_orig(buf), len2);
1633 }
1634
1635 es->buf_err = err_pos;
1636 es->when = date; // user-visible date
1637 es->srv = objt_server(target);
1638 es->oe = other_end;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001639 if (sess && objt_conn(sess->origin) && conn_get_src(__objt_conn(sess->origin)))
Willy Tarreau026efc72019-07-17 15:20:02 +02001640 es->src = *__objt_conn(sess->origin)->src;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001641 else
1642 memset(&es->src, 0, sizeof(es->src));
1643
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001644 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
1645 es->buf_out = buf_out;
1646 es->buf_ofs = buf_ofs;
1647
1648 /* be sure to indicate the offset of the first IN byte */
1649 if (es->buf_ofs >= es->buf_len)
1650 es->buf_ofs -= es->buf_len;
1651 else
1652 es->buf_ofs = 0;
1653
1654 /* protocol-specific part now */
1655 if (ctx)
1656 es->ctx = *ctx;
1657 else
1658 memset(&es->ctx, 0, sizeof(es->ctx));
1659 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001660
1661 /* note: we still lock since we have to be certain that nobody is
1662 * dumping the output while we free.
1663 */
1664 HA_SPIN_LOCK(PROXY_LOCK, &proxy->lock);
1665 if (is_back) {
1666 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
1667 } else {
1668 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
1669 }
1670 free(es);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001671 HA_SPIN_UNLOCK(PROXY_LOCK, &proxy->lock);
1672}
1673
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001674/* Configure all proxies which lack a maxconn setting to use the global one by
1675 * default. This avoids the common mistake consisting in setting maxconn only
1676 * in the global section and discovering the hard way that it doesn't propagate
1677 * through the frontends. These values are also propagated through the various
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001678 * targeted backends, whose fullconn is finally calculated if not yet set.
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001679 */
1680void proxy_adjust_all_maxconn()
1681{
1682 struct proxy *curproxy;
1683 struct switching_rule *swrule1, *swrule2;
1684
1685 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001686 if (curproxy->disabled)
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001687 continue;
1688
1689 if (!(curproxy->cap & PR_CAP_FE))
1690 continue;
1691
1692 if (!curproxy->maxconn)
1693 curproxy->maxconn = global.maxconn;
1694
1695 /* update the target backend's fullconn count : default_backend */
1696 if (curproxy->defbe.be)
1697 curproxy->defbe.be->tot_fe_maxconn += curproxy->maxconn;
1698 else if ((curproxy->cap & PR_CAP_LISTEN) == PR_CAP_LISTEN)
1699 curproxy->tot_fe_maxconn += curproxy->maxconn;
1700
1701 list_for_each_entry(swrule1, &curproxy->switching_rules, list) {
1702 /* For each target of switching rules, we update their
1703 * tot_fe_maxconn, except if a previous rule points to
1704 * the same backend or to the default backend.
1705 */
1706 if (swrule1->be.backend != curproxy->defbe.be) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01001707 /* note: swrule1->be.backend isn't a backend if the rule
1708 * is dynamic, it's an expression instead, so it must not
1709 * be dereferenced as a backend before being certain it is.
1710 */
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001711 list_for_each_entry(swrule2, &curproxy->switching_rules, list) {
1712 if (swrule2 == swrule1) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01001713 if (!swrule1->dynamic)
1714 swrule1->be.backend->tot_fe_maxconn += curproxy->maxconn;
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001715 break;
1716 }
1717 else if (!swrule2->dynamic && swrule2->be.backend == swrule1->be.backend) {
1718 /* there are multiple refs of this backend */
1719 break;
1720 }
1721 }
1722 }
1723 }
1724 }
1725
1726 /* automatically compute fullconn if not set. We must not do it in the
1727 * loop above because cross-references are not yet fully resolved.
1728 */
1729 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001730 if (curproxy->disabled)
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001731 continue;
1732
1733 /* If <fullconn> is not set, let's set it to 10% of the sum of
1734 * the possible incoming frontend's maxconns.
1735 */
1736 if (!curproxy->fullconn && (curproxy->cap & PR_CAP_BE)) {
1737 /* we have the sum of the maxconns in <total>. We only
1738 * keep 10% of that sum to set the default fullconn, with
1739 * a hard minimum of 1 (to avoid a divide by zero).
1740 */
1741 curproxy->fullconn = (curproxy->tot_fe_maxconn + 9) / 10;
1742 if (!curproxy->fullconn)
1743 curproxy->fullconn = 1;
1744 }
1745 }
1746}
1747
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001748/* Config keywords below */
1749
Willy Tarreaudc13c112013-06-21 23:16:39 +02001750static struct cfg_kw_list cfg_kws = {ILH, {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001751 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001752 { CFG_LISTEN, "timeout", proxy_parse_timeout },
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +02001753 { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
1754 { CFG_LISTEN, "contimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
1755 { 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 +01001756 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001757 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001758 { CFG_LISTEN, "declare", proxy_parse_declare },
Olivier Houcharda254a372019-04-05 15:30:12 +02001759 { CFG_LISTEN, "retry-on", proxy_parse_retry_on },
Willy Tarreau52543212020-07-09 05:58:51 +02001760#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001761 { CFG_LISTEN, "clitcpka-cnt", proxy_parse_tcpka_cnt },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001762 { CFG_LISTEN, "srvtcpka-cnt", proxy_parse_tcpka_cnt },
Willy Tarreau52543212020-07-09 05:58:51 +02001763#endif
1764#ifdef TCP_KEEPIDLE
1765 { CFG_LISTEN, "clitcpka-idle", proxy_parse_tcpka_idle },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001766 { CFG_LISTEN, "srvtcpka-idle", proxy_parse_tcpka_idle },
Willy Tarreau52543212020-07-09 05:58:51 +02001767#endif
1768#ifdef TCP_KEEPINTVL
1769 { CFG_LISTEN, "clitcpka-intvl", proxy_parse_tcpka_intvl },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001770 { CFG_LISTEN, "srvtcpka-intvl", proxy_parse_tcpka_intvl },
Willy Tarreau52543212020-07-09 05:58:51 +02001771#endif
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001772 { 0, NULL, NULL },
1773}};
1774
Willy Tarreau0108d902018-11-25 19:14:37 +01001775INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1776
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001777/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1778 * adequate error messages and returns NULL. This function is designed to be used by
1779 * functions requiring a frontend on the CLI.
1780 */
1781struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1782{
1783 struct proxy *px;
1784
1785 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001786 cli_err(appctx, "A frontend name is expected.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001787 return NULL;
1788 }
1789
1790 px = proxy_fe_by_name(arg);
1791 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001792 cli_err(appctx, "No such frontend.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001793 return NULL;
1794 }
1795 return px;
1796}
1797
Olivier Houchard614f8d72017-03-14 20:08:46 +01001798/* Expects to find a backend named <arg> and returns it, otherwise displays various
1799 * adequate error messages and returns NULL. This function is designed to be used by
1800 * functions requiring a frontend on the CLI.
1801 */
1802struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
1803{
1804 struct proxy *px;
1805
1806 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001807 cli_err(appctx, "A backend name is expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01001808 return NULL;
1809 }
1810
1811 px = proxy_be_by_name(arg);
1812 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001813 cli_err(appctx, "No such backend.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01001814 return NULL;
1815 }
1816 return px;
1817}
1818
1819
Willy Tarreau69f591e2020-07-01 07:00:59 +02001820/* parse a "show servers [state|conn]" CLI line, returns 0 if it wants to start
1821 * the dump or 1 if it stops immediately. If an argument is specified, it will
1822 * set the proxy pointer into cli.p0 and its ID into cli.i0. It sets cli.o0 to
1823 * 0 for "state", or 1 for "conn".
William Lallemanda6c5f332016-11-19 02:25:36 +01001824 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001825static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01001826{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001827 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001828
Willy Tarreau69f591e2020-07-01 07:00:59 +02001829 appctx->ctx.cli.o0 = *args[2] == 'c'; // "conn" vs "state"
1830
William Lallemanda6c5f332016-11-19 02:25:36 +01001831 /* check if a backend name has been provided */
1832 if (*args[3]) {
1833 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001834 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001835
Willy Tarreau9d008692019-08-09 11:21:01 +02001836 if (!px)
1837 return cli_err(appctx, "Can't find backend.\n");
1838
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001839 appctx->ctx.cli.p0 = px;
1840 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001841 }
1842 return 0;
1843}
1844
Willy Tarreau6ff81432020-07-01 07:02:42 +02001845/* dumps server state information for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001846 * These information are all the parameters which may change during HAProxy runtime.
1847 * By default, we only export to the last known server state file format.
1848 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001849 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1850 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001851 */
Willy Tarreau6ff81432020-07-01 07:02:42 +02001852static int dump_servers_state(struct stream_interface *si)
William Lallemanda6c5f332016-11-19 02:25:36 +01001853{
1854 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001855 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001856 struct server *srv;
1857 char srv_addr[INET6_ADDRSTRLEN + 1];
1858 time_t srv_time_since_last_change;
1859 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001860 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01001861
William Lallemanda6c5f332016-11-19 02:25:36 +01001862 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau6daac192019-02-02 17:39:53 +01001863 if (!(proc_mask(px->bind_proc) & pid_bit))
William Lallemanda6c5f332016-11-19 02:25:36 +01001864 return 1;
1865
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001866 if (!appctx->ctx.cli.p1)
1867 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001868
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001869 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1870 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001871 srv_addr[0] = '\0';
1872
1873 switch (srv->addr.ss_family) {
1874 case AF_INET:
1875 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in *)&srv->addr)->sin_addr,
1876 srv_addr, INET_ADDRSTRLEN + 1);
1877 break;
1878 case AF_INET6:
1879 inet_ntop(srv->addr.ss_family, &((struct sockaddr_in6 *)&srv->addr)->sin6_addr,
1880 srv_addr, INET6_ADDRSTRLEN + 1);
1881 break;
Daniel Corbett9215ffa2018-05-19 19:43:24 -04001882 default:
1883 memcpy(srv_addr, "-\0", 2);
1884 break;
William Lallemanda6c5f332016-11-19 02:25:36 +01001885 }
1886 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001887 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001888 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1889
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001890 srvrecord = NULL;
1891 if (srv->srvrq && srv->srvrq->name)
1892 srvrecord = srv->srvrq->name;
1893
Willy Tarreau69f591e2020-07-01 07:00:59 +02001894 if (appctx->ctx.cli.o0 == 0) {
1895 /* show servers state */
1896 chunk_printf(&trash,
1897 "%d %s "
1898 "%d %s %s "
1899 "%d %d %d %d %ld "
1900 "%d %d %d %d %d "
1901 "%d %d %s %u %s"
1902 "\n",
1903 px->uuid, px->id,
1904 srv->puid, srv->id, srv_addr,
1905 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
1906 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
1907 bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port,
1908 srvrecord ? srvrecord : "-");
1909 } else {
1910 /* show servers conn */
1911 int thr;
1912
1913 chunk_printf(&trash,
1914 "%s/%s %d/%d %s %u - %u %u %u %u %u %u %d %u",
1915 px->id, srv->id, px->uuid, srv->puid, srv_addr,srv->svc_port,
1916 srv->pool_purge_delay,
1917 srv->curr_used_conns, srv->max_used_conns, srv->est_need_conns,
1918 srv->curr_idle_nb, srv->curr_safe_nb, (int)srv->max_idle_conns, srv->curr_idle_conns);
1919
Willy Tarreau42abe682020-07-02 15:19:57 +02001920 for (thr = 0; thr < global.nbthread && srv->curr_idle_thr; thr++)
Willy Tarreau69f591e2020-07-01 07:00:59 +02001921 chunk_appendf(&trash, " %u", srv->curr_idle_thr[thr]);
1922
1923 chunk_appendf(&trash, "\n");
1924 }
1925
Willy Tarreau06d80a92017-10-19 14:32:15 +02001926 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001927 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001928 return 0;
1929 }
1930 }
1931 return 1;
1932}
1933
1934/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001935 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1936 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001937 */
1938static int cli_io_handler_servers_state(struct appctx *appctx)
1939{
1940 struct stream_interface *si = appctx->owner;
William Lallemanda6c5f332016-11-19 02:25:36 +01001941 struct proxy *curproxy;
1942
1943 chunk_reset(&trash);
1944
1945 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001946 if (!appctx->ctx.cli.p0)
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001947 appctx->ctx.cli.p0 = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01001948 appctx->st2 = STAT_ST_HEAD;
1949 }
1950
1951 if (appctx->st2 == STAT_ST_HEAD) {
Willy Tarreau69f591e2020-07-01 07:00:59 +02001952 if (appctx->ctx.cli.o0 == 0)
1953 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
1954 else
1955 chunk_printf(&trash,
1956 "# 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",
1957 global.nbthread);
1958
Willy Tarreau06d80a92017-10-19 14:32:15 +02001959 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001960 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001961 return 0;
1962 }
1963 appctx->st2 = STAT_ST_INFO;
1964 }
1965
1966 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001967 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1968 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001969 /* servers are only in backends */
1970 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreau6ff81432020-07-01 07:02:42 +02001971 if (!dump_servers_state(si))
William Lallemanda6c5f332016-11-19 02:25:36 +01001972 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001973 }
1974 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001975 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001976 break;
1977 }
1978
1979 return 1;
1980}
1981
Willy Tarreau608ea592016-12-16 18:01:15 +01001982/* Parses backend list and simply report backend names. It keeps the proxy
1983 * pointer in cli.p0.
1984 */
William Lallemand933efcd2016-11-22 12:34:16 +01001985static int cli_io_handler_show_backend(struct appctx *appctx)
1986{
William Lallemand933efcd2016-11-22 12:34:16 +01001987 struct stream_interface *si = appctx->owner;
1988 struct proxy *curproxy;
1989
1990 chunk_reset(&trash);
1991
Willy Tarreau608ea592016-12-16 18:01:15 +01001992 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001993 chunk_printf(&trash, "# name\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001994 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001995 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001996 return 0;
1997 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001998 appctx->ctx.cli.p0 = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01001999 }
2000
Willy Tarreau608ea592016-12-16 18:01:15 +01002001 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
2002 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01002003
2004 /* looking for backends only */
2005 if (!(curproxy->cap & PR_CAP_BE))
2006 continue;
2007
2008 /* we don't want to list a backend which is bound to this process */
Willy Tarreau6daac192019-02-02 17:39:53 +01002009 if (!(proc_mask(curproxy->bind_proc) & pid_bit))
William Lallemand933efcd2016-11-22 12:34:16 +01002010 continue;
2011
2012 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreau06d80a92017-10-19 14:32:15 +02002013 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002014 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01002015 return 0;
2016 }
2017 }
2018
2019 return 1;
2020}
William Lallemanda6c5f332016-11-19 02:25:36 +01002021
Willy Tarreaua275a372018-08-21 14:50:44 +02002022/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
2023 *
2024 * Grabs the proxy lock and each server's lock.
2025 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002026static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01002027{
2028 struct proxy *px;
2029 struct server *s;
2030
2031 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2032 return 1;
2033
2034 px = cli_find_backend(appctx, args[3]);
2035 if (!px)
2036 return 1;
2037
Willy Tarreau5e83d992019-07-30 11:59:34 +02002038 /* Note: this lock is to make sure this doesn't change while another
2039 * thread is in srv_set_dyncookie().
2040 */
Willy Tarreaua275a372018-08-21 14:50:44 +02002041 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002042 px->ck_opts |= PR_CK_DYNAMIC;
Willy Tarreau5e83d992019-07-30 11:59:34 +02002043 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002044
Willy Tarreaua275a372018-08-21 14:50:44 +02002045 for (s = px->srv; s != NULL; s = s->next) {
2046 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002047 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02002048 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2049 }
2050
Olivier Houchard614f8d72017-03-14 20:08:46 +01002051 return 1;
2052}
2053
Willy Tarreaua275a372018-08-21 14:50:44 +02002054/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
2055 *
2056 * Grabs the proxy lock and each server's lock.
2057 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002058static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01002059{
2060 struct proxy *px;
2061 struct server *s;
2062
2063 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2064 return 1;
2065
2066 px = cli_find_backend(appctx, args[3]);
2067 if (!px)
2068 return 1;
2069
Willy Tarreau5e83d992019-07-30 11:59:34 +02002070 /* Note: this lock is to make sure this doesn't change while another
2071 * thread is in srv_set_dyncookie().
2072 */
Willy Tarreaua275a372018-08-21 14:50:44 +02002073 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002074 px->ck_opts &= ~PR_CK_DYNAMIC;
Willy Tarreau5e83d992019-07-30 11:59:34 +02002075 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002076
2077 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02002078 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002079 if (!(s->flags & SRV_F_COOKIESET)) {
2080 free(s->cookie);
2081 s->cookie = NULL;
2082 }
Willy Tarreaua275a372018-08-21 14:50:44 +02002083 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002084 }
2085
2086 return 1;
2087}
2088
Willy Tarreaua275a372018-08-21 14:50:44 +02002089/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
2090 *
2091 * Grabs the proxy lock and each server's lock.
2092 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002093static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01002094{
2095 struct proxy *px;
2096 struct server *s;
2097 char *newkey;
2098
2099 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2100 return 1;
2101
2102 px = cli_find_backend(appctx, args[3]);
2103 if (!px)
2104 return 1;
2105
Willy Tarreau9d008692019-08-09 11:21:01 +02002106 if (!*args[4])
2107 return cli_err(appctx, "String value expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01002108
2109 newkey = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002110 if (!newkey)
2111 return cli_err(appctx, "Failed to allocate memory.\n");
Willy Tarreaua275a372018-08-21 14:50:44 +02002112
Willy Tarreau5e83d992019-07-30 11:59:34 +02002113 /* Note: this lock is to make sure this doesn't change while another
2114 * thread is in srv_set_dyncookie().
2115 */
Willy Tarreaua275a372018-08-21 14:50:44 +02002116 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002117 free(px->dyncookie_key);
2118 px->dyncookie_key = newkey;
Willy Tarreau5e83d992019-07-30 11:59:34 +02002119 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002120
Willy Tarreaua275a372018-08-21 14:50:44 +02002121 for (s = px->srv; s != NULL; s = s->next) {
2122 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002123 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02002124 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2125 }
2126
Olivier Houchard614f8d72017-03-14 20:08:46 +01002127 return 1;
2128}
2129
Willy Tarreaua275a372018-08-21 14:50:44 +02002130/* Parses the "set maxconn frontend" directive, it always returns 1.
2131 *
2132 * Grabs the proxy lock.
2133 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002134static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002135{
2136 struct proxy *px;
2137 struct listener *l;
2138 int v;
2139
2140 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2141 return 1;
2142
2143 px = cli_find_frontend(appctx, args[3]);
2144 if (!px)
2145 return 1;
2146
Willy Tarreau9d008692019-08-09 11:21:01 +02002147 if (!*args[4])
2148 return cli_err(appctx, "Integer value expected.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002149
2150 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002151 if (v < 0)
2152 return cli_err(appctx, "Value out of range.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002153
2154 /* OK, the value is fine, so we assign it to the proxy and to all of
2155 * its listeners. The blocked ones will be dequeued.
2156 */
Willy Tarreaua275a372018-08-21 14:50:44 +02002157 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2158
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002159 px->maxconn = v;
2160 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002161 if (l->state == LI_FULL)
2162 resume_listener(l);
2163 }
2164
Willy Tarreau241797a2019-12-10 14:10:52 +01002165 if (px->maxconn > px->feconn)
2166 dequeue_proxy_listeners(px);
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002167
Willy Tarreaua275a372018-08-21 14:50:44 +02002168 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2169
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002170 return 1;
2171}
2172
Willy Tarreaua275a372018-08-21 14:50:44 +02002173/* Parses the "shutdown frontend" directive, it always returns 1.
2174 *
2175 * Grabs the proxy lock.
2176 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002177static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002178{
2179 struct proxy *px;
2180
2181 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2182 return 1;
2183
2184 px = cli_find_frontend(appctx, args[2]);
2185 if (!px)
2186 return 1;
2187
Willy Tarreauc3914d42020-09-24 08:39:22 +02002188 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002189 return cli_msg(appctx, LOG_NOTICE, "Frontend was already shut down.\n");
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002190
William Dauchy1e2256d2020-01-25 23:45:18 +01002191 ha_warning("Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01002192 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
William Dauchy1e2256d2020-01-25 23:45:18 +01002193 send_log(px, LOG_WARNING, "Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002194 px->id, px->fe_counters.cum_conn, px->be_counters.cum_conn);
Willy Tarreaua275a372018-08-21 14:50:44 +02002195
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002196 stop_proxy(px);
2197 return 1;
2198}
2199
Willy Tarreaua275a372018-08-21 14:50:44 +02002200/* Parses the "disable frontend" directive, it always returns 1.
2201 *
2202 * Grabs the proxy lock.
2203 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002204static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01002205{
2206 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02002207 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002208
2209 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2210 return 1;
2211
2212 px = cli_find_frontend(appctx, args[2]);
2213 if (!px)
2214 return 1;
2215
Willy Tarreauc3914d42020-09-24 08:39:22 +02002216 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002217 return cli_msg(appctx, LOG_NOTICE, "Frontend was previously shut down, cannot disable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002218
Willy Tarreauf18d9682020-09-24 08:04:27 +02002219 if (!px->li_ready)
2220 return cli_msg(appctx, LOG_NOTICE, "All sockets are already disabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002221
Willy Tarreaua275a372018-08-21 14:50:44 +02002222 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2223 ret = pause_proxy(px);
2224 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2225
Willy Tarreau9d008692019-08-09 11:21:01 +02002226 if (!ret)
2227 return cli_err(appctx, "Failed to pause frontend, check logs for precise cause.\n");
2228
Willy Tarreau15b9e682016-11-24 11:55:28 +01002229 return 1;
2230}
2231
Willy Tarreaua275a372018-08-21 14:50:44 +02002232/* Parses the "enable frontend" directive, it always returns 1.
2233 *
2234 * Grabs the proxy lock.
2235 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002236static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01002237{
2238 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02002239 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002240
2241 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2242 return 1;
2243
2244 px = cli_find_frontend(appctx, args[2]);
2245 if (!px)
2246 return 1;
2247
Willy Tarreauc3914d42020-09-24 08:39:22 +02002248 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002249 return cli_err(appctx, "Frontend was previously shut down, cannot enable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002250
Willy Tarreauf18d9682020-09-24 08:04:27 +02002251 if (px->li_ready == px->li_all)
2252 return cli_msg(appctx, LOG_NOTICE, "All sockets are already enabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002253
Willy Tarreaua275a372018-08-21 14:50:44 +02002254 HA_SPIN_LOCK(PROXY_LOCK, &px->lock);
2255 ret = resume_proxy(px);
2256 HA_SPIN_UNLOCK(PROXY_LOCK, &px->lock);
2257
Willy Tarreau9d008692019-08-09 11:21:01 +02002258 if (!ret)
2259 return cli_err(appctx, "Failed to resume frontend, check logs for precise cause (port conflict?).\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002260 return 1;
2261}
2262
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002263/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
2264 * now.
2265 */
2266static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
2267{
2268 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2269 return 1;
2270
2271 if (*args[2]) {
2272 struct proxy *px;
2273
2274 px = proxy_find_by_name(args[2], 0, 0);
2275 if (px)
2276 appctx->ctx.errors.iid = px->uuid;
2277 else
2278 appctx->ctx.errors.iid = atoi(args[2]);
2279
Willy Tarreau9d008692019-08-09 11:21:01 +02002280 if (!appctx->ctx.errors.iid)
2281 return cli_err(appctx, "No such proxy.\n");
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002282 }
2283 else
2284 appctx->ctx.errors.iid = -1; // dump all proxies
2285
2286 appctx->ctx.errors.flag = 0;
2287 if (strcmp(args[3], "request") == 0)
2288 appctx->ctx.errors.flag |= 4; // ignore response
2289 else if (strcmp(args[3], "response") == 0)
2290 appctx->ctx.errors.flag |= 2; // ignore request
2291 appctx->ctx.errors.px = NULL;
2292 return 0;
2293}
2294
2295/* This function dumps all captured errors onto the stream interface's
2296 * read buffer. It returns 0 if the output buffer is full and it needs
2297 * to be called again, otherwise non-zero.
2298 */
2299static int cli_io_handler_show_errors(struct appctx *appctx)
2300{
2301 struct stream_interface *si = appctx->owner;
2302 extern const char *monthname[12];
2303
2304 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2305 return 1;
2306
2307 chunk_reset(&trash);
2308
2309 if (!appctx->ctx.errors.px) {
2310 /* the function had not been called yet, let's prepare the
2311 * buffer for a response.
2312 */
2313 struct tm tm;
2314
2315 get_localtime(date.tv_sec, &tm);
2316 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2317 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2318 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2319 error_snapshot_id);
2320
Willy Tarreau36b27362018-09-07 19:55:44 +02002321 if (ci_putchk(si_ic(si), &trash) == -1)
2322 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002323
2324 appctx->ctx.errors.px = proxies_list;
2325 appctx->ctx.errors.bol = 0;
2326 appctx->ctx.errors.ptr = -1;
2327 }
2328
2329 /* we have two inner loops here, one for the proxy, the other one for
2330 * the buffer.
2331 */
2332 while (appctx->ctx.errors.px) {
2333 struct error_snapshot *es;
2334
Willy Tarreau36b27362018-09-07 19:55:44 +02002335 HA_SPIN_LOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2336
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002337 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002338 es = appctx->ctx.errors.px->invalid_req;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002339 if (appctx->ctx.errors.flag & 2) // skip req
2340 goto next;
2341 }
2342 else {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002343 es = appctx->ctx.errors.px->invalid_rep;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002344 if (appctx->ctx.errors.flag & 4) // skip resp
2345 goto next;
2346 }
2347
Willy Tarreauc55015e2018-09-07 19:02:32 +02002348 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002349 goto next;
2350
2351 if (appctx->ctx.errors.iid >= 0 &&
2352 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002353 (!es->oe || es->oe->uuid != appctx->ctx.errors.iid))
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002354 goto next;
2355
2356 if (appctx->ctx.errors.ptr < 0) {
2357 /* just print headers now */
2358
2359 char pn[INET6_ADDRSTRLEN];
2360 struct tm tm;
2361 int port;
2362
2363 get_localtime(es->when.tv_sec, &tm);
2364 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2365 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2366 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2367
2368 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2369 case AF_INET:
2370 case AF_INET6:
2371 port = get_host_port(&es->src);
2372 break;
2373 default:
2374 port = 0;
2375 }
2376
2377 switch (appctx->ctx.errors.flag & 1) {
2378 case 0:
2379 chunk_appendf(&trash,
2380 " frontend %s (#%d): invalid request\n"
2381 " backend %s (#%d)",
2382 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002383 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2384 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002385 break;
2386 case 1:
2387 chunk_appendf(&trash,
2388 " backend %s (#%d): invalid response\n"
2389 " frontend %s (#%d)",
2390 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002391 es->oe ? es->oe->id : "<NONE>" , es->oe ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002392 break;
2393 }
2394
2395 chunk_appendf(&trash,
2396 ", server %s (#%d), event #%u, src %s:%d\n"
2397 " buffer starts at %llu (including %u out), %u free,\n"
2398 " len %u, wraps at %u, error at position %u\n",
2399 es->srv ? es->srv->id : "<NONE>",
2400 es->srv ? es->srv->puid : -1,
2401 es->ev_id, pn, port,
2402 es->buf_ofs, es->buf_out,
2403 global.tune.bufsize - es->buf_out - es->buf_len,
2404 es->buf_len, es->buf_wrap, es->buf_err);
2405
2406 if (es->show)
2407 es->show(&trash, es);
2408
2409 chunk_appendf(&trash, " \n");
2410
Willy Tarreau36b27362018-09-07 19:55:44 +02002411 if (ci_putchk(si_ic(si), &trash) == -1)
2412 goto cant_send_unlock;
2413
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002414 appctx->ctx.errors.ptr = 0;
2415 appctx->ctx.errors.ev_id = es->ev_id;
2416 }
2417
2418 if (appctx->ctx.errors.ev_id != es->ev_id) {
2419 /* the snapshot changed while we were dumping it */
2420 chunk_appendf(&trash,
2421 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau36b27362018-09-07 19:55:44 +02002422 if (ci_putchk(si_ic(si), &trash) == -1)
2423 goto cant_send_unlock;
2424
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002425 goto next;
2426 }
2427
2428 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002429 while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002430 int newptr;
2431 int newline;
2432
2433 newline = appctx->ctx.errors.bol;
2434 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
2435 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau36b27362018-09-07 19:55:44 +02002436 goto cant_send_unlock;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002437
Willy Tarreau36b27362018-09-07 19:55:44 +02002438 if (ci_putchk(si_ic(si), &trash) == -1)
2439 goto cant_send_unlock;
2440
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002441 appctx->ctx.errors.ptr = newptr;
2442 appctx->ctx.errors.bol = newline;
2443 };
2444 next:
Willy Tarreau36b27362018-09-07 19:55:44 +02002445 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002446 appctx->ctx.errors.bol = 0;
2447 appctx->ctx.errors.ptr = -1;
2448 appctx->ctx.errors.flag ^= 1;
2449 if (!(appctx->ctx.errors.flag & 1))
2450 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2451 }
2452
2453 /* dump complete */
2454 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02002455
2456 cant_send_unlock:
2457 HA_SPIN_UNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
2458 cant_send:
Willy Tarreaudb398432018-11-15 11:08:52 +01002459 si_rx_room_blk(si);
Willy Tarreau36b27362018-09-07 19:55:44 +02002460 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002461}
2462
William Lallemanda6c5f332016-11-19 02:25:36 +01002463/* register cli keywords */
2464static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01002465 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
2466 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002467 { { "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 +02002468 { { "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 +01002469 { { "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 +01002470 { { "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 +01002471 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
Olivier Houchard614f8d72017-03-14 20:08:46 +01002472 { { "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 },
2473 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
2474 { { "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 +02002475 { { "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 +01002476 {{},}
2477}};
2478
Willy Tarreau0108d902018-11-25 19:14:37 +01002479INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002480
2481/*
2482 * Local variables:
2483 * c-indent-level: 8
2484 * c-basic-offset: 8
2485 * End:
2486 */