blob: c53ea02ce9bd2f095adb4aad635868e68dfce795 [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
Willy Tarreaubaaee002006-06-26 02:48:02 +020013#include <unistd.h>
Willy Tarreauc8f24f82007-11-30 18:38:35 +010014#include <string.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020015#include <sys/types.h>
16#include <sys/socket.h>
17#include <sys/stat.h>
18
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <import/eb32tree.h>
20#include <import/ebistree.h>
21
Amaury Denoyelle27fefa12021-03-24 16:13:20 +010022#include <haproxy/acl.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020023#include <haproxy/api.h>
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +020024#include <haproxy/applet.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020025#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 Tarreau76838932021-02-12 08:49:47 +010033#include <haproxy/http_htx.h>
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +010034#include <haproxy/http_ext.h>
Aurelien DARRAGON7abc9222023-05-11 10:30:27 +020035#include <haproxy/http_rules.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020036#include <haproxy/listener.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020037#include <haproxy/log.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020038#include <haproxy/obj_type-t.h>
Willy Tarreau3c2a7c22020-06-04 18:38:21 +020039#include <haproxy/peers.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020040#include <haproxy/pool.h>
Willy Tarreau626f3a72020-10-07 16:52:43 +020041#include <haproxy/protocol.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020042#include <haproxy/proto_tcp.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020043#include <haproxy/proxy.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020044#include <haproxy/sc_strm.h>
Frédéric Lécaille748ece62022-05-21 23:58:40 +020045#include <haproxy/quic_tp.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020046#include <haproxy/server-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020047#include <haproxy/signal.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020048#include <haproxy/stats-t.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020049#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020050#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020051#include <haproxy/task.h>
Willy Tarreau76838932021-02-12 08:49:47 +010052#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020053#include <haproxy/time.h>
Willy Tarreaue6844832021-05-08 13:02:07 +020054#include <haproxy/tools.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau918ff602011-07-25 16:33:49 +020057int listeners; /* # of proxy listeners, set by cfgparse */
Olivier Houchardfbc74e82017-11-24 16:54:05 +010058struct proxy *proxies_list = NULL; /* list of all existing proxies */
Willy Tarreau53fb4ae2009-10-04 23:04:08 +020059struct eb_root used_proxy_id = EB_ROOT; /* list of proxy IDs in use */
Willy Tarreauf79d9502014-03-15 07:22:35 +010060struct eb_root proxy_by_name = EB_ROOT; /* tree of proxies sorted by name */
Willy Tarreau0a0f6a72021-02-12 13:52:11 +010061struct eb_root defproxy_by_name = EB_ROOT; /* tree of default proxies sorted by name (dups possible) */
Willy Tarreau10479e42010-12-12 14:00:34 +010062unsigned int error_snapshot_id = 0; /* global ID assigned to each error then incremented */
Willy Tarreaubaaee002006-06-26 02:48:02 +020063
Willy Tarreau72a04232022-05-05 17:00:20 +020064/* CLI context used during "show servers {state|conn}" */
65struct show_srv_ctx {
66 struct proxy *px; /* current proxy to dump or NULL */
67 struct server *sv; /* current server to dump or NULL */
68 uint only_pxid; /* dump only this proxy ID when explicit */
69 int show_conn; /* non-zero = "conn" otherwise "state" */
Willy Tarreaud741e9c2022-05-05 19:26:18 +020070 enum {
71 SHOW_SRV_HEAD = 0,
72 SHOW_SRV_LIST,
73 } state;
Willy Tarreau72a04232022-05-05 17:00:20 +020074};
75
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010076/* proxy->options */
77const struct cfg_opt cfg_opts[] =
78{
79 { "abortonclose", PR_O_ABRT_CLOSE, PR_CAP_BE, 0, 0 },
80 { "allbackups", PR_O_USE_ALL_BK, PR_CAP_BE, 0, 0 },
81 { "checkcache", PR_O_CHK_CACHE, PR_CAP_BE, 0, PR_MODE_HTTP },
82 { "clitcpka", PR_O_TCP_CLI_KA, PR_CAP_FE, 0, 0 },
83 { "contstats", PR_O_CONTSTATS, PR_CAP_FE, 0, 0 },
84 { "dontlognull", PR_O_NULLNOLOG, PR_CAP_FE, 0, 0 },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010085 { "http-buffer-request", PR_O_WREQ_BODY, PR_CAP_FE | PR_CAP_BE, 0, PR_MODE_HTTP },
86 { "http-ignore-probes", PR_O_IGNORE_PRB, PR_CAP_FE, 0, PR_MODE_HTTP },
William Dauchya9dd9012022-01-05 22:53:24 +010087 { "idle-close-on-response", PR_O_IDLE_CLOSE_RESP, PR_CAP_FE, 0, PR_MODE_HTTP },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010088 { "prefer-last-server", PR_O_PREF_LAST, PR_CAP_BE, 0, PR_MODE_HTTP },
89 { "logasap", PR_O_LOGASAP, PR_CAP_FE, 0, 0 },
90 { "nolinger", PR_O_TCP_NOLING, PR_CAP_FE | PR_CAP_BE, 0, 0 },
91 { "persist", PR_O_PERSIST, PR_CAP_BE, 0, 0 },
92 { "srvtcpka", PR_O_TCP_SRV_KA, PR_CAP_BE, 0, 0 },
Willy Tarreaue5733232019-05-22 19:24:06 +020093#ifdef USE_TPROXY
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +010094 { "transparent", PR_O_TRANSP, PR_CAP_BE, 0, 0 },
95#else
96 { "transparent", 0, 0, 0, 0 },
97#endif
98
99 { NULL, 0, 0, 0, 0 }
100};
101
102/* proxy->options2 */
103const struct cfg_opt cfg_opts2[] =
104{
Willy Tarreaue5733232019-05-22 19:24:06 +0200105#ifdef USE_LINUX_SPLICE
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100106 { "splice-request", PR_O2_SPLIC_REQ, PR_CAP_FE|PR_CAP_BE, 0, 0 },
107 { "splice-response", PR_O2_SPLIC_RTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
108 { "splice-auto", PR_O2_SPLIC_AUT, PR_CAP_FE|PR_CAP_BE, 0, 0 },
109#else
110 { "splice-request", 0, 0, 0, 0 },
111 { "splice-response", 0, 0, 0, 0 },
112 { "splice-auto", 0, 0, 0, 0 },
113#endif
114 { "accept-invalid-http-request", PR_O2_REQBUG_OK, PR_CAP_FE, 0, PR_MODE_HTTP },
115 { "accept-invalid-http-response", PR_O2_RSPBUG_OK, PR_CAP_BE, 0, PR_MODE_HTTP },
116 { "dontlog-normal", PR_O2_NOLOGNORM, PR_CAP_FE, 0, 0 },
117 { "log-separate-errors", PR_O2_LOGERRORS, PR_CAP_FE, 0, 0 },
118 { "log-health-checks", PR_O2_LOGHCHKS, PR_CAP_BE, 0, 0 },
119 { "socket-stats", PR_O2_SOCKSTAT, PR_CAP_FE, 0, 0 },
120 { "tcp-smart-accept", PR_O2_SMARTACC, PR_CAP_FE, 0, 0 },
121 { "tcp-smart-connect", PR_O2_SMARTCON, PR_CAP_BE, 0, 0 },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100122 { "independent-streams", PR_O2_INDEPSTR, PR_CAP_FE|PR_CAP_BE, 0, 0 },
123 { "http-use-proxy-header", PR_O2_USE_PXHDR, PR_CAP_FE, 0, PR_MODE_HTTP },
124 { "http-pretend-keepalive", PR_O2_FAKE_KA, PR_CAP_BE, 0, PR_MODE_HTTP },
125 { "http-no-delay", PR_O2_NODELAY, PR_CAP_FE|PR_CAP_BE, 0, PR_MODE_HTTP },
Christopher Faulet98fbe952019-07-22 16:18:24 +0200126
Christopher Fauleta9e934b2022-09-06 10:09:40 +0200127 {"h1-case-adjust-bogus-client", PR_O2_H1_ADJ_BUGCLI, PR_CAP_FE, 0, 0 },
128 {"h1-case-adjust-bogus-server", PR_O2_H1_ADJ_BUGSRV, PR_CAP_BE, 0, 0 },
Christopher Faulet89aed322020-06-02 17:33:56 +0200129 {"disable-h2-upgrade", PR_O2_NO_H2_UPGRADE, PR_CAP_FE, 0, PR_MODE_HTTP },
Willy Tarreau3a1f5fd2018-11-11 15:40:36 +0100130 { NULL, 0, 0, 0 }
131};
132
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100133static void free_stick_rules(struct list *rules)
134{
135 struct sticking_rule *rule, *ruleb;
136
137 list_for_each_entry_safe(rule, ruleb, rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200138 LIST_DELETE(&rule->list);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100139 free_acl_cond(rule->cond);
140 release_sample_expr(rule->expr);
141 free(rule);
142 }
143}
144
145void free_proxy(struct proxy *p)
146{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +0200147 struct server *s;
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100148 struct cap_hdr *h,*h_next;
149 struct listener *l,*l_next;
150 struct bind_conf *bind_conf, *bind_back;
151 struct acl_cond *cond, *condb;
152 struct acl *acl, *aclb;
153 struct server_rule *srule, *sruleb;
154 struct switching_rule *rule, *ruleb;
155 struct redirect_rule *rdr, *rdrb;
156 struct logsrv *log, *logb;
157 struct logformat_node *lf, *lfb;
158 struct proxy_deinit_fct *pxdf;
159 struct server_deinit_fct *srvdf;
160
William Lallemandb0281a42021-08-20 10:16:37 +0200161 if (!p)
162 return;
163
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100164 free(p->conf.file);
165 free(p->id);
166 free(p->cookie_name);
167 free(p->cookie_domain);
168 free(p->cookie_attrs);
169 free(p->lbprm.arg_str);
Willy Tarreau7c9a0fe2022-04-25 10:25:34 +0200170 release_sample_expr(p->lbprm.expr);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100171 free(p->server_state_file_name);
172 free(p->capture_name);
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +0100173 istfree(&p->monitor_uri);
Aurelien DARRAGON31695352024-06-10 18:17:34 +0200174 istfree(&p->server_id_hdr_name);
Aurelien DARRAGONf174b6f2024-06-10 18:37:51 +0200175 ha_free(&p->check_command);
176 ha_free(&p->check_path);
Aurelien DARRAGON56b70252024-06-10 18:48:49 +0200177 ha_free(&p->dyncookie_key);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100178 free(p->rdp_cookie_name);
179 free(p->invalid_rep);
180 free(p->invalid_req);
Aurelien DARRAGON010d78b2024-06-10 19:23:36 +0200181 ha_free(&p->conn_src.iface_name);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100182#if defined(CONFIG_HAP_TRANSPARENT)
183 free(p->conn_src.bind_hdr_name);
184#endif
185 if (p->conf.logformat_string != default_http_log_format &&
186 p->conf.logformat_string != default_tcp_log_format &&
Remi Tricot-Le Breton98b930d2021-07-29 09:45:52 +0200187 p->conf.logformat_string != clf_http_log_format &&
William Lallemandbe6a8732022-12-22 15:37:01 +0100188 p->conf.logformat_string != default_https_log_format &&
189 p->conf.logformat_string != httpclient_log_format)
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100190 free(p->conf.logformat_string);
191
192 free(p->conf.lfs_file);
193 free(p->conf.uniqueid_format_string);
194 istfree(&p->header_unique_id);
195 free(p->conf.uif_file);
196 if ((p->lbprm.algo & BE_LB_LKUP) == BE_LB_LKUP_MAP)
197 free(p->lbprm.map.srv);
198
199 if (p->conf.logformat_sd_string != default_rfc5424_sd_log_format)
200 free(p->conf.logformat_sd_string);
201 free(p->conf.lfsd_file);
202
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +0200203 free(p->conf.error_logformat_string);
204 free(p->conf.elfs_file);
205
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100206 list_for_each_entry_safe(cond, condb, &p->mon_fail_cond, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200207 LIST_DELETE(&cond->list);
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200208 free_acl_cond(cond);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100209 }
210
211 EXTRA_COUNTERS_FREE(p->extra_counters_fe);
212 EXTRA_COUNTERS_FREE(p->extra_counters_be);
213
214 list_for_each_entry_safe(acl, aclb, &p->acl, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200215 LIST_DELETE(&acl->list);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100216 prune_acl(acl);
217 free(acl);
218 }
219
220 list_for_each_entry_safe(srule, sruleb, &p->server_rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200221 LIST_DELETE(&srule->list);
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200222 free_acl_cond(srule->cond);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100223 list_for_each_entry_safe(lf, lfb, &srule->expr, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200224 LIST_DELETE(&lf->list);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100225 release_sample_expr(lf->expr);
226 free(lf->arg);
227 free(lf);
228 }
229 free(srule->file);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100230 free(srule);
231 }
232
233 list_for_each_entry_safe(rule, ruleb, &p->switching_rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200234 LIST_DELETE(&rule->list);
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200235 free_acl_cond(rule->cond);
Christopher Fauletea6475c2024-04-05 20:47:34 +0200236 if (rule->dynamic) {
237 list_for_each_entry_safe(lf, lfb, &rule->be.expr, list) {
238 LIST_DELETE(&lf->list);
239 release_sample_expr(lf->expr);
240 free(lf->arg);
241 free(lf);
242 }
243 }
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100244 free(rule->file);
245 free(rule);
246 }
247
248 list_for_each_entry_safe(rdr, rdrb, &p->redirect_rules, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200249 LIST_DELETE(&rdr->list);
Aurelien DARRAGON7abc9222023-05-11 10:30:27 +0200250 http_free_redirect_rule(rdr);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100251 }
252
253 list_for_each_entry_safe(log, logb, &p->logsrvs, list) {
Willy Tarreau211ea252022-03-17 19:47:33 +0100254 LIST_DEL_INIT(&log->list);
255 free_logsrv(log);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100256 }
257
Aurelien DARRAGON2982d882024-06-10 15:54:49 +0200258 chunk_destroy(&p->log_tag);
259
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100260 list_for_each_entry_safe(lf, lfb, &p->logformat, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200261 LIST_DELETE(&lf->list);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100262 release_sample_expr(lf->expr);
263 free(lf->arg);
264 free(lf);
265 }
266
267 list_for_each_entry_safe(lf, lfb, &p->logformat_sd, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200268 LIST_DELETE(&lf->list);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100269 release_sample_expr(lf->expr);
270 free(lf->arg);
271 free(lf);
272 }
273
274 list_for_each_entry_safe(lf, lfb, &p->format_unique_id, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200275 LIST_DELETE(&lf->list);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100276 release_sample_expr(lf->expr);
277 free(lf->arg);
278 free(lf);
279 }
280
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +0200281 list_for_each_entry_safe(lf, lfb, &p->logformat_error, list) {
282 LIST_DELETE(&lf->list);
283 release_sample_expr(lf->expr);
284 free(lf->arg);
285 free(lf);
286 }
287
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100288 free_act_rules(&p->tcp_req.inspect_rules);
289 free_act_rules(&p->tcp_rep.inspect_rules);
290 free_act_rules(&p->tcp_req.l4_rules);
291 free_act_rules(&p->tcp_req.l5_rules);
292 free_act_rules(&p->http_req_rules);
293 free_act_rules(&p->http_res_rules);
294 free_act_rules(&p->http_after_res_rules);
295
296 free_stick_rules(&p->storersp_rules);
297 free_stick_rules(&p->sticking_rules);
298
299 h = p->req_cap;
300 while (h) {
Christopher Faulet57962282022-04-25 14:30:58 +0200301 if (p->defpx && h == p->defpx->req_cap)
302 break;
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100303 h_next = h->next;
304 free(h->name);
305 pool_destroy(h->pool);
306 free(h);
307 h = h_next;
308 }/* end while(h) */
309
310 h = p->rsp_cap;
311 while (h) {
Christopher Faulet57962282022-04-25 14:30:58 +0200312 if (p->defpx && h == p->defpx->rsp_cap)
313 break;
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100314 h_next = h->next;
315 free(h->name);
316 pool_destroy(h->pool);
317 free(h);
318 h = h_next;
319 }/* end while(h) */
320
321 s = p->srv;
322 while (s) {
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100323 list_for_each_entry(srvdf, &server_deinit_list, list)
324 srvdf->fct(s);
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +0200325 s = srv_drop(s);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100326 }/* end while(s) */
327
Aurelien DARRAGON899b5472023-06-01 12:07:43 +0200328 /* also free default-server parameters since some of them might have
329 * been dynamically allocated (e.g.: config hints, cookies, ssl..)
330 */
331 srv_free_params(&p->defsrv);
332
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100333 list_for_each_entry_safe(l, l_next, &p->conf.listeners, by_fe) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200334 LIST_DELETE(&l->by_fe);
335 LIST_DELETE(&l->by_bind);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100336 free(l->name);
Amaury Denoyellef68b2cb2022-01-25 16:21:47 +0100337 free(l->per_thr);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100338 free(l->counters);
339
340 EXTRA_COUNTERS_FREE(l->extra_counters);
341 free(l);
342 }
343
344 /* Release unused SSL configs. */
345 list_for_each_entry_safe(bind_conf, bind_back, &p->conf.bind, by_fe) {
346 if (bind_conf->xprt->destroy_bind_conf)
347 bind_conf->xprt->destroy_bind_conf(bind_conf);
348 free(bind_conf->file);
349 free(bind_conf->arg);
Aurelien DARRAGON12d6b9a2023-06-01 10:58:44 +0200350 free(bind_conf->settings.interface);
Willy Tarreau2b718102021-04-21 07:32:39 +0200351 LIST_DELETE(&bind_conf->by_fe);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100352 free(bind_conf);
353 }
354
355 flt_deinit(p);
356
357 list_for_each_entry(pxdf, &proxy_deinit_list, list)
358 pxdf->fct(p);
359
360 free(p->desc);
Aurelien DARRAGONb2e2ec52023-01-09 11:09:03 +0100361
362 http_ext_clean(p);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100363
364 task_destroy(p->task);
365
366 pool_destroy(p->req_cap_pool);
367 pool_destroy(p->rsp_cap_pool);
Aurelien DARRAGONfebaa3e2023-11-16 16:18:14 +0100368
Aurelien DARRAGON6ce5cd02023-11-16 16:17:12 +0100369 stktable_deinit(p->table);
Aurelien DARRAGONfebaa3e2023-11-16 16:18:14 +0100370 ha_free(&p->table);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100371
372 HA_RWLOCK_DESTROY(&p->lbprm.lock);
373 HA_RWLOCK_DESTROY(&p->lock);
Christopher Faulet57962282022-04-25 14:30:58 +0200374
375 proxy_unref_defaults(p);
Amaury Denoyelle27fefa12021-03-24 16:13:20 +0100376 ha_free(&p);
377}
378
Willy Tarreau977b8e42006-12-29 14:19:17 +0100379/*
Willy Tarreau816eb542007-11-04 07:04:43 +0100380 * This function returns a string containing a name describing capabilities to
381 * report comprehensible error messages. Specifically, it will return the words
Willy Tarreau80dc6fe2021-02-12 09:43:33 +0100382 * "frontend", "backend" when appropriate, "defaults" if it corresponds to a
383 * defaults section, or "proxy" for all other cases including the proxies
384 * declared in "listen" mode.
Willy Tarreau977b8e42006-12-29 14:19:17 +0100385 */
Willy Tarreau816eb542007-11-04 07:04:43 +0100386const char *proxy_cap_str(int cap)
Willy Tarreau977b8e42006-12-29 14:19:17 +0100387{
Willy Tarreau80dc6fe2021-02-12 09:43:33 +0100388 if (cap & PR_CAP_DEF)
389 return "defaults";
390
Willy Tarreau816eb542007-11-04 07:04:43 +0100391 if ((cap & PR_CAP_LISTEN) != PR_CAP_LISTEN) {
392 if (cap & PR_CAP_FE)
393 return "frontend";
394 else if (cap & PR_CAP_BE)
395 return "backend";
Willy Tarreau816eb542007-11-04 07:04:43 +0100396 }
397 return "proxy";
Willy Tarreau977b8e42006-12-29 14:19:17 +0100398}
399
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100400/*
401 * This function returns a string containing the mode of the proxy in a format
402 * suitable for error messages.
403 */
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100404const char *proxy_mode_str(int mode) {
405
406 if (mode == PR_MODE_TCP)
407 return "tcp";
408 else if (mode == PR_MODE_HTTP)
409 return "http";
William Lallemandcf62f7e2018-10-26 14:47:40 +0200410 else if (mode == PR_MODE_CLI)
411 return "cli";
William Lallemand06715af2022-03-08 11:50:59 +0100412 else if (mode == PR_MODE_SYSLOG)
413 return "syslog";
414 else if (mode == PR_MODE_PEERS)
415 return "peers";
Krzysztof Piotr Oledzki6eb730d2007-11-03 23:41:58 +0100416 else
417 return "unknown";
418}
419
Willy Tarreau31a3cea2021-03-15 11:11:55 +0100420/* try to find among known options the one that looks closest to <word> by
421 * counting transitions between letters, digits and other characters. Will
422 * return the best matching word if found, otherwise NULL. An optional array
423 * of extra words to compare may be passed in <extra>, but it must then be
424 * terminated by a NULL entry. If unused it may be NULL.
425 */
426const char *proxy_find_best_option(const char *word, const char **extra)
427{
428 uint8_t word_sig[1024];
429 uint8_t list_sig[1024];
430 const char *best_ptr = NULL;
431 int dist, best_dist = INT_MAX;
432 int index;
433
434 make_word_fingerprint(word_sig, word);
435
436 for (index = 0; cfg_opts[index].name; index++) {
437 make_word_fingerprint(list_sig, cfg_opts[index].name);
438 dist = word_fingerprint_distance(word_sig, list_sig);
439 if (dist < best_dist) {
440 best_dist = dist;
441 best_ptr = cfg_opts[index].name;
442 }
443 }
444
445 for (index = 0; cfg_opts2[index].name; index++) {
446 make_word_fingerprint(list_sig, cfg_opts2[index].name);
447 dist = word_fingerprint_distance(word_sig, list_sig);
448 if (dist < best_dist) {
449 best_dist = dist;
450 best_ptr = cfg_opts2[index].name;
451 }
452 }
453
454 while (extra && *extra) {
455 make_word_fingerprint(list_sig, *extra);
456 dist = word_fingerprint_distance(word_sig, list_sig);
457 if (dist < best_dist) {
458 best_dist = dist;
459 best_ptr = *extra;
460 }
461 extra++;
462 }
463
464 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
465 best_ptr = NULL;
466 return best_ptr;
467}
468
Natalie Chen84b78692021-09-10 13:13:19 +0800469int test_backend_server(const char *bk_name, char *uri,
470 struct proxy **bk, struct server **sv)
471{
472 struct proxy *p;
473 struct server *s;
474 int uri_len = strlen(uri);
475 *sv = NULL;
476 p = proxy_be_by_name(bk_name);
477 if (bk)
478 *bk = p;
479 if (!p || (p->lbprm.algo & BE_LB_PARM) != BE_LB_HASH_URI)
480 return 0;
481 fprintf(stderr, "%s\n", uri);
482 s = get_server_uh(p, uri, uri_len, NULL);
483 *sv = s;
484 if (!s)
485 return 0;
486 return 1;
487}
488
Willy Tarreauf3950172009-10-10 18:35:51 +0200489/*
490 * This function scans the list of backends and servers to retrieve the first
491 * backend and the first server with the given names, and sets them in both
492 * parameters. It returns zero if either is not found, or non-zero and sets
493 * the ones it did not found to NULL. If a NULL pointer is passed for the
494 * backend, only the pointer to the server will be updated.
495 */
496int get_backend_server(const char *bk_name, const char *sv_name,
497 struct proxy **bk, struct server **sv)
498{
499 struct proxy *p;
500 struct server *s;
Willy Tarreau7ecc4202014-03-15 07:57:11 +0100501 int sid;
Willy Tarreauf3950172009-10-10 18:35:51 +0200502
503 *sv = NULL;
504
Willy Tarreau050536d2012-10-04 08:47:34 +0200505 sid = -1;
Willy Tarreaucfeaa472009-10-10 22:33:08 +0200506 if (*sv_name == '#')
507 sid = atoi(sv_name + 1);
508
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200509 p = proxy_be_by_name(bk_name);
Willy Tarreauf3950172009-10-10 18:35:51 +0200510 if (bk)
511 *bk = p;
512 if (!p)
513 return 0;
514
515 for (s = p->srv; s; s = s->next)
Willy Tarreau4055a102012-11-15 00:15:18 +0100516 if ((sid >= 0 && s->puid == sid) ||
517 (sid < 0 && strcmp(s->id, sv_name) == 0))
Willy Tarreauf3950172009-10-10 18:35:51 +0200518 break;
519 *sv = s;
520 if (!s)
521 return 0;
522 return 1;
523}
524
Willy Tarreaue219db72007-12-03 01:30:13 +0100525/* This function parses a "timeout" statement in a proxy section. It returns
526 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200527 * return zero, it will write an error or warning message into a preallocated
528 * buffer returned at <err>. The trailing is not be written. The function must
529 * be called with <args> pointing to the first command line word, with <proxy>
530 * pointing to the proxy being parsed, and <defpx> to the default proxy or NULL.
531 * As a special case for compatibility with older configs, it also accepts
532 * "{cli|srv|con}timeout" in args[0].
Willy Tarreaue219db72007-12-03 01:30:13 +0100533 */
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200534static int proxy_parse_timeout(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100535 const struct proxy *defpx, const char *file, int line,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200536 char **err)
Willy Tarreaue219db72007-12-03 01:30:13 +0100537{
538 unsigned timeout;
539 int retval, cap;
540 const char *res, *name;
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200541 int *tv = NULL;
Willy Tarreau01825162021-03-09 09:53:46 +0100542 const int *td = NULL;
Willy Tarreaue219db72007-12-03 01:30:13 +0100543
544 retval = 0;
Willy Tarreau9de1bbd2008-07-09 20:34:27 +0200545
546 /* simply skip "timeout" but remain compatible with old form */
547 if (strcmp(args[0], "timeout") == 0)
548 args++;
549
Willy Tarreaue219db72007-12-03 01:30:13 +0100550 name = args[0];
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100551 if (strcmp(args[0], "client") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100552 name = "client";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100553 tv = &proxy->timeout.client;
554 td = &defpx->timeout.client;
Willy Tarreaue219db72007-12-03 01:30:13 +0100555 cap = PR_CAP_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100556 } else if (strcmp(args[0], "tarpit") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100557 tv = &proxy->timeout.tarpit;
558 td = &defpx->timeout.tarpit;
Willy Tarreau51c9bde2008-01-06 13:40:03 +0100559 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100560 } else if (strcmp(args[0], "http-keep-alive") == 0) {
Willy Tarreaub16a5742010-01-10 14:46:16 +0100561 tv = &proxy->timeout.httpka;
562 td = &defpx->timeout.httpka;
563 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100564 } else if (strcmp(args[0], "http-request") == 0) {
Willy Tarreau036fae02008-01-06 13:24:40 +0100565 tv = &proxy->timeout.httpreq;
566 td = &defpx->timeout.httpreq;
Willy Tarreaucd7afc02009-07-12 10:03:17 +0200567 cap = PR_CAP_FE | PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100568 } else if (strcmp(args[0], "server") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100569 name = "server";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100570 tv = &proxy->timeout.server;
571 td = &defpx->timeout.server;
Willy Tarreaue219db72007-12-03 01:30:13 +0100572 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100573 } else if (strcmp(args[0], "connect") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100574 name = "connect";
Willy Tarreaud7c30f92007-12-03 01:38:36 +0100575 tv = &proxy->timeout.connect;
576 td = &defpx->timeout.connect;
Willy Tarreaue219db72007-12-03 01:30:13 +0100577 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100578 } else if (strcmp(args[0], "check") == 0) {
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100579 tv = &proxy->timeout.check;
580 td = &defpx->timeout.check;
581 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100582 } else if (strcmp(args[0], "queue") == 0) {
Willy Tarreaue219db72007-12-03 01:30:13 +0100583 tv = &proxy->timeout.queue;
584 td = &defpx->timeout.queue;
585 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100586 } else if (strcmp(args[0], "tunnel") == 0) {
Willy Tarreauce887fd2012-05-12 12:50:00 +0200587 tv = &proxy->timeout.tunnel;
588 td = &defpx->timeout.tunnel;
589 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100590 } else if (strcmp(args[0], "client-fin") == 0) {
Willy Tarreau05cdd962014-05-10 14:30:07 +0200591 tv = &proxy->timeout.clientfin;
592 td = &defpx->timeout.clientfin;
593 cap = PR_CAP_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100594 } else if (strcmp(args[0], "server-fin") == 0) {
Willy Tarreau05cdd962014-05-10 14:30:07 +0200595 tv = &proxy->timeout.serverfin;
596 td = &defpx->timeout.serverfin;
597 cap = PR_CAP_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100598 } else if (strcmp(args[0], "clitimeout") == 0) {
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200599 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout client'.", args[0]);
600 return -1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100601 } else if (strcmp(args[0], "srvtimeout") == 0) {
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200602 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout server'.", args[0]);
603 return -1;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100604 } else if (strcmp(args[0], "contimeout") == 0) {
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +0200605 memprintf(err, "the '%s' directive is not supported anymore since HAProxy 2.1. Use 'timeout connect'.", args[0]);
606 return -1;
Willy Tarreaue219db72007-12-03 01:30:13 +0100607 } else {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200608 memprintf(err,
609 "'timeout' supports 'client', 'server', 'connect', 'check', "
Willy Tarreau05cdd962014-05-10 14:30:07 +0200610 "'queue', 'http-keep-alive', 'http-request', 'tunnel', 'tarpit', "
611 "'client-fin' and 'server-fin' (got '%s')",
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200612 args[0]);
Willy Tarreaue219db72007-12-03 01:30:13 +0100613 return -1;
614 }
615
616 if (*args[1] == 0) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200617 memprintf(err, "'timeout %s' expects an integer value (in milliseconds)", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100618 return -1;
619 }
620
621 res = parse_time_err(args[1], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200622 if (res == PARSE_TIME_OVER) {
623 memprintf(err, "timer overflow in argument '%s' to 'timeout %s' (maximum value is 2147483647 ms or ~24.8 days)",
624 args[1], name);
625 return -1;
626 }
627 else if (res == PARSE_TIME_UNDER) {
628 memprintf(err, "timer underflow in argument '%s' to 'timeout %s' (minimum non-null value is 1 ms)",
629 args[1], name);
630 return -1;
631 }
632 else if (res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200633 memprintf(err, "unexpected character '%c' in 'timeout %s'", *res, name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100634 return -1;
635 }
636
637 if (!(proxy->cap & cap)) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200638 memprintf(err, "'timeout %s' will be ignored because %s '%s' has no %s capability",
639 name, proxy_type_str(proxy), proxy->id,
640 (cap & PR_CAP_BE) ? "backend" : "frontend");
Willy Tarreaue219db72007-12-03 01:30:13 +0100641 retval = 1;
642 }
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200643 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200644 memprintf(err, "overwriting 'timeout %s' which was already specified", name);
Willy Tarreaue219db72007-12-03 01:30:13 +0100645 retval = 1;
646 }
647
Willy Tarreaufac5b592014-05-22 08:24:46 +0200648 if (*args[2] != 0) {
649 memprintf(err, "'timeout %s' : unexpected extra argument '%s' after value '%s'.", name, args[2], args[1]);
650 retval = -1;
651 }
652
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200653 *tv = MS_TO_TICKS(timeout);
Willy Tarreaue219db72007-12-03 01:30:13 +0100654 return retval;
655}
656
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100657/* This function parses a "rate-limit" statement in a proxy section. It returns
658 * -1 if there is any error, 1 for a warning, otherwise zero. If it does not
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200659 * return zero, it will write an error or warning message into a preallocated
660 * buffer returned at <err>. The function must be called with <args> pointing
661 * to the first command line word, with <proxy> pointing to the proxy being
662 * parsed, and <defpx> to the default proxy or NULL.
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100663 */
664static int proxy_parse_rate_limit(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100665 const struct proxy *defpx, const char *file, int line,
Willy Tarreau28a47d62012-09-18 20:02:48 +0200666 char **err)
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100667{
William Dauchybb9da0b2020-01-16 01:34:27 +0100668 int retval;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200669 char *res;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100670 unsigned int *tv = NULL;
Willy Tarreau01825162021-03-09 09:53:46 +0100671 const unsigned int *td = NULL;
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100672 unsigned int val;
673
674 retval = 0;
675
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200676 if (strcmp(args[1], "sessions") == 0) {
Willy Tarreau13a34bd2009-05-10 18:52:49 +0200677 tv = &proxy->fe_sps_lim;
678 td = &defpx->fe_sps_lim;
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200679 }
680 else {
681 memprintf(err, "'%s' only supports 'sessions' (got '%s')", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100682 return -1;
683 }
684
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200685 if (*args[2] == 0) {
686 memprintf(err, "'%s %s' expects expects an integer value (in sessions/second)", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100687 return -1;
688 }
689
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200690 val = strtoul(args[2], &res, 0);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100691 if (*res) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200692 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 +0100693 return -1;
694 }
695
William Dauchybb9da0b2020-01-16 01:34:27 +0100696 if (!(proxy->cap & PR_CAP_FE)) {
697 memprintf(err, "%s %s will be ignored because %s '%s' has no frontend capability",
698 args[0], args[1], proxy_type_str(proxy), proxy->id);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100699 retval = 1;
700 }
701 else if (defpx && *tv != *td) {
Willy Tarreau0a3dd742012-05-08 19:47:01 +0200702 memprintf(err, "overwriting %s %s which was already specified", args[0], args[1]);
Willy Tarreau3a7d2072009-03-05 23:48:25 +0100703 retval = 1;
704 }
705
706 *tv = val;
707 return retval;
708}
709
Willy Tarreauc35362a2014-04-25 13:58:37 +0200710/* This function parses a "max-keep-alive-queue" statement in a proxy section.
711 * It returns -1 if there is any error, 1 for a warning, otherwise zero. If it
712 * does not return zero, it will write an error or warning message into a
713 * preallocated buffer returned at <err>. The function must be called with
714 * <args> pointing to the first command line word, with <proxy> pointing to
715 * the proxy being parsed, and <defpx> to the default proxy or NULL.
716 */
717static int proxy_parse_max_ka_queue(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100718 const struct proxy *defpx, const char *file, int line,
Willy Tarreauc35362a2014-04-25 13:58:37 +0200719 char **err)
720{
721 int retval;
722 char *res;
723 unsigned int val;
724
725 retval = 0;
726
727 if (*args[1] == 0) {
728 memprintf(err, "'%s' expects expects an integer value (or -1 to disable)", args[0]);
729 return -1;
730 }
731
732 val = strtol(args[1], &res, 0);
733 if (*res) {
734 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
735 return -1;
736 }
737
738 if (!(proxy->cap & PR_CAP_BE)) {
739 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
740 args[0], proxy_type_str(proxy), proxy->id);
741 retval = 1;
742 }
743
744 /* we store <val+1> so that a user-facing value of -1 is stored as zero (default) */
745 proxy->max_ka_queue = val + 1;
746 return retval;
747}
748
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200749/* This function parses a "declare" statement in a proxy section. It returns -1
750 * if there is any error, 1 for warning, otherwise 0. If it does not return zero,
751 * it will write an error or warning message into a preallocated buffer returned
752 * at <err>. The function must be called with <args> pointing to the first command
753 * line word, with <proxy> pointing to the proxy being parsed, and <defpx> to the
754 * default proxy or NULL.
755 */
756static int proxy_parse_declare(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100757 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200758 char **err)
759{
760 /* Capture keyword wannot be declared in a default proxy. */
761 if (curpx == defpx) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800762 memprintf(err, "'%s' not available in default section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200763 return -1;
764 }
765
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -0700766 /* Capture keyword is only available in frontend. */
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200767 if (!(curpx->cap & PR_CAP_FE)) {
Joseph Herlant9edebb82018-11-15 11:50:44 -0800768 memprintf(err, "'%s' only available in frontend or listen section", args[0]);
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200769 return -1;
770 }
771
772 /* Check mandatory second keyword. */
773 if (!args[1] || !*args[1]) {
774 memprintf(err, "'%s' needs a second keyword that specify the type of declaration ('capture')", args[0]);
775 return -1;
776 }
777
Joseph Herlant59dd2952018-11-15 11:46:55 -0800778 /* Actually, declare is only available for declaring capture
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200779 * slot, but in the future it can declare maps or variables.
Joseph Herlant59dd2952018-11-15 11:46:55 -0800780 * So, this section permits to check and switch according with
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200781 * the second keyword.
782 */
783 if (strcmp(args[1], "capture") == 0) {
784 char *error = NULL;
785 long len;
786 struct cap_hdr *hdr;
787
788 /* Check the next keyword. */
789 if (!args[2] || !*args[2] ||
790 (strcmp(args[2], "response") != 0 &&
791 strcmp(args[2], "request") != 0)) {
792 memprintf(err, "'%s %s' requires a direction ('request' or 'response')", args[0], args[1]);
793 return -1;
794 }
795
796 /* Check the 'len' keyword. */
797 if (!args[3] || !*args[3] || strcmp(args[3], "len") != 0) {
798 memprintf(err, "'%s %s' requires a capture length ('len')", args[0], args[1]);
799 return -1;
800 }
801
802 /* Check the length value. */
803 if (!args[4] || !*args[4]) {
804 memprintf(err, "'%s %s': 'len' requires a numeric value that represents the "
805 "capture length",
806 args[0], args[1]);
807 return -1;
808 }
809
810 /* convert the length value. */
811 len = strtol(args[4], &error, 10);
812 if (*error != '\0') {
813 memprintf(err, "'%s %s': cannot parse the length '%s'.",
814 args[0], args[1], args[3]);
815 return -1;
816 }
817
818 /* check length. */
819 if (len <= 0) {
820 memprintf(err, "length must be > 0");
821 return -1;
822 }
823
824 /* register the capture. */
Vincent Bernat02779b62016-04-03 13:48:43 +0200825 hdr = calloc(1, sizeof(*hdr));
Remi Tricot-Le Breton55ba0d62021-05-12 18:04:46 +0200826 if (!hdr) {
827 memprintf(err, "proxy '%s': out of memory while registering a capture", curpx->id);
828 return -1;
829 }
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +0200830 hdr->name = NULL; /* not a header capture */
831 hdr->namelen = 0;
832 hdr->len = len;
833 hdr->pool = create_pool("caphdr", hdr->len + 1, MEM_F_SHARED);
834
835 if (strcmp(args[2], "request") == 0) {
836 hdr->next = curpx->req_cap;
837 hdr->index = curpx->nb_req_cap++;
838 curpx->req_cap = hdr;
839 }
840 if (strcmp(args[2], "response") == 0) {
841 hdr->next = curpx->rsp_cap;
842 hdr->index = curpx->nb_rsp_cap++;
843 curpx->rsp_cap = hdr;
844 }
845 return 0;
846 }
847 else {
848 memprintf(err, "unknown declaration type '%s' (supports 'capture')", args[1]);
849 return -1;
850 }
851}
852
Olivier Houcharda254a372019-04-05 15:30:12 +0200853/* This function parses a "retry-on" statement */
854static int
855proxy_parse_retry_on(char **args, int section, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +0100856 const struct proxy *defpx, const char *file, int line,
Olivier Houcharda254a372019-04-05 15:30:12 +0200857 char **err)
858{
859 int i;
860
861 if (!(*args[1])) {
862 memprintf(err, "'%s' needs at least one keyword to specify when to retry", args[0]);
863 return -1;
864 }
865 if (!(curpx->cap & PR_CAP_BE)) {
866 memprintf(err, "'%s' only available in backend or listen section", args[0]);
867 return -1;
868 }
869 curpx->retry_type = 0;
870 for (i = 1; *(args[i]); i++) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100871 if (strcmp(args[i], "conn-failure") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200872 curpx->retry_type |= PR_RE_CONN_FAILED;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100873 else if (strcmp(args[i], "empty-response") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200874 curpx->retry_type |= PR_RE_DISCONNECTED;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100875 else if (strcmp(args[i], "response-timeout") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200876 curpx->retry_type |= PR_RE_TIMEOUT;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100877 else if (strcmp(args[i], "401") == 0)
Julien Pivotto2de240a2020-11-12 11:14:05 +0100878 curpx->retry_type |= PR_RE_401;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100879 else if (strcmp(args[i], "403") == 0)
Julien Pivotto2de240a2020-11-12 11:14:05 +0100880 curpx->retry_type |= PR_RE_403;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100881 else if (strcmp(args[i], "404") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200882 curpx->retry_type |= PR_RE_404;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100883 else if (strcmp(args[i], "408") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200884 curpx->retry_type |= PR_RE_408;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100885 else if (strcmp(args[i], "425") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200886 curpx->retry_type |= PR_RE_425;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100887 else if (strcmp(args[i], "500") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200888 curpx->retry_type |= PR_RE_500;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100889 else if (strcmp(args[i], "501") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200890 curpx->retry_type |= PR_RE_501;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100891 else if (strcmp(args[i], "502") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200892 curpx->retry_type |= PR_RE_502;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100893 else if (strcmp(args[i], "503") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200894 curpx->retry_type |= PR_RE_503;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100895 else if (strcmp(args[i], "504") == 0)
Olivier Houcharda254a372019-04-05 15:30:12 +0200896 curpx->retry_type |= PR_RE_504;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100897 else if (strcmp(args[i], "0rtt-rejected") == 0)
Olivier Houchard865d8392019-05-03 22:46:27 +0200898 curpx->retry_type |= PR_RE_EARLY_ERROR;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100899 else if (strcmp(args[i], "junk-response") == 0)
Olivier Houcharde3249a92019-05-03 23:01:47 +0200900 curpx->retry_type |= PR_RE_JUNK_REQUEST;
Olivier Houchardddf0e032019-05-10 18:05:40 +0200901 else if (!(strcmp(args[i], "all-retryable-errors")))
902 curpx->retry_type |= PR_RE_CONN_FAILED | PR_RE_DISCONNECTED |
903 PR_RE_TIMEOUT | PR_RE_500 | PR_RE_502 |
904 PR_RE_503 | PR_RE_504 | PR_RE_EARLY_ERROR |
905 PR_RE_JUNK_REQUEST;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100906 else if (strcmp(args[i], "none") == 0) {
Olivier Houcharda254a372019-04-05 15:30:12 +0200907 if (i != 1 || *args[i + 1]) {
908 memprintf(err, "'%s' 'none' keyworld only usable alone", args[0]);
909 return -1;
910 }
911 } else {
912 memprintf(err, "'%s': unknown keyword '%s'", args[0], args[i]);
913 return -1;
914 }
915
916 }
917
918
919 return 0;
920}
921
Willy Tarreau52543212020-07-09 05:58:51 +0200922#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900923/* This function parses "{cli|srv}tcpka-cnt" statements */
924static int proxy_parse_tcpka_cnt(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100925 const struct proxy *defpx, const char *file, int line,
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900926 char **err)
927{
928 int retval;
929 char *res;
930 unsigned int tcpka_cnt;
931
932 retval = 0;
933
934 if (*args[1] == 0) {
935 memprintf(err, "'%s' expects an integer value", args[0]);
936 return -1;
937 }
938
939 tcpka_cnt = strtol(args[1], &res, 0);
940 if (*res) {
941 memprintf(err, "'%s' : unexpected character '%c' in integer value '%s'", args[0], *res, args[1]);
942 return -1;
943 }
944
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100945 if (strcmp(args[0], "clitcpka-cnt") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900946 if (!(proxy->cap & PR_CAP_FE)) {
947 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
948 args[0], proxy_type_str(proxy), proxy->id);
949 retval = 1;
950 }
951 proxy->clitcpka_cnt = tcpka_cnt;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100952 } else if (strcmp(args[0], "srvtcpka-cnt") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900953 if (!(proxy->cap & PR_CAP_BE)) {
954 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
955 args[0], proxy_type_str(proxy), proxy->id);
956 retval = 1;
957 }
958 proxy->srvtcpka_cnt = tcpka_cnt;
959 } else {
960 /* unreachable */
961 memprintf(err, "'%s': unknown keyword", args[0]);
962 return -1;
963 }
964
965 return retval;
966}
Willy Tarreau52543212020-07-09 05:58:51 +0200967#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900968
Willy Tarreau52543212020-07-09 05:58:51 +0200969#ifdef TCP_KEEPIDLE
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900970/* This function parses "{cli|srv}tcpka-idle" statements */
971static int proxy_parse_tcpka_idle(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +0100972 const struct proxy *defpx, const char *file, int line,
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +0900973 char **err)
974{
975 int retval;
976 const char *res;
977 unsigned int tcpka_idle;
978
979 retval = 0;
980
981 if (*args[1] == 0) {
982 memprintf(err, "'%s' expects an integer value", args[0]);
983 return -1;
984 }
985 res = parse_time_err(args[1], &tcpka_idle, TIME_UNIT_S);
986 if (res == PARSE_TIME_OVER) {
987 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
988 args[1], args[0]);
989 return -1;
990 }
991 else if (res == PARSE_TIME_UNDER) {
992 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
993 args[1], args[0]);
994 return -1;
995 }
996 else if (res) {
997 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
998 return -1;
999 }
1000
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001001 if (strcmp(args[0], "clitcpka-idle") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001002 if (!(proxy->cap & PR_CAP_FE)) {
1003 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
1004 args[0], proxy_type_str(proxy), proxy->id);
1005 retval = 1;
1006 }
1007 proxy->clitcpka_idle = tcpka_idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001008 } else if (strcmp(args[0], "srvtcpka-idle") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001009 if (!(proxy->cap & PR_CAP_BE)) {
1010 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
1011 args[0], proxy_type_str(proxy), proxy->id);
1012 retval = 1;
1013 }
1014 proxy->srvtcpka_idle = tcpka_idle;
1015 } else {
1016 /* unreachable */
1017 memprintf(err, "'%s': unknown keyword", args[0]);
1018 return -1;
1019 }
1020
1021 return retval;
1022}
Willy Tarreau52543212020-07-09 05:58:51 +02001023#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001024
Willy Tarreau52543212020-07-09 05:58:51 +02001025#ifdef TCP_KEEPINTVL
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001026/* This function parses "{cli|srv}tcpka-intvl" statements */
1027static int proxy_parse_tcpka_intvl(char **args, int section, struct proxy *proxy,
Willy Tarreau01825162021-03-09 09:53:46 +01001028 const struct proxy *defpx, const char *file, int line,
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001029 char **err)
1030{
1031 int retval;
1032 const char *res;
1033 unsigned int tcpka_intvl;
1034
1035 retval = 0;
1036
1037 if (*args[1] == 0) {
1038 memprintf(err, "'%s' expects an integer value", args[0]);
1039 return -1;
1040 }
1041 res = parse_time_err(args[1], &tcpka_intvl, TIME_UNIT_S);
1042 if (res == PARSE_TIME_OVER) {
1043 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
1044 args[1], args[0]);
1045 return -1;
1046 }
1047 else if (res == PARSE_TIME_UNDER) {
1048 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
1049 args[1], args[0]);
1050 return -1;
1051 }
1052 else if (res) {
1053 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
1054 return -1;
1055 }
1056
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001057 if (strcmp(args[0], "clitcpka-intvl") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001058 if (!(proxy->cap & PR_CAP_FE)) {
1059 memprintf(err, "%s will be ignored because %s '%s' has no frontend capability",
1060 args[0], proxy_type_str(proxy), proxy->id);
1061 retval = 1;
1062 }
1063 proxy->clitcpka_intvl = tcpka_intvl;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001064 } else if (strcmp(args[0], "srvtcpka-intvl") == 0) {
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001065 if (!(proxy->cap & PR_CAP_BE)) {
1066 memprintf(err, "%s will be ignored because %s '%s' has no backend capability",
1067 args[0], proxy_type_str(proxy), proxy->id);
1068 retval = 1;
1069 }
1070 proxy->srvtcpka_intvl = tcpka_intvl;
1071 } else {
1072 /* unreachable */
1073 memprintf(err, "'%s': unknown keyword", args[0]);
1074 return -1;
1075 }
1076
1077 return retval;
1078}
Willy Tarreau52543212020-07-09 05:58:51 +02001079#endif
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09001080
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001081/* This function inserts proxy <px> into the tree of known proxies (regular
1082 * ones or defaults depending on px->cap & PR_CAP_DEF). The proxy's name is
1083 * used as the storing key so it must already have been initialized.
Willy Tarreauf79d9502014-03-15 07:22:35 +01001084 */
1085void proxy_store_name(struct proxy *px)
1086{
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001087 struct eb_root *root = (px->cap & PR_CAP_DEF) ? &defproxy_by_name : &proxy_by_name;
1088
Willy Tarreauf79d9502014-03-15 07:22:35 +01001089 px->conf.by_name.key = px->id;
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001090 ebis_insert(root, &px->conf.by_name);
Willy Tarreauf79d9502014-03-15 07:22:35 +01001091}
1092
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001093/* Returns a pointer to the first proxy matching capabilities <cap> and id
1094 * <id>. NULL is returned if no match is found. If <table> is non-zero, it
1095 * only considers proxies having a table.
Willy Tarreaubc216c42011-08-02 11:25:54 +02001096 */
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001097struct proxy *proxy_find_by_id(int id, int cap, int table)
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001098{
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001099 struct eb32_node *n;
Willy Tarreaubc216c42011-08-02 11:25:54 +02001100
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001101 for (n = eb32_lookup(&used_proxy_id, id); n; n = eb32_next(n)) {
1102 struct proxy *px = container_of(n, struct proxy, conf.id);
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001103
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001104 if (px->uuid != id)
1105 break;
Alex Williams96532db2009-11-01 21:27:13 -05001106
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001107 if ((px->cap & cap) != cap)
1108 continue;
Alex Williams96532db2009-11-01 21:27:13 -05001109
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001110 if (table && (!px->table || !px->table->size))
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001111 continue;
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001112
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001113 return px;
1114 }
1115 return NULL;
1116}
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001117
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001118/* Returns a pointer to the first proxy matching either name <name>, or id
1119 * <name> if <name> begins with a '#'. NULL is returned if no match is found.
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001120 * If <table> is non-zero, it only considers proxies having a table. The search
1121 * is made into the regular proxies, unless <cap> has PR_CAP_DEF set in which
1122 * case it's searched into the defproxy tree.
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001123 */
1124struct proxy *proxy_find_by_name(const char *name, int cap, int table)
1125{
1126 struct proxy *curproxy;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001127
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001128 if (*name == '#' && !(cap & PR_CAP_DEF)) {
Willy Tarreau3c56a7d2015-05-26 15:25:32 +02001129 curproxy = proxy_find_by_id(atoi(name + 1), cap, table);
1130 if (curproxy)
Willy Tarreauc739aa82015-05-26 11:35:41 +02001131 return curproxy;
Alex Williams96532db2009-11-01 21:27:13 -05001132 }
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001133 else {
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001134 struct eb_root *root;
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001135 struct ebpt_node *node;
1136
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001137 root = (cap & PR_CAP_DEF) ? &defproxy_by_name : &proxy_by_name;
1138 for (node = ebis_lookup(root, name); node; node = ebpt_next(node)) {
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001139 curproxy = container_of(node, struct proxy, conf.by_name);
Alex Williams96532db2009-11-01 21:27:13 -05001140
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001141 if (strcmp(curproxy->id, name) != 0)
1142 break;
1143
1144 if ((curproxy->cap & cap) != cap)
1145 continue;
1146
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01001147 if (table && (!curproxy->table || !curproxy->table->size))
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001148 continue;
1149
Willy Tarreauc739aa82015-05-26 11:35:41 +02001150 return curproxy;
Willy Tarreaucfd837f2014-03-15 07:43:51 +01001151 }
1152 }
Willy Tarreauc739aa82015-05-26 11:35:41 +02001153 return NULL;
Alex Williams96532db2009-11-01 21:27:13 -05001154}
1155
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001156/* Finds the best match for a proxy with capabilities <cap>, name <name> and id
1157 * <id>. At most one of <id> or <name> may be different provided that <cap> is
1158 * valid. Either <id> or <name> may be left unspecified (0). The purpose is to
1159 * find a proxy based on some information from a previous configuration, across
1160 * reloads or during information exchange between peers.
1161 *
1162 * Names are looked up first if present, then IDs are compared if present. In
1163 * case of an inexact match whatever is forced in the configuration has
1164 * precedence in the following order :
1165 * - 1) forced ID (proves a renaming / change of proxy type)
1166 * - 2) proxy name+type (may indicate a move if ID differs)
1167 * - 3) automatic ID+type (may indicate a renaming)
1168 *
1169 * Depending on what is found, we can end up in the following situations :
1170 *
1171 * name id cap | possible causes
1172 * -------------+-----------------
1173 * -- -- -- | nothing found
1174 * -- -- ok | nothing found
1175 * -- ok -- | proxy deleted, ID points to next one
1176 * -- ok ok | proxy renamed, or deleted with ID pointing to next one
1177 * ok -- -- | proxy deleted, but other half with same name still here (before)
1178 * ok -- ok | proxy's ID changed (proxy moved in the config file)
1179 * ok ok -- | proxy deleted, but other half with same name still here (after)
1180 * ok ok ok | perfect match
1181 *
1182 * Upon return if <diff> is not NULL, it is zeroed then filled with up to 3 bits :
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001183 * - PR_FBM_MISMATCH_ID : proxy was found but ID differs
1184 * (and ID was not zero)
1185 * - PR_FBM_MISMATCH_NAME : proxy was found by ID but name differs
1186 * (and name was not NULL)
1187 * - PR_FBM_MISMATCH_PROXYTYPE : a proxy of different type was found with
1188 * the same name and/or id
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001189 *
1190 * Only a valid proxy is returned. If capabilities do not match, NULL is
1191 * returned. The caller can check <diff> to report detailed warnings / errors,
1192 * and decide whether or not to use what was found.
1193 */
1194struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff)
1195{
1196 struct proxy *byname;
1197 struct proxy *byid;
1198
1199 if (!name && !id)
1200 return NULL;
1201
1202 if (diff)
1203 *diff = 0;
1204
1205 byname = byid = NULL;
1206
1207 if (name) {
1208 byname = proxy_find_by_name(name, cap, 0);
1209 if (byname && (!id || byname->uuid == id))
1210 return byname;
1211 }
1212
Joseph Herlant59dd2952018-11-15 11:46:55 -08001213 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001214 * - name not set
1215 * - name set but not found
1216 * - name found, but ID doesn't match.
1217 */
1218 if (id) {
1219 byid = proxy_find_by_id(id, cap, 0);
1220 if (byid) {
1221 if (byname) {
1222 /* id+type found, name+type found, but not all 3.
1223 * ID wins only if forced, otherwise name wins.
1224 */
1225 if (byid->options & PR_O_FORCED_ID) {
1226 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001227 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001228 return byid;
1229 }
1230 else {
1231 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001232 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001233 return byname;
1234 }
1235 }
1236
Joseph Herlant59dd2952018-11-15 11:46:55 -08001237 /* remaining possibilities :
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001238 * - name not set
1239 * - name set but not found
1240 */
1241 if (name && diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001242 *diff |= PR_FBM_MISMATCH_NAME;
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001243 return byid;
1244 }
1245
1246 /* ID not found */
1247 if (byname) {
1248 if (diff)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001249 *diff |= PR_FBM_MISMATCH_ID;
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001250 return byname;
1251 }
1252 }
1253
Joseph Herlant59dd2952018-11-15 11:46:55 -08001254 /* All remaining possibilities will lead to NULL. If we can report more
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001255 * detailed information to the caller about changed types and/or name,
1256 * we'll do it. For example, we could detect that "listen foo" was
1257 * split into "frontend foo_ft" and "backend foo_bk" if IDs are forced.
1258 * - name not set, ID not found
1259 * - name not found, ID not set
1260 * - name not found, ID not found
1261 */
1262 if (!diff)
1263 return NULL;
1264
1265 if (name) {
1266 byname = proxy_find_by_name(name, 0, 0);
1267 if (byname && (!id || byname->uuid == id))
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001268 *diff |= PR_FBM_MISMATCH_PROXYTYPE;
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001269 }
1270
1271 if (id) {
1272 byid = proxy_find_by_id(id, 0, 0);
1273 if (byid) {
1274 if (!name)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001275 *diff |= PR_FBM_MISMATCH_PROXYTYPE; /* only type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001276 else if (byid->options & PR_O_FORCED_ID)
Baptiste Assmann8a027cc2015-07-03 11:03:33 +02001277 *diff |= PR_FBM_MISMATCH_NAME | PR_FBM_MISMATCH_PROXYTYPE; /* name and type changed */
Willy Tarreaueb3e3482015-05-27 16:46:26 +02001278 /* otherwise it's a different proxy that was returned */
1279 }
1280 }
1281 return NULL;
1282}
1283
Willy Tarreaubaaee002006-06-26 02:48:02 +02001284/*
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001285 * This function finds a server with matching name within selected proxy.
1286 * It also checks if there are more matching servers with
1287 * requested name as this often leads into unexpected situations.
1288 */
1289
1290struct server *findserver(const struct proxy *px, const char *name) {
1291
1292 struct server *cursrv, *target = NULL;
1293
1294 if (!px)
1295 return NULL;
1296
1297 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001298 if (strcmp(cursrv->id, name) != 0)
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001299 continue;
1300
1301 if (!target) {
1302 target = cursrv;
1303 continue;
1304 }
1305
Christopher Faulet767a84b2017-11-24 16:50:31 +01001306 ha_alert("Refusing to use duplicated server '%s' found in proxy: %s!\n",
1307 name, px->id);
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01001308
1309 return NULL;
1310 }
1311
1312 return target;
1313}
1314
Aurelien DARRAGON4e5e2662023-02-22 09:55:05 +01001315/*
1316 * This function finds a server with matching "<puid> x <rid>" within
1317 * selected proxy <px>.
1318 * Using the combination of proxy-uid + revision id ensures that the function
1319 * will either return the server we're expecting or NULL if it has been removed
1320 * from the proxy.
1321 */
1322struct server *findserver_unique_id(const struct proxy *px, int puid, uint32_t rid) {
1323
1324 struct server *cursrv;
1325
1326 if (!px)
1327 return NULL;
1328
1329 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
1330 if (cursrv->puid == puid && cursrv->rid == rid)
1331 return cursrv;
1332 }
1333
1334 return NULL;
1335}
1336
1337/*
1338 * This function finds a server with matching "<name> x <rid>" within
1339 * selected proxy <px>.
1340 * Using the combination of name + revision id ensures that the function will
1341 * either return the server we're expecting or NULL if it has been removed
1342 * from the proxy.
1343 */
1344struct server *findserver_unique_name(const struct proxy *px, const char *name, uint32_t rid) {
1345
1346 struct server *cursrv;
1347
1348 if (!px)
1349 return NULL;
1350
1351 for (cursrv = px->srv; cursrv; cursrv = cursrv->next) {
1352 if (!strcmp(cursrv->id, name) && cursrv->rid == rid)
1353 return cursrv;
1354 }
1355
1356 return NULL;
1357}
1358
Willy Tarreauff01a212009-03-15 13:46:16 +01001359/* This function checks that the designated proxy has no http directives
1360 * enabled. It will output a warning if there are, and will fix some of them.
1361 * It returns the number of fatal errors encountered. This should be called
1362 * at the end of the configuration parsing if the proxy is not in http mode.
1363 * The <file> argument is used to construct the error message.
1364 */
Willy Tarreau915e1eb2009-06-22 15:48:36 +02001365int proxy_cfg_ensure_no_http(struct proxy *curproxy)
Willy Tarreauff01a212009-03-15 13:46:16 +01001366{
1367 if (curproxy->cookie_name != NULL) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001368 ha_warning("cookie will be ignored for %s '%s' (needs 'mode http').\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001369 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +01001370 }
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +01001371 if (isttest(curproxy->monitor_uri)) {
Amaury Denoyelle11124302021-06-04 18:22:08 +02001372 ha_warning("monitor-uri will be ignored for %s '%s' (needs 'mode http').\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001373 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +01001374 }
Willy Tarreauf3e49f92009-10-03 12:21:20 +02001375 if (curproxy->lbprm.algo & BE_LB_NEED_HTTP) {
Willy Tarreauff01a212009-03-15 13:46:16 +01001376 curproxy->lbprm.algo &= ~BE_LB_ALGO;
1377 curproxy->lbprm.algo |= BE_LB_ALGO_RR;
Amaury Denoyelle11124302021-06-04 18:22:08 +02001378 ha_warning("Layer 7 hash not possible for %s '%s' (needs 'mode http'). Falling back to round robin.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01001379 proxy_type_str(curproxy), curproxy->id);
Willy Tarreauff01a212009-03-15 13:46:16 +01001380 }
Willy Tarreau17804162009-11-09 21:27:51 +01001381 if (curproxy->to_log & (LW_REQ | LW_RESP)) {
1382 curproxy->to_log &= ~(LW_REQ | LW_RESP);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001383 ha_warning("parsing [%s:%d] : HTTP log/header format not usable with %s '%s' (needs 'mode http').\n",
1384 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
1385 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau17804162009-11-09 21:27:51 +01001386 }
Willy Tarreau62a61232013-04-12 18:13:46 +02001387 if (curproxy->conf.logformat_string == default_http_log_format ||
1388 curproxy->conf.logformat_string == clf_http_log_format) {
1389 /* Note: we don't change the directive's file:line number */
1390 curproxy->conf.logformat_string = default_tcp_log_format;
Christopher Faulet767a84b2017-11-24 16:50:31 +01001391 ha_warning("parsing [%s:%d] : 'option httplog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
1392 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
1393 proxy_type_str(curproxy), curproxy->id);
Willy Tarreau196729e2012-05-31 19:30:26 +02001394 }
Remi Tricot-Le Breton98b930d2021-07-29 09:45:52 +02001395 else if (curproxy->conf.logformat_string == default_https_log_format) {
1396 /* Note: we don't change the directive's file:line number */
1397 curproxy->conf.logformat_string = default_tcp_log_format;
1398 ha_warning("parsing [%s:%d] : 'option httpslog' not usable with %s '%s' (needs 'mode http'). Falling back to 'option tcplog'.\n",
1399 curproxy->conf.lfs_file, curproxy->conf.lfs_line,
1400 proxy_type_str(curproxy), curproxy->id);
1401 }
Willy Tarreau196729e2012-05-31 19:30:26 +02001402
Willy Tarreauff01a212009-03-15 13:46:16 +01001403 return 0;
1404}
1405
Willy Tarreau237250c2011-07-29 01:49:03 +02001406/* Perform the most basic initialization of a proxy :
1407 * memset(), list_init(*), reset_timeouts(*).
Willy Tarreaub249e842011-09-07 18:41:08 +02001408 * Any new proxy or peer should be initialized via this function.
Willy Tarreau237250c2011-07-29 01:49:03 +02001409 */
1410void init_new_proxy(struct proxy *p)
1411{
1412 memset(p, 0, sizeof(struct proxy));
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001413 p->obj_type = OBJ_TYPE_PROXY;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02001414 queue_init(&p->queue, p, NULL);
Willy Tarreau237250c2011-07-29 01:49:03 +02001415 LIST_INIT(&p->acl);
1416 LIST_INIT(&p->http_req_rules);
Willy Tarreaue365c0b2013-06-11 16:06:12 +02001417 LIST_INIT(&p->http_res_rules);
Christopher Faulet6d0c3df2020-01-22 09:26:35 +01001418 LIST_INIT(&p->http_after_res_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001419 LIST_INIT(&p->redirect_rules);
1420 LIST_INIT(&p->mon_fail_cond);
1421 LIST_INIT(&p->switching_rules);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001422 LIST_INIT(&p->server_rules);
Willy Tarreau237250c2011-07-29 01:49:03 +02001423 LIST_INIT(&p->persist_rules);
1424 LIST_INIT(&p->sticking_rules);
1425 LIST_INIT(&p->storersp_rules);
1426 LIST_INIT(&p->tcp_req.inspect_rules);
1427 LIST_INIT(&p->tcp_rep.inspect_rules);
1428 LIST_INIT(&p->tcp_req.l4_rules);
Willy Tarreau620408f2016-10-21 16:37:51 +02001429 LIST_INIT(&p->tcp_req.l5_rules);
Olivier Houchard859dc802019-08-08 15:47:21 +02001430 MT_LIST_INIT(&p->listener_queue);
William Lallemand0f99e342011-10-12 17:50:54 +02001431 LIST_INIT(&p->logsrvs);
William Lallemand723b73a2012-02-08 16:37:49 +01001432 LIST_INIT(&p->logformat);
Dragan Dosen0b85ece2015-09-25 19:17:44 +02001433 LIST_INIT(&p->logformat_sd);
William Lallemanda73203e2012-03-12 12:48:57 +01001434 LIST_INIT(&p->format_unique_id);
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02001435 LIST_INIT(&p->logformat_error);
Willy Tarreau2a65ff02012-09-13 17:54:29 +02001436 LIST_INIT(&p->conf.bind);
Willy Tarreau4348fad2012-09-20 16:48:07 +02001437 LIST_INIT(&p->conf.listeners);
Christopher Faulet76edc0f2020-01-13 15:52:01 +01001438 LIST_INIT(&p->conf.errors);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02001439 LIST_INIT(&p->conf.args.list);
Christopher Faulet443ea1a2016-02-04 13:40:26 +01001440 LIST_INIT(&p->filter_configs);
Christopher Faulet7a1e2e12020-04-02 18:05:11 +02001441 LIST_INIT(&p->tcpcheck_rules.preset_vars);
Willy Tarreau237250c2011-07-29 01:49:03 +02001442
Willy Tarreau3b06eae2021-02-12 09:15:16 +01001443 p->defsrv.id = "default-server";
1444 p->conf.used_listener_id = EB_ROOT;
1445 p->conf.used_server_id = EB_ROOT;
1446 p->used_server_addr = EB_ROOT_UNIQUE;
1447
Willy Tarreau237250c2011-07-29 01:49:03 +02001448 /* Timeouts are defined as -1 */
1449 proxy_reset_timeouts(p);
1450 p->tcp_rep.inspect_delay = TICK_ETERNITY;
Willy Tarreau050536d2012-10-04 08:47:34 +02001451
1452 /* initial uuid is unassigned (-1) */
1453 p->uuid = -1;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001454
Olivier Houcharda254a372019-04-05 15:30:12 +02001455 /* Default to only allow L4 retries */
1456 p->retry_type = PR_RE_CONN_FAILED;
1457
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01001458 p->extra_counters_fe = NULL;
1459 p->extra_counters_be = NULL;
1460
Willy Tarreauac66d6b2020-10-20 17:24:27 +02001461 HA_RWLOCK_INIT(&p->lock);
Willy Tarreau489f0742023-11-13 09:17:05 +01001462
1463 /* initialize the default settings */
1464 proxy_preset_defaults(p);
Willy Tarreau237250c2011-07-29 01:49:03 +02001465}
1466
Willy Tarreau144289b2021-02-12 08:19:01 +01001467/* Preset default settings onto proxy <defproxy>. */
1468void proxy_preset_defaults(struct proxy *defproxy)
1469{
1470 defproxy->mode = PR_MODE_TCP;
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001471 defproxy->flags = 0;
William Lallemand6bb77b92021-07-28 15:48:16 +02001472 if (!(defproxy->cap & PR_CAP_INT)) {
Amaury Denoyelle6f26fae2021-03-24 10:49:34 +01001473 defproxy->maxconn = cfg_maxpconn;
1474 defproxy->conn_retries = CONN_RETRIES;
1475 }
Willy Tarreau144289b2021-02-12 08:19:01 +01001476 defproxy->redispatch_after = 0;
1477 defproxy->options = PR_O_REUSE_SAFE;
William Lallemand6bb77b92021-07-28 15:48:16 +02001478 if (defproxy->cap & PR_CAP_INT)
Amaury Denoyelle6f26fae2021-03-24 10:49:34 +01001479 defproxy->options2 |= PR_O2_INDEPSTR;
Willy Tarreau144289b2021-02-12 08:19:01 +01001480 defproxy->max_out_conns = MAX_SRV_LIST;
1481
Amaury Denoyelle5cc599e2024-02-27 15:33:59 +01001482 srv_settings_init(&defproxy->defsrv);
Willy Tarreau144289b2021-02-12 08:19:01 +01001483
1484 defproxy->email_alert.level = LOG_ALERT;
1485 defproxy->load_server_state_from_file = PR_SRV_STATE_FILE_UNSPEC;
Amaury Denoyelle6f26fae2021-03-24 10:49:34 +01001486
William Lallemand6bb77b92021-07-28 15:48:16 +02001487 if (defproxy->cap & PR_CAP_INT)
Amaury Denoyelle6f26fae2021-03-24 10:49:34 +01001488 defproxy->timeout.connect = 5000;
Willy Tarreau144289b2021-02-12 08:19:01 +01001489}
1490
Willy Tarreaua3320a02021-02-12 10:38:49 +01001491/* Frees all dynamic settings allocated on a default proxy that's about to be
1492 * destroyed. This is a subset of the complete proxy deinit code, but these
1493 * should probably be merged ultimately. Note that most of the fields are not
1494 * even reset, so extreme care is required here, and calling
1495 * proxy_preset_defaults() afterwards would be safer.
1496 */
1497void proxy_free_defaults(struct proxy *defproxy)
1498{
Christopher Faulet6db9a972021-10-15 14:33:34 +02001499 struct acl *acl, *aclb;
Willy Tarreau211ea252022-03-17 19:47:33 +01001500 struct logsrv *log, *logb;
Christopher Faulet57962282022-04-25 14:30:58 +02001501 struct cap_hdr *h,*h_next;
Christopher Faulet6db9a972021-10-15 14:33:34 +02001502
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001503 ha_free(&defproxy->id);
1504 ha_free(&defproxy->conf.file);
Willy Tarreau8bb771a2023-11-23 14:28:14 +01001505 ha_free((char **)&defproxy->defsrv.conf.file);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001506 ha_free(&defproxy->check_command);
1507 ha_free(&defproxy->check_path);
1508 ha_free(&defproxy->cookie_name);
1509 ha_free(&defproxy->rdp_cookie_name);
1510 ha_free(&defproxy->dyncookie_key);
1511 ha_free(&defproxy->cookie_domain);
1512 ha_free(&defproxy->cookie_attrs);
1513 ha_free(&defproxy->lbprm.arg_str);
1514 ha_free(&defproxy->capture_name);
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +01001515 istfree(&defproxy->monitor_uri);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001516 ha_free(&defproxy->defbe.name);
1517 ha_free(&defproxy->conn_src.iface_name);
Aurelien DARRAGON010d78b2024-06-10 19:23:36 +02001518#if defined(CONFIG_HAP_TRANSPARENT)
1519 ha_free(&defproxy->conn_src.bind_hdr_name);
1520#endif
Aurelien DARRAGON44300fd2024-06-10 19:36:53 +02001521 istfree(&defproxy->header_unique_id);
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001522 istfree(&defproxy->server_id_hdr_name);
Willy Tarreaua3320a02021-02-12 10:38:49 +01001523
Aurelien DARRAGONb2e2ec52023-01-09 11:09:03 +01001524 http_ext_clean(defproxy);
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +01001525
Christopher Faulet6db9a972021-10-15 14:33:34 +02001526 list_for_each_entry_safe(acl, aclb, &defproxy->acl, list) {
1527 LIST_DELETE(&acl->list);
1528 prune_acl(acl);
1529 free(acl);
1530 }
1531
1532 free_act_rules(&defproxy->tcp_req.inspect_rules);
1533 free_act_rules(&defproxy->tcp_rep.inspect_rules);
1534 free_act_rules(&defproxy->tcp_req.l4_rules);
1535 free_act_rules(&defproxy->tcp_req.l5_rules);
1536 free_act_rules(&defproxy->http_req_rules);
1537 free_act_rules(&defproxy->http_res_rules);
1538 free_act_rules(&defproxy->http_after_res_rules);
1539
Christopher Faulet57962282022-04-25 14:30:58 +02001540 h = defproxy->req_cap;
1541 while (h) {
1542 h_next = h->next;
1543 free(h->name);
1544 pool_destroy(h->pool);
1545 free(h);
1546 h = h_next;
1547 }
1548
1549 h = defproxy->rsp_cap;
1550 while (h) {
1551 h_next = h->next;
1552 free(h->name);
1553 pool_destroy(h->pool);
1554 free(h);
1555 h = h_next;
1556 }
1557
Willy Tarreaua3320a02021-02-12 10:38:49 +01001558 if (defproxy->conf.logformat_string != default_http_log_format &&
1559 defproxy->conf.logformat_string != default_tcp_log_format &&
Remi Tricot-Le Breton98b930d2021-07-29 09:45:52 +02001560 defproxy->conf.logformat_string != clf_http_log_format &&
1561 defproxy->conf.logformat_string != default_https_log_format) {
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001562 ha_free(&defproxy->conf.logformat_string);
Willy Tarreau818ec782021-02-12 10:48:53 +01001563 }
Willy Tarreaua3320a02021-02-12 10:38:49 +01001564
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001565 if (defproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1566 ha_free(&defproxy->conf.logformat_sd_string);
Willy Tarreaua3320a02021-02-12 10:38:49 +01001567
Willy Tarreau211ea252022-03-17 19:47:33 +01001568 list_for_each_entry_safe(log, logb, &defproxy->logsrvs, list) {
1569 LIST_DEL_INIT(&log->list);
1570 free_logsrv(log);
1571 }
1572
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001573 ha_free(&defproxy->conf.uniqueid_format_string);
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02001574 ha_free(&defproxy->conf.error_logformat_string);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01001575 ha_free(&defproxy->conf.lfs_file);
1576 ha_free(&defproxy->conf.lfsd_file);
1577 ha_free(&defproxy->conf.uif_file);
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02001578 ha_free(&defproxy->conf.elfs_file);
Willy Tarreaua3320a02021-02-12 10:38:49 +01001579 chunk_destroy(&defproxy->log_tag);
1580
1581 free_email_alert(defproxy);
1582 proxy_release_conf_errors(defproxy);
1583 deinit_proxy_tcpcheck(defproxy);
1584
1585 /* FIXME: we cannot free uri_auth because it might already be used by
1586 * another proxy (legacy code for stats URI ...). Refcount anyone ?
1587 */
1588}
1589
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001590/* delete a defproxy from the tree if still in it, frees its content and its
1591 * storage. Nothing is done if <px> is NULL or if it doesn't have PR_CAP_DEF
1592 * set, allowing to pass it the direct result of a lookup function.
1593 */
1594void proxy_destroy_defaults(struct proxy *px)
1595{
1596 if (!px)
1597 return;
1598 if (!(px->cap & PR_CAP_DEF))
1599 return;
Christopher Faulet27c8d202021-10-13 09:50:53 +02001600 BUG_ON(px->conf.refcount != 0);
Willy Tarreau0a0f6a72021-02-12 13:52:11 +01001601 ebpt_delete(&px->conf.by_name);
1602 proxy_free_defaults(px);
1603 free(px);
1604}
1605
Christopher Faulet27c8d202021-10-13 09:50:53 +02001606/* delete all unreferenced default proxies. A default proxy is unreferenced if
1607 * its refcount is equal to zero.
1608 */
1609void proxy_destroy_all_unref_defaults()
Willy Tarreaue90904d2021-02-12 14:08:31 +01001610{
1611 struct ebpt_node *n;
1612
Christopher Faulet27c8d202021-10-13 09:50:53 +02001613 n = ebpt_first(&defproxy_by_name);
1614 while (n) {
Willy Tarreaue90904d2021-02-12 14:08:31 +01001615 struct proxy *px = container_of(n, struct proxy, conf.by_name);
1616 BUG_ON(!(px->cap & PR_CAP_DEF));
Christopher Faulet27c8d202021-10-13 09:50:53 +02001617 n = ebpt_next(n);
1618 if (!px->conf.refcount)
1619 proxy_destroy_defaults(px);
Willy Tarreaue90904d2021-02-12 14:08:31 +01001620 }
1621}
1622
Christopher Faulet27c8d202021-10-13 09:50:53 +02001623/* Add a reference on the default proxy <defpx> for the proxy <px> Nothing is
1624 * done if <px> already references <defpx>. Otherwise, the default proxy
1625 * refcount is incremented by one. For now, this operation is not thread safe
1626 * and is perform during init stage only.
1627 */
1628void proxy_ref_defaults(struct proxy *px, struct proxy *defpx)
1629{
1630 if (px->defpx == defpx)
1631 return;
1632 BUG_ON(px->defpx != NULL);
1633 px->defpx = defpx;
1634 defpx->conf.refcount++;
1635}
1636
1637/* proxy <px> removes its reference on its default proxy. The default proxy
1638 * refcount is decremented by one. If it was the last reference, the
1639 * corresponding default proxy is destroyed. For now this operation is not
1640 * thread safe and is performed during deinit staged only.
1641*/
1642void proxy_unref_defaults(struct proxy *px)
1643{
1644 if (px->defpx == NULL)
1645 return;
1646 if (!--px->defpx->conf.refcount)
1647 proxy_destroy_defaults(px->defpx);
1648 px->defpx = NULL;
1649}
1650
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001651/* Allocates a new proxy <name> of type <cap>.
1652 * Returns the proxy instance on success. On error, NULL is returned.
Willy Tarreau76838932021-02-12 08:49:47 +01001653 */
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001654struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg)
Willy Tarreau76838932021-02-12 08:49:47 +01001655{
Willy Tarreau76838932021-02-12 08:49:47 +01001656 struct proxy *curproxy;
Willy Tarreau76838932021-02-12 08:49:47 +01001657
1658 if ((curproxy = calloc(1, sizeof(*curproxy))) == NULL) {
1659 memprintf(errmsg, "proxy '%s': out of memory", name);
1660 goto fail;
1661 }
1662
1663 init_new_proxy(curproxy);
Willy Tarreau69530f52023-04-28 09:16:15 +02001664 curproxy->last_change = ns_to_sec(now_ns);
Willy Tarreau76838932021-02-12 08:49:47 +01001665 curproxy->id = strdup(name);
1666 curproxy->cap = cap;
Amaury Denoyelle6f26fae2021-03-24 10:49:34 +01001667
William Lallemand6bb77b92021-07-28 15:48:16 +02001668 if (!(cap & PR_CAP_INT))
Amaury Denoyelle6f26fae2021-03-24 10:49:34 +01001669 proxy_store_name(curproxy);
Willy Tarreau76838932021-02-12 08:49:47 +01001670
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001671 done:
1672 return curproxy;
1673
1674 fail:
1675 /* Note: in case of fatal error here, we WILL make valgrind unhappy,
1676 * but its not worth trying to unroll everything here just before
1677 * quitting.
1678 */
1679 free(curproxy);
1680 return NULL;
1681}
1682
1683/* Copy the proxy settings from <defproxy> to <curproxy>.
1684 * Returns 0 on success.
1685 * Returns 1 on error. <errmsg> will be allocated with an error description.
1686 */
1687static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defproxy,
1688 char **errmsg)
1689{
1690 struct logsrv *tmplogsrv;
1691 char *tmpmsg = NULL;
Willy Tarreau3b06eae2021-02-12 09:15:16 +01001692
1693 /* set default values from the specified default proxy */
Christopher Faulet6bb86532022-08-03 11:31:55 +02001694 srv_settings_cpy(&curproxy->defsrv, &defproxy->defsrv, 0);
Willy Tarreau76838932021-02-12 08:49:47 +01001695
Christopher Faulet56717802021-10-13 10:10:09 +02001696 curproxy->flags = (defproxy->flags & PR_FL_DISABLED); /* Only inherit from disabled flag */
Willy Tarreau76838932021-02-12 08:49:47 +01001697 curproxy->options = defproxy->options;
1698 curproxy->options2 = defproxy->options2;
1699 curproxy->no_options = defproxy->no_options;
1700 curproxy->no_options2 = defproxy->no_options2;
Willy Tarreau76838932021-02-12 08:49:47 +01001701 curproxy->retry_type = defproxy->retry_type;
Christopher Fauletee08d6c2021-10-13 15:40:15 +02001702 curproxy->tcp_req.inspect_delay = defproxy->tcp_req.inspect_delay;
1703 curproxy->tcp_rep.inspect_delay = defproxy->tcp_rep.inspect_delay;
Willy Tarreau76838932021-02-12 08:49:47 +01001704
Aurelien DARRAGONb2e2ec52023-01-09 11:09:03 +01001705 http_ext_clean(curproxy);
1706 http_ext_dup(defproxy, curproxy);
Willy Tarreau76838932021-02-12 08:49:47 +01001707
Tim Duesterhusb4b03772022-03-05 00:52:43 +01001708 if (isttest(defproxy->server_id_hdr_name))
1709 curproxy->server_id_hdr_name = istdup(defproxy->server_id_hdr_name);
Willy Tarreau76838932021-02-12 08:49:47 +01001710
1711 /* initialize error relocations */
1712 if (!proxy_dup_default_conf_errors(curproxy, defproxy, &tmpmsg)) {
1713 memprintf(errmsg, "proxy '%s' : %s", curproxy->id, tmpmsg);
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001714 free(tmpmsg);
1715 return 1;
Willy Tarreau76838932021-02-12 08:49:47 +01001716 }
1717
1718 if (curproxy->cap & PR_CAP_FE) {
1719 curproxy->maxconn = defproxy->maxconn;
1720 curproxy->backlog = defproxy->backlog;
1721 curproxy->fe_sps_lim = defproxy->fe_sps_lim;
1722
1723 curproxy->to_log = defproxy->to_log & ~LW_COOKIE & ~LW_REQHDR & ~ LW_RSPHDR;
1724 curproxy->max_out_conns = defproxy->max_out_conns;
1725
1726 curproxy->clitcpka_cnt = defproxy->clitcpka_cnt;
1727 curproxy->clitcpka_idle = defproxy->clitcpka_idle;
1728 curproxy->clitcpka_intvl = defproxy->clitcpka_intvl;
1729 }
1730
1731 if (curproxy->cap & PR_CAP_BE) {
1732 curproxy->lbprm.algo = defproxy->lbprm.algo;
1733 curproxy->lbprm.hash_balance_factor = defproxy->lbprm.hash_balance_factor;
1734 curproxy->fullconn = defproxy->fullconn;
1735 curproxy->conn_retries = defproxy->conn_retries;
1736 curproxy->redispatch_after = defproxy->redispatch_after;
1737 curproxy->max_ka_queue = defproxy->max_ka_queue;
1738
1739 curproxy->tcpcheck_rules.flags = (defproxy->tcpcheck_rules.flags & ~TCPCHK_RULES_UNUSED_RS);
1740 curproxy->tcpcheck_rules.list = defproxy->tcpcheck_rules.list;
1741 if (!LIST_ISEMPTY(&defproxy->tcpcheck_rules.preset_vars)) {
1742 if (!dup_tcpcheck_vars(&curproxy->tcpcheck_rules.preset_vars,
1743 &defproxy->tcpcheck_rules.preset_vars)) {
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001744 memprintf(errmsg, "proxy '%s': failed to duplicate tcpcheck preset-vars", curproxy->id);
1745 return 1;
Willy Tarreau76838932021-02-12 08:49:47 +01001746 }
1747 }
1748
1749 curproxy->ck_opts = defproxy->ck_opts;
Aurelien DARRAGONb2bb9252022-12-28 15:37:57 +01001750
Willy Tarreau76838932021-02-12 08:49:47 +01001751 if (defproxy->cookie_name)
1752 curproxy->cookie_name = strdup(defproxy->cookie_name);
1753 curproxy->cookie_len = defproxy->cookie_len;
1754
1755 if (defproxy->dyncookie_key)
1756 curproxy->dyncookie_key = strdup(defproxy->dyncookie_key);
1757 if (defproxy->cookie_domain)
1758 curproxy->cookie_domain = strdup(defproxy->cookie_domain);
1759
1760 if (defproxy->cookie_maxidle)
1761 curproxy->cookie_maxidle = defproxy->cookie_maxidle;
1762
1763 if (defproxy->cookie_maxlife)
1764 curproxy->cookie_maxlife = defproxy->cookie_maxlife;
1765
1766 if (defproxy->rdp_cookie_name)
1767 curproxy->rdp_cookie_name = strdup(defproxy->rdp_cookie_name);
1768 curproxy->rdp_cookie_len = defproxy->rdp_cookie_len;
1769
1770 if (defproxy->cookie_attrs)
1771 curproxy->cookie_attrs = strdup(defproxy->cookie_attrs);
1772
1773 if (defproxy->lbprm.arg_str)
1774 curproxy->lbprm.arg_str = strdup(defproxy->lbprm.arg_str);
1775 curproxy->lbprm.arg_len = defproxy->lbprm.arg_len;
1776 curproxy->lbprm.arg_opt1 = defproxy->lbprm.arg_opt1;
1777 curproxy->lbprm.arg_opt2 = defproxy->lbprm.arg_opt2;
1778 curproxy->lbprm.arg_opt3 = defproxy->lbprm.arg_opt3;
vend_natalie.chenb749f592019-12-09 14:43:49 +08001779 curproxy->lbprm.arg_opt4 = defproxy->lbprm.arg_opt4;
Willy Tarreau76838932021-02-12 08:49:47 +01001780
1781 if (defproxy->conn_src.iface_name)
1782 curproxy->conn_src.iface_name = strdup(defproxy->conn_src.iface_name);
1783 curproxy->conn_src.iface_len = defproxy->conn_src.iface_len;
1784 curproxy->conn_src.opts = defproxy->conn_src.opts;
1785#if defined(CONFIG_HAP_TRANSPARENT)
1786 curproxy->conn_src.tproxy_addr = defproxy->conn_src.tproxy_addr;
1787#endif
1788 curproxy->load_server_state_from_file = defproxy->load_server_state_from_file;
1789
1790 curproxy->srvtcpka_cnt = defproxy->srvtcpka_cnt;
1791 curproxy->srvtcpka_idle = defproxy->srvtcpka_idle;
1792 curproxy->srvtcpka_intvl = defproxy->srvtcpka_intvl;
1793 }
1794
1795 if (curproxy->cap & PR_CAP_FE) {
1796 if (defproxy->capture_name)
1797 curproxy->capture_name = strdup(defproxy->capture_name);
1798 curproxy->capture_namelen = defproxy->capture_namelen;
1799 curproxy->capture_len = defproxy->capture_len;
Christopher Faulet57962282022-04-25 14:30:58 +02001800
1801 curproxy->nb_req_cap = defproxy->nb_req_cap;
1802 curproxy->req_cap = defproxy->req_cap;
1803
1804 curproxy->nb_rsp_cap = defproxy->nb_rsp_cap;
1805 curproxy->rsp_cap = defproxy->rsp_cap;
Willy Tarreau76838932021-02-12 08:49:47 +01001806 }
1807
1808 if (curproxy->cap & PR_CAP_FE) {
1809 curproxy->timeout.client = defproxy->timeout.client;
1810 curproxy->timeout.clientfin = defproxy->timeout.clientfin;
1811 curproxy->timeout.tarpit = defproxy->timeout.tarpit;
1812 curproxy->timeout.httpreq = defproxy->timeout.httpreq;
1813 curproxy->timeout.httpka = defproxy->timeout.httpka;
Tim Duesterhus4b1fcaa2022-03-05 00:52:40 +01001814 if (isttest(defproxy->monitor_uri))
1815 curproxy->monitor_uri = istdup(defproxy->monitor_uri);
Willy Tarreau76838932021-02-12 08:49:47 +01001816 if (defproxy->defbe.name)
1817 curproxy->defbe.name = strdup(defproxy->defbe.name);
1818
1819 /* get either a pointer to the logformat string or a copy of it */
1820 curproxy->conf.logformat_string = defproxy->conf.logformat_string;
1821 if (curproxy->conf.logformat_string &&
1822 curproxy->conf.logformat_string != default_http_log_format &&
1823 curproxy->conf.logformat_string != default_tcp_log_format &&
Remi Tricot-Le Breton98b930d2021-07-29 09:45:52 +02001824 curproxy->conf.logformat_string != clf_http_log_format &&
1825 curproxy->conf.logformat_string != default_https_log_format)
Willy Tarreau76838932021-02-12 08:49:47 +01001826 curproxy->conf.logformat_string = strdup(curproxy->conf.logformat_string);
1827
1828 if (defproxy->conf.lfs_file) {
1829 curproxy->conf.lfs_file = strdup(defproxy->conf.lfs_file);
1830 curproxy->conf.lfs_line = defproxy->conf.lfs_line;
1831 }
1832
1833 /* get either a pointer to the logformat string for RFC5424 structured-data or a copy of it */
1834 curproxy->conf.logformat_sd_string = defproxy->conf.logformat_sd_string;
1835 if (curproxy->conf.logformat_sd_string &&
1836 curproxy->conf.logformat_sd_string != default_rfc5424_sd_log_format)
1837 curproxy->conf.logformat_sd_string = strdup(curproxy->conf.logformat_sd_string);
1838
1839 if (defproxy->conf.lfsd_file) {
1840 curproxy->conf.lfsd_file = strdup(defproxy->conf.lfsd_file);
1841 curproxy->conf.lfsd_line = defproxy->conf.lfsd_line;
1842 }
Remi Tricot-Le Bretonfe21fe72021-08-31 12:08:52 +02001843
1844 curproxy->conf.error_logformat_string = defproxy->conf.error_logformat_string;
1845 if (curproxy->conf.error_logformat_string)
1846 curproxy->conf.error_logformat_string = strdup(curproxy->conf.error_logformat_string);
1847
1848 if (defproxy->conf.elfs_file) {
1849 curproxy->conf.elfs_file = strdup(defproxy->conf.elfs_file);
1850 curproxy->conf.elfs_line = defproxy->conf.elfs_line;
1851 }
Willy Tarreau76838932021-02-12 08:49:47 +01001852 }
1853
1854 if (curproxy->cap & PR_CAP_BE) {
1855 curproxy->timeout.connect = defproxy->timeout.connect;
1856 curproxy->timeout.server = defproxy->timeout.server;
1857 curproxy->timeout.serverfin = defproxy->timeout.serverfin;
1858 curproxy->timeout.check = defproxy->timeout.check;
1859 curproxy->timeout.queue = defproxy->timeout.queue;
1860 curproxy->timeout.tarpit = defproxy->timeout.tarpit;
1861 curproxy->timeout.httpreq = defproxy->timeout.httpreq;
1862 curproxy->timeout.httpka = defproxy->timeout.httpka;
1863 curproxy->timeout.tunnel = defproxy->timeout.tunnel;
1864 curproxy->conn_src.source_addr = defproxy->conn_src.source_addr;
1865 }
1866
1867 curproxy->mode = defproxy->mode;
1868 curproxy->uri_auth = defproxy->uri_auth; /* for stats */
1869
1870 /* copy default logsrvs to curproxy */
1871 list_for_each_entry(tmplogsrv, &defproxy->logsrvs, list) {
Aurelien DARRAGON77679312023-07-05 15:52:19 +02001872 struct logsrv *node = dup_logsrv(tmplogsrv);
Willy Tarreau76838932021-02-12 08:49:47 +01001873
1874 if (!node) {
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001875 memprintf(errmsg, "proxy '%s': out of memory", curproxy->id);
1876 return 1;
Willy Tarreau76838932021-02-12 08:49:47 +01001877 }
Willy Tarreau2b718102021-04-21 07:32:39 +02001878 LIST_APPEND(&curproxy->logsrvs, &node->list);
Willy Tarreau76838932021-02-12 08:49:47 +01001879 }
1880
1881 curproxy->conf.uniqueid_format_string = defproxy->conf.uniqueid_format_string;
1882 if (curproxy->conf.uniqueid_format_string)
1883 curproxy->conf.uniqueid_format_string = strdup(curproxy->conf.uniqueid_format_string);
1884
1885 chunk_dup(&curproxy->log_tag, &defproxy->log_tag);
1886
1887 if (defproxy->conf.uif_file) {
1888 curproxy->conf.uif_file = strdup(defproxy->conf.uif_file);
1889 curproxy->conf.uif_line = defproxy->conf.uif_line;
1890 }
1891
1892 /* copy default header unique id */
1893 if (isttest(defproxy->header_unique_id)) {
1894 const struct ist copy = istdup(defproxy->header_unique_id);
1895
1896 if (!isttest(copy)) {
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001897 memprintf(errmsg, "proxy '%s': out of memory for unique-id-header", curproxy->id);
1898 return 1;
Willy Tarreau76838932021-02-12 08:49:47 +01001899 }
1900 curproxy->header_unique_id = copy;
1901 }
1902
1903 /* default compression options */
1904 if (defproxy->comp != NULL) {
1905 curproxy->comp = calloc(1, sizeof(*curproxy->comp));
Remi Tricot-Le Breton18a82ba2021-05-12 18:07:27 +02001906 if (!curproxy->comp) {
1907 memprintf(errmsg, "proxy '%s': out of memory for default compression options", curproxy->id);
1908 return 1;
1909 }
Olivier Houcharddb573e92023-04-05 17:32:36 +02001910 curproxy->comp->algos_res = defproxy->comp->algos_res;
1911 curproxy->comp->algo_req = defproxy->comp->algo_req;
1912 curproxy->comp->types_res = defproxy->comp->types_res;
1913 curproxy->comp->types_req = defproxy->comp->types_req;
Willy Tarreau862588a2023-05-10 16:39:00 +02001914 curproxy->comp->flags = defproxy->comp->flags;
Willy Tarreau76838932021-02-12 08:49:47 +01001915 }
1916
Willy Tarreau76838932021-02-12 08:49:47 +01001917 if (defproxy->check_path)
1918 curproxy->check_path = strdup(defproxy->check_path);
1919 if (defproxy->check_command)
1920 curproxy->check_command = strdup(defproxy->check_command);
1921
1922 if (defproxy->email_alert.mailers.name)
1923 curproxy->email_alert.mailers.name = strdup(defproxy->email_alert.mailers.name);
1924 if (defproxy->email_alert.from)
1925 curproxy->email_alert.from = strdup(defproxy->email_alert.from);
1926 if (defproxy->email_alert.to)
1927 curproxy->email_alert.to = strdup(defproxy->email_alert.to);
1928 if (defproxy->email_alert.myhostname)
1929 curproxy->email_alert.myhostname = strdup(defproxy->email_alert.myhostname);
1930 curproxy->email_alert.level = defproxy->email_alert.level;
1931 curproxy->email_alert.set = defproxy->email_alert.set;
Willy Tarreau3b06eae2021-02-12 09:15:16 +01001932
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001933 return 0;
1934}
1935
1936/* Allocates a new proxy <name> of type <cap> found at position <file:linenum>,
1937 * preset it from the defaults of <defproxy> and returns it. In case of error,
1938 * an alert is printed and NULL is returned.
1939 */
1940struct proxy *parse_new_proxy(const char *name, unsigned int cap,
1941 const char *file, int linenum,
1942 const struct proxy *defproxy)
1943{
1944 struct proxy *curproxy = NULL;
Willy Tarreau282b6a72022-02-24 16:37:19 +01001945 char *errmsg = NULL;
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001946
1947 if (!(curproxy = alloc_new_proxy(name, cap, &errmsg))) {
1948 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
1949 free(errmsg);
1950 return NULL;
1951 }
1952
1953 if (defproxy) {
1954 if (proxy_defproxy_cpy(curproxy, defproxy, &errmsg)) {
1955 ha_alert("parsing [%s:%d] : %s\n", file, linenum, errmsg);
1956 free(errmsg);
1957
1958 ha_free(&curproxy);
1959 return NULL;
1960 }
1961 }
Amaury Denoyelle476b9ad2021-03-23 17:27:05 +01001962
1963 curproxy->conf.args.file = curproxy->conf.file = strdup(file);
1964 curproxy->conf.args.line = curproxy->conf.line = linenum;
1965
Willy Tarreau76838932021-02-12 08:49:47 +01001966 return curproxy;
Willy Tarreau76838932021-02-12 08:49:47 +01001967}
1968
Aurelien DARRAGONd46f4372022-09-09 15:51:37 +02001969/* to be called under the proxy lock after pausing some listeners. This will
1970 * automatically update the p->flags flag
1971 */
1972void proxy_cond_pause(struct proxy *p)
1973{
1974 if (p->li_ready)
1975 return;
1976 p->flags |= PR_FL_PAUSED;
1977}
1978
1979/* to be called under the proxy lock after resuming some listeners. This will
1980 * automatically update the p->flags flag
1981 */
1982void proxy_cond_resume(struct proxy *p)
1983{
1984 if (!p->li_ready)
1985 return;
1986 p->flags &= ~PR_FL_PAUSED;
1987}
1988
Willy Tarreauacde1522020-10-07 16:31:39 +02001989/* to be called under the proxy lock after stopping some listeners. This will
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001990 * automatically update the p->flags flag after stopping the last one, and
Willy Tarreauacde1522020-10-07 16:31:39 +02001991 * will emit a log indicating the proxy's condition. The function is idempotent
1992 * so that it will not emit multiple logs; a proxy will be disabled only once.
1993 */
1994void proxy_cond_disable(struct proxy *p)
1995{
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001996 if (p->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Willy Tarreauacde1522020-10-07 16:31:39 +02001997 return;
1998
1999 if (p->li_ready + p->li_paused > 0)
2000 return;
2001
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002002 p->flags |= PR_FL_STOPPED;
Willy Tarreauacde1522020-10-07 16:31:39 +02002003
Willy Tarreauacde1522020-10-07 16:31:39 +02002004 /* Note: syslog proxies use their own loggers so while it's somewhat OK
2005 * to report them being stopped as a warning, we must not spam their log
2006 * servers which are in fact production servers. For other types (CLI,
2007 * peers, etc) we must not report them at all as they're not really on
2008 * the data plane but on the control plane.
2009 */
William Lallemand7f954692023-05-14 23:23:36 +02002010 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP || p->mode == PR_MODE_SYSLOG) && !(p->cap & PR_CAP_INT))
Willy Tarreauacde1522020-10-07 16:31:39 +02002011 ha_warning("Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
Amaury Denoyelle1c8372b2024-04-04 18:08:46 +02002012 p->id, p->fe_counters.cum_conn, p->be_counters.cum_sess);
Willy Tarreauacde1522020-10-07 16:31:39 +02002013
William Lallemand7f954692023-05-14 23:23:36 +02002014 if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) && !(p->cap & PR_CAP_INT))
Willy Tarreauacde1522020-10-07 16:31:39 +02002015 send_log(p, LOG_WARNING, "Proxy %s stopped (cumulated conns: FE: %lld, BE: %lld).\n",
Amaury Denoyelle1c8372b2024-04-04 18:08:46 +02002016 p->id, p->fe_counters.cum_conn, p->be_counters.cum_sess);
Willy Tarreauacde1522020-10-07 16:31:39 +02002017
Willy Tarreauacde1522020-10-07 16:31:39 +02002018 if (p->table && p->table->size && p->table->sync_task)
2019 task_wakeup(p->table->sync_task, TASK_WOKEN_MSG);
2020
2021 if (p->task)
2022 task_wakeup(p->task, TASK_WOKEN_MSG);
2023}
2024
Krzysztof Piotr Oledzkic8b16fc2008-02-18 01:26:35 +01002025/*
Willy Tarreau918ff602011-07-25 16:33:49 +02002026 * This is the proxy management task. It enables proxies when there are enough
Willy Tarreau87b09662015-04-03 00:22:06 +02002027 * free streams, or stops them when the table is full. It is designed to be
Willy Tarreau918ff602011-07-25 16:33:49 +02002028 * called as a task which is woken up upon stopping or when rate limiting must
2029 * be enforced.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002030 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01002031struct task *manage_proxy(struct task *t, void *context, unsigned int state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002032{
Olivier Houchard9f6af332018-05-25 14:04:04 +02002033 struct proxy *p = context;
Willy Tarreau918ff602011-07-25 16:33:49 +02002034 int next = TICK_ETERNITY;
Willy Tarreau79584222009-03-06 09:18:27 +01002035 unsigned int wait;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002036
Willy Tarreau918ff602011-07-25 16:33:49 +02002037 /* We should periodically try to enable listeners waiting for a
2038 * global resource here.
2039 */
Willy Tarreaubaaee002006-06-26 02:48:02 +02002040
Willy Tarreau3a925c12013-09-04 17:54:01 +02002041 /* If the proxy holds a stick table, we need to purge all unused
2042 * entries. These are all the ones in the table with ref_cnt == 0
2043 * and all the ones in the pool used to allocate new entries. Any
Willy Tarreau87b09662015-04-03 00:22:06 +02002044 * entry attached to an existing stream waiting for a store will
Willy Tarreau3a925c12013-09-04 17:54:01 +02002045 * be in neither list. Any entry being dumped will have ref_cnt > 0.
2046 * However we protect tables that are being synced to peers.
2047 */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002048 if (unlikely(stopping && (p->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) && p->table && p->table->current)) {
Emeric Brun2cc201f2021-04-23 12:21:26 +02002049
2050 if (!p->table->refcnt) {
2051 /* !table->refcnt means there
2052 * is no more pending full resync
2053 * to push to a new process and
2054 * we are free to flush the table.
2055 */
Aurelien DARRAGON7f01f0a2023-03-29 16:18:50 +02002056 int budget;
2057 int cleaned_up;
2058
2059 /* We purposely enforce a budget limitation since we don't want
2060 * to spend too much time purging old entries
2061 *
2062 * This is known to cause the watchdog to occasionnaly trigger if
2063 * the table is huge and all entries become available for purge
2064 * at the same time
2065 *
2066 * Moreover, we must also anticipate the pool_gc() call which
2067 * will also be much slower if there is too much work at once
2068 */
2069 budget = MIN(p->table->current, (1 << 15)); /* max: 32K */
2070 cleaned_up = stktable_trash_oldest(p->table, budget);
2071 if (cleaned_up) {
2072 /* immediately release freed memory since we are stopping */
Aurelien DARRAGON6e1fe252023-03-28 15:14:48 +02002073 pool_gc(NULL);
Aurelien DARRAGON7f01f0a2023-03-29 16:18:50 +02002074 if (cleaned_up > (budget / 2)) {
2075 /* most of the budget was used to purge entries,
2076 * it is very likely that there are still trashable
2077 * entries in the table, reschedule a new cleanup
2078 * attempt ASAP
2079 */
2080 t->expire = TICK_ETERNITY;
2081 task_wakeup(t, TASK_WOKEN_RES);
2082 return t;
2083 }
2084 }
Willy Tarreau3a925c12013-09-04 17:54:01 +02002085 }
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +01002086 if (p->table->current) {
Aurelien DARRAGON7f01f0a2023-03-29 16:18:50 +02002087 /* some entries still remain but are not yet available
2088 * for cleanup, let's recheck in one second
2089 */
Willy Tarreau3a925c12013-09-04 17:54:01 +02002090 next = tick_first(next, tick_add(now_ms, 1000));
2091 }
2092 }
2093
Willy Tarreau918ff602011-07-25 16:33:49 +02002094 /* the rest below is just for frontends */
2095 if (!(p->cap & PR_CAP_FE))
2096 goto out;
Willy Tarreauf3f8c702011-07-25 07:37:28 +02002097
Willy Tarreau918ff602011-07-25 16:33:49 +02002098 /* check the various reasons we may find to block the frontend */
Willy Tarreau6b3bf732020-09-24 07:35:46 +02002099 if (unlikely(p->feconn >= p->maxconn))
Willy Tarreau918ff602011-07-25 16:33:49 +02002100 goto out;
Willy Tarreau3a7d2072009-03-05 23:48:25 +01002101
Willy Tarreau918ff602011-07-25 16:33:49 +02002102 if (p->fe_sps_lim &&
2103 (wait = next_event_delay(&p->fe_sess_per_sec, p->fe_sps_lim, 0))) {
2104 /* we're blocking because a limit was reached on the number of
2105 * requests/s on the frontend. We want to re-check ASAP, which
2106 * means in 1 ms before estimated expiration date, because the
2107 * timer will have settled down.
2108 */
2109 next = tick_first(next, tick_add(now_ms, wait));
2110 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002111 }
Willy Tarreau918ff602011-07-25 16:33:49 +02002112
2113 /* The proxy is not limited so we can re-enable any waiting listener */
Willy Tarreau241797a2019-12-10 14:10:52 +01002114 dequeue_proxy_listeners(p);
Willy Tarreau918ff602011-07-25 16:33:49 +02002115 out:
2116 t->expire = next;
2117 task_queue(t);
2118 return t;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002119}
2120
2121
Willy Tarreau10080712021-09-07 10:49:45 +02002122static int proxy_parse_grace(char **args, int section_type, struct proxy *curpx,
2123 const struct proxy *defpx, const char *file, int line,
2124 char **err)
2125{
2126 const char *res;
2127
2128 if (!*args[1]) {
2129 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
2130 return -1;
2131 }
2132 res = parse_time_err(args[1], &global.grace_delay, TIME_UNIT_MS);
2133 if (res == PARSE_TIME_OVER) {
2134 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
2135 args[1], args[0]);
2136 return -1;
2137 }
2138 else if (res == PARSE_TIME_UNDER) {
2139 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
2140 args[1], args[0]);
2141 return -1;
2142 }
2143 else if (res) {
2144 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
2145 return -1;
2146 }
2147 return 0;
2148}
2149
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002150static int proxy_parse_hard_stop_after(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01002151 const struct proxy *defpx, const char *file, int line,
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002152 char **err)
2153{
2154 const char *res;
2155
2156 if (!*args[1]) {
2157 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
2158 return -1;
2159 }
2160 res = parse_time_err(args[1], &global.hard_stop_after, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002161 if (res == PARSE_TIME_OVER) {
2162 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
2163 args[1], args[0]);
2164 return -1;
2165 }
2166 else if (res == PARSE_TIME_UNDER) {
2167 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
2168 args[1], args[0]);
2169 return -1;
2170 }
2171 else if (res) {
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002172 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
2173 return -1;
2174 }
2175 return 0;
2176}
2177
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02002178static int proxy_parse_close_spread_time(char **args, int section_type, struct proxy *curpx,
2179 const struct proxy *defpx, const char *file, int line,
2180 char **err)
2181{
2182 const char *res;
2183
2184 if (!*args[1]) {
2185 memprintf(err, "'%s' expects <time> as argument.\n", args[0]);
2186 return -1;
2187 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02002188
2189 /* If close-spread-time is set to "infinite", disable the active connection
2190 * closing during soft-stop.
2191 */
2192 if (strcmp(args[1], "infinite") == 0) {
2193 global.tune.options |= GTUNE_DISABLE_ACTIVE_CLOSE;
2194 global.close_spread_time = TICK_ETERNITY;
2195 return 0;
2196 }
2197
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02002198 res = parse_time_err(args[1], &global.close_spread_time, TIME_UNIT_MS);
2199 if (res == PARSE_TIME_OVER) {
2200 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
2201 args[1], args[0]);
2202 return -1;
2203 }
2204 else if (res == PARSE_TIME_UNDER) {
2205 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
2206 args[1], args[0]);
2207 return -1;
2208 }
2209 else if (res) {
2210 memprintf(err, "unexpected character '%c' in argument to <%s>.\n", *res, args[0]);
2211 return -1;
2212 }
Remi Tricot-Le Breton4d7fdc62022-04-26 15:17:18 +02002213 global.tune.options &= ~GTUNE_DISABLE_ACTIVE_CLOSE;
2214
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02002215 return 0;
2216}
2217
Willy Tarreau144f84a2021-03-02 16:09:26 +01002218struct task *hard_stop(struct task *t, void *context, unsigned int state)
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002219{
2220 struct proxy *p;
2221 struct stream *s;
Willy Tarreau0d038252021-02-24 11:13:59 +01002222 int thr;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002223
2224 if (killed) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002225 ha_warning("Some tasks resisted to hard-stop, exiting now.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002226 send_log(NULL, LOG_WARNING, "Some tasks resisted to hard-stop, exiting now.\n");
Willy Tarreau7067b3a2019-06-02 11:11:29 +02002227 killed = 2;
Willy Tarreau0d038252021-02-24 11:13:59 +01002228 for (thr = 0; thr < global.nbthread; thr++)
Willy Tarreaub2f38c12023-01-19 19:14:18 +01002229 if (_HA_ATOMIC_LOAD(&ha_thread_info[thr].tg->threads_enabled) & ha_thread_info[thr].ltid_bit)
Willy Tarreau0d038252021-02-24 11:13:59 +01002230 wake_thread(thr);
Willy Tarreau7067b3a2019-06-02 11:11:29 +02002231 t->expire = TICK_ETERNITY;
2232 return t;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002233 }
2234
Christopher Faulet767a84b2017-11-24 16:50:31 +01002235 ha_warning("soft-stop running for too long, performing a hard-stop.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002236 send_log(NULL, LOG_WARNING, "soft-stop running for too long, performing a hard-stop.\n");
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002237 p = proxies_list;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002238 while (p) {
2239 if ((p->cap & PR_CAP_FE) && (p->feconn > 0)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002240 ha_warning("Proxy %s hard-stopped (%d remaining conns will be closed).\n",
2241 p->id, p->feconn);
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002242 send_log(p, LOG_WARNING, "Proxy %s hard-stopped (%d remaining conns will be closed).\n",
2243 p->id, p->feconn);
2244 }
2245 p = p->next;
2246 }
Willy Tarreau92b887e2021-02-24 11:08:56 +01002247
2248 thread_isolate();
Willy Tarreaua698eb62021-02-24 10:37:01 +01002249
2250 for (thr = 0; thr < global.nbthread; thr++) {
Willy Tarreaub4e34762021-09-30 19:02:18 +02002251 list_for_each_entry(s, &ha_thread_ctx[thr].streams, list) {
Willy Tarreaua698eb62021-02-24 10:37:01 +01002252 stream_shutdown(s, SF_ERR_KILLED);
2253 }
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002254 }
Willy Tarreaua698eb62021-02-24 10:37:01 +01002255
Willy Tarreau92b887e2021-02-24 11:08:56 +01002256 thread_release();
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002257
2258 killed = 1;
2259 t->expire = tick_add(now_ms, MS_TO_TICKS(1000));
2260 return t;
2261}
2262
Willy Tarreau10080712021-09-07 10:49:45 +02002263/* perform the soft-stop right now (i.e. unbind listeners) */
2264static void do_soft_stop_now()
Willy Tarreaubaaee002006-06-26 02:48:02 +02002265{
Christopher Faulet48678e42023-03-14 14:33:11 +01002266 struct proxy *p;
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002267 struct task *task;
Willy Tarreaubaaee002006-06-26 02:48:02 +02002268
William Dauchy3894d972019-12-28 15:36:02 +01002269 /* disable busy polling to avoid cpu eating for the new process */
2270 global.tune.options &= ~GTUNE_BUSY_POLLING;
Willy Tarreau10080712021-09-07 10:49:45 +02002271
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02002272 if (tick_isset(global.close_spread_time)) {
2273 global.close_spread_end = tick_add(now_ms, global.close_spread_time);
2274 }
2275
Willy Tarreau10080712021-09-07 10:49:45 +02002276 /* schedule a hard-stop after a delay if needed */
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002277 if (tick_isset(global.hard_stop_after)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002278 task = task_new_anywhere();
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002279 if (task) {
2280 task->process = hard_stop;
2281 task_schedule(task, tick_add(now_ms, global.hard_stop_after));
2282 }
2283 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002284 ha_alert("out of memory trying to allocate the hard-stop task.\n");
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002285 }
2286 }
Willy Tarreau626f3a72020-10-07 16:52:43 +02002287
Willy Tarreau1b927eb2022-07-15 19:15:02 +02002288 /* we isolate so that we have a chance of stopping listeners in other groups */
2289 thread_isolate();
2290
Willy Tarreaueb778242021-06-11 16:27:10 +02002291 /* stop all stoppable listeners */
Willy Tarreau626f3a72020-10-07 16:52:43 +02002292 protocol_stop_now();
2293
Willy Tarreau1b927eb2022-07-15 19:15:02 +02002294 thread_release();
2295
Christopher Faulet48678e42023-03-14 14:33:11 +01002296 /* Loop on proxies to stop backends */
2297 p = proxies_list;
2298 while (p) {
2299 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
2300 proxy_cond_disable(p);
2301 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
2302 p = p->next;
2303 }
2304
Willy Tarreaud0807c32010-08-27 18:26:11 +02002305 /* signal zero is used to broadcast the "stopping" event */
2306 signal_handler(0);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002307}
2308
Willy Tarreau10080712021-09-07 10:49:45 +02002309/* triggered by a soft-stop delayed with `grace` */
2310static struct task *grace_expired(struct task *t, void *context, unsigned int state)
2311{
2312 ha_notice("Grace period expired, proceeding with soft-stop now.\n");
2313 send_log(NULL, LOG_NOTICE, "Grace period expired, proceeding with soft-stop now.\n");
2314 do_soft_stop_now();
2315 task_destroy(t);
2316 return NULL;
2317}
2318
2319/*
2320 * this function disables health-check servers so that the process will quickly be ignored
2321 * by load balancers.
2322 */
2323void soft_stop(void)
2324{
2325 struct task *task;
2326
2327 stopping = 1;
2328
2329 if (tick_isset(global.grace_delay)) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002330 task = task_new_anywhere();
Willy Tarreau10080712021-09-07 10:49:45 +02002331 if (task) {
2332 ha_notice("Scheduling a soft-stop in %u ms.\n", global.grace_delay);
2333 send_log(NULL, LOG_WARNING, "Scheduling a soft-stop in %u ms.\n", global.grace_delay);
2334 task->process = grace_expired;
2335 task_schedule(task, tick_add(now_ms, global.grace_delay));
2336 return;
2337 }
2338 else {
2339 ha_alert("out of memory trying to allocate the stop-stop task, stopping now.\n");
2340 }
2341 }
2342
2343 /* no grace (or failure to enforce it): stop now */
2344 do_soft_stop_now();
2345}
2346
Willy Tarreaubaaee002006-06-26 02:48:02 +02002347
Willy Tarreaube58c382011-07-24 18:28:10 +02002348/* Temporarily disables listening on all of the proxy's listeners. Upon
Willy Tarreaua17c91b2020-09-24 07:44:34 +02002349 * success, the proxy enters the PR_PAUSED state. The function returns 0
Willy Tarreauce8fe252011-09-07 19:14:57 +02002350 * if it fails, or non-zero on success.
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002351 * The function takes the proxy's lock so it's safe to
2352 * call from multiple places.
Willy Tarreaubaaee002006-06-26 02:48:02 +02002353 */
Willy Tarreauce8fe252011-09-07 19:14:57 +02002354int pause_proxy(struct proxy *p)
Willy Tarreaubaaee002006-06-26 02:48:02 +02002355{
2356 struct listener *l;
Willy Tarreauce8fe252011-09-07 19:14:57 +02002357
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002358 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
2359
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002360 if (!(p->cap & PR_CAP_FE) || (p->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) || !p->li_ready)
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002361 goto end;
Willy Tarreauce8fe252011-09-07 19:14:57 +02002362
Willy Tarreaua17c91b2020-09-24 07:44:34 +02002363 list_for_each_entry(l, &p->conf.listeners, by_fe)
Aurelien DARRAGONd3ffba42023-02-13 17:45:08 +01002364 suspend_listener(l, 1, 0);
Willy Tarreauce8fe252011-09-07 19:14:57 +02002365
Willy Tarreaua17c91b2020-09-24 07:44:34 +02002366 if (p->li_ready) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002367 ha_warning("%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Willy Tarreauce8fe252011-09-07 19:14:57 +02002368 send_log(p, LOG_WARNING, "%s %s failed to enter pause mode.\n", proxy_cap_str(p->cap), p->id);
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002369 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreauce8fe252011-09-07 19:14:57 +02002370 return 0;
2371 }
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002372end:
2373 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreauce8fe252011-09-07 19:14:57 +02002374 return 1;
Willy Tarreauda250db2008-10-12 12:07:48 +02002375}
2376
Willy Tarreauda250db2008-10-12 12:07:48 +02002377/*
2378 * This function completely stops a proxy and releases its listeners. It has
2379 * to be called when going down in order to release the ports so that another
2380 * process may bind to them. It must also be called on disabled proxies at the
William Lallemandc59f9882018-11-16 16:57:21 +01002381 * end of start-up. If all listeners are closed, the proxy is set to the
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002382 * PR_STOPPED state.
2383 * The function takes the proxy's lock so it's safe to
Willy Tarreau3de3cd42019-07-24 17:42:44 +02002384 * call from multiple places.
Willy Tarreauda250db2008-10-12 12:07:48 +02002385 */
2386void stop_proxy(struct proxy *p)
2387{
2388 struct listener *l;
2389
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002390 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau3de3cd42019-07-24 17:42:44 +02002391
Willy Tarreau322b9b92020-10-07 16:20:34 +02002392 list_for_each_entry(l, &p->conf.listeners, by_fe)
Aurelien DARRAGON4059e092023-02-06 17:06:03 +01002393 stop_listener(l, 1, 0, 0);
Willy Tarreau3de3cd42019-07-24 17:42:44 +02002394
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002395 if (!(p->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) && !p->li_ready) {
Willy Tarreauba296872020-10-16 15:10:11 +02002396 /* might be just a backend */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002397 p->flags |= PR_FL_STOPPED;
Willy Tarreauba296872020-10-16 15:10:11 +02002398 }
2399
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002400 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +02002401}
2402
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002403/* This function resumes listening on the specified proxy. It scans all of its
2404 * listeners and tries to enable them all. If any of them fails, the proxy is
2405 * put back to the paused state. It returns 1 upon success, or zero if an error
2406 * is encountered.
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002407 * The function takes the proxy's lock so it's safe to
2408 * call from multiple places.
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002409 */
2410int resume_proxy(struct proxy *p)
2411{
2412 struct listener *l;
2413 int fail;
2414
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002415 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
2416
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002417 if ((p->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) || !p->li_paused)
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002418 goto end;
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002419
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002420 fail = 0;
Willy Tarreau4348fad2012-09-20 16:48:07 +02002421 list_for_each_entry(l, &p->conf.listeners, by_fe) {
Aurelien DARRAGON4059e092023-02-06 17:06:03 +01002422 if (!resume_listener(l, 1, 0)) {
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002423 int port;
2424
Willy Tarreau37159062020-08-27 07:48:42 +02002425 port = get_host_port(&l->rx.addr);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002426 if (port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002427 ha_warning("Port %d busy while trying to enable %s %s.\n",
2428 port, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002429 send_log(p, LOG_WARNING, "Port %d busy while trying to enable %s %s.\n",
2430 port, proxy_cap_str(p->cap), p->id);
2431 }
2432 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002433 ha_warning("Bind on socket %d busy while trying to enable %s %s.\n",
2434 l->luid, proxy_cap_str(p->cap), p->id);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002435 send_log(p, LOG_WARNING, "Bind on socket %d busy while trying to enable %s %s.\n",
2436 l->luid, proxy_cap_str(p->cap), p->id);
2437 }
2438
2439 /* Another port might have been enabled. Let's stop everything. */
2440 fail = 1;
2441 break;
2442 }
2443 }
2444
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002445 if (fail) {
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002446 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
2447 /* pause_proxy will take PROXY_LOCK */
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002448 pause_proxy(p);
2449 return 0;
2450 }
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02002451end:
2452 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
Willy Tarreauc03ebbf2011-09-07 21:33:14 +02002453 return 1;
2454}
2455
Willy Tarreau87b09662015-04-03 00:22:06 +02002456/* Set current stream's backend to <be>. Nothing is done if the
2457 * stream already had a backend assigned, which is indicated by
Willy Tarreaue7dff022015-04-03 01:14:29 +02002458 * s->flags & SF_BE_ASSIGNED.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002459 * All flags, stats and counters which need be updated are updated.
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02002460 * Returns 1 if done, 0 in case of internal error, eg: lack of resource.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002461 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002462int stream_set_backend(struct stream *s, struct proxy *be)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002463{
Christopher Faulet6c1fd982021-03-15 10:42:02 +01002464 unsigned int req_ana;
2465
Willy Tarreaue7dff022015-04-03 01:14:29 +02002466 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02002467 return 1;
Christopher Faulet41179042016-06-21 11:54:52 +02002468
2469 if (flt_set_stream_backend(s, be) < 0)
2470 return 0;
2471
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002472 s->be = be;
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002473 HA_ATOMIC_UPDATE_MAX(&be->be_counters.conn_max,
Willy Tarreau1db42732021-04-06 11:44:07 +02002474 HA_ATOMIC_ADD_FETCH(&be->beconn, 1));
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002475 proxy_inc_be_ctr(be);
2476
Willy Tarreau87b09662015-04-03 00:22:06 +02002477 /* assign new parameters to the stream from the new backend */
Willy Tarreaucb041662022-05-17 19:44:42 +02002478 s->scb->flags &= ~SC_FL_INDEP_STR;
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02002479 if (be->options2 & PR_O2_INDEPSTR)
Willy Tarreaucb041662022-05-17 19:44:42 +02002480 s->scb->flags |= SC_FL_INDEP_STR;
Willy Tarreauf27b5ea2009-10-03 22:01:18 +02002481
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002482 /* We want to enable the backend-specific analysers except those which
2483 * were already run as part of the frontend/listener. Note that it would
2484 * be more reliable to store the list of analysers that have been run,
2485 * but what we do here is OK for now.
2486 */
Christopher Faulet6c1fd982021-03-15 10:42:02 +01002487 req_ana = be->be_req_ana;
2488 if (!(strm_fe(s)->options & PR_O_WREQ_BODY) && be->options & PR_O_WREQ_BODY) {
2489 /* The backend request to parse a request body while it was not
2490 * performed on the frontend, so add the corresponding analyser
2491 */
2492 req_ana |= AN_REQ_HTTP_BODY;
2493 }
2494 if (IS_HTX_STRM(s) && strm_fe(s)->mode != PR_MODE_HTTP) {
2495 /* The stream was already upgraded to HTTP, so remove analysers
2496 * set during the upgrade
2497 */
2498 req_ana &= ~(AN_REQ_WAIT_HTTP|AN_REQ_HTTP_PROCESS_FE);
2499 }
Willy Tarreau7866e8e2023-01-12 18:39:42 +01002500 s->req.analysers |= req_ana & ~(strm_li(s) ? strm_li(s)->bind_conf->analysers : 0);
Willy Tarreau9fbe18e2015-05-01 22:42:08 +02002501
Christopher Faulet6c1fd982021-03-15 10:42:02 +01002502 if (!IS_HTX_STRM(s) && be->mode == PR_MODE_HTTP) {
Christopher Fauletbbe68542019-04-08 10:53:51 +02002503 /* If we chain a TCP frontend to an HTX backend, we must upgrade
2504 * the client mux */
Christopher Fauletae863c62021-03-15 12:03:44 +01002505 if (!stream_set_http_mode(s, NULL))
Christopher Fauleteec7f8a2019-12-20 15:59:20 +01002506 return 0;
Christopher Faulet6c1fd982021-03-15 10:42:02 +01002507 }
2508 else if (IS_HTX_STRM(s) && be->mode != PR_MODE_HTTP) {
2509 /* If a TCP backend is assgiend to an HTX stream, return an
2510 * error. It may happens for a new stream on a previously
2511 * upgraded connections. */
2512 if (!(s->flags & SF_ERR_MASK))
2513 s->flags |= SF_ERR_INTERNAL;
2514 return 0;
2515 }
2516 else {
2517 /* If the target backend requires HTTP processing, we have to allocate
2518 * the HTTP transaction if we did not have one.
2519 */
2520 if (unlikely(!s->txn && be->http_needed && !http_create_txn(s)))
2521 return 0;
Willy Tarreaueee5b512015-04-03 23:46:31 +02002522 }
2523
2524 s->flags |= SF_BE_ASSIGNED;
Willy Tarreau96e31212011-05-30 18:10:30 +02002525 if (be->options2 & PR_O2_NODELAY) {
Christopher Faulet68ef2182023-03-17 15:38:18 +01002526 s->scf->flags |= SC_FL_SND_NEVERWAIT;
2527 s->scb->flags |= SC_FL_SND_NEVERWAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02002528 }
2529
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02002530 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002531}
2532
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002533/* Capture a bad request or response and archive it in the proxy's structure.
2534 * It is relatively protocol-agnostic so it requires that a number of elements
2535 * are passed :
2536 * - <proxy> is the proxy where the error was detected and where the snapshot
2537 * needs to be stored
Joseph Herlant59dd2952018-11-15 11:46:55 -08002538 * - <is_back> indicates that the error happened when receiving the response
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002539 * - <other_end> is a pointer to the proxy on the other side when known
2540 * - <target> is the target of the connection, usually a server or a proxy
2541 * - <sess> is the session which experienced the error
2542 * - <ctx> may be NULL or should contain any info relevant to the protocol
2543 * - <buf> is the buffer containing the offending data
2544 * - <buf_ofs> is the position of this buffer's input data in the input
2545 * stream, starting at zero. It may be passed as zero if unknown.
2546 * - <buf_out> is the portion of <buf->data> which was already forwarded and
2547 * which precedes the buffer's input. The buffer's input starts at
2548 * buf->head + buf_out.
2549 * - <err_pos> is the pointer to the faulty byte in the buffer's input.
2550 * - <show> is the callback to use to display <ctx>. It may be NULL.
2551 */
2552void proxy_capture_error(struct proxy *proxy, int is_back,
2553 struct proxy *other_end, enum obj_type *target,
2554 const struct session *sess,
2555 const struct buffer *buf, long buf_ofs,
2556 unsigned int buf_out, unsigned int err_pos,
2557 const union error_snapshot_ctx *ctx,
2558 void (*show)(struct buffer *, const struct error_snapshot *))
2559{
2560 struct error_snapshot *es;
2561 unsigned int buf_len;
2562 int len1, len2;
Willy Tarreauc55015e2018-09-07 19:02:32 +02002563 unsigned int ev_id;
2564
Willy Tarreau18515722021-04-06 11:57:41 +02002565 ev_id = HA_ATOMIC_FETCH_ADD(&error_snapshot_id, 1);
Willy Tarreauc55015e2018-09-07 19:02:32 +02002566
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002567 buf_len = b_data(buf) - buf_out;
2568
2569 es = malloc(sizeof(*es) + buf_len);
Willy Tarreauc55015e2018-09-07 19:02:32 +02002570 if (!es)
2571 return;
2572
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002573 es->buf_len = buf_len;
2574 es->ev_id = ev_id;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002575
Christopher Faulet47a72102020-01-06 11:37:00 +01002576 len1 = b_size(buf) - b_peek_ofs(buf, buf_out);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002577 if (len1 > buf_len)
2578 len1 = buf_len;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002579
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002580 if (len1) {
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002581 memcpy(es->buf, b_peek(buf, buf_out), len1);
Willy Tarreau4bc7d902018-09-07 20:07:17 +02002582 len2 = buf_len - len1;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002583 if (len2)
2584 memcpy(es->buf + len1, b_orig(buf), len2);
2585 }
2586
2587 es->buf_err = err_pos;
2588 es->when = date; // user-visible date
2589 es->srv = objt_server(target);
2590 es->oe = other_end;
Olivier Houchardbdb00c52020-03-12 15:30:17 +01002591 if (sess && objt_conn(sess->origin) && conn_get_src(__objt_conn(sess->origin)))
Willy Tarreau026efc72019-07-17 15:20:02 +02002592 es->src = *__objt_conn(sess->origin)->src;
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002593 else
2594 memset(&es->src, 0, sizeof(es->src));
2595
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002596 es->buf_wrap = b_wrap(buf) - b_peek(buf, buf_out);
2597 es->buf_out = buf_out;
2598 es->buf_ofs = buf_ofs;
2599
2600 /* be sure to indicate the offset of the first IN byte */
2601 if (es->buf_ofs >= es->buf_len)
2602 es->buf_ofs -= es->buf_len;
2603 else
2604 es->buf_ofs = 0;
2605
2606 /* protocol-specific part now */
2607 if (ctx)
2608 es->ctx = *ctx;
2609 else
2610 memset(&es->ctx, 0, sizeof(es->ctx));
2611 es->show = show;
Willy Tarreauc55015e2018-09-07 19:02:32 +02002612
2613 /* note: we still lock since we have to be certain that nobody is
2614 * dumping the output while we free.
2615 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002616 HA_RWLOCK_WRLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauc55015e2018-09-07 19:02:32 +02002617 if (is_back) {
2618 es = HA_ATOMIC_XCHG(&proxy->invalid_rep, es);
2619 } else {
2620 es = HA_ATOMIC_XCHG(&proxy->invalid_req, es);
2621 }
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002622 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &proxy->lock);
Willy Tarreauda9f2582022-09-17 11:07:19 +02002623 ha_free(&es);
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002624}
2625
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002626/* Configure all proxies which lack a maxconn setting to use the global one by
2627 * default. This avoids the common mistake consisting in setting maxconn only
2628 * in the global section and discovering the hard way that it doesn't propagate
2629 * through the frontends. These values are also propagated through the various
Ilya Shipitsin47d17182020-06-21 21:42:57 +05002630 * targeted backends, whose fullconn is finally calculated if not yet set.
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002631 */
2632void proxy_adjust_all_maxconn()
2633{
2634 struct proxy *curproxy;
2635 struct switching_rule *swrule1, *swrule2;
2636
2637 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002638 if (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002639 continue;
2640
2641 if (!(curproxy->cap & PR_CAP_FE))
2642 continue;
2643
2644 if (!curproxy->maxconn)
2645 curproxy->maxconn = global.maxconn;
2646
2647 /* update the target backend's fullconn count : default_backend */
2648 if (curproxy->defbe.be)
2649 curproxy->defbe.be->tot_fe_maxconn += curproxy->maxconn;
2650 else if ((curproxy->cap & PR_CAP_LISTEN) == PR_CAP_LISTEN)
2651 curproxy->tot_fe_maxconn += curproxy->maxconn;
2652
2653 list_for_each_entry(swrule1, &curproxy->switching_rules, list) {
2654 /* For each target of switching rules, we update their
2655 * tot_fe_maxconn, except if a previous rule points to
2656 * the same backend or to the default backend.
2657 */
2658 if (swrule1->be.backend != curproxy->defbe.be) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01002659 /* note: swrule1->be.backend isn't a backend if the rule
2660 * is dynamic, it's an expression instead, so it must not
2661 * be dereferenced as a backend before being certain it is.
2662 */
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002663 list_for_each_entry(swrule2, &curproxy->switching_rules, list) {
2664 if (swrule2 == swrule1) {
Frédéric Lécaille2365fb02019-03-07 15:02:52 +01002665 if (!swrule1->dynamic)
2666 swrule1->be.backend->tot_fe_maxconn += curproxy->maxconn;
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002667 break;
2668 }
2669 else if (!swrule2->dynamic && swrule2->be.backend == swrule1->be.backend) {
2670 /* there are multiple refs of this backend */
2671 break;
2672 }
2673 }
2674 }
2675 }
2676 }
2677
2678 /* automatically compute fullconn if not set. We must not do it in the
2679 * loop above because cross-references are not yet fully resolved.
2680 */
2681 for (curproxy = proxies_list; curproxy; curproxy = curproxy->next) {
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02002682 if (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Willy Tarreauc8d5b952019-02-27 17:25:52 +01002683 continue;
2684
2685 /* If <fullconn> is not set, let's set it to 10% of the sum of
2686 * the possible incoming frontend's maxconns.
2687 */
2688 if (!curproxy->fullconn && (curproxy->cap & PR_CAP_BE)) {
2689 /* we have the sum of the maxconns in <total>. We only
2690 * keep 10% of that sum to set the default fullconn, with
2691 * a hard minimum of 1 (to avoid a divide by zero).
2692 */
2693 curproxy->fullconn = (curproxy->tot_fe_maxconn + 9) / 10;
2694 if (!curproxy->fullconn)
2695 curproxy->fullconn = 1;
2696 }
2697 }
2698}
2699
Willy Tarreau75fb65a2018-09-07 17:43:26 +02002700/* Config keywords below */
2701
Willy Tarreaudc13c112013-06-21 23:16:39 +02002702static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau10080712021-09-07 10:49:45 +02002703 { CFG_GLOBAL, "grace", proxy_parse_grace },
Cyril Bonté203ec5a2017-03-23 22:44:13 +01002704 { CFG_GLOBAL, "hard-stop-after", proxy_parse_hard_stop_after },
Remi Tricot-Le Bretonb5d968d2022-04-08 18:04:18 +02002705 { CFG_GLOBAL, "close-spread-time", proxy_parse_close_spread_time },
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002706 { CFG_LISTEN, "timeout", proxy_parse_timeout },
Tim Duesterhus86e6b6e2019-05-14 20:57:59 +02002707 { CFG_LISTEN, "clitimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
2708 { CFG_LISTEN, "contimeout", proxy_parse_timeout }, /* This keyword actually fails to parse, this line remains for better error messages. */
2709 { 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 +01002710 { CFG_LISTEN, "rate-limit", proxy_parse_rate_limit },
Willy Tarreauc35362a2014-04-25 13:58:37 +02002711 { CFG_LISTEN, "max-keep-alive-queue", proxy_parse_max_ka_queue },
Thierry FOURNIERa0a1b752015-05-26 17:44:32 +02002712 { CFG_LISTEN, "declare", proxy_parse_declare },
Olivier Houcharda254a372019-04-05 15:30:12 +02002713 { CFG_LISTEN, "retry-on", proxy_parse_retry_on },
Willy Tarreau52543212020-07-09 05:58:51 +02002714#ifdef TCP_KEEPCNT
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09002715 { CFG_LISTEN, "clitcpka-cnt", proxy_parse_tcpka_cnt },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09002716 { CFG_LISTEN, "srvtcpka-cnt", proxy_parse_tcpka_cnt },
Willy Tarreau52543212020-07-09 05:58:51 +02002717#endif
2718#ifdef TCP_KEEPIDLE
2719 { CFG_LISTEN, "clitcpka-idle", proxy_parse_tcpka_idle },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09002720 { CFG_LISTEN, "srvtcpka-idle", proxy_parse_tcpka_idle },
Willy Tarreau52543212020-07-09 05:58:51 +02002721#endif
2722#ifdef TCP_KEEPINTVL
2723 { CFG_LISTEN, "clitcpka-intvl", proxy_parse_tcpka_intvl },
MIZUTA Takeshib24bc0d2020-07-09 11:13:20 +09002724 { CFG_LISTEN, "srvtcpka-intvl", proxy_parse_tcpka_intvl },
Willy Tarreau52543212020-07-09 05:58:51 +02002725#endif
Willy Tarreau9de1bbd2008-07-09 20:34:27 +02002726 { 0, NULL, NULL },
2727}};
2728
Willy Tarreau0108d902018-11-25 19:14:37 +01002729INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
2730
Willy Tarreau960f2cb2016-11-24 12:02:29 +01002731/* Expects to find a frontend named <arg> and returns it, otherwise displays various
2732 * adequate error messages and returns NULL. This function is designed to be used by
2733 * functions requiring a frontend on the CLI.
2734 */
2735struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg)
2736{
2737 struct proxy *px;
2738
2739 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02002740 cli_err(appctx, "A frontend name is expected.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01002741 return NULL;
2742 }
2743
2744 px = proxy_fe_by_name(arg);
2745 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02002746 cli_err(appctx, "No such frontend.\n");
Willy Tarreau960f2cb2016-11-24 12:02:29 +01002747 return NULL;
2748 }
2749 return px;
2750}
2751
Olivier Houchard614f8d72017-03-14 20:08:46 +01002752/* Expects to find a backend named <arg> and returns it, otherwise displays various
2753 * adequate error messages and returns NULL. This function is designed to be used by
2754 * functions requiring a frontend on the CLI.
2755 */
2756struct proxy *cli_find_backend(struct appctx *appctx, const char *arg)
2757{
2758 struct proxy *px;
2759
2760 if (!*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02002761 cli_err(appctx, "A backend name is expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01002762 return NULL;
2763 }
2764
2765 px = proxy_be_by_name(arg);
2766 if (!px) {
Willy Tarreau9d008692019-08-09 11:21:01 +02002767 cli_err(appctx, "No such backend.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01002768 return NULL;
2769 }
2770 return px;
2771}
2772
2773
Willy Tarreau69f591e2020-07-01 07:00:59 +02002774/* parse a "show servers [state|conn]" CLI line, returns 0 if it wants to start
2775 * the dump or 1 if it stops immediately. If an argument is specified, it will
Willy Tarreau72a04232022-05-05 17:00:20 +02002776 * reserve a show_srv_ctx context and set the proxy pointer into ->px, its ID
2777 * into ->only_pxid, and ->show_conn to 0 for "state", or 1 for "conn".
William Lallemanda6c5f332016-11-19 02:25:36 +01002778 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002779static int cli_parse_show_servers(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemanda6c5f332016-11-19 02:25:36 +01002780{
Willy Tarreau72a04232022-05-05 17:00:20 +02002781 struct show_srv_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01002782 struct proxy *px;
William Lallemanda6c5f332016-11-19 02:25:36 +01002783
Willy Tarreau72a04232022-05-05 17:00:20 +02002784 ctx->show_conn = *args[2] == 'c'; // "conn" vs "state"
Willy Tarreau69f591e2020-07-01 07:00:59 +02002785
William Lallemanda6c5f332016-11-19 02:25:36 +01002786 /* check if a backend name has been provided */
2787 if (*args[3]) {
2788 /* read server state from local file */
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01002789 px = proxy_be_by_name(args[3]);
William Lallemanda6c5f332016-11-19 02:25:36 +01002790
Willy Tarreau9d008692019-08-09 11:21:01 +02002791 if (!px)
2792 return cli_err(appctx, "Can't find backend.\n");
2793
Willy Tarreau72a04232022-05-05 17:00:20 +02002794 ctx->px = px;
2795 ctx->only_pxid = px->uuid;
William Lallemanda6c5f332016-11-19 02:25:36 +01002796 }
2797 return 0;
2798}
2799
William Dauchyd1a7b852021-02-11 22:51:26 +01002800/* helper to dump server addr */
2801static void dump_server_addr(const struct sockaddr_storage *addr, char *addr_str)
2802{
2803 addr_str[0] = '\0';
2804 switch (addr->ss_family) {
2805 case AF_INET:
2806 case AF_INET6:
2807 addr_to_str(addr, addr_str, INET6_ADDRSTRLEN + 1);
2808 break;
2809 default:
2810 memcpy(addr_str, "-\0", 2);
2811 break;
2812 }
2813}
2814
Willy Tarreau6ff81432020-07-01 07:02:42 +02002815/* dumps server state information for all the servers found in backend cli.p0.
William Lallemanda6c5f332016-11-19 02:25:36 +01002816 * These information are all the parameters which may change during HAProxy runtime.
Willy Tarreau72a04232022-05-05 17:00:20 +02002817 * By default, we only export to the last known server state file format. These
2818 * information can be used at next startup to recover same level of server
2819 * state. It takes its context from show_srv_ctx, with the proxy pointer from
2820 * ->px, the proxy's id ->only_pxid, the server's pointer from ->sv, and the
2821 * choice of what to dump from ->show_conn.
William Lallemanda6c5f332016-11-19 02:25:36 +01002822 */
Willy Tarreau475e4632022-05-27 10:26:46 +02002823static int dump_servers_state(struct stconn *sc)
William Lallemanda6c5f332016-11-19 02:25:36 +01002824{
Willy Tarreau475e4632022-05-27 10:26:46 +02002825 struct appctx *appctx = __sc_appctx(sc);
Willy Tarreau72a04232022-05-05 17:00:20 +02002826 struct show_srv_ctx *ctx = appctx->svcctx;
2827 struct proxy *px = ctx->px;
William Lallemanda6c5f332016-11-19 02:25:36 +01002828 struct server *srv;
2829 char srv_addr[INET6_ADDRSTRLEN + 1];
William Dauchyd1a7b852021-02-11 22:51:26 +01002830 char srv_agent_addr[INET6_ADDRSTRLEN + 1];
2831 char srv_check_addr[INET6_ADDRSTRLEN + 1];
William Lallemanda6c5f332016-11-19 02:25:36 +01002832 time_t srv_time_since_last_change;
2833 int bk_f_forced_id, srv_f_forced_id;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002834 char *srvrecord;
William Lallemanda6c5f332016-11-19 02:25:36 +01002835
Willy Tarreau72a04232022-05-05 17:00:20 +02002836 if (!ctx->sv)
2837 ctx->sv = px->srv;
William Lallemanda6c5f332016-11-19 02:25:36 +01002838
Willy Tarreau72a04232022-05-05 17:00:20 +02002839 for (; ctx->sv != NULL; ctx->sv = srv->next) {
2840 srv = ctx->sv;
William Lallemanda6c5f332016-11-19 02:25:36 +01002841
William Dauchyd1a7b852021-02-11 22:51:26 +01002842 dump_server_addr(&srv->addr, srv_addr);
2843 dump_server_addr(&srv->check.addr, srv_check_addr);
2844 dump_server_addr(&srv->agent.addr, srv_agent_addr);
2845
Willy Tarreau69530f52023-04-28 09:16:15 +02002846 srv_time_since_last_change = ns_to_sec(now_ns) - srv->last_change;
Willy Tarreau3c92f2a2016-12-16 18:23:39 +01002847 bk_f_forced_id = px->options & PR_O_FORCED_ID ? 1 : 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01002848 srv_f_forced_id = srv->flags & SRV_F_FORCED_ID ? 1 : 0;
2849
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002850 srvrecord = NULL;
2851 if (srv->srvrq && srv->srvrq->name)
2852 srvrecord = srv->srvrq->name;
2853
Willy Tarreau72a04232022-05-05 17:00:20 +02002854 if (ctx->show_conn == 0) {
Willy Tarreau69f591e2020-07-01 07:00:59 +02002855 /* show servers state */
2856 chunk_printf(&trash,
2857 "%d %s "
2858 "%d %s %s "
2859 "%d %d %d %d %ld "
2860 "%d %d %d %d %d "
William Dauchyf6370442020-11-14 19:25:33 +01002861 "%d %d %s %u "
William Dauchyd1a7b852021-02-11 22:51:26 +01002862 "%s %d %d "
2863 "%s %s %d"
Willy Tarreau69f591e2020-07-01 07:00:59 +02002864 "\n",
Erwan Le Goasacfdf762022-09-14 17:48:55 +02002865 px->uuid, HA_ANON_CLI(px->id),
Erwan Le Goas9ac3ccb2022-09-29 10:27:33 +02002866 srv->puid, HA_ANON_CLI(srv->id),
2867 hash_ipanon(appctx->cli_anon_key, srv_addr, 0),
Erwan Le Goasacfdf762022-09-14 17:48:55 +02002868 srv->cur_state, srv->cur_admin, srv->uweight, srv->iweight,
2869 (long int)srv_time_since_last_change,
2870 srv->check.status, srv->check.result, srv->check.health,
Willy Tarreau99521ab2022-10-12 21:40:31 +02002871 srv->check.state & 0x0F, srv->agent.state & 0x1F,
Erwan Le Goas1caa5352022-09-29 10:28:44 +02002872 bk_f_forced_id, srv_f_forced_id,
2873 srv->hostname ? HA_ANON_CLI(srv->hostname) : "-", srv->svc_port,
William Dauchyd1a7b852021-02-11 22:51:26 +01002874 srvrecord ? srvrecord : "-", srv->use_ssl, srv->check.port,
2875 srv_check_addr, srv_agent_addr, srv->agent.port);
Willy Tarreau69f591e2020-07-01 07:00:59 +02002876 } else {
2877 /* show servers conn */
2878 int thr;
2879
2880 chunk_printf(&trash,
2881 "%s/%s %d/%d %s %u - %u %u %u %u %u %u %d %u",
Erwan Le Goasacfdf762022-09-14 17:48:55 +02002882 HA_ANON_CLI(px->id), HA_ANON_CLI(srv->id),
Erwan Le Goas9ac3ccb2022-09-29 10:27:33 +02002883 px->uuid, srv->puid, hash_ipanon(appctx->cli_anon_key, srv_addr, 0),
Erwan Le Goasacfdf762022-09-14 17:48:55 +02002884 srv->svc_port, srv->pool_purge_delay,
Willy Tarreau69f591e2020-07-01 07:00:59 +02002885 srv->curr_used_conns, srv->max_used_conns, srv->est_need_conns,
2886 srv->curr_idle_nb, srv->curr_safe_nb, (int)srv->max_idle_conns, srv->curr_idle_conns);
2887
Willy Tarreau42abe682020-07-02 15:19:57 +02002888 for (thr = 0; thr < global.nbthread && srv->curr_idle_thr; thr++)
Willy Tarreau69f591e2020-07-01 07:00:59 +02002889 chunk_appendf(&trash, " %u", srv->curr_idle_thr[thr]);
2890
2891 chunk_appendf(&trash, "\n");
2892 }
2893
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002894 if (applet_putchk(appctx, &trash) == -1) {
William Lallemanda6c5f332016-11-19 02:25:36 +01002895 return 0;
2896 }
2897 }
2898 return 1;
2899}
2900
2901/* Parses backend list or simply use backend name provided by the user to return
Willy Tarreau72a04232022-05-05 17:00:20 +02002902 * states of servers to stdout. It takes its context from show_srv_ctx and dumps
2903 * proxy ->px and stops if ->only_pxid is non-null.
William Lallemanda6c5f332016-11-19 02:25:36 +01002904 */
2905static int cli_io_handler_servers_state(struct appctx *appctx)
2906{
Willy Tarreau72a04232022-05-05 17:00:20 +02002907 struct show_srv_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02002908 struct stconn *sc = appctx_sc(appctx);
William Lallemanda6c5f332016-11-19 02:25:36 +01002909 struct proxy *curproxy;
2910
Willy Tarreaud741e9c2022-05-05 19:26:18 +02002911 if (ctx->state == SHOW_SRV_HEAD) {
Willy Tarreau72a04232022-05-05 17:00:20 +02002912 if (ctx->show_conn == 0)
Willy Tarreau69f591e2020-07-01 07:00:59 +02002913 chunk_printf(&trash, "%d\n# %s\n", SRV_STATE_FILE_VERSION, SRV_STATE_FILE_FIELD_NAMES);
2914 else
2915 chunk_printf(&trash,
2916 "# 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",
2917 global.nbthread);
2918
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002919 if (applet_putchk(appctx, &trash) == -1)
William Lallemanda6c5f332016-11-19 02:25:36 +01002920 return 0;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002921
Willy Tarreaud741e9c2022-05-05 19:26:18 +02002922 ctx->state = SHOW_SRV_LIST;
2923
2924 if (!ctx->px)
2925 ctx->px = proxies_list;
William Lallemanda6c5f332016-11-19 02:25:36 +01002926 }
2927
Willy Tarreau72a04232022-05-05 17:00:20 +02002928 for (; ctx->px != NULL; ctx->px = curproxy->next) {
2929 curproxy = ctx->px;
William Lallemanda6c5f332016-11-19 02:25:36 +01002930 /* servers are only in backends */
William Lallemanda39e6262021-08-25 18:15:31 +02002931 if ((curproxy->cap & PR_CAP_BE) && !(curproxy->cap & PR_CAP_INT)) {
Willy Tarreau475e4632022-05-27 10:26:46 +02002932 if (!dump_servers_state(sc))
William Lallemanda6c5f332016-11-19 02:25:36 +01002933 return 0;
William Lallemanda6c5f332016-11-19 02:25:36 +01002934 }
2935 /* only the selected proxy is dumped */
Willy Tarreau72a04232022-05-05 17:00:20 +02002936 if (ctx->only_pxid)
William Lallemanda6c5f332016-11-19 02:25:36 +01002937 break;
2938 }
2939
2940 return 1;
2941}
2942
Willy Tarreau608ea592016-12-16 18:01:15 +01002943/* Parses backend list and simply report backend names. It keeps the proxy
Willy Tarreauacf6a442022-05-05 17:05:38 +02002944 * pointer in svcctx since there's nothing else to store there.
Willy Tarreau608ea592016-12-16 18:01:15 +01002945 */
William Lallemand933efcd2016-11-22 12:34:16 +01002946static int cli_io_handler_show_backend(struct appctx *appctx)
2947{
William Lallemand933efcd2016-11-22 12:34:16 +01002948 struct proxy *curproxy;
2949
2950 chunk_reset(&trash);
2951
Willy Tarreauacf6a442022-05-05 17:05:38 +02002952 if (!appctx->svcctx) {
William Lallemand933efcd2016-11-22 12:34:16 +01002953 chunk_printf(&trash, "# name\n");
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002954 if (applet_putchk(appctx, &trash) == -1)
William Lallemand933efcd2016-11-22 12:34:16 +01002955 return 0;
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002956
Willy Tarreauacf6a442022-05-05 17:05:38 +02002957 appctx->svcctx = proxies_list;
William Lallemand933efcd2016-11-22 12:34:16 +01002958 }
2959
Willy Tarreauacf6a442022-05-05 17:05:38 +02002960 for (; appctx->svcctx != NULL; appctx->svcctx = curproxy->next) {
2961 curproxy = appctx->svcctx;
William Lallemand933efcd2016-11-22 12:34:16 +01002962
Willy Tarreau4f9f1572022-05-05 17:10:03 +02002963 /* looking for non-internal backends only */
2964 if ((curproxy->cap & (PR_CAP_BE|PR_CAP_INT)) != PR_CAP_BE)
William Lallemand933efcd2016-11-22 12:34:16 +01002965 continue;
2966
William Lallemand933efcd2016-11-22 12:34:16 +01002967 chunk_appendf(&trash, "%s\n", curproxy->id);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02002968 if (applet_putchk(appctx, &trash) == -1)
William Lallemand933efcd2016-11-22 12:34:16 +01002969 return 0;
William Lallemand933efcd2016-11-22 12:34:16 +01002970 }
2971
2972 return 1;
2973}
William Lallemanda6c5f332016-11-19 02:25:36 +01002974
Willy Tarreaua275a372018-08-21 14:50:44 +02002975/* Parses the "enable dynamic-cookies backend" directive, it always returns 1.
2976 *
2977 * Grabs the proxy lock and each server's lock.
2978 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02002979static int cli_parse_enable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01002980{
2981 struct proxy *px;
2982 struct server *s;
2983
2984 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
2985 return 1;
2986
2987 px = cli_find_backend(appctx, args[3]);
2988 if (!px)
2989 return 1;
2990
Willy Tarreau5e83d992019-07-30 11:59:34 +02002991 /* Note: this lock is to make sure this doesn't change while another
2992 * thread is in srv_set_dyncookie().
2993 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002994 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002995 px->ck_opts |= PR_CK_DYNAMIC;
Willy Tarreauac66d6b2020-10-20 17:24:27 +02002996 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01002997
Willy Tarreaua275a372018-08-21 14:50:44 +02002998 for (s = px->srv; s != NULL; s = s->next) {
2999 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003000 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02003001 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
3002 }
3003
Olivier Houchard614f8d72017-03-14 20:08:46 +01003004 return 1;
3005}
3006
Willy Tarreaua275a372018-08-21 14:50:44 +02003007/* Parses the "disable dynamic-cookies backend" directive, it always returns 1.
3008 *
3009 * Grabs the proxy lock and each server's lock.
3010 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003011static int cli_parse_disable_dyncookie_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01003012{
3013 struct proxy *px;
3014 struct server *s;
3015
3016 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3017 return 1;
3018
3019 px = cli_find_backend(appctx, args[3]);
3020 if (!px)
3021 return 1;
3022
Willy Tarreau5e83d992019-07-30 11:59:34 +02003023 /* Note: this lock is to make sure this doesn't change while another
3024 * thread is in srv_set_dyncookie().
3025 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02003026 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003027 px->ck_opts &= ~PR_CK_DYNAMIC;
Willy Tarreauac66d6b2020-10-20 17:24:27 +02003028 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003029
3030 for (s = px->srv; s != NULL; s = s->next) {
Willy Tarreaua275a372018-08-21 14:50:44 +02003031 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003032 if (!(s->flags & SRV_F_COOKIESET))
3033 ha_free(&s->cookie);
Willy Tarreaua275a372018-08-21 14:50:44 +02003034 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003035 }
3036
3037 return 1;
3038}
3039
Willy Tarreaua275a372018-08-21 14:50:44 +02003040/* Parses the "set dynamic-cookie-key backend" directive, it always returns 1.
3041 *
3042 * Grabs the proxy lock and each server's lock.
3043 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003044static int cli_parse_set_dyncookie_key_backend(char **args, char *payload, struct appctx *appctx, void *private)
Olivier Houchard614f8d72017-03-14 20:08:46 +01003045{
3046 struct proxy *px;
3047 struct server *s;
3048 char *newkey;
3049
3050 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3051 return 1;
3052
3053 px = cli_find_backend(appctx, args[3]);
3054 if (!px)
3055 return 1;
3056
Willy Tarreau9d008692019-08-09 11:21:01 +02003057 if (!*args[4])
3058 return cli_err(appctx, "String value expected.\n");
Olivier Houchard614f8d72017-03-14 20:08:46 +01003059
3060 newkey = strdup(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003061 if (!newkey)
3062 return cli_err(appctx, "Failed to allocate memory.\n");
Willy Tarreaua275a372018-08-21 14:50:44 +02003063
Willy Tarreau5e83d992019-07-30 11:59:34 +02003064 /* Note: this lock is to make sure this doesn't change while another
3065 * thread is in srv_set_dyncookie().
3066 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02003067 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003068 free(px->dyncookie_key);
3069 px->dyncookie_key = newkey;
Willy Tarreauac66d6b2020-10-20 17:24:27 +02003070 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003071
Willy Tarreaua275a372018-08-21 14:50:44 +02003072 for (s = px->srv; s != NULL; s = s->next) {
3073 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houchard614f8d72017-03-14 20:08:46 +01003074 srv_set_dyncookie(s);
Willy Tarreaua275a372018-08-21 14:50:44 +02003075 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
3076 }
3077
Olivier Houchard614f8d72017-03-14 20:08:46 +01003078 return 1;
3079}
3080
Willy Tarreaua275a372018-08-21 14:50:44 +02003081/* Parses the "set maxconn frontend" directive, it always returns 1.
3082 *
3083 * Grabs the proxy lock.
3084 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003085static int cli_parse_set_maxconn_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003086{
3087 struct proxy *px;
3088 struct listener *l;
3089 int v;
3090
3091 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3092 return 1;
3093
3094 px = cli_find_frontend(appctx, args[3]);
3095 if (!px)
3096 return 1;
3097
Willy Tarreau9d008692019-08-09 11:21:01 +02003098 if (!*args[4])
3099 return cli_err(appctx, "Integer value expected.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003100
3101 v = atoi(args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02003102 if (v < 0)
3103 return cli_err(appctx, "Value out of range.\n");
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003104
3105 /* OK, the value is fine, so we assign it to the proxy and to all of
3106 * its listeners. The blocked ones will be dequeued.
3107 */
Willy Tarreauac66d6b2020-10-20 17:24:27 +02003108 HA_RWLOCK_WRLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02003109
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003110 px->maxconn = v;
3111 list_for_each_entry(l, &px->conf.listeners, by_fe) {
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003112 if (l->state == LI_FULL)
Aurelien DARRAGONf5d98932023-02-06 17:19:58 +01003113 relax_listener(l, 1, 0);
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003114 }
3115
Willy Tarreau241797a2019-12-10 14:10:52 +01003116 if (px->maxconn > px->feconn)
3117 dequeue_proxy_listeners(px);
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003118
Willy Tarreauac66d6b2020-10-20 17:24:27 +02003119 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock);
Willy Tarreaua275a372018-08-21 14:50:44 +02003120
Willy Tarreauc429a1f2016-11-23 16:22:04 +01003121 return 1;
3122}
3123
Willy Tarreaua275a372018-08-21 14:50:44 +02003124/* Parses the "shutdown frontend" directive, it always returns 1.
3125 *
3126 * Grabs the proxy lock.
3127 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003128static int cli_parse_shutdown_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau5212d7f2016-11-24 11:13:06 +01003129{
3130 struct proxy *px;
3131
3132 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3133 return 1;
3134
3135 px = cli_find_frontend(appctx, args[2]);
3136 if (!px)
3137 return 1;
3138
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003139 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Willy Tarreau9d008692019-08-09 11:21:01 +02003140 return cli_msg(appctx, LOG_NOTICE, "Frontend was already shut down.\n");
Willy Tarreau5212d7f2016-11-24 11:13:06 +01003141
Willy Tarreau5212d7f2016-11-24 11:13:06 +01003142 stop_proxy(px);
3143 return 1;
3144}
3145
Willy Tarreaua275a372018-08-21 14:50:44 +02003146/* Parses the "disable frontend" directive, it always returns 1.
3147 *
3148 * Grabs the proxy lock.
3149 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003150static int cli_parse_disable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01003151{
3152 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02003153 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01003154
3155 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3156 return 1;
3157
3158 px = cli_find_frontend(appctx, args[2]);
3159 if (!px)
3160 return 1;
3161
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003162 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Willy Tarreau9d008692019-08-09 11:21:01 +02003163 return cli_msg(appctx, LOG_NOTICE, "Frontend was previously shut down, cannot disable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01003164
Willy Tarreauf18d9682020-09-24 08:04:27 +02003165 if (!px->li_ready)
3166 return cli_msg(appctx, LOG_NOTICE, "All sockets are already disabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01003167
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02003168 /* pause_proxy will take PROXY_LOCK */
Willy Tarreaua275a372018-08-21 14:50:44 +02003169 ret = pause_proxy(px);
Willy Tarreaua275a372018-08-21 14:50:44 +02003170
Willy Tarreau9d008692019-08-09 11:21:01 +02003171 if (!ret)
3172 return cli_err(appctx, "Failed to pause frontend, check logs for precise cause.\n");
3173
Willy Tarreau15b9e682016-11-24 11:55:28 +01003174 return 1;
3175}
3176
Willy Tarreaua275a372018-08-21 14:50:44 +02003177/* Parses the "enable frontend" directive, it always returns 1.
3178 *
3179 * Grabs the proxy lock.
3180 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003181static int cli_parse_enable_frontend(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau15b9e682016-11-24 11:55:28 +01003182{
3183 struct proxy *px;
Willy Tarreaua275a372018-08-21 14:50:44 +02003184 int ret;
Willy Tarreau15b9e682016-11-24 11:55:28 +01003185
3186 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3187 return 1;
3188
3189 px = cli_find_frontend(appctx, args[2]);
3190 if (!px)
3191 return 1;
3192
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02003193 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED))
Willy Tarreau9d008692019-08-09 11:21:01 +02003194 return cli_err(appctx, "Frontend was previously shut down, cannot enable.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01003195
Willy Tarreauf18d9682020-09-24 08:04:27 +02003196 if (px->li_ready == px->li_all)
3197 return cli_msg(appctx, LOG_NOTICE, "All sockets are already enabled.\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01003198
Aurelien DARRAGON7d000772022-09-08 14:35:35 +02003199 /* resume_proxy will take PROXY_LOCK */
Willy Tarreaua275a372018-08-21 14:50:44 +02003200 ret = resume_proxy(px);
Willy Tarreaua275a372018-08-21 14:50:44 +02003201
Willy Tarreau9d008692019-08-09 11:21:01 +02003202 if (!ret)
3203 return cli_err(appctx, "Failed to resume frontend, check logs for precise cause (port conflict?).\n");
Willy Tarreau15b9e682016-11-24 11:55:28 +01003204 return 1;
3205}
3206
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003207/* appctx context used during "show errors" */
3208struct show_errors_ctx {
3209 struct proxy *px; /* current proxy being dumped, NULL = not started yet. */
3210 unsigned int flag; /* bit0: buffer being dumped, 0 = req, 1 = resp ; bit1=skip req ; bit2=skip resp. */
3211 unsigned int ev_id; /* event ID of error being dumped */
3212 int iid; /* if >= 0, ID of the proxy to filter on */
3213 int ptr; /* <0: headers, >=0 : text pointer to restart from */
3214 int bol; /* pointer to beginning of current line */
3215};
3216
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003217/* "show errors" handler for the CLI. Returns 0 if wants to continue, 1 to stop
3218 * now.
3219 */
3220static int cli_parse_show_errors(char **args, char *payload, struct appctx *appctx, void *private)
3221{
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003222 struct show_errors_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
3223
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003224 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3225 return 1;
3226
3227 if (*args[2]) {
3228 struct proxy *px;
3229
3230 px = proxy_find_by_name(args[2], 0, 0);
3231 if (px)
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003232 ctx->iid = px->uuid;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003233 else
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003234 ctx->iid = atoi(args[2]);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003235
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003236 if (!ctx->iid)
Willy Tarreau9d008692019-08-09 11:21:01 +02003237 return cli_err(appctx, "No such proxy.\n");
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003238 }
3239 else
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003240 ctx->iid = -1; // dump all proxies
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003241
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003242 ctx->flag = 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003243 if (strcmp(args[3], "request") == 0)
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003244 ctx->flag |= 4; // ignore response
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003245 else if (strcmp(args[3], "response") == 0)
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003246 ctx->flag |= 2; // ignore request
3247 ctx->px = NULL;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003248 return 0;
3249}
3250
Willy Tarreau4596fe22022-05-17 19:07:51 +02003251/* This function dumps all captured errors onto the stream connector's
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003252 * read buffer. It returns 0 if the output buffer is full and it needs
3253 * to be called again, otherwise non-zero.
3254 */
3255static int cli_io_handler_show_errors(struct appctx *appctx)
3256{
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003257 struct show_errors_ctx *ctx = appctx->svcctx;
Willy Tarreauc12b3212022-05-27 11:08:15 +02003258 struct stconn *sc = appctx_sc(appctx);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003259 extern const char *monthname[12];
3260
Christopher Faulet87633c32023-04-03 18:32:50 +02003261 /* FIXME: Don't watch the other side !*/
Christopher Faulet208c7122023-04-13 16:16:15 +02003262 if (unlikely(sc_opposite(sc)->flags & SC_FL_SHUT_DONE))
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003263 return 1;
3264
3265 chunk_reset(&trash);
3266
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003267 if (!ctx->px) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003268 /* the function had not been called yet, let's prepare the
3269 * buffer for a response.
3270 */
3271 struct tm tm;
3272
3273 get_localtime(date.tv_sec, &tm);
3274 chunk_appendf(&trash, "Total events captured on [%02d/%s/%04d:%02d:%02d:%02d.%03d] : %u\n",
3275 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3276 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(date.tv_usec/1000),
3277 error_snapshot_id);
3278
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003279 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau36b27362018-09-07 19:55:44 +02003280 goto cant_send;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003281
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003282 ctx->px = proxies_list;
3283 ctx->bol = 0;
3284 ctx->ptr = -1;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003285 }
3286
3287 /* we have two inner loops here, one for the proxy, the other one for
3288 * the buffer.
3289 */
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003290 while (ctx->px) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003291 struct error_snapshot *es;
3292
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003293 HA_RWLOCK_RDLOCK(PROXY_LOCK, &ctx->px->lock);
Willy Tarreau36b27362018-09-07 19:55:44 +02003294
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003295 if ((ctx->flag & 1) == 0) {
3296 es = ctx->px->invalid_req;
3297 if (ctx->flag & 2) // skip req
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003298 goto next;
3299 }
3300 else {
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003301 es = ctx->px->invalid_rep;
3302 if (ctx->flag & 4) // skip resp
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003303 goto next;
3304 }
3305
Willy Tarreauc55015e2018-09-07 19:02:32 +02003306 if (!es)
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003307 goto next;
3308
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003309 if (ctx->iid >= 0 &&
3310 ctx->px->uuid != ctx->iid &&
3311 (!es->oe || es->oe->uuid != ctx->iid))
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003312 goto next;
3313
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003314 if (ctx->ptr < 0) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003315 /* just print headers now */
3316
3317 char pn[INET6_ADDRSTRLEN];
3318 struct tm tm;
3319 int port;
3320
3321 get_localtime(es->when.tv_sec, &tm);
3322 chunk_appendf(&trash, " \n[%02d/%s/%04d:%02d:%02d:%02d.%03d]",
3323 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
3324 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(es->when.tv_usec/1000));
3325
3326 switch (addr_to_str(&es->src, pn, sizeof(pn))) {
3327 case AF_INET:
3328 case AF_INET6:
3329 port = get_host_port(&es->src);
3330 break;
3331 default:
3332 port = 0;
3333 }
3334
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003335 switch (ctx->flag & 1) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003336 case 0:
3337 chunk_appendf(&trash,
3338 " frontend %s (#%d): invalid request\n"
3339 " backend %s (#%d)",
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003340 ctx->px->id, ctx->px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003341 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->id : "<NONE>",
3342 (es->oe && es->oe->cap & PR_CAP_BE) ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003343 break;
3344 case 1:
3345 chunk_appendf(&trash,
3346 " backend %s (#%d): invalid response\n"
3347 " frontend %s (#%d)",
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003348 ctx->px->id, ctx->px->uuid,
Olivier Houchardbdb00c52020-03-12 15:30:17 +01003349 es->oe ? es->oe->id : "<NONE>" , es->oe ? es->oe->uuid : -1);
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003350 break;
3351 }
3352
3353 chunk_appendf(&trash,
3354 ", server %s (#%d), event #%u, src %s:%d\n"
3355 " buffer starts at %llu (including %u out), %u free,\n"
3356 " len %u, wraps at %u, error at position %u\n",
3357 es->srv ? es->srv->id : "<NONE>",
3358 es->srv ? es->srv->puid : -1,
3359 es->ev_id, pn, port,
3360 es->buf_ofs, es->buf_out,
3361 global.tune.bufsize - es->buf_out - es->buf_len,
3362 es->buf_len, es->buf_wrap, es->buf_err);
3363
3364 if (es->show)
3365 es->show(&trash, es);
3366
3367 chunk_appendf(&trash, " \n");
3368
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003369 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau36b27362018-09-07 19:55:44 +02003370 goto cant_send_unlock;
3371
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003372 ctx->ptr = 0;
3373 ctx->ev_id = es->ev_id;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003374 }
3375
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003376 if (ctx->ev_id != es->ev_id) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003377 /* the snapshot changed while we were dumping it */
3378 chunk_appendf(&trash,
3379 " WARNING! update detected on this snapshot, dump interrupted. Please re-check!\n");
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003380 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau36b27362018-09-07 19:55:44 +02003381 goto cant_send_unlock;
3382
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003383 goto next;
3384 }
3385
3386 /* OK, ptr >= 0, so we have to dump the current line */
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003387 while (ctx->ptr < es->buf_len && ctx->ptr < global.tune.bufsize) {
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003388 int newptr;
3389 int newline;
3390
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003391 newline = ctx->bol;
3392 newptr = dump_text_line(&trash, es->buf, global.tune.bufsize, es->buf_len, &newline, ctx->ptr);
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003393 if (newptr == ctx->ptr) {
3394 sc_need_room(sc, 0);
Willy Tarreau36b27362018-09-07 19:55:44 +02003395 goto cant_send_unlock;
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02003396 }
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003397
Willy Tarreaud0a06d52022-05-18 15:07:19 +02003398 if (applet_putchk(appctx, &trash) == -1)
Willy Tarreau36b27362018-09-07 19:55:44 +02003399 goto cant_send_unlock;
3400
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003401 ctx->ptr = newptr;
3402 ctx->bol = newline;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003403 };
3404 next:
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003405 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &ctx->px->lock);
3406 ctx->bol = 0;
3407 ctx->ptr = -1;
3408 ctx->flag ^= 1;
3409 if (!(ctx->flag & 1))
3410 ctx->px = ctx->px->next;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003411 }
3412
3413 /* dump complete */
3414 return 1;
Willy Tarreau36b27362018-09-07 19:55:44 +02003415
3416 cant_send_unlock:
Willy Tarreau0fd8f0e2022-05-03 11:24:24 +02003417 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &ctx->px->lock);
Willy Tarreau36b27362018-09-07 19:55:44 +02003418 cant_send:
Willy Tarreau36b27362018-09-07 19:55:44 +02003419 return 0;
Willy Tarreauddb68ac2018-09-07 18:34:24 +02003420}
3421
William Lallemanda6c5f332016-11-19 02:25:36 +01003422/* register cli keywords */
3423static struct cli_kw_list cli_kws = {{ },{
Willy Tarreaub205bfd2021-05-07 11:38:37 +02003424 { { "disable", "frontend", NULL }, "disable frontend <frontend> : temporarily disable specific frontend", cli_parse_disable_frontend, NULL, NULL },
3425 { { "enable", "frontend", NULL }, "enable frontend <frontend> : re-enable specific frontend", cli_parse_enable_frontend, NULL, NULL },
3426 { { "set", "maxconn", "frontend", NULL }, "set maxconn frontend <frontend> <value> : change a frontend's maxconn setting", cli_parse_set_maxconn_frontend, NULL },
3427 { { "show","servers", "conn", NULL }, "show servers conn [<backend>] : dump server connections status (all or for a single backend)", cli_parse_show_servers, cli_io_handler_servers_state },
3428 { { "show","servers", "state", NULL }, "show servers state [<backend>] : dump volatile server information (all or for a single backend)", cli_parse_show_servers, cli_io_handler_servers_state },
3429 { { "show", "backend", NULL }, "show backend : list backends in the current running config", NULL, cli_io_handler_show_backend },
3430 { { "shutdown", "frontend", NULL }, "shutdown frontend <frontend> : stop a specific frontend", cli_parse_shutdown_frontend, NULL, NULL },
3431 { { "set", "dynamic-cookie-key", "backend", NULL }, "set dynamic-cookie-key backend <bk> <k> : change a backend secret key for dynamic cookies", cli_parse_set_dyncookie_key_backend, NULL },
3432 { { "enable", "dynamic-cookie", "backend", NULL }, "enable dynamic-cookie backend <bk> : enable dynamic cookies on a specific backend", cli_parse_enable_dyncookie_backend, NULL },
3433 { { "disable", "dynamic-cookie", "backend", NULL }, "disable dynamic-cookie backend <bk> : disable dynamic cookies on a specific backend", cli_parse_disable_dyncookie_backend, NULL },
3434 { { "show", "errors", NULL }, "show errors [<px>] [request|response] : report last request and/or response errors for each proxy", cli_parse_show_errors, cli_io_handler_show_errors, NULL },
William Lallemanda6c5f332016-11-19 02:25:36 +01003435 {{},}
3436}};
3437
Willy Tarreau0108d902018-11-25 19:14:37 +01003438INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Willy Tarreaubaaee002006-06-26 02:48:02 +02003439
3440/*
3441 * Local variables:
3442 * c-indent-level: 8
3443 * c-basic-offset: 8
3444 * End:
3445 */