blob: 009ebbc125c37ac20b1140cf6c87f2f64ebcc807 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Proxy variables and functions.
3 *
Willy Tarreau3a7d2072009-03-05 23:48:25 +01004 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <fcntl.h>
14#include <unistd.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010015#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020016#include <sys/types.h>
17#include <sys/socket.h>
18#include <sys/stat.h>
19
Willy Tarreaub2551052020-06-09 09:07:15 +020020#include <import/eb32tree.h>
21#include <import/ebistree.h>
22
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020024#include <haproxy/applet-t.h>
25#include <haproxy/capture-t.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020026#include <haproxy/cfgparse.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020028#include <haproxy/errors.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/fd.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020030#include <haproxy/filters.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020031#include <haproxy/global.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020032#include <haproxy/http_ana.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020033#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020034#include <haproxy/log.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020035#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020036#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020037#include <haproxy/pool.h>
Willy Tarreau626f3a72020-10-07 16:52:43 +020038#include <haproxy/protocol.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020039#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020040#include <haproxy/proxy.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020041#include <haproxy/server-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020043#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020045#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020046#include <haproxy/task.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020047#include <haproxy/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
Willy Tarreau918ff602011-07-25 16:33:49 +020050int listeners; /* # of proxy listeners, set by cfgparse */
Olivier Houchardfbc74e82017-11-24 16:54:05 +010051struct proxy *proxies_list = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020052struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010053struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau10479e42010-12-12 14:00:34 +010054unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010056/* proxy->options */
57const struct cfg_opt cfg_opts[] =
58{
59 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0, 0 },
60 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0, 0 },
61 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0, PR_MODE_HTTP },
62 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0, 0 },
63 { "contstats", PR_O_CONTSTATS, PR_CAP_FE, 0, 0 },
64 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0, 0 },
65 { "http_proxy", PR_O_HTTP_PROXY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
66 { "http-buffer-request", PR_O_WREQ_BODY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
67 { "http-ignore-probes", PR_O_IGNORE_PRB, PR_CAP_FE, 0, PR_MODE_HTTP },
68 { "prefer-last-server", PR_O_PREF_LAST, PR_CAP_BE, 0, PR_MODE_HTTP },
69 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0, 0 },
70 { "nolinger", PR_O_TCP_NOLING, PR_CAP_FE | PR_CAP_BE, 0, 0 },
71 { "persist", PR_O_PERSIST, PR_CAP_BE, 0, 0 },
72 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0, 0 },
Willy Tarreaue5733232019-05-22 19:24:06 +020073#ifdef USE_TPROXY
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010074 { "transparent", PR_O_TRANSP, PR_CAP_BE, 0, 0 },
75#else
76 { "transparent", 0, 0, 0, 0 },
77#endif
78
79 { NULL, 0, 0, 0, 0 }
80};
81
82/* proxy->options2 */
83const struct cfg_opt cfg_opts2[] =
84{
Willy Tarreaue5733232019-05-22 19:24:06 +020085#ifdef USE_LINUX_SPLICE
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010086 { "splice-request", PR_O2_SPLIC_REQ, PR_CAP_FE|PR_CAP_BE, 0, 0 },
87 { "splice-response", PR_O2_SPLIC_RTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
88 { "splice-auto", PR_O2_SPLIC_AUT, PR_CAP_FE|PR_CAP_BE, 0, 0 },
89#else
90 { "splice-request", 0, 0, 0, 0 },
91 { "splice-response", 0, 0, 0, 0 },
92 { "splice-auto", 0, 0, 0, 0 },
93#endif
94 { "accept-invalid-http-request", PR_O2_REQBUG_OK, PR_CAP_FE, 0, PR_MODE_HTTP },
95 { "accept-invalid-http-response", PR_O2_RSPBUG_OK, PR_CAP_BE, 0, PR_MODE_HTTP },
96 { "dontlog-normal", PR_O2_NOLOGNORM, PR_CAP_FE, 0, 0 },
97 { "log-separate-errors", PR_O2_LOGERRORS, PR_CAP_FE, 0, 0 },
98 { "log-health-checks", PR_O2_LOGHCHKS, PR_CAP_BE, 0, 0 },
99 { "socket-stats", PR_O2_SOCKSTAT, PR_CAP_FE, 0, 0 },
100 { "tcp-smart-accept", PR_O2_SMARTACC, PR_CAP_FE, 0, 0 },
101 { "tcp-smart-connect", PR_O2_SMARTCON, PR_CAP_BE, 0, 0 },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100102 { "independent-streams", PR_O2_INDEPSTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
103 { "http-use-proxy-header", PR_O2_USE_PXHDR, PR_CAP_FE, 0, PR_MODE_HTTP },
104 { "http-pretend-keepalive", PR_O2_FAKE_KA, PR_CAP_BE, 0, PR_MODE_HTTP },
105 { "http-no-delay", PR_O2_NODELAY, PR_CAP_FE|PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet31930372019-07-15 10:16:58 +0200106 { "http-use-htx", 0, PR_CAP_FE|PR_CAP_BE, 0, 0 }, // deprecated
Christopher Faulet98fbe952019-07-22 16:18:24 +0200107
108 {"h1-case-adjust-bogus-client", PR_O2_H1_ADJ_BUGCLI, PR_CAP_FE, 0, PR_MODE_HTTP },
109 {"h1-case-adjust-bogus-server", PR_O2_H1_ADJ_BUGSRV, PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet89aed322020-06-02 17:33:56 +0200110 {"disable-h2-upgrade", PR_O2_NO_H2_UPGRADE, PR_CAP_FE, 0, PR_MODE_HTTP },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100111 { NULL, 0, 0, 0 }
112};
113
Willy Tarreau977b8e42006-12-29 14:19:17 +0100114/*
Willy Tarreau816eb542007-11-04 07:04:43 +0100115 * This function returns a string containing a name describing capabilities to
116 * report comprehensible error messages. Specifically, it will return the words
Christopher Faulet898566e2016-10-26 11:06:28 +0200117 * "frontend", "backend" when appropriate, or "proxy" for all other
Willy Tarreau816eb542007-11-04 07:04:43 +0100118 * cases including the proxies declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +0100119 */
Willy Tarreau816eb542007-11-04 07:04:43 +0100120const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +0100121{
Willy Tarreau816eb542007-11-04 07:04:43 +0100122 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
123 if (cap & PR_CAP_FE)
124 return "frontend";
125 else if (cap & PR_CAP_BE)
126 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +0100127 }
128 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +0100129}
130
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100131/*
132 * This function returns a string containing the mode of the proxy in a format
133 * suitable for error messages.
134 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100135const char *proxy_mode_str(int mode) {
136
137 if (mode == PR_MODE_TCP)
138 return "tcp";
139 else if (mode == PR_MODE_HTTP)
140 return "http";
William Lallemandcf62f7e2018-10-26 14:47:40 +0200141 else if (mode == PR_MODE_CLI)
142 return "cli";
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100143 else
144 return "unknown";
145}
146
Willy Tarreauf3950172009-10-10 18:35:51 +0200147/*
148 * This function scans the list of backends and servers to retrieve the first
149 * backend and the first server with the given names, and sets them in both
150 * parameters. It returns zero if either is not found, or non-zero and sets
151 * the ones it did not found to NULL. If a NULL pointer is passed for the
152 * backend, only the pointer to the server will be updated.
153 */
154int get_backend_server(const char *bk_name, const char *sv_name,
155 struct proxy **bk, struct server **sv)
156{
157 struct proxy *p;
158 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100159 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200160
161 *sv = NULL;
162
Willy Tarreau050536d2012-10-04 08:47:34 +0200163 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200164 if (*sv_name == '#')
165 sid = atoi(sv_name + 1);
166
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200167 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200168 if (bk)
169 *bk = p;
170 if (!p)
171 return 0;
172
173 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100174 if ((sid >= 0 && s->puid == sid) ||
175 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200176 break;
177 *sv = s;
178 if (!s)
179 return 0;
180 return 1;
181}
182
Willy Tarreaue219db72007-12-03 01:30:13 +0100183/* This function parses a "timeout" statement in a proxy section. It returns
184 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200185 * return zero, it will write an error or warning message into a preallocated
186 * buffer returned at <err>. The trailing is not be written. The function must
187 * be called with <args> pointing to the first command line word, with <proxy>
188 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
189 * As a special case for compatibility with older configs, it also accepts
190 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100191 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200192static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200193 struct proxy *defpx, const char *file, int line,
194 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100195{
196 unsigned timeout;
197 int retval, cap;
198 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200199 int *tv = NULL;
200 int *td = NULL;
Willy Tarreaue219db72007-12-03 01:30:13 +0100201
202 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200203
204 /* simply skip "timeout" but remain compatible with old form */
205 if (strcmp(args[0], "timeout") == 0)
206 args++;
207
Willy Tarreaue219db72007-12-03 01:30:13 +0100208 name = args[0];
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100209 if (strcmp(args[0], "client") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100210 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100211 tv = &proxy->timeout.client;
212 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100213 cap = PR_CAP_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100214 } else if (strcmp(args[0], "tarpit") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100215 tv = &proxy->timeout.tarpit;
216 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100217 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100218 } else if (strcmp(args[0], "http-keep-alive") == 0) {
Willy Tarreaub16a5742010-01-10 14:46:16 +0100219 tv = &proxy->timeout.httpka;
220 td = &defpx->timeout.httpka;
221 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100222 } else if (strcmp(args[0], "http-request") == 0) {
Willy Tarreau036fae02008-01-06 13:24:40 +0100223 tv = &proxy->timeout.httpreq;
224 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200225 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100226 } else if (strcmp(args[0], "server") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100227 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100228 tv = &proxy->timeout.server;
229 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100230 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100231 } else if (strcmp(args[0], "connect") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100232 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100233 tv = &proxy->timeout.connect;
234 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100235 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100236 } else if (strcmp(args[0], "check") == 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100237 tv = &proxy->timeout.check;
238 td = &defpx->timeout.check;
239 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100240 } else if (strcmp(args[0], "queue") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100241 tv = &proxy->timeout.queue;
242 td = &defpx->timeout.queue;
243 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100244 } else if (strcmp(args[0], "tunnel") == 0) {
Willy Tarreauce887fd2012-05-12 12:50:00 +0200245 tv = &proxy->timeout.tunnel;
246 td = &defpx->timeout.tunnel;
247 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100248 } else if (strcmp(args[0], "client-fin") == 0) {
Willy Tarreau05cdd962014-05-10 14:30:07 +0200249 tv = &proxy->timeout.clientfin;
250 td = &defpx->timeout.clientfin;
251 cap = PR_CAP_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100252 } else if (strcmp(args[0], "server-fin") == 0) {
Willy Tarreau05cdd962014-05-10 14:30:07 +0200253 tv = &proxy->timeout.serverfin;
254 td = &defpx->timeout.serverfin;
255 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100256 } else if (strcmp(args[0], "clitimeout") == 0) {
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200257 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout client'.", args[0]);
258 return -1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100259 } else if (strcmp(args[0], "srvtimeout") == 0) {
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200260 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout server'.", args[0]);
261 return -1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100262 } else if (strcmp(args[0], "contimeout") == 0) {
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200263 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout connect'.", args[0]);
264 return -1;
Willy Tarreaue219db72007-12-03 01:30:13 +0100265 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200266 memprintf(err,
267 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200268 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
269 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200270 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100271 return -1;
272 }
273
274 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200275 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100276 return -1;
277 }
278
279 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200280 if (res == PARSE_TIME_OVER) {
281 memprintf(err, "timer overflow in argument '%s' to 'timeout %s' (maximum value is 2147483647 ms or ~24.8 days)",
282 args[1], name);
283 return -1;
284 }
285 else if (res == PARSE_TIME_UNDER) {
286 memprintf(err, "timer underflow in argument '%s' to 'timeout %s' (minimum non-null value is 1 ms)",
287 args[1], name);
288 return -1;
289 }
290 else if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200291 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100292 return -1;
293 }
294
295 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200296 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
297 name, proxy_type_str(proxy), proxy->id,
298 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100299 retval = 1;
300 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200301 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200302 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100303 retval = 1;
304 }
305
Willy Tarreaufac5b592014-05-22 08:24:46 +0200306 if (*args[2] != 0) {
307 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
308 retval = -1;
309 }
310
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200311 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100312 return retval;
313}
314
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100315/* This function parses a "rate-limit" statement in a proxy section. It returns
316 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200317 * return zero, it will write an error or warning message into a preallocated
318 * buffer returned at <err>. The function must be called with <args> pointing
319 * to the first command line word, with <proxy> pointing to the proxy being
320 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100321 */
322static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200323 struct proxy *defpx, const char *file, int line,
324 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100325{
William Dauchybb9da0b2020-01-16 01:34:27 +0100326 int retval;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200327 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100328 unsigned int *tv = NULL;
329 unsigned int *td = NULL;
330 unsigned int val;
331
332 retval = 0;
333
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200334 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200335 tv = &proxy->fe_sps_lim;
336 td = &defpx->fe_sps_lim;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200337 }
338 else {
339 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100340 return -1;
341 }
342
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200343 if (*args[2] == 0) {
344 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100345 return -1;
346 }
347
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200348 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100349 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200350 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 +0100351 return -1;
352 }
353
William Dauchybb9da0b2020-01-16 01:34:27 +0100354 if (!(proxy->cap & PR_CAP_FE)) {
355 memprintf(err, "%s %s will be ignored because %s '%s' has no frontend capability",
356 args[0], args[1], proxy_type_str(proxy), proxy->id);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100357 retval = 1;
358 }
359 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200360 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100361 retval = 1;
362 }
363
364 *tv = val;
365 return retval;
366}
367
Willy Tarreauc35362a2014-04-25 13:58:37 +0200368/* This function parses a "max-keep-alive-queue" statement in a proxy section.
369 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
370 * does not return zero, it will write an error or warning message into a
371 * preallocated buffer returned at <err>. The function must be called with
372 * <args> pointing to the first command line word, with <proxy> pointing to
373 * the proxy being parsed, and <defpx> to the default proxy or NULL.
374 */
375static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
376 struct proxy *defpx, const char *file, int line,
377 char **err)
378{
379 int retval;
380 char *res;
381 unsigned int val;
382
383 retval = 0;
384
385 if (*args[1] == 0) {
386 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
387 return -1;
388 }
389
390 val = strtol(args[1], &res, 0);
391 if (*res) {
392 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
393 return -1;
394 }
395
396 if (!(proxy->cap & PR_CAP_BE)) {
397 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
398 args[0], proxy_type_str(proxy), proxy->id);
399 retval = 1;
400 }
401
402 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
403 proxy->max_ka_queue = val + 1;
404 return retval;
405}
406
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200407/* This function parses a "declare" statement in a proxy section. It returns -1
408 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
409 * it will write an error or warning message into a preallocated buffer returned
410 * at <err>. The function must be called with <args> pointing to the first command
411 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
412 * default proxy or NULL.
413 */
414static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
415 struct proxy *defpx, const char *file, int line,
416 char **err)
417{
418 /* Capture keyword wannot be declared in a default proxy. */
419 if (curpx == defpx) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800420 memprintf(err, "'%s' not available in default section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200421 return -1;
422 }
423
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700424 /* Capture keyword is only available in frontend. */
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200425 if (!(curpx->cap & PR_CAP_FE)) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800426 memprintf(err, "'%s' only available in frontend or listen section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200427 return -1;
428 }
429
430 /* Check mandatory second keyword. */
431 if (!args[1] || !*args[1]) {
432 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
433 return -1;
434 }
435
Joseph Herlant59dd2952018-11-15 11:46:55 -0800436 /* Actually, declare is only available for declaring capture
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200437 * slot, but in the future it can declare maps or variables.
Joseph Herlant59dd2952018-11-15 11:46:55 -0800438 * So, this section permits to check and switch according with
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200439 * the second keyword.
440 */
441 if (strcmp(args[1], "capture") == 0) {
442 char *error = NULL;
443 long len;
444 struct cap_hdr *hdr;
445
446 /* Check the next keyword. */
447 if (!args[2] || !*args[2] ||
448 (strcmp(args[2], "response") != 0 &&
449 strcmp(args[2], "request") != 0)) {
450 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
451 return -1;
452 }
453
454 /* Check the 'len' keyword. */
455 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
456 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
457 return -1;
458 }
459
460 /* Check the length value. */
461 if (!args[4] || !*args[4]) {
462 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
463 "capture length",
464 args[0], args[1]);
465 return -1;
466 }
467
468 /* convert the length value. */
469 len = strtol(args[4], &error, 10);
470 if (*error != '\0') {
471 memprintf(err, "'%s %s': cannot parse the length '%s'.",
472 args[0], args[1], args[3]);
473 return -1;
474 }
475
476 /* check length. */
477 if (len <= 0) {
478 memprintf(err, "length must be > 0");
479 return -1;
480 }
481
482 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200483 hdr = calloc(1, sizeof(*hdr));
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200484 hdr->name = NULL; /* not a header capture */
485 hdr->namelen = 0;
486 hdr->len = len;
487 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
488
489 if (strcmp(args[2], "request") == 0) {
490 hdr->next = curpx->req_cap;
491 hdr->index = curpx->nb_req_cap++;
492 curpx->req_cap = hdr;
493 }
494 if (strcmp(args[2], "response") == 0) {
495 hdr->next = curpx->rsp_cap;
496 hdr->index = curpx->nb_rsp_cap++;
497 curpx->rsp_cap = hdr;
498 }
499 return 0;
500 }
501 else {
502 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
503 return -1;
504 }
505}
506
Olivier Houcharda254a372019-04-05 15:30:12 +0200507/* This function parses a "retry-on" statement */
508static int
509proxy_parse_retry_on(char **args, int section, struct proxy *curpx,
510 struct proxy *defpx, const char *file, int line,
511 char **err)
512{
513 int i;
514
515 if (!(*args[1])) {
516 memprintf(err, "'%s' needs at least one keyword to specify when to retry", args[0]);
517 return -1;
518 }
519 if (!(curpx->cap & PR_CAP_BE)) {
520 memprintf(err, "'%s' only available in backend or listen section", args[0]);
521 return -1;
522 }
523 curpx->retry_type = 0;
524 for (i = 1; *(args[i]); i++) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100525 if (strcmp(args[i], "conn-failure") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200526 curpx->retry_type |= PR_RE_CONN_FAILED;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100527 else if (strcmp(args[i], "empty-response") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200528 curpx->retry_type |= PR_RE_DISCONNECTED;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100529 else if (strcmp(args[i], "response-timeout") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200530 curpx->retry_type |= PR_RE_TIMEOUT;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100531 else if (strcmp(args[i], "401") == 0)
Julien Pivotto2de240a2020-11-12 11:14:05 +0100532 curpx->retry_type |= PR_RE_401;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100533 else if (strcmp(args[i], "403") == 0)
Julien Pivotto2de240a2020-11-12 11:14:05 +0100534 curpx->retry_type |= PR_RE_403;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100535 else if (strcmp(args[i], "404") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200536 curpx->retry_type |= PR_RE_404;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100537 else if (strcmp(args[i], "408") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200538 curpx->retry_type |= PR_RE_408;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100539 else if (strcmp(args[i], "425") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200540 curpx->retry_type |= PR_RE_425;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100541 else if (strcmp(args[i], "500") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200542 curpx->retry_type |= PR_RE_500;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100543 else if (strcmp(args[i], "501") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200544 curpx->retry_type |= PR_RE_501;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100545 else if (strcmp(args[i], "502") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200546 curpx->retry_type |= PR_RE_502;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100547 else if (strcmp(args[i], "503") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200548 curpx->retry_type |= PR_RE_503;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100549 else if (strcmp(args[i], "504") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200550 curpx->retry_type |= PR_RE_504;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100551 else if (strcmp(args[i], "0rtt-rejected") == 0)
Olivier Houchard865d8392019-05-03 22:46:27 +0200552 curpx->retry_type |= PR_RE_EARLY_ERROR;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100553 else if (strcmp(args[i], "junk-response") == 0)
Olivier Houcharde3249a92019-05-03 23:01:47 +0200554 curpx->retry_type |= PR_RE_JUNK_REQUEST;
Olivier Houchardddf0e032019-05-10 18:05:40 +0200555 else if (!(strcmp(args[i], "all-retryable-errors")))
556 curpx->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED |
557 PR_RE_TIMEOUT | PR_RE_500 | PR_RE_502 |
558 PR_RE_503 | PR_RE_504 | PR_RE_EARLY_ERROR |
559 PR_RE_JUNK_REQUEST;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100560 else if (strcmp(args[i], "none") == 0) {
Olivier Houcharda254a372019-04-05 15:30:12 +0200561 if (i != 1 || *args[i + 1]) {
562 memprintf(err, "'%s' 'none' keyworld only usable alone", args[0]);
563 return -1;
564 }
565 } else {
566 memprintf(err, "'%s': unknown keyword '%s'", args[0], args[i]);
567 return -1;
568 }
569
570 }
571
572
573 return 0;
574}
575
Willy Tarreau52543212020-07-09 05:58:51 +0200576#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900577/* This function parses "{cli|srv}tcpka-cnt" statements */
578static int proxy_parse_tcpka_cnt(char **args, int section, struct proxy *proxy,
579 struct proxy *defpx, const char *file, int line,
580 char **err)
581{
582 int retval;
583 char *res;
584 unsigned int tcpka_cnt;
585
586 retval = 0;
587
588 if (*args[1] == 0) {
589 memprintf(err, "'%s' expects an integer value", args[0]);
590 return -1;
591 }
592
593 tcpka_cnt = strtol(args[1], &res, 0);
594 if (*res) {
595 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
596 return -1;
597 }
598
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100599 if (strcmp(args[0], "clitcpka-cnt") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900600 if (!(proxy->cap & PR_CAP_FE)) {
601 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
602 args[0], proxy_type_str(proxy), proxy->id);
603 retval = 1;
604 }
605 proxy->clitcpka_cnt = tcpka_cnt;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100606 } else if (strcmp(args[0], "srvtcpka-cnt") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900607 if (!(proxy->cap & PR_CAP_BE)) {
608 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
609 args[0], proxy_type_str(proxy), proxy->id);
610 retval = 1;
611 }
612 proxy->srvtcpka_cnt = tcpka_cnt;
613 } else {
614 /* unreachable */
615 memprintf(err, "'%s': unknown keyword", args[0]);
616 return -1;
617 }
618
619 return retval;
620}
Willy Tarreau52543212020-07-09 05:58:51 +0200621#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900622
Willy Tarreau52543212020-07-09 05:58:51 +0200623#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900624/* This function parses "{cli|srv}tcpka-idle" statements */
625static int proxy_parse_tcpka_idle(char **args, int section, struct proxy *proxy,
626 struct proxy *defpx, const char *file, int line,
627 char **err)
628{
629 int retval;
630 const char *res;
631 unsigned int tcpka_idle;
632
633 retval = 0;
634
635 if (*args[1] == 0) {
636 memprintf(err, "'%s' expects an integer value", args[0]);
637 return -1;
638 }
639 res = parse_time_err(args[1], &tcpka_idle, TIME_UNIT_S);
640 if (res == PARSE_TIME_OVER) {
641 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
642 args[1], args[0]);
643 return -1;
644 }
645 else if (res == PARSE_TIME_UNDER) {
646 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
647 args[1], args[0]);
648 return -1;
649 }
650 else if (res) {
651 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
652 return -1;
653 }
654
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100655 if (strcmp(args[0], "clitcpka-idle") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900656 if (!(proxy->cap & PR_CAP_FE)) {
657 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
658 args[0], proxy_type_str(proxy), proxy->id);
659 retval = 1;
660 }
661 proxy->clitcpka_idle = tcpka_idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100662 } else if (strcmp(args[0], "srvtcpka-idle") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900663 if (!(proxy->cap & PR_CAP_BE)) {
664 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
665 args[0], proxy_type_str(proxy), proxy->id);
666 retval = 1;
667 }
668 proxy->srvtcpka_idle = tcpka_idle;
669 } else {
670 /* unreachable */
671 memprintf(err, "'%s': unknown keyword", args[0]);
672 return -1;
673 }
674
675 return retval;
676}
Willy Tarreau52543212020-07-09 05:58:51 +0200677#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900678
Willy Tarreau52543212020-07-09 05:58:51 +0200679#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900680/* This function parses "{cli|srv}tcpka-intvl" statements */
681static int proxy_parse_tcpka_intvl(char **args, int section, struct proxy *proxy,
682 struct proxy *defpx, const char *file, int line,
683 char **err)
684{
685 int retval;
686 const char *res;
687 unsigned int tcpka_intvl;
688
689 retval = 0;
690
691 if (*args[1] == 0) {
692 memprintf(err, "'%s' expects an integer value", args[0]);
693 return -1;
694 }
695 res = parse_time_err(args[1], &tcpka_intvl, TIME_UNIT_S);
696 if (res == PARSE_TIME_OVER) {
697 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
698 args[1], args[0]);
699 return -1;
700 }
701 else if (res == PARSE_TIME_UNDER) {
702 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
703 args[1], args[0]);
704 return -1;
705 }
706 else if (res) {
707 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
708 return -1;
709 }
710
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100711 if (strcmp(args[0], "clitcpka-intvl") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900712 if (!(proxy->cap & PR_CAP_FE)) {
713 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
714 args[0], proxy_type_str(proxy), proxy->id);
715 retval = 1;
716 }
717 proxy->clitcpka_intvl = tcpka_intvl;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100718 } else if (strcmp(args[0], "srvtcpka-intvl") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900719 if (!(proxy->cap & PR_CAP_BE)) {
720 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
721 args[0], proxy_type_str(proxy), proxy->id);
722 retval = 1;
723 }
724 proxy->srvtcpka_intvl = tcpka_intvl;
725 } else {
726 /* unreachable */
727 memprintf(err, "'%s': unknown keyword", args[0]);
728 return -1;
729 }
730
731 return retval;
732}
Willy Tarreau52543212020-07-09 05:58:51 +0200733#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900734
Willy Tarreauf79d9502014-03-15 07:22:35 +0100735/* This function inserts proxy <px> into the tree of known proxies. The proxy's
736 * name is used as the storing key so it must already have been initialized.
737 */
738void proxy_store_name(struct proxy *px)
739{
740 px->conf.by_name.key = px->id;
741 ebis_insert(&proxy_by_name, &px->conf.by_name);
742}
743
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200744/* Returns a pointer to the first proxy matching capabilities <cap> and id
745 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
746 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +0200747 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200748struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200749{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200750 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +0200751
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200752 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
753 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100754
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200755 if (px->uuid != id)
756 break;
Alex Williams96532db2009-11-01 21:27:13 -0500757
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200758 if ((px->cap & cap) != cap)
759 continue;
Alex Williams96532db2009-11-01 21:27:13 -0500760
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100761 if (table && (!px->table || !px->table->size))
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200762 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100763
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200764 return px;
765 }
766 return NULL;
767}
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100768
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200769/* Returns a pointer to the first proxy matching either name <name>, or id
770 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
771 * If <table> is non-zero, it only considers proxies having a table.
772 */
773struct proxy *proxy_find_by_name(const char *name, int cap, int table)
774{
775 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200776
Willy Tarreau3c56a7d2015-05-26 15:25:32 +0200777 if (*name == '#') {
778 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
779 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +0200780 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -0500781 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100782 else {
783 struct ebpt_node *node;
784
785 for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) {
786 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -0500787
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100788 if (strcmp(curproxy->id, name) != 0)
789 break;
790
791 if ((curproxy->cap & cap) != cap)
792 continue;
793
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100794 if (table && (!curproxy->table || !curproxy->table->size))
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200795 continue;
796
Willy Tarreauc739aa82015-05-26 11:35:41 +0200797 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +0100798 }
799 }
Willy Tarreauc739aa82015-05-26 11:35:41 +0200800 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -0500801}
802
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200803/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
804 * <id>. At most one of <id> or <name> may be different provided that <cap> is
805 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
806 * find a proxy based on some information from a previous configuration, across
807 * reloads or during information exchange between peers.
808 *
809 * Names are looked up first if present, then IDs are compared if present. In
810 * case of an inexact match whatever is forced in the configuration has
811 * precedence in the following order :
812 * - 1) forced ID (proves a renaming / change of proxy type)
813 * - 2) proxy name+type (may indicate a move if ID differs)
814 * - 3) automatic ID+type (may indicate a renaming)
815 *
816 * Depending on what is found, we can end up in the following situations :
817 *
818 * name id cap | possible causes
819 * -------------+-----------------
820 * -- -- -- | nothing found
821 * -- -- ok | nothing found
822 * -- ok -- | proxy deleted, ID points to next one
823 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
824 * ok -- -- | proxy deleted, but other half with same name still here (before)
825 * ok -- ok | proxy's ID changed (proxy moved in the config file)
826 * ok ok -- | proxy deleted, but other half with same name still here (after)
827 * ok ok ok | perfect match
828 *
829 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200830 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
831 * (and ID was not zero)
832 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
833 * (and name was not NULL)
834 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
835 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200836 *
837 * Only a valid proxy is returned. If capabilities do not match, NULL is
838 * returned. The caller can check <diff> to report detailed warnings / errors,
839 * and decide whether or not to use what was found.
840 */
841struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
842{
843 struct proxy *byname;
844 struct proxy *byid;
845
846 if (!name && !id)
847 return NULL;
848
849 if (diff)
850 *diff = 0;
851
852 byname = byid = NULL;
853
854 if (name) {
855 byname = proxy_find_by_name(name, cap, 0);
856 if (byname && (!id || byname->uuid == id))
857 return byname;
858 }
859
Joseph Herlant59dd2952018-11-15 11:46:55 -0800860 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200861 * - name not set
862 * - name set but not found
863 * - name found, but ID doesn't match.
864 */
865 if (id) {
866 byid = proxy_find_by_id(id, cap, 0);
867 if (byid) {
868 if (byname) {
869 /* id+type found, name+type found, but not all 3.
870 * ID wins only if forced, otherwise name wins.
871 */
872 if (byid->options & PR_O_FORCED_ID) {
873 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200874 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200875 return byid;
876 }
877 else {
878 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200879 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200880 return byname;
881 }
882 }
883
Joseph Herlant59dd2952018-11-15 11:46:55 -0800884 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200885 * - name not set
886 * - name set but not found
887 */
888 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200889 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200890 return byid;
891 }
892
893 /* ID not found */
894 if (byname) {
895 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200896 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200897 return byname;
898 }
899 }
900
Joseph Herlant59dd2952018-11-15 11:46:55 -0800901 /* All remaining possibilities will lead to NULL. If we can report more
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200902 * detailed information to the caller about changed types and/or name,
903 * we'll do it. For example, we could detect that "listen foo" was
904 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
905 * - name not set, ID not found
906 * - name not found, ID not set
907 * - name not found, ID not found
908 */
909 if (!diff)
910 return NULL;
911
912 if (name) {
913 byname = proxy_find_by_name(name, 0, 0);
914 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200915 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200916 }
917
918 if (id) {
919 byid = proxy_find_by_id(id, 0, 0);
920 if (byid) {
921 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200922 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200923 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +0200924 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +0200925 /* otherwise it's a different proxy that was returned */
926 }
927 }
928 return NULL;
929}
930
Willy Tarreaubaaee002006-06-26 02:48:02 +0200931/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100932 * This function finds a server with matching name within selected proxy.
933 * It also checks if there are more matching servers with
934 * requested name as this often leads into unexpected situations.
935 */
936
937struct server *findserver(const struct proxy *px, const char *name) {
938
939 struct server *cursrv, *target = NULL;
940
941 if (!px)
942 return NULL;
943
944 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100945 if (strcmp(cursrv->id, name) != 0)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100946 continue;
947
948 if (!target) {
949 target = cursrv;
950 continue;
951 }
952
Christopher Faulet767a84b2017-11-24 16:50:31 +0100953 ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
954 name, px->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +0100955
956 return NULL;
957 }
958
959 return target;
960}
961
Willy Tarreauff01a212009-03-15 13:46:16 +0100962/* This function checks that the designated proxy has no http directives
963 * enabled. It will output a warning if there are, and will fix some of them.
964 * It returns the number of fatal errors encountered. This should be called
965 * at the end of the configuration parsing if the proxy is not in http mode.
966 * The <file> argument is used to construct the error message.
967 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +0200968int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +0100969{
970 if (curproxy->cookie_name != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100971 ha_warning("config : cookie will be ignored for %s '%s' (needs 'mode http').\n",
972 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100973 }
Willy Tarreauff01a212009-03-15 13:46:16 +0100974 if (curproxy->monitor_uri != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100975 ha_warning("config : monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
976 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100977 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200978 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +0100979 curproxy->lbprm.algo &= ~BE_LB_ALGO;
980 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100981 ha_warning("config : Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
982 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +0100983 }
Willy Tarreau17804162009-11-09 21:27:51 +0100984 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
985 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Christopher Faulet767a84b2017-11-24 16:50:31 +0100986 ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
987 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
988 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau17804162009-11-09 21:27:51 +0100989 }
Willy Tarreau62a61232013-04-12 18:13:46 +0200990 if (curproxy->conf.logformat_string == default_http_log_format ||
991 curproxy->conf.logformat_string == clf_http_log_format) {
992 /* Note: we don't change the directive's file:line number */
993 curproxy->conf.logformat_string = default_tcp_log_format;
Christopher Faulet767a84b2017-11-24 16:50:31 +0100994 ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
995 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
996 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau196729e2012-05-31 19:30:26 +0200997 }
998
Willy Tarreauff01a212009-03-15 13:46:16 +0100999 return 0;
1000}
1001
Willy Tarreau237250c2011-07-29 01:49:03 +02001002/* Perform the most basic initialization of a proxy :
1003 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +02001004 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +02001005 */
1006void init_new_proxy(struct proxy *p)
1007{
1008 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001009 p->obj_type = OBJ_TYPE_PROXY;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001010 p->pendconns = EB_ROOT;
Willy Tarreau237250c2011-07-29 01:49:03 +02001011 LIST_INIT(&p->acl);
1012 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001013 LIST_INIT(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001014 LIST_INIT(&p->http_after_res_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001015 LIST_INIT(&p->redirect_rules);
1016 LIST_INIT(&p->mon_fail_cond);
1017 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001018 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001019 LIST_INIT(&p->persist_rules);
1020 LIST_INIT(&p->sticking_rules);
1021 LIST_INIT(&p->storersp_rules);
1022 LIST_INIT(&p->tcp_req.inspect_rules);
1023 LIST_INIT(&p->tcp_rep.inspect_rules);
1024 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +02001025 LIST_INIT(&p->tcp_req.l5_rules);
Olivier Houchard859dc802019-08-08 15:47:21 +02001026 MT_LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +02001027 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +01001028 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001029 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +01001030 LIST_INIT(&p->format_unique_id);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001031 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001032 LIST_INIT(&p->conf.listeners);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001033 LIST_INIT(&p->conf.errors);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001034 LIST_INIT(&p->conf.args.list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001035 LIST_INIT(&p->filter_configs);
Christopher Faulet7a1e2e12020-04-02 18:05:11 +02001036 LIST_INIT(&p->tcpcheck_rules.preset_vars);
Willy Tarreau237250c2011-07-29 01:49:03 +02001037
1038 /* Timeouts are defined as -1 */
1039 proxy_reset_timeouts(p);
1040 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +02001041
1042 /* initial uuid is unassigned (-1) */
1043 p->uuid = -1;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001044
Olivier Houcharda254a372019-04-05 15:30:12 +02001045 /* Default to only allow L4 retries */
1046 p->retry_type = PR_RE_CONN_FAILED;
1047
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01001048 p->extra_counters_fe = NULL;
1049 p->extra_counters_be = NULL;
1050
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001051 HA_RWLOCK_INIT(&p->lock);
Willy Tarreau237250c2011-07-29 01:49:03 +02001052}
1053
Willy Tarreau144289b2021-02-12 08:19:01 +01001054/* Preset default settings onto proxy <defproxy>. */
1055void proxy_preset_defaults(struct proxy *defproxy)
1056{
1057 defproxy->mode = PR_MODE_TCP;
1058 defproxy->disabled = 0;
1059 defproxy->maxconn = cfg_maxpconn;
1060 defproxy->conn_retries = CONN_RETRIES;
1061 defproxy->redispatch_after = 0;
1062 defproxy->options = PR_O_REUSE_SAFE;
1063 defproxy->max_out_conns = MAX_SRV_LIST;
1064
1065 defproxy->defsrv.check.inter = DEF_CHKINTR;
1066 defproxy->defsrv.check.fastinter = 0;
1067 defproxy->defsrv.check.downinter = 0;
1068 defproxy->defsrv.agent.inter = DEF_CHKINTR;
1069 defproxy->defsrv.agent.fastinter = 0;
1070 defproxy->defsrv.agent.downinter = 0;
1071 defproxy->defsrv.check.rise = DEF_RISETIME;
1072 defproxy->defsrv.check.fall = DEF_FALLTIME;
1073 defproxy->defsrv.agent.rise = DEF_AGENT_RISETIME;
1074 defproxy->defsrv.agent.fall = DEF_AGENT_FALLTIME;
1075 defproxy->defsrv.check.port = 0;
1076 defproxy->defsrv.agent.port = 0;
1077 defproxy->defsrv.maxqueue = 0;
1078 defproxy->defsrv.minconn = 0;
1079 defproxy->defsrv.maxconn = 0;
1080 defproxy->defsrv.max_reuse = -1;
1081 defproxy->defsrv.max_idle_conns = -1;
1082 defproxy->defsrv.pool_purge_delay = 5000;
1083 defproxy->defsrv.slowstart = 0;
1084 defproxy->defsrv.onerror = DEF_HANA_ONERR;
1085 defproxy->defsrv.consecutive_errors_limit = DEF_HANA_ERRLIMIT;
1086 defproxy->defsrv.uweight = defproxy->defsrv.iweight = 1;
1087
1088 defproxy->email_alert.level = LOG_ALERT;
1089 defproxy->load_server_state_from_file = PR_SRV_STATE_FILE_UNSPEC;
1090#if defined(USE_QUIC)
1091 quic_transport_params_init(&defproxy->defsrv.quic_params, 0);
1092#endif
1093}
1094
Willy Tarreauacde1522020-10-07 16:31:39 +02001095/* to be called under the proxy lock after stopping some listeners. This will
1096 * automatically update the p->disabled flag after stopping the last one, and
1097 * will emit a log indicating the proxy's condition. The function is idempotent
1098 * so that it will not emit multiple logs; a proxy will be disabled only once.
1099 */
1100void proxy_cond_disable(struct proxy *p)
1101{
1102 if (p->disabled)
1103 return;
1104
1105 if (p->li_ready + p->li_paused > 0)
1106 return;
1107
1108 p->disabled = 1;
1109
1110 if (!(proc_mask(p->bind_proc) & pid_bit))
1111 goto silent;
1112
1113 /* Note: syslog proxies use their own loggers so while it's somewhat OK
1114 * to report them being stopped as a warning, we must not spam their log
1115 * servers which are in fact production servers. For other types (CLI,
1116 * peers, etc) we must not report them at all as they're not really on
1117 * the data plane but on the control plane.
1118 */
1119 if (p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP || p->mode == PR_MODE_SYSLOG)
1120 ha_warning("Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
1121 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
1122
1123 if (p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP)
1124 send_log(p, LOG_WARNING, "Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
1125 p->id, p->fe_counters.cum_conn, p->be_counters.cum_conn);
1126
1127 silent:
1128 if (p->table && p->table->size && p->table->sync_task)
1129 task_wakeup(p->table->sync_task, TASK_WOKEN_MSG);
1130
1131 if (p->task)
1132 task_wakeup(p->task, TASK_WOKEN_MSG);
1133}
1134
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001135/*
Willy Tarreau918ff602011-07-25 16:33:49 +02001136 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +02001137 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +02001138 * called as a task which is woken up upon stopping or when rate limiting must
1139 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001140 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001141struct task *manage_proxy(struct task *t, void *context, unsigned short state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001142{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001143 struct proxy *p = context;
Willy Tarreau918ff602011-07-25 16:33:49 +02001144 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +01001145 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001146
Willy Tarreau918ff602011-07-25 16:33:49 +02001147 /* We should periodically try to enable listeners waiting for a
1148 * global resource here.
1149 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001150
Willy Tarreau918ff602011-07-25 16:33:49 +02001151 /* first, let's check if we need to stop the proxy */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001152 if (unlikely(stopping && !p->disabled)) {
Willy Tarreau918ff602011-07-25 16:33:49 +02001153 int t;
1154 t = tick_remain(now_ms, p->stop_time);
1155 if (t == 0) {
Willy Tarreau918ff602011-07-25 16:33:49 +02001156 stop_proxy(p);
1157 /* try to free more memory */
Willy Tarreaubafbe012017-11-24 17:34:44 +01001158 pool_gc(NULL);
Willy Tarreau918ff602011-07-25 16:33:49 +02001159 }
1160 else {
1161 next = tick_first(next, p->stop_time);
1162 }
1163 }
Willy Tarreauf3f8c702011-07-25 07:37:28 +02001164
Willy Tarreau3a925c12013-09-04 17:54:01 +02001165 /* If the proxy holds a stick table, we need to purge all unused
1166 * entries. These are all the ones in the table with ref_cnt == 0
1167 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +02001168 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +02001169 * be in neither list. Any entry being dumped will have ref_cnt > 0.
1170 * However we protect tables that are being synced to peers.
1171 */
Willy Tarreauc3914d42020-09-24 08:39:22 +02001172 if (unlikely(stopping && p->disabled && p->table && p->table->current)) {
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001173 if (!p->table->syncing) {
1174 stktable_trash_oldest(p->table, p->table->current);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001175 pool_gc(NULL);
Willy Tarreau3a925c12013-09-04 17:54:01 +02001176 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001177 if (p->table->current) {
Willy Tarreau3a925c12013-09-04 17:54:01 +02001178 /* some entries still remain, let's recheck in one second */
1179 next = tick_first(next, tick_add(now_ms, 1000));
1180 }
1181 }
1182
Willy Tarreau918ff602011-07-25 16:33:49 +02001183 /* the rest below is just for frontends */
1184 if (!(p->cap & PR_CAP_FE))
1185 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +02001186
Willy Tarreau918ff602011-07-25 16:33:49 +02001187 /* check the various reasons we may find to block the frontend */
Willy Tarreau6b3bf732020-09-24 07:35:46 +02001188 if (unlikely(p->feconn >= p->maxconn))
Willy Tarreau918ff602011-07-25 16:33:49 +02001189 goto out;
Willy Tarreau3a7d2072009-03-05 23:48:25 +01001190
Willy Tarreau918ff602011-07-25 16:33:49 +02001191 if (p->fe_sps_lim &&
1192 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
1193 /* we're blocking because a limit was reached on the number of
1194 * requests/s on the frontend. We want to re-check ASAP, which
1195 * means in 1 ms before estimated expiration date, because the
1196 * timer will have settled down.
1197 */
1198 next = tick_first(next, tick_add(now_ms, wait));
1199 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001200 }
Willy Tarreau918ff602011-07-25 16:33:49 +02001201
1202 /* The proxy is not limited so we can re-enable any waiting listener */
Willy Tarreau241797a2019-12-10 14:10:52 +01001203 dequeue_proxy_listeners(p);
Willy Tarreau918ff602011-07-25 16:33:49 +02001204 out:
1205 t->expire = next;
1206 task_queue(t);
1207 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001208}
1209
1210
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001211static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx,
1212 struct proxy *defpx, const char *file, int line,
1213 char **err)
1214{
1215 const char *res;
1216
1217 if (!*args[1]) {
1218 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
1219 return -1;
1220 }
1221 res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02001222 if (res == PARSE_TIME_OVER) {
1223 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1224 args[1], args[0]);
1225 return -1;
1226 }
1227 else if (res == PARSE_TIME_UNDER) {
1228 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1229 args[1], args[0]);
1230 return -1;
1231 }
1232 else if (res) {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001233 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
1234 return -1;
1235 }
1236 return 0;
1237}
1238
Olivier Houchard9f6af332018-05-25 14:04:04 +02001239struct task *hard_stop(struct task *t, void *context, unsigned short state)
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001240{
1241 struct proxy *p;
1242 struct stream *s;
1243
1244 if (killed) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001245 ha_warning("Some tasks resisted to hard-stop, exiting now.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001246 send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n");
Willy Tarreau7067b3a2019-06-02 11:11:29 +02001247 killed = 2;
1248 t->expire = TICK_ETERNITY;
1249 return t;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001250 }
1251
Christopher Faulet767a84b2017-11-24 16:50:31 +01001252 ha_warning("soft-stop running for too long, performing a hard-stop.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001253 send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n");
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001254 p = proxies_list;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001255 while (p) {
1256 if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1258 p->id, p->feconn);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001259 send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n",
1260 p->id, p->feconn);
1261 }
1262 p = p->next;
1263 }
1264 list_for_each_entry(s, &streams, list) {
1265 stream_shutdown(s, SF_ERR_KILLED);
1266 }
1267
1268 killed = 1;
1269 t->expire = tick_add(now_ms, MS_TO_TICKS(1000));
1270 return t;
1271}
1272
Willy Tarreaubaaee002006-06-26 02:48:02 +02001273/*
1274 * this function disables health-check servers so that the process will quickly be ignored
1275 * by load balancers. Note that if a proxy was already in the PAUSED state, then its grace
1276 * time will not be used since it would already not listen anymore to the socket.
1277 */
1278void soft_stop(void)
1279{
1280 struct proxy *p;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001281 struct task *task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282
1283 stopping = 1;
William Dauchy3894d972019-12-28 15:36:02 +01001284 /* disable busy polling to avoid cpu eating for the new process */
1285 global.tune.options &= ~GTUNE_BUSY_POLLING;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001286 if (tick_isset(global.hard_stop_after)) {
Emeric Brunc60def82017-09-27 14:59:38 +02001287 task = task_new(MAX_THREADS_MASK);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001288 if (task) {
1289 task->process = hard_stop;
1290 task_schedule(task, tick_add(now_ms, global.hard_stop_after));
1291 }
1292 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001293 ha_alert("out of memory trying to allocate the hard-stop task.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001294 }
1295 }
Willy Tarreau626f3a72020-10-07 16:52:43 +02001296
1297 /* stop all stoppable listeners, resulting in disabling all proxies
1298 * that don't use a grace period.
1299 */
1300 protocol_stop_now();
1301
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001302 p = proxies_list;
Willy Tarreaub0b37bc2008-06-23 14:00:57 +02001303 tv_update_date(0,1); /* else, the old time before select will be used */
Willy Tarreaubaaee002006-06-26 02:48:02 +02001304 while (p) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001305 if (!p->disabled) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001306 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 +02001307 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 +02001308 p->stop_time = tick_add(now_ms, p->grace);
Emeric Brun5a8c0a92010-09-23 18:44:36 +02001309
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001310 /* Note: do not wake up stopped proxies' task nor their tables'
1311 * tasks as these ones might point to already released entries.
1312 */
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001313 if (p->table && p->table->size && p->table->sync_task)
1314 task_wakeup(p->table->sync_task, TASK_WOKEN_MSG);
Willy Tarreau20b7afb2015-09-28 16:35:04 +02001315
1316 if (p->task)
1317 task_wakeup(p->task, TASK_WOKEN_MSG);
1318 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02001319 p = p->next;
1320 }
Willy Tarreaubbe11b12011-07-25 11:16:24 +02001321
Willy Tarreaud0807c32010-08-27 18:26:11 +02001322 /* signal zero is used to broadcast the "stopping" event */
1323 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001324}
1325
1326
Willy Tarreaube58c382011-07-24 18:28:10 +02001327/* Temporarily disables listening on all of the proxy's listeners. Upon
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001328 * success, the proxy enters the PR_PAUSED state. The function returns 0
Willy Tarreauce8fe252011-09-07 19:14:57 +02001329 * if it fails, or non-zero on success.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001330 */
Willy Tarreauce8fe252011-09-07 19:14:57 +02001331int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001332{
1333 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +02001334
Willy Tarreauc3914d42020-09-24 08:39:22 +02001335 if (!(p->cap & PR_CAP_FE) || p->disabled || !p->li_ready)
Willy Tarreauce8fe252011-09-07 19:14:57 +02001336 return 1;
1337
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001338 list_for_each_entry(l, &p->conf.listeners, by_fe)
1339 pause_listener(l);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001340
Willy Tarreaua17c91b2020-09-24 07:44:34 +02001341 if (p->li_ready) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001342 ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02001343 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
1344 return 0;
1345 }
Willy Tarreauce8fe252011-09-07 19:14:57 +02001346 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +02001347}
1348
Willy Tarreauda250db2008-10-12 12:07:48 +02001349/*
1350 * This function completely stops a proxy and releases its listeners. It has
1351 * to be called when going down in order to release the ports so that another
1352 * process may bind to them. It must also be called on disabled proxies at the
William Lallemandc59f9882018-11-16 16:57:21 +01001353 * end of start-up. If all listeners are closed, the proxy is set to the
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001354 * PR_STSTOPPED state. The function takes the proxy's lock so it's safe to
1355 * call from multiple places.
Willy Tarreauda250db2008-10-12 12:07:48 +02001356 */
1357void stop_proxy(struct proxy *p)
1358{
1359 struct listener *l;
1360
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001361 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001362
Willy Tarreau322b9b92020-10-07 16:20:34 +02001363 list_for_each_entry(l, &p->conf.listeners, by_fe)
1364 stop_listener(l, 1, 0, 0);
Willy Tarreau3de3cd42019-07-24 17:42:44 +02001365
Willy Tarreauba296872020-10-16 15:10:11 +02001366 if (!p->disabled && !p->li_ready) {
1367 /* might be just a backend */
1368 p->disabled = 1;
1369 }
1370
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001371 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02001372}
1373
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001374/* This function resumes listening on the specified proxy. It scans all of its
1375 * listeners and tries to enable them all. If any of them fails, the proxy is
1376 * put back to the paused state. It returns 1 upon success, or zero if an error
1377 * is encountered.
1378 */
1379int resume_proxy(struct proxy *p)
1380{
1381 struct listener *l;
1382 int fail;
1383
Willy Tarreauc3914d42020-09-24 08:39:22 +02001384 if (p->disabled || !p->li_paused)
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001385 return 1;
1386
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001387 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02001388 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001389 if (!resume_listener(l)) {
1390 int port;
1391
Willy Tarreau37159062020-08-27 07:48:42 +02001392 port = get_host_port(&l->rx.addr);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001393 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001394 ha_warning("Port %d busy while trying to enable %s %s.\n",
1395 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001396 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
1397 port, proxy_cap_str(p->cap), p->id);
1398 }
1399 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001400 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
1401 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001402 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
1403 l->luid, proxy_cap_str(p->cap), p->id);
1404 }
1405
1406 /* Another port might have been enabled. Let's stop everything. */
1407 fail = 1;
1408 break;
1409 }
1410 }
1411
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02001412 if (fail) {
1413 pause_proxy(p);
1414 return 0;
1415 }
1416 return 1;
1417}
1418
Willy Tarreau87b09662015-04-03 00:22:06 +02001419/* Set current stream's backend to <be>. Nothing is done if the
1420 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02001421 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001422 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001423 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001424 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001425int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001426{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001427 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001428 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02001429
1430 if (flt_set_stream_backend(s, be) < 0)
1431 return 0;
1432
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001433 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001434 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
1435 HA_ATOMIC_ADD(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001436 proxy_inc_be_ctr(be);
1437
Willy Tarreau87b09662015-04-03 00:22:06 +02001438 /* assign new parameters to the stream from the new backend */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02001439 s->si[1].flags &= ~SI_FL_INDEP_STR;
1440 if (be->options2 & PR_O2_INDEPSTR)
1441 s->si[1].flags |= SI_FL_INDEP_STR;
1442
Hongbo Longe39683c2017-03-10 18:41:51 +01001443 if (tick_isset(be->timeout.serverfin))
1444 s->si[1].hcto = be->timeout.serverfin;
1445
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001446 /* We want to enable the backend-specific analysers except those which
1447 * were already run as part of the frontend/listener. Note that it would
1448 * be more reliable to store the list of analysers that have been run,
1449 * but what we do here is OK for now.
1450 */
Christopher Faulet70e2f272017-01-09 16:33:19 +01001451 s->req.analysers |= be->be_req_ana & ~(strm_li(s) ? strm_li(s)->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001452
Willy Tarreau51aecc72009-07-12 09:47:04 +02001453 /* If the target backend requires HTTP processing, we have to allocate
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001454 * the HTTP transaction if we did not have one.
Willy Tarreau51aecc72009-07-12 09:47:04 +02001455 */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001456 if (unlikely(!s->txn && be->http_needed)) {
1457 if (unlikely(!http_alloc_txn(s)))
Willy Tarreau51aecc72009-07-12 09:47:04 +02001458 return 0; /* not enough memory */
Willy Tarreau39e4f622010-05-31 17:01:36 +02001459
1460 /* and now initialize the HTTP transaction state */
1461 http_init_txn(s);
Willy Tarreau51aecc72009-07-12 09:47:04 +02001462 }
1463
Christopher Faulet309c6412015-12-02 09:57:32 +01001464 /* Be sure to filter request headers if the backend is an HTTP proxy and
1465 * if there are filters attached to the stream. */
1466 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
Christopher Faulet0184ea72017-01-05 14:06:34 +01001467 s->req.analysers |= AN_REQ_FLT_HTTP_HDRS;
Christopher Faulet309c6412015-12-02 09:57:32 +01001468
Willy Tarreaueee5b512015-04-03 23:46:31 +02001469 if (s->txn) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02001470 /* If we chain a TCP frontend to an HTX backend, we must upgrade
1471 * the client mux */
Christopher Faulet60d29b32019-07-15 15:00:25 +02001472 if (!IS_HTX_STRM(s) && be->mode == PR_MODE_HTTP) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02001473 struct connection *conn = objt_conn(strm_sess(s)->origin);
1474 struct conn_stream *cs = objt_cs(s->si[0].end);
1475
1476 if (conn && cs) {
1477 si_rx_endp_more(&s->si[0]);
Olivier Houchard2ab3dad2019-07-03 13:08:18 +02001478 /* Make sure we're unsubscribed, the the new
1479 * mux will probably want to subscribe to
1480 * the underlying XPRT
1481 */
1482 if (s->si[0].wait_event.events)
1483 conn->mux->unsubscribe(cs, s->si[0].wait_event.events,
1484 &s->si[0].wait_event);
Christopher Fauletc985f6c2019-07-15 11:42:52 +02001485 if (conn_upgrade_mux_fe(conn, cs, &s->req.buf, ist(""), PROTO_MODE_HTTP) == -1)
Christopher Fauletbbe68542019-04-08 10:53:51 +02001486 return 0;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001487 if (strcmp(conn->mux->name, "H2") == 0) {
Olivier Houchard4c18f942019-07-31 18:05:26 +02001488 /* For HTTP/2, destroy the conn_stream,
1489 * disable logging, and pretend that we
1490 * failed, to that the stream is
1491 * silently destroyed. The new mux
1492 * will create new streams.
1493 */
Olivier Houchard59dd06d2019-08-09 18:01:15 +02001494 cs_free(cs);
Olivier Houchard4c18f942019-07-31 18:05:26 +02001495 si_detach_endpoint(&s->si[0]);
1496 s->logs.logwait = 0;
1497 s->logs.level = 0;
1498 s->flags |= SF_IGNORE;
1499 return 0;
1500 }
Christopher Fauletcdd1e2a2021-01-21 17:31:04 +01001501 s->req.flags &= ~(CF_READ_PARTIAL|CF_AUTO_CONNECT);
1502 s->req.total = 0;
1503 s->flags |= SF_IGNORE;
Christopher Fauletbbe68542019-04-08 10:53:51 +02001504 }
1505 }
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01001506 else if (IS_HTX_STRM(s) && be->mode != PR_MODE_HTTP) {
1507 /* If a TCP backend is assgiend to an HTX stream, return
1508 * an error. It may happens for a new stream on a
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001509 * previously upgraded connections. */
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01001510 if (!(s->flags & SF_ERR_MASK))
1511 s->flags |= SF_ERR_INTERNAL;
1512 return 0;
1513 }
Christopher Fauletbbe68542019-04-08 10:53:51 +02001514
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001515 /* we may request to parse a request body */
Christopher Faulet711ed6a2019-07-16 14:16:10 +02001516 if (be->options & PR_O_WREQ_BODY)
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02001517 s->req.analysers |= AN_REQ_HTTP_BODY;
Willy Tarreaueee5b512015-04-03 23:46:31 +02001518 }
1519
1520 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02001521 if (be->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001522 s->req.flags |= CF_NEVER_WAIT;
1523 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02001524 }
1525
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001526 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001527}
1528
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001529/* Capture a bad request or response and archive it in the proxy's structure.
1530 * It is relatively protocol-agnostic so it requires that a number of elements
1531 * are passed :
1532 * - <proxy> is the proxy where the error was detected and where the snapshot
1533 * needs to be stored
Joseph Herlant59dd2952018-11-15 11:46:55 -08001534 * - <is_back> indicates that the error happened when receiving the response
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001535 * - <other_end> is a pointer to the proxy on the other side when known
1536 * - <target> is the target of the connection, usually a server or a proxy
1537 * - <sess> is the session which experienced the error
1538 * - <ctx> may be NULL or should contain any info relevant to the protocol
1539 * - <buf> is the buffer containing the offending data
1540 * - <buf_ofs> is the position of this buffer's input data in the input
1541 * stream, starting at zero. It may be passed as zero if unknown.
1542 * - <buf_out> is the portion of <buf->data> which was already forwarded and
1543 * which precedes the buffer's input. The buffer's input starts at
1544 * buf->head + buf_out.
1545 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
1546 * - <show> is the callback to use to display <ctx>. It may be NULL.
1547 */
1548void proxy_capture_error(struct proxy *proxy, int is_back,
1549 struct proxy *other_end, enum obj_type *target,
1550 const struct session *sess,
1551 const struct buffer *buf, long buf_ofs,
1552 unsigned int buf_out, unsigned int err_pos,
1553 const union error_snapshot_ctx *ctx,
1554 void (*show)(struct buffer *, const struct error_snapshot *))
1555{
1556 struct error_snapshot *es;
1557 unsigned int buf_len;
1558 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001559 unsigned int ev_id;
1560
1561 ev_id = HA_ATOMIC_XADD(&error_snapshot_id, 1);
1562
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001563 buf_len = b_data(buf) - buf_out;
1564
1565 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001566 if (!es)
1567 return;
1568
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001569 es->buf_len = buf_len;
1570 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001571
Christopher Faulet47a72102020-01-06 11:37:00 +01001572 len1 = b_size(buf) - b_peek_ofs(buf, buf_out);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001573 if (len1 > buf_len)
1574 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001575
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001576 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001577 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02001578 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001579 if (len2)
1580 memcpy(es->buf + len1, b_orig(buf), len2);
1581 }
1582
1583 es->buf_err = err_pos;
1584 es->when = date; // user-visible date
1585 es->srv = objt_server(target);
1586 es->oe = other_end;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01001587 if (sess && objt_conn(sess->origin) && conn_get_src(__objt_conn(sess->origin)))
Willy Tarreau026efc72019-07-17 15:20:02 +02001588 es->src = *__objt_conn(sess->origin)->src;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001589 else
1590 memset(&es->src, 0, sizeof(es->src));
1591
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001592 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
1593 es->buf_out = buf_out;
1594 es->buf_ofs = buf_ofs;
1595
1596 /* be sure to indicate the offset of the first IN byte */
1597 if (es->buf_ofs >= es->buf_len)
1598 es->buf_ofs -= es->buf_len;
1599 else
1600 es->buf_ofs = 0;
1601
1602 /* protocol-specific part now */
1603 if (ctx)
1604 es->ctx = *ctx;
1605 else
1606 memset(&es->ctx, 0, sizeof(es->ctx));
1607 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02001608
1609 /* note: we still lock since we have to be certain that nobody is
1610 * dumping the output while we free.
1611 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001612 HA_RWLOCK_WRLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauc55015e2018-09-07 19:02:32 +02001613 if (is_back) {
1614 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
1615 } else {
1616 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
1617 }
1618 free(es);
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001619 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001620}
1621
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001622/* Configure all proxies which lack a maxconn setting to use the global one by
1623 * default. This avoids the common mistake consisting in setting maxconn only
1624 * in the global section and discovering the hard way that it doesn't propagate
1625 * through the frontends. These values are also propagated through the various
Ilya Shipitsin47d17182020-06-21 21:42:57 +05001626 * targeted backends, whose fullconn is finally calculated if not yet set.
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001627 */
1628void proxy_adjust_all_maxconn()
1629{
1630 struct proxy *curproxy;
1631 struct switching_rule *swrule1, *swrule2;
1632
1633 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001634 if (curproxy->disabled)
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001635 continue;
1636
1637 if (!(curproxy->cap & PR_CAP_FE))
1638 continue;
1639
1640 if (!curproxy->maxconn)
1641 curproxy->maxconn = global.maxconn;
1642
1643 /* update the target backend's fullconn count : default_backend */
1644 if (curproxy->defbe.be)
1645 curproxy->defbe.be->tot_fe_maxconn += curproxy->maxconn;
1646 else if ((curproxy->cap & PR_CAP_LISTEN) == PR_CAP_LISTEN)
1647 curproxy->tot_fe_maxconn += curproxy->maxconn;
1648
1649 list_for_each_entry(swrule1, &curproxy->switching_rules, list) {
1650 /* For each target of switching rules, we update their
1651 * tot_fe_maxconn, except if a previous rule points to
1652 * the same backend or to the default backend.
1653 */
1654 if (swrule1->be.backend != curproxy->defbe.be) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01001655 /* note: swrule1->be.backend isn't a backend if the rule
1656 * is dynamic, it's an expression instead, so it must not
1657 * be dereferenced as a backend before being certain it is.
1658 */
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001659 list_for_each_entry(swrule2, &curproxy->switching_rules, list) {
1660 if (swrule2 == swrule1) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01001661 if (!swrule1->dynamic)
1662 swrule1->be.backend->tot_fe_maxconn += curproxy->maxconn;
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001663 break;
1664 }
1665 else if (!swrule2->dynamic && swrule2->be.backend == swrule1->be.backend) {
1666 /* there are multiple refs of this backend */
1667 break;
1668 }
1669 }
1670 }
1671 }
1672 }
1673
1674 /* automatically compute fullconn if not set. We must not do it in the
1675 * loop above because cross-references are not yet fully resolved.
1676 */
1677 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Willy Tarreauc3914d42020-09-24 08:39:22 +02001678 if (curproxy->disabled)
Willy Tarreauc8d5b952019-02-27 17:25:52 +01001679 continue;
1680
1681 /* If <fullconn> is not set, let's set it to 10% of the sum of
1682 * the possible incoming frontend's maxconns.
1683 */
1684 if (!curproxy->fullconn && (curproxy->cap & PR_CAP_BE)) {
1685 /* we have the sum of the maxconns in <total>. We only
1686 * keep 10% of that sum to set the default fullconn, with
1687 * a hard minimum of 1 (to avoid a divide by zero).
1688 */
1689 curproxy->fullconn = (curproxy->tot_fe_maxconn + 9) / 10;
1690 if (!curproxy->fullconn)
1691 curproxy->fullconn = 1;
1692 }
1693 }
1694}
1695
Willy Tarreau75fb65a2018-09-07 17:43:26 +02001696/* Config keywords below */
1697
Willy Tarreaudc13c112013-06-21 23:16:39 +02001698static struct cfg_kw_list cfg_kws = {ILH, {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01001699 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001700 { CFG_LISTEN, "timeout", proxy_parse_timeout },
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +02001701 { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
1702 { CFG_LISTEN, "contimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
1703 { 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 +01001704 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02001705 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02001706 { CFG_LISTEN, "declare", proxy_parse_declare },
Olivier Houcharda254a372019-04-05 15:30:12 +02001707 { CFG_LISTEN, "retry-on", proxy_parse_retry_on },
Willy Tarreau52543212020-07-09 05:58:51 +02001708#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001709 { CFG_LISTEN, "clitcpka-cnt", proxy_parse_tcpka_cnt },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001710 { CFG_LISTEN, "srvtcpka-cnt", proxy_parse_tcpka_cnt },
Willy Tarreau52543212020-07-09 05:58:51 +02001711#endif
1712#ifdef TCP_KEEPIDLE
1713 { CFG_LISTEN, "clitcpka-idle", proxy_parse_tcpka_idle },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001714 { CFG_LISTEN, "srvtcpka-idle", proxy_parse_tcpka_idle },
Willy Tarreau52543212020-07-09 05:58:51 +02001715#endif
1716#ifdef TCP_KEEPINTVL
1717 { CFG_LISTEN, "clitcpka-intvl", proxy_parse_tcpka_intvl },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001718 { CFG_LISTEN, "srvtcpka-intvl", proxy_parse_tcpka_intvl },
Willy Tarreau52543212020-07-09 05:58:51 +02001719#endif
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02001720 { 0, NULL, NULL },
1721}};
1722
Willy Tarreau0108d902018-11-25 19:14:37 +01001723INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
1724
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001725/* Expects to find a frontend named <arg> and returns it, otherwise displays various
1726 * adequate error messages and returns NULL. This function is designed to be used by
1727 * functions requiring a frontend on the CLI.
1728 */
1729struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
1730{
1731 struct proxy *px;
1732
1733 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001734 cli_err(appctx, "A frontend name is expected.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001735 return NULL;
1736 }
1737
1738 px = proxy_fe_by_name(arg);
1739 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001740 cli_err(appctx, "No such frontend.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01001741 return NULL;
1742 }
1743 return px;
1744}
1745
Olivier Houchard614f8d72017-03-14 20:08:46 +01001746/* Expects to find a backend named <arg> and returns it, otherwise displays various
1747 * adequate error messages and returns NULL. This function is designed to be used by
1748 * functions requiring a frontend on the CLI.
1749 */
1750struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
1751{
1752 struct proxy *px;
1753
1754 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001755 cli_err(appctx, "A backend name is expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01001756 return NULL;
1757 }
1758
1759 px = proxy_be_by_name(arg);
1760 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02001761 cli_err(appctx, "No such backend.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01001762 return NULL;
1763 }
1764 return px;
1765}
1766
1767
Willy Tarreau69f591e2020-07-01 07:00:59 +02001768/* parse a "show servers [state|conn]" CLI line, returns 0 if it wants to start
1769 * the dump or 1 if it stops immediately. If an argument is specified, it will
1770 * set the proxy pointer into cli.p0 and its ID into cli.i0. It sets cli.o0 to
1771 * 0 for "state", or 1 for "conn".
William Lallemanda6c5f332016-11-19 02:25:36 +01001772 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001773static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01001774{
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001775 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01001776
Willy Tarreau69f591e2020-07-01 07:00:59 +02001777 appctx->ctx.cli.o0 = *args[2] == 'c'; // "conn" vs "state"
1778
William Lallemanda6c5f332016-11-19 02:25:36 +01001779 /* check if a backend name has been provided */
1780 if (*args[3]) {
1781 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001782 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01001783
Willy Tarreau9d008692019-08-09 11:21:01 +02001784 if (!px)
1785 return cli_err(appctx, "Can't find backend.\n");
1786
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001787 appctx->ctx.cli.p0 = px;
1788 appctx->ctx.cli.i0 = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01001789 }
1790 return 0;
1791}
1792
William Dauchyd1a7b852021-02-11 22:51:26 +01001793/* helper to dump server addr */
1794static void dump_server_addr(const struct sockaddr_storage *addr, char *addr_str)
1795{
1796 addr_str[0] = '\0';
1797 switch (addr->ss_family) {
1798 case AF_INET:
1799 case AF_INET6:
1800 addr_to_str(addr, addr_str, INET6_ADDRSTRLEN + 1);
1801 break;
1802 default:
1803 memcpy(addr_str, "-\0", 2);
1804 break;
1805 }
1806}
1807
Willy Tarreau6ff81432020-07-01 07:02:42 +02001808/* dumps server state information for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01001809 * These information are all the parameters which may change during HAProxy runtime.
1810 * By default, we only export to the last known server state file format.
1811 * These information can be used at next startup to recover same level of server state.
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001812 * It uses the proxy pointer from cli.p0, the proxy's id from cli.i0 and the server's
1813 * pointer from cli.p1.
William Lallemanda6c5f332016-11-19 02:25:36 +01001814 */
Willy Tarreau6ff81432020-07-01 07:02:42 +02001815static int dump_servers_state(struct stream_interface *si)
William Lallemanda6c5f332016-11-19 02:25:36 +01001816{
1817 struct appctx *appctx = __objt_appctx(si->end);
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001818 struct proxy *px = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001819 struct server *srv;
1820 char srv_addr[INET6_ADDRSTRLEN + 1];
William Dauchyd1a7b852021-02-11 22:51:26 +01001821 char srv_agent_addr[INET6_ADDRSTRLEN + 1];
1822 char srv_check_addr[INET6_ADDRSTRLEN + 1];
William Lallemanda6c5f332016-11-19 02:25:36 +01001823 time_t srv_time_since_last_change;
1824 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001825 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01001826
William Lallemanda6c5f332016-11-19 02:25:36 +01001827 /* we don't want to report any state if the backend is not enabled on this process */
Willy Tarreau6daac192019-02-02 17:39:53 +01001828 if (!(proc_mask(px->bind_proc) & pid_bit))
William Lallemanda6c5f332016-11-19 02:25:36 +01001829 return 1;
1830
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001831 if (!appctx->ctx.cli.p1)
1832 appctx->ctx.cli.p1 = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01001833
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001834 for (; appctx->ctx.cli.p1 != NULL; appctx->ctx.cli.p1 = srv->next) {
1835 srv = appctx->ctx.cli.p1;
William Lallemanda6c5f332016-11-19 02:25:36 +01001836
William Dauchyd1a7b852021-02-11 22:51:26 +01001837 dump_server_addr(&srv->addr, srv_addr);
1838 dump_server_addr(&srv->check.addr, srv_check_addr);
1839 dump_server_addr(&srv->agent.addr, srv_agent_addr);
1840
William Lallemanda6c5f332016-11-19 02:25:36 +01001841 srv_time_since_last_change = now.tv_sec - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001842 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001843 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
1844
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02001845 srvrecord = NULL;
1846 if (srv->srvrq && srv->srvrq->name)
1847 srvrecord = srv->srvrq->name;
1848
Willy Tarreau69f591e2020-07-01 07:00:59 +02001849 if (appctx->ctx.cli.o0 == 0) {
1850 /* show servers state */
1851 chunk_printf(&trash,
1852 "%d %s "
1853 "%d %s %s "
1854 "%d %d %d %d %ld "
1855 "%d %d %d %d %d "
William Dauchyf6370442020-11-14 19:25:33 +01001856 "%d %d %s %u "
William Dauchyd1a7b852021-02-11 22:51:26 +01001857 "%s %d %d "
1858 "%s %s %d"
Willy Tarreau69f591e2020-07-01 07:00:59 +02001859 "\n",
1860 px->uuid, px->id,
1861 srv->puid, srv->id, srv_addr,
1862 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight, (long int)srv_time_since_last_change,
1863 srv->check.status, srv->check.result, srv->check.health, srv->check.state, srv->agent.state,
1864 bk_f_forced_id, srv_f_forced_id, srv->hostname ? srv->hostname : "-", srv->svc_port,
William Dauchyd1a7b852021-02-11 22:51:26 +01001865 srvrecord ? srvrecord : "-", srv->use_ssl, srv->check.port,
1866 srv_check_addr, srv_agent_addr, srv->agent.port);
Willy Tarreau69f591e2020-07-01 07:00:59 +02001867 } else {
1868 /* show servers conn */
1869 int thr;
1870
1871 chunk_printf(&trash,
1872 "%s/%s %d/%d %s %u - %u %u %u %u %u %u %d %u",
1873 px->id, srv->id, px->uuid, srv->puid, srv_addr,srv->svc_port,
1874 srv->pool_purge_delay,
1875 srv->curr_used_conns, srv->max_used_conns, srv->est_need_conns,
1876 srv->curr_idle_nb, srv->curr_safe_nb, (int)srv->max_idle_conns, srv->curr_idle_conns);
1877
Willy Tarreau42abe682020-07-02 15:19:57 +02001878 for (thr = 0; thr < global.nbthread && srv->curr_idle_thr; thr++)
Willy Tarreau69f591e2020-07-01 07:00:59 +02001879 chunk_appendf(&trash, " %u", srv->curr_idle_thr[thr]);
1880
1881 chunk_appendf(&trash, "\n");
1882 }
1883
Willy Tarreau06d80a92017-10-19 14:32:15 +02001884 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001885 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001886 return 0;
1887 }
1888 }
1889 return 1;
1890}
1891
1892/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001893 * states of servers to stdout. It dumps proxy <cli.p0> and stops if <cli.i0> is
1894 * non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01001895 */
1896static int cli_io_handler_servers_state(struct appctx *appctx)
1897{
1898 struct stream_interface *si = appctx->owner;
William Lallemanda6c5f332016-11-19 02:25:36 +01001899 struct proxy *curproxy;
1900
1901 chunk_reset(&trash);
1902
1903 if (appctx->st2 == STAT_ST_INIT) {
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001904 if (!appctx->ctx.cli.p0)
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001905 appctx->ctx.cli.p0 = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01001906 appctx->st2 = STAT_ST_HEAD;
1907 }
1908
1909 if (appctx->st2 == STAT_ST_HEAD) {
Willy Tarreau69f591e2020-07-01 07:00:59 +02001910 if (appctx->ctx.cli.o0 == 0)
1911 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
1912 else
1913 chunk_printf(&trash,
1914 "# 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",
1915 global.nbthread);
1916
Willy Tarreau06d80a92017-10-19 14:32:15 +02001917 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001918 si_rx_room_blk(si);
William Lallemanda6c5f332016-11-19 02:25:36 +01001919 return 0;
1920 }
1921 appctx->st2 = STAT_ST_INFO;
1922 }
1923
1924 /* STAT_ST_INFO */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001925 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1926 curproxy = appctx->ctx.cli.p0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001927 /* servers are only in backends */
1928 if (curproxy->cap & PR_CAP_BE) {
Willy Tarreau6ff81432020-07-01 07:02:42 +02001929 if (!dump_servers_state(si))
William Lallemanda6c5f332016-11-19 02:25:36 +01001930 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01001931 }
1932 /* only the selected proxy is dumped */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01001933 if (appctx->ctx.cli.i0)
William Lallemanda6c5f332016-11-19 02:25:36 +01001934 break;
1935 }
1936
1937 return 1;
1938}
1939
Willy Tarreau608ea592016-12-16 18:01:15 +01001940/* Parses backend list and simply report backend names. It keeps the proxy
1941 * pointer in cli.p0.
1942 */
William Lallemand933efcd2016-11-22 12:34:16 +01001943static int cli_io_handler_show_backend(struct appctx *appctx)
1944{
William Lallemand933efcd2016-11-22 12:34:16 +01001945 struct stream_interface *si = appctx->owner;
1946 struct proxy *curproxy;
1947
1948 chunk_reset(&trash);
1949
Willy Tarreau608ea592016-12-16 18:01:15 +01001950 if (!appctx->ctx.cli.p0) {
William Lallemand933efcd2016-11-22 12:34:16 +01001951 chunk_printf(&trash, "# name\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02001952 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001953 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001954 return 0;
1955 }
Olivier Houchardfbc74e82017-11-24 16:54:05 +01001956 appctx->ctx.cli.p0 = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01001957 }
1958
Willy Tarreau608ea592016-12-16 18:01:15 +01001959 for (; appctx->ctx.cli.p0 != NULL; appctx->ctx.cli.p0 = curproxy->next) {
1960 curproxy = appctx->ctx.cli.p0;
William Lallemand933efcd2016-11-22 12:34:16 +01001961
1962 /* looking for backends only */
1963 if (!(curproxy->cap & PR_CAP_BE))
1964 continue;
1965
1966 /* we don't want to list a backend which is bound to this process */
Willy Tarreau6daac192019-02-02 17:39:53 +01001967 if (!(proc_mask(curproxy->bind_proc) & pid_bit))
William Lallemand933efcd2016-11-22 12:34:16 +01001968 continue;
1969
1970 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001971 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001972 si_rx_room_blk(si);
William Lallemand933efcd2016-11-22 12:34:16 +01001973 return 0;
1974 }
1975 }
1976
1977 return 1;
1978}
William Lallemanda6c5f332016-11-19 02:25:36 +01001979
Willy Tarreaua275a372018-08-21 14:50:44 +02001980/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
1981 *
1982 * Grabs the proxy lock and each server's lock.
1983 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02001984static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01001985{
1986 struct proxy *px;
1987 struct server *s;
1988
1989 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
1990 return 1;
1991
1992 px = cli_find_backend(appctx, args[3]);
1993 if (!px)
1994 return 1;
1995
Willy Tarreau5e83d992019-07-30 11:59:34 +02001996 /* Note: this lock is to make sure this doesn't change while another
1997 * thread is in srv_set_dyncookie().
1998 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001999 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002000 px->ck_opts |= PR_CK_DYNAMIC;
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002001 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002002
Willy Tarreaua275a372018-08-21 14:50:44 +02002003 for (s = px->srv; s != NULL; s = s->next) {
2004 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002005 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02002006 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2007 }
2008
Olivier Houchard614f8d72017-03-14 20:08:46 +01002009 return 1;
2010}
2011
Willy Tarreaua275a372018-08-21 14:50:44 +02002012/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
2013 *
2014 * Grabs the proxy lock and each server's lock.
2015 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002016static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01002017{
2018 struct proxy *px;
2019 struct server *s;
2020
2021 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2022 return 1;
2023
2024 px = cli_find_backend(appctx, args[3]);
2025 if (!px)
2026 return 1;
2027
Willy Tarreau5e83d992019-07-30 11:59:34 +02002028 /* Note: this lock is to make sure this doesn't change while another
2029 * thread is in srv_set_dyncookie().
2030 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002031 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002032 px->ck_opts &= ~PR_CK_DYNAMIC;
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002033 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002034
2035 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02002036 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002037 if (!(s->flags & SRV_F_COOKIESET)) {
2038 free(s->cookie);
2039 s->cookie = NULL;
2040 }
Willy Tarreaua275a372018-08-21 14:50:44 +02002041 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002042 }
2043
2044 return 1;
2045}
2046
Willy Tarreaua275a372018-08-21 14:50:44 +02002047/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
2048 *
2049 * Grabs the proxy lock and each server's lock.
2050 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002051static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01002052{
2053 struct proxy *px;
2054 struct server *s;
2055 char *newkey;
2056
2057 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2058 return 1;
2059
2060 px = cli_find_backend(appctx, args[3]);
2061 if (!px)
2062 return 1;
2063
Willy Tarreau9d008692019-08-09 11:21:01 +02002064 if (!*args[4])
2065 return cli_err(appctx, "String value expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01002066
2067 newkey = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002068 if (!newkey)
2069 return cli_err(appctx, "Failed to allocate memory.\n");
Willy Tarreaua275a372018-08-21 14:50:44 +02002070
Willy Tarreau5e83d992019-07-30 11:59:34 +02002071 /* Note: this lock is to make sure this doesn't change while another
2072 * thread is in srv_set_dyncookie().
2073 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002074 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002075 free(px->dyncookie_key);
2076 px->dyncookie_key = newkey;
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002077 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002078
Willy Tarreaua275a372018-08-21 14:50:44 +02002079 for (s = px->srv; s != NULL; s = s->next) {
2080 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002081 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02002082 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
2083 }
2084
Olivier Houchard614f8d72017-03-14 20:08:46 +01002085 return 1;
2086}
2087
Willy Tarreaua275a372018-08-21 14:50:44 +02002088/* Parses the "set maxconn frontend" directive, it always returns 1.
2089 *
2090 * Grabs the proxy lock.
2091 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002092static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002093{
2094 struct proxy *px;
2095 struct listener *l;
2096 int v;
2097
2098 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2099 return 1;
2100
2101 px = cli_find_frontend(appctx, args[3]);
2102 if (!px)
2103 return 1;
2104
Willy Tarreau9d008692019-08-09 11:21:01 +02002105 if (!*args[4])
2106 return cli_err(appctx, "Integer value expected.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002107
2108 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02002109 if (v < 0)
2110 return cli_err(appctx, "Value out of range.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002111
2112 /* OK, the value is fine, so we assign it to the proxy and to all of
2113 * its listeners. The blocked ones will be dequeued.
2114 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002115 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02002116
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002117 px->maxconn = v;
2118 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002119 if (l->state == LI_FULL)
2120 resume_listener(l);
2121 }
2122
Willy Tarreau241797a2019-12-10 14:10:52 +01002123 if (px->maxconn > px->feconn)
2124 dequeue_proxy_listeners(px);
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002125
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002126 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02002127
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002128 return 1;
2129}
2130
Willy Tarreaua275a372018-08-21 14:50:44 +02002131/* Parses the "shutdown frontend" directive, it always returns 1.
2132 *
2133 * Grabs the proxy lock.
2134 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002135static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002136{
2137 struct proxy *px;
2138
2139 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2140 return 1;
2141
2142 px = cli_find_frontend(appctx, args[2]);
2143 if (!px)
2144 return 1;
2145
Willy Tarreauc3914d42020-09-24 08:39:22 +02002146 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002147 return cli_msg(appctx, LOG_NOTICE, "Frontend was already shut down.\n");
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002148
Willy Tarreau5212d7f2016-11-24 11:13:06 +01002149 stop_proxy(px);
2150 return 1;
2151}
2152
Willy Tarreaua275a372018-08-21 14:50:44 +02002153/* Parses the "disable frontend" directive, it always returns 1.
2154 *
2155 * Grabs the proxy lock.
2156 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002157static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01002158{
2159 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02002160 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002161
2162 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2163 return 1;
2164
2165 px = cli_find_frontend(appctx, args[2]);
2166 if (!px)
2167 return 1;
2168
Willy Tarreauc3914d42020-09-24 08:39:22 +02002169 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002170 return cli_msg(appctx, LOG_NOTICE, "Frontend was previously shut down, cannot disable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002171
Willy Tarreauf18d9682020-09-24 08:04:27 +02002172 if (!px->li_ready)
2173 return cli_msg(appctx, LOG_NOTICE, "All sockets are already disabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002174
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002175 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02002176 ret = pause_proxy(px);
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002177 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02002178
Willy Tarreau9d008692019-08-09 11:21:01 +02002179 if (!ret)
2180 return cli_err(appctx, "Failed to pause frontend, check logs for precise cause.\n");
2181
Willy Tarreau15b9e682016-11-24 11:55:28 +01002182 return 1;
2183}
2184
Willy Tarreaua275a372018-08-21 14:50:44 +02002185/* Parses the "enable frontend" directive, it always returns 1.
2186 *
2187 * Grabs the proxy lock.
2188 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002189static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01002190{
2191 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02002192 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01002193
2194 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2195 return 1;
2196
2197 px = cli_find_frontend(appctx, args[2]);
2198 if (!px)
2199 return 1;
2200
Willy Tarreauc3914d42020-09-24 08:39:22 +02002201 if (px->disabled)
Willy Tarreau9d008692019-08-09 11:21:01 +02002202 return cli_err(appctx, "Frontend was previously shut down, cannot enable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002203
Willy Tarreauf18d9682020-09-24 08:04:27 +02002204 if (px->li_ready == px->li_all)
2205 return cli_msg(appctx, LOG_NOTICE, "All sockets are already enabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002206
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002207 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02002208 ret = resume_proxy(px);
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002209 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02002210
Willy Tarreau9d008692019-08-09 11:21:01 +02002211 if (!ret)
2212 return cli_err(appctx, "Failed to resume frontend, check logs for precise cause (port conflict?).\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01002213 return 1;
2214}
2215
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002216/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
2217 * now.
2218 */
2219static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
2220{
2221 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2222 return 1;
2223
2224 if (*args[2]) {
2225 struct proxy *px;
2226
2227 px = proxy_find_by_name(args[2], 0, 0);
2228 if (px)
2229 appctx->ctx.errors.iid = px->uuid;
2230 else
2231 appctx->ctx.errors.iid = atoi(args[2]);
2232
Willy Tarreau9d008692019-08-09 11:21:01 +02002233 if (!appctx->ctx.errors.iid)
2234 return cli_err(appctx, "No such proxy.\n");
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002235 }
2236 else
2237 appctx->ctx.errors.iid = -1; // dump all proxies
2238
2239 appctx->ctx.errors.flag = 0;
2240 if (strcmp(args[3], "request") == 0)
2241 appctx->ctx.errors.flag |= 4; // ignore response
2242 else if (strcmp(args[3], "response") == 0)
2243 appctx->ctx.errors.flag |= 2; // ignore request
2244 appctx->ctx.errors.px = NULL;
2245 return 0;
2246}
2247
2248/* This function dumps all captured errors onto the stream interface's
2249 * read buffer. It returns 0 if the output buffer is full and it needs
2250 * to be called again, otherwise non-zero.
2251 */
2252static int cli_io_handler_show_errors(struct appctx *appctx)
2253{
2254 struct stream_interface *si = appctx->owner;
2255 extern const char *monthname[12];
2256
2257 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
2258 return 1;
2259
2260 chunk_reset(&trash);
2261
2262 if (!appctx->ctx.errors.px) {
2263 /* the function had not been called yet, let's prepare the
2264 * buffer for a response.
2265 */
2266 struct tm tm;
2267
2268 get_localtime(date.tv_sec, &tm);
2269 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
2270 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2271 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
2272 error_snapshot_id);
2273
Willy Tarreau36b27362018-09-07 19:55:44 +02002274 if (ci_putchk(si_ic(si), &trash) == -1)
2275 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002276
2277 appctx->ctx.errors.px = proxies_list;
2278 appctx->ctx.errors.bol = 0;
2279 appctx->ctx.errors.ptr = -1;
2280 }
2281
2282 /* we have two inner loops here, one for the proxy, the other one for
2283 * the buffer.
2284 */
2285 while (appctx->ctx.errors.px) {
2286 struct error_snapshot *es;
2287
Willy Tarreau55039082020-10-20 17:38:10 +02002288 HA_RWLOCK_RDLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreau36b27362018-09-07 19:55:44 +02002289
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002290 if ((appctx->ctx.errors.flag & 1) == 0) {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002291 es = appctx->ctx.errors.px->invalid_req;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002292 if (appctx->ctx.errors.flag & 2) // skip req
2293 goto next;
2294 }
2295 else {
Willy Tarreauc55015e2018-09-07 19:02:32 +02002296 es = appctx->ctx.errors.px->invalid_rep;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002297 if (appctx->ctx.errors.flag & 4) // skip resp
2298 goto next;
2299 }
2300
Willy Tarreauc55015e2018-09-07 19:02:32 +02002301 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002302 goto next;
2303
2304 if (appctx->ctx.errors.iid >= 0 &&
2305 appctx->ctx.errors.px->uuid != appctx->ctx.errors.iid &&
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002306 (!es->oe || es->oe->uuid != appctx->ctx.errors.iid))
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002307 goto next;
2308
2309 if (appctx->ctx.errors.ptr < 0) {
2310 /* just print headers now */
2311
2312 char pn[INET6_ADDRSTRLEN];
2313 struct tm tm;
2314 int port;
2315
2316 get_localtime(es->when.tv_sec, &tm);
2317 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
2318 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2319 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
2320
2321 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
2322 case AF_INET:
2323 case AF_INET6:
2324 port = get_host_port(&es->src);
2325 break;
2326 default:
2327 port = 0;
2328 }
2329
2330 switch (appctx->ctx.errors.flag & 1) {
2331 case 0:
2332 chunk_appendf(&trash,
2333 " frontend %s (#%d): invalid request\n"
2334 " backend %s (#%d)",
2335 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002336 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
2337 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002338 break;
2339 case 1:
2340 chunk_appendf(&trash,
2341 " backend %s (#%d): invalid response\n"
2342 " frontend %s (#%d)",
2343 appctx->ctx.errors.px->id, appctx->ctx.errors.px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002344 es->oe ? es->oe->id : "<NONE>" , es->oe ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002345 break;
2346 }
2347
2348 chunk_appendf(&trash,
2349 ", server %s (#%d), event #%u, src %s:%d\n"
2350 " buffer starts at %llu (including %u out), %u free,\n"
2351 " len %u, wraps at %u, error at position %u\n",
2352 es->srv ? es->srv->id : "<NONE>",
2353 es->srv ? es->srv->puid : -1,
2354 es->ev_id, pn, port,
2355 es->buf_ofs, es->buf_out,
2356 global.tune.bufsize - es->buf_out - es->buf_len,
2357 es->buf_len, es->buf_wrap, es->buf_err);
2358
2359 if (es->show)
2360 es->show(&trash, es);
2361
2362 chunk_appendf(&trash, " \n");
2363
Willy Tarreau36b27362018-09-07 19:55:44 +02002364 if (ci_putchk(si_ic(si), &trash) == -1)
2365 goto cant_send_unlock;
2366
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002367 appctx->ctx.errors.ptr = 0;
2368 appctx->ctx.errors.ev_id = es->ev_id;
2369 }
2370
2371 if (appctx->ctx.errors.ev_id != es->ev_id) {
2372 /* the snapshot changed while we were dumping it */
2373 chunk_appendf(&trash,
2374 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreau36b27362018-09-07 19:55:44 +02002375 if (ci_putchk(si_ic(si), &trash) == -1)
2376 goto cant_send_unlock;
2377
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002378 goto next;
2379 }
2380
2381 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002382 while (appctx->ctx.errors.ptr < es->buf_len && appctx->ctx.errors.ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002383 int newptr;
2384 int newline;
2385
2386 newline = appctx->ctx.errors.bol;
2387 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, appctx->ctx.errors.ptr);
2388 if (newptr == appctx->ctx.errors.ptr)
Willy Tarreau36b27362018-09-07 19:55:44 +02002389 goto cant_send_unlock;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002390
Willy Tarreau36b27362018-09-07 19:55:44 +02002391 if (ci_putchk(si_ic(si), &trash) == -1)
2392 goto cant_send_unlock;
2393
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002394 appctx->ctx.errors.ptr = newptr;
2395 appctx->ctx.errors.bol = newline;
2396 };
2397 next:
Willy Tarreau55039082020-10-20 17:38:10 +02002398 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002399 appctx->ctx.errors.bol = 0;
2400 appctx->ctx.errors.ptr = -1;
2401 appctx->ctx.errors.flag ^= 1;
2402 if (!(appctx->ctx.errors.flag & 1))
2403 appctx->ctx.errors.px = appctx->ctx.errors.px->next;
2404 }
2405
2406 /* dump complete */
2407 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02002408
2409 cant_send_unlock:
Willy Tarreau55039082020-10-20 17:38:10 +02002410 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &appctx->ctx.errors.px->lock);
Willy Tarreau36b27362018-09-07 19:55:44 +02002411 cant_send:
Willy Tarreaudb398432018-11-15 11:08:52 +01002412 si_rx_room_blk(si);
Willy Tarreau36b27362018-09-07 19:55:44 +02002413 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02002414}
2415
William Lallemanda6c5f332016-11-19 02:25:36 +01002416/* register cli keywords */
2417static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau15b9e682016-11-24 11:55:28 +01002418 { { "disable", "frontend", NULL }, "disable frontend : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
2419 { { "enable", "frontend", NULL }, "enable frontend : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
Willy Tarreauc429a1f2016-11-23 16:22:04 +01002420 { { "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 +02002421 { { "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 +01002422 { { "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 +01002423 { { "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 +01002424 { { "shutdown", "frontend", NULL }, "shutdown frontend : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
Olivier Houchard614f8d72017-03-14 20:08:46 +01002425 { { "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 },
2426 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
2427 { { "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 +02002428 { { "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 +01002429 {{},}
2430}};
2431
Willy Tarreau0108d902018-11-25 19:14:37 +01002432INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002433
2434/*
2435 * Local variables:
2436 * c-indent-level: 8
2437 * c-basic-offset: 8
2438 * End:
2439 */