blob: 7796d1dd4b277baa72a83848a711de2fa8772cc7 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau0a4b0ab2020-06-11 11:22:44 +020014#include <sys/types.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020017#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020018
Willy Tarreau260f3242021-10-06 18:30:04 +020019#include <import/ebmbtree.h>
20
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020022#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020023#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020025#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020027#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020028#include <haproxy/dict-t.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020032#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020033#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/port_range.h>
35#include <haproxy/protocol.h>
Willy Tarreaub00a8e32021-05-08 20:18:59 +020036#include <haproxy/proxy.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020037#include <haproxy/queue.h>
Emeric Brunc9437992021-02-12 19:42:55 +010038#include <haproxy/resolvers.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020040#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020041#include <haproxy/server.h>
Amaury Denoyellef99f77a2021-03-08 17:13:32 +010042#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020043#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020045#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020046#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020047#include <haproxy/time.h>
Willy Tarreauba6300e2021-05-08 14:09:40 +020048#include <haproxy/tools.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020049#include <haproxy/xxhash.h>
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +010050#include <haproxy/event_hdl.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020051
Baptiste Assmannda29fe22019-06-13 13:24:29 +020052
Aurelien DARRAGON76e25552023-04-18 11:00:17 +020053static void srv_update_status(struct server *s, int type, int cause);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010054static int srv_apply_lastaddr(struct server *srv, int *err_code);
William Dauchy6318d332020-05-02 21:52:36 +020055static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010057/* extra keywords used as value for other arguments. They are used as
58 * suggestions for mistyped words.
Willy Tarreau49c2b452021-03-12 09:58:04 +010059 */
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010060static const char *extra_kw_list[] = {
61 "ipv4", "ipv6", "legacy", "octet-count",
Amaury Denoyelle3b89c112021-03-12 16:04:00 +010062 "fail-check", "sudden-death", "mark-down",
Willy Tarreau49c2b452021-03-12 09:58:04 +010063 NULL /* must be last */
64};
65
Willy Tarreau21faa912012-10-10 08:27:36 +020066/* List head of all known server keywords */
Willy Tarreauca1acd62022-03-29 15:02:44 +020067struct srv_kw_list srv_keywords = {
Willy Tarreau21faa912012-10-10 08:27:36 +020068 .list = LIST_HEAD_INIT(srv_keywords.list)
69};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020070
Willy Tarreauaf613e82020-06-05 08:40:51 +020071__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010072struct eb_root idle_conn_srv = EB_ROOT;
Willy Tarreau14015b82021-04-10 17:33:15 +020073struct task *idle_conn_task __read_mostly = NULL;
Willy Tarreau198e92a2021-03-05 10:23:32 +010074struct list servers_list = LIST_HEAD_INIT(servers_list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010075
Aurelien DARRAGON61e38942022-11-17 16:10:35 +010076/* SERVER DELETE(n)->ADD global tracker:
77 * This is meant to provide srv->rid (revision id) value.
78 * Revision id allows to differentiate between a previously existing
79 * deleted server and a new server reusing deleted server name/id.
80 *
81 * start value is 0 (even value)
82 * LSB is used to specify that one or multiple srv delete in a row
83 * were performed.
84 * When adding a new server, increment by 1 if current
85 * value is odd (odd = LSB set),
86 * because adding a new server after one or
87 * multiple deletions means we could potentially be reusing old names:
88 * Increase the revision id to prevent mixups between old and new names.
89 *
90 * srv->rid is calculated from cnt even values only.
91 * sizeof(srv_id_reuse_cnt) must be twice sizeof(srv->rid)
92 *
93 * Wraparound is expected and should not cause issues
94 * (with current design we allow up to 4 billion unique revisions)
95 *
96 * Counter is only used under thread_isolate (cli_add/cli_del),
97 * no need for atomic ops.
98 */
99static uint64_t srv_id_reuse_cnt = 0;
100
Frédéric Lécaille7da71292019-05-20 09:47:07 +0200101/* The server names dictionary */
Thayne McCombs92149f92020-11-20 01:28:26 -0700102struct dict server_key_dict = {
103 .name = "server keys",
Frédéric Lécaille7da71292019-05-20 09:47:07 +0200104 .values = EB_ROOT_UNIQUE,
105};
106
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +0200107static const char *srv_adm_st_chg_cause_str[] = {
108 [SRV_ADM_STCHGC_NONE] = "",
109 [SRV_ADM_STCHGC_DNS_NOENT] = "entry removed from SRV record",
110 [SRV_ADM_STCHGC_DNS_NOIP] = "No IP for server ",
111 [SRV_ADM_STCHGC_DNS_NX] = "DNS NX status",
112 [SRV_ADM_STCHGC_DNS_TIMEOUT] = "DNS timeout status",
113 [SRV_ADM_STCHGC_DNS_REFUSED] = "DNS refused status",
114 [SRV_ADM_STCHGC_DNS_UNSPEC] = "unspecified DNS error",
115 [SRV_ADM_STCHGC_STATS_DISABLE] = "'disable' on stats page",
116 [SRV_ADM_STCHGC_STATS_STOP] = "'stop' on stats page"
117};
118
119const char *srv_adm_st_chg_cause(enum srv_adm_st_chg_cause cause)
120{
121 return srv_adm_st_chg_cause_str[cause];
122}
123
Aurelien DARRAGON1746b562023-04-04 10:17:40 +0200124static const char *srv_op_st_chg_cause_str[] = {
125 [SRV_OP_STCHGC_NONE] = "",
126 [SRV_OP_STCHGC_HEALTH] = "",
127 [SRV_OP_STCHGC_AGENT] = "",
128 [SRV_OP_STCHGC_CLI] = "changed from CLI",
129 [SRV_OP_STCHGC_LUA] = "changed from Lua script",
130 [SRV_OP_STCHGC_STATS_WEB] = "changed from Web interface",
131 [SRV_OP_STCHGC_STATEFILE] = "changed from server-state after a reload"
132};
133
134const char *srv_op_st_chg_cause(enum srv_op_st_chg_cause cause)
135{
136 return srv_op_st_chg_cause_str[cause];
137}
138
Simon Hormana3608442013-11-01 16:46:15 +0900139int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +0200140{
Willy Tarreau69530f52023-04-28 09:16:15 +0200141 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= ns_to_sec(now_ns)) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200142 return s->down_time;
143
Willy Tarreau69530f52023-04-28 09:16:15 +0200144 return ns_to_sec(now_ns) - s->last_change + s->down_time;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200145}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500147int srv_lastsession(const struct server *s)
148{
149 if (s->counters.last_sess)
Willy Tarreau69530f52023-04-28 09:16:15 +0200150 return ns_to_sec(now_ns) - s->counters.last_sess;
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500151
152 return -1;
153}
154
Simon Horman4a741432013-02-23 15:35:38 +0900155int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +0200156{
Simon Horman4a741432013-02-23 15:35:38 +0900157 const struct server *s = check->server;
158
Aurelien DARRAGON7d541a92022-12-07 12:17:24 +0100159 if ((check->state & (CHK_ST_CONFIGURED|CHK_ST_FASTINTER)) == CHK_ST_CONFIGURED &&
160 (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900161 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100162
Emeric Brun52a91d32017-08-31 14:41:55 +0200163 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900164 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100165
Simon Horman4a741432013-02-23 15:35:38 +0900166 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100167}
168
Aurelien DARRAGON23f352f2023-03-24 17:02:53 +0100169/* fill common server event data members struct
170 * must be called with server lock or under thread isolate
171 */
172static inline void _srv_event_hdl_prepare(struct event_hdl_cb_data_server *cb_data,
173 struct server *srv, uint8_t thread_isolate)
174{
175 /* safe data assignments */
176 cb_data->safe.puid = srv->puid;
177 cb_data->safe.rid = srv->rid;
178 cb_data->safe.flags = srv->flags;
179 snprintf(cb_data->safe.name, sizeof(cb_data->safe.name), "%s", srv->id);
180 cb_data->safe.proxy_name[0] = '\0';
181 cb_data->safe.proxy_uuid = -1; /* default value */
182 if (srv->proxy) {
183 cb_data->safe.proxy_uuid = srv->proxy->uuid;
184 snprintf(cb_data->safe.proxy_name, sizeof(cb_data->safe.proxy_name), "%s", srv->proxy->id);
185 }
186 /* unsafe data assignments */
187 cb_data->unsafe.ptr = srv;
188 cb_data->unsafe.thread_isolate = thread_isolate;
189 cb_data->unsafe.srv_lock = !thread_isolate;
190}
191
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +0200192/* take an event-check snapshot from a live check */
193void _srv_event_hdl_prepare_checkres(struct event_hdl_cb_data_server_checkres *checkres,
194 struct check *check)
195{
196 checkres->agent = !!(check->state & CHK_ST_AGENT);
197 checkres->result = check->result;
198 checkres->duration = check->duration;
199 checkres->reason.status = check->status;
200 checkres->reason.code = check->code;
201 checkres->health.cur = check->health;
202 checkres->health.rise = check->rise;
203 checkres->health.fall = check->fall;
204}
205
206/* Prepare SERVER_STATE event
207 *
208 * This special event will contain extra hints related to the state change
209 *
210 * Must be called with server lock held
211 */
212void _srv_event_hdl_prepare_state(struct event_hdl_cb_data_server_state *cb_data,
213 struct server *srv, int type, int cause,
214 enum srv_state prev_state, int requeued)
215{
216 /* state event provides additional info about the server state change */
217 cb_data->safe.type = type;
218 cb_data->safe.new_state = srv->cur_state;
219 cb_data->safe.old_state = prev_state;
220 cb_data->safe.requeued = requeued;
221 if (type) {
222 /* administrative */
223 cb_data->safe.adm_st_chg.cause = cause;
224 }
225 else {
226 /* operational */
227 cb_data->safe.op_st_chg.cause = cause;
228 if (cause == SRV_OP_STCHGC_HEALTH || cause == SRV_OP_STCHGC_AGENT) {
229 struct check *check = (cause == SRV_OP_STCHGC_HEALTH) ? &srv->check : &srv->agent;
230
231 /* provide additional check-related state change result */
232 _srv_event_hdl_prepare_checkres(&cb_data->safe.op_st_chg.check, check);
233 }
234 }
235}
236
Aurelien DARRAGON306a5fc2023-04-25 10:44:41 +0200237/* server event publishing helper: publish in both global and
238 * server dedicated subscription list.
239 */
240#define _srv_event_hdl_publish(e, d, s) \
241 ({ \
242 /* publish in server dedicated sub list */ \
243 event_hdl_publish(&s->e_subs, e, EVENT_HDL_CB_DATA(&d));\
244 /* publish in global subscription list */ \
245 event_hdl_publish(NULL, e, EVENT_HDL_CB_DATA(&d)); \
246 })
247
248/* General server event publishing:
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100249 * Use this to publish EVENT_HDL_SUB_SERVER family type event
Aurelien DARRAGON306a5fc2023-04-25 10:44:41 +0200250 * from srv facility.
251 *
252 * server ptr must be valid.
253 * Must be called with srv lock or under thread_isolate.
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100254 */
Aurelien DARRAGON23f352f2023-03-24 17:02:53 +0100255static void srv_event_hdl_publish(struct event_hdl_sub_type event,
256 struct server *srv, uint8_t thread_isolate)
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100257{
258 struct event_hdl_cb_data_server cb_data;
259
Aurelien DARRAGON23f352f2023-03-24 17:02:53 +0100260 /* prepare event data */
261 _srv_event_hdl_prepare(&cb_data, srv, thread_isolate);
Aurelien DARRAGON306a5fc2023-04-25 10:44:41 +0200262 _srv_event_hdl_publish(event, cb_data, srv);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100263}
264
Aurelien DARRAGONdcbc2d22023-03-30 10:19:08 +0200265/* Publish SERVER_CHECK event
266 *
267 * This special event will contain extra hints related to the check itself
268 *
269 * Must be called with server lock held
270 */
271void srv_event_hdl_publish_check(struct server *srv, struct check *check)
272{
273 struct event_hdl_cb_data_server_check cb_data;
274
275 /* check event provides additional info about the server check */
276 _srv_event_hdl_prepare_checkres(&cb_data.safe.res, check);
277
278 cb_data.unsafe.ptr = check;
279
280 /* prepare event data (common server data) */
281 _srv_event_hdl_prepare((struct event_hdl_cb_data_server *)&cb_data, srv, 0);
282
283 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_CHECK, cb_data, srv);
284}
285
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100286/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100287 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200288 * Unlikely, but it can happen. The server's proxy must be at least
289 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100290 */
291static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100292{
293 struct proxy *p = s->proxy;
294 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100295
296 for (tmpserv = p->srv; tmpserv != NULL;
297 tmpserv = tmpserv->next) {
298 if (tmpserv == s)
299 continue;
300 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
301 continue;
302 if (tmpserv->cookie &&
303 strcmp(tmpserv->cookie, s->cookie) == 0) {
304 ha_warning("We generated two equal cookies for two different servers.\n"
305 "Please change the secret key for '%s'.\n",
306 s->proxy->id);
307 }
308 }
309
310}
311
Willy Tarreau46b7f532018-08-21 11:54:26 +0200312/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200313 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200314 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100315void srv_set_dyncookie(struct server *s)
316{
317 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100318 char *tmpbuf;
319 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100320 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100321 size_t buffer_len;
322 int addr_len;
323 int port;
324
Willy Tarreau595e7672020-10-20 17:30:08 +0200325 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200326
Olivier Houchard4e694042017-03-14 20:01:29 +0100327 if ((s->flags & SRV_F_COOKIESET) ||
328 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
329 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200330 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100331 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100332
333 if (s->addr.ss_family != AF_INET &&
334 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200335 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100336 /*
337 * Buffer to calculate the cookie value.
338 * The buffer contains the secret key + the server IP address
339 * + the TCP port.
340 */
341 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
342 /*
343 * The TCP port should use only 2 bytes, but is stored in
344 * an unsigned int in struct server, so let's use 4, to be
345 * on the safe side.
346 */
347 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200348 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100349 memcpy(tmpbuf, p->dyncookie_key, key_len);
350 memcpy(&(tmpbuf[key_len]),
351 s->addr.ss_family == AF_INET ?
352 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
353 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
354 addr_len);
355 /*
356 * Make sure it's the same across all the load balancers,
357 * no matter their endianness.
358 */
359 port = htonl(s->svc_port);
360 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
361 hash_value = XXH64(tmpbuf, buffer_len, 0);
362 memprintf(&s->cookie, "%016llx", hash_value);
363 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200364 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100365 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100366
367 /* Don't bother checking if the dyncookie is duplicated if
368 * the server is marked as "disabled", maybe it doesn't have
369 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100370 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100371 if (!(s->next_admin & SRV_ADMF_FMAINT))
372 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200373 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200374 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100375}
376
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200377/* Returns true if it's possible to reuse an idle connection from server <srv>
378 * for a websocket stream. This is the case if server is configured to use the
379 * same protocol for both HTTP and websocket streams. This depends on the value
380 * of "proto", "alpn" and "ws" keywords.
381 */
382int srv_check_reuse_ws(struct server *srv)
383{
384 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
385 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
386 * for mux selection.
387 */
388 const struct ist srv_mux = srv->mux_proto ?
389 srv->mux_proto->token : IST_NULL;
390
391 switch (srv->ws) {
392 /* "auto" means use the same protocol : reuse is possible. */
393 case SRV_WS_AUTO:
394 return 1;
395
396 /* "h2" means use h2 for websocket : reuse is possible if
397 * server mux is h2.
398 */
399 case SRV_WS_H2:
400 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
401 return 1;
402 break;
403
404 /* "h1" means use h1 for websocket : reuse is possible if
405 * server mux is h1.
406 */
407 case SRV_WS_H1:
408 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
409 return 1;
410 break;
411 }
412 }
413 else {
414 /* ALPN selection.
415 * Based on the assumption that only "h2" and "http/1.1" token
416 * are used on server ALPN.
417 */
418 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
419 srv->ssl_ctx.alpn_len);
420
421 switch (srv->ws) {
422 case SRV_WS_AUTO:
423 /* for auto mode, consider reuse as possible if the
424 * server uses a single protocol ALPN
425 */
426 if (!istchr(alpn, ','))
427 return 1;
428 break;
429
430 case SRV_WS_H2:
431 return isteq(alpn, ist("\x02h2"));
432
433 case SRV_WS_H1:
434 return isteq(alpn, ist("\x08http/1.1"));
435 }
436 }
437
438 return 0;
439}
440
441/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
442 * is a valid value indicating to use the fallback mux.
443 */
444const struct mux_ops *srv_get_ws_proto(struct server *srv)
445{
446 const struct mux_proto_list *mux = NULL;
447
448 switch (srv->ws) {
449 case SRV_WS_AUTO:
450 mux = srv->mux_proto;
451 break;
452
453 case SRV_WS_H1:
454 mux = get_mux_proto(ist("h1"));
455 break;
456
457 case SRV_WS_H2:
458 mux = get_mux_proto(ist("h2"));
459 break;
460 }
461
462 return mux ? mux->mux : NULL;
463}
464
Willy Tarreau21faa912012-10-10 08:27:36 +0200465/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200466 * Must be called with the server lock held. The server is first removed from
467 * the proxy tree if it was already attached. If <reattach> is true, the server
468 * will then be attached in the proxy tree. The proxy lock is held to
469 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700470 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200471static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700472{
473 struct proxy *p = s->proxy;
474 char *key;
475
476 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
477
478 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700479 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700480 free(key);
481 return;
482 }
483
484 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
485 ebpt_delete(&s->addr_node);
486 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
487
488 free(s->addr_node.key);
489 }
490
491 s->addr_node.key = key;
492
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200493 if (reattach) {
494 if (s->addr_node.key) {
495 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
496 ebis_insert(&p->used_server_addr, &s->addr_node);
497 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
498 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700499 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700500}
501
502/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200503 * Registers the server keyword list <kwl> as a list of valid keywords for next
504 * parsing sessions.
505 */
506void srv_register_keywords(struct srv_kw_list *kwl)
507{
Willy Tarreau2b718102021-04-21 07:32:39 +0200508 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200509}
510
511/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
512 * keyword is found with a NULL ->parse() function, then an attempt is made to
513 * find one with a valid ->parse() function. This way it is possible to declare
514 * platform-dependant, known keywords as NULL, then only declare them as valid
515 * if some options are met. Note that if the requested keyword contains an
516 * opening parenthesis, everything from this point is ignored.
517 */
518struct srv_kw *srv_find_kw(const char *kw)
519{
520 int index;
521 const char *kwend;
522 struct srv_kw_list *kwl;
523 struct srv_kw *ret = NULL;
524
525 kwend = strchr(kw, '(');
526 if (!kwend)
527 kwend = kw + strlen(kw);
528
529 list_for_each_entry(kwl, &srv_keywords.list, list) {
530 for (index = 0; kwl->kw[index].kw != NULL; index++) {
531 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
532 kwl->kw[index].kw[kwend-kw] == 0) {
533 if (kwl->kw[index].parse)
534 return &kwl->kw[index]; /* found it !*/
535 else
536 ret = &kwl->kw[index]; /* may be OK */
537 }
538 }
539 }
540 return ret;
541}
542
543/* Dumps all registered "server" keywords to the <out> string pointer. The
544 * unsupported keywords are only dumped if their supported form was not
545 * found.
546 */
547void srv_dump_kws(char **out)
548{
549 struct srv_kw_list *kwl;
550 int index;
551
Christopher Faulet784063e2020-05-18 12:14:18 +0200552 if (!out)
553 return;
554
Willy Tarreau21faa912012-10-10 08:27:36 +0200555 *out = NULL;
556 list_for_each_entry(kwl, &srv_keywords.list, list) {
557 for (index = 0; kwl->kw[index].kw != NULL; index++) {
558 if (kwl->kw[index].parse ||
559 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
560 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
561 kwl->scope,
562 kwl->kw[index].kw,
563 kwl->kw[index].skip ? " <arg>" : "",
564 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
565 kwl->kw[index].parse ? "" : " (not supported)");
566 }
567 }
568 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100569}
570
571/* Try to find in srv_keyword the word that looks closest to <word> by counting
572 * transitions between letters, digits and other characters. Will return the
573 * best matching word if found, otherwise NULL. An optional array of extra
574 * words to compare may be passed in <extra>, but it must then be terminated
575 * by a NULL entry. If unused it may be NULL.
576 */
577static const char *srv_find_best_kw(const char *word)
578{
579 uint8_t word_sig[1024];
580 uint8_t list_sig[1024];
581 const struct srv_kw_list *kwl;
582 const char *best_ptr = NULL;
583 int dist, best_dist = INT_MAX;
584 const char **extra;
585 int index;
586
587 make_word_fingerprint(word_sig, word);
588 list_for_each_entry(kwl, &srv_keywords.list, list) {
589 for (index = 0; kwl->kw[index].kw != NULL; index++) {
590 make_word_fingerprint(list_sig, kwl->kw[index].kw);
591 dist = word_fingerprint_distance(word_sig, list_sig);
592 if (dist < best_dist) {
593 best_dist = dist;
594 best_ptr = kwl->kw[index].kw;
595 }
596 }
597 }
598
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100599 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100600 make_word_fingerprint(list_sig, *extra);
601 dist = word_fingerprint_distance(word_sig, list_sig);
602 if (dist < best_dist) {
603 best_dist = dist;
604 best_ptr = *extra;
605 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100606 }
607
608 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
609 best_ptr = NULL;
610
611 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200612}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100613
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100614/* Parse the "backup" server keyword */
615static int srv_parse_backup(char **args, int *cur_arg,
616 struct proxy *curproxy, struct server *newsrv, char **err)
617{
618 newsrv->flags |= SRV_F_BACKUP;
619 return 0;
620}
621
Alexander Liu2a54bb72019-05-22 19:44:48 +0800622
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100623/* Parse the "cookie" server keyword */
624static int srv_parse_cookie(char **args, int *cur_arg,
625 struct proxy *curproxy, struct server *newsrv, char **err)
626{
627 char *arg;
628
629 arg = args[*cur_arg + 1];
630 if (!*arg) {
631 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
632 return ERR_ALERT | ERR_FATAL;
633 }
634
635 free(newsrv->cookie);
636 newsrv->cookie = strdup(arg);
637 newsrv->cklen = strlen(arg);
638 newsrv->flags |= SRV_F_COOKIESET;
639 return 0;
640}
641
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100642/* Parse the "disabled" server keyword */
643static int srv_parse_disabled(char **args, int *cur_arg,
644 struct proxy *curproxy, struct server *newsrv, char **err)
645{
Emeric Brun52a91d32017-08-31 14:41:55 +0200646 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
647 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100648 newsrv->check.state |= CHK_ST_PAUSED;
649 newsrv->check.health = 0;
650 return 0;
651}
652
653/* Parse the "enabled" server keyword */
654static int srv_parse_enabled(char **args, int *cur_arg,
655 struct proxy *curproxy, struct server *newsrv, char **err)
656{
Amaury Denoyellee4308e92024-03-29 17:47:23 +0100657 if (newsrv->flags & SRV_F_DYNAMIC) {
658 ha_warning("Keyword 'enabled' is ignored for dynamic servers. It will be rejected from 3.0 onward.");
659 return 0;
660 }
661
Emeric Brun52a91d32017-08-31 14:41:55 +0200662 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
663 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100664 newsrv->check.state &= ~CHK_ST_PAUSED;
665 newsrv->check.health = newsrv->check.rise;
666 return 0;
667}
668
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100669/* Parse the "error-limit" server keyword */
670static int srv_parse_error_limit(char **args, int *cur_arg,
671 struct proxy *curproxy, struct server *newsrv, char **err)
672{
673 if (!*args[*cur_arg + 1]) {
674 memprintf(err, "'%s' expects an integer argument.",
675 args[*cur_arg]);
676 return ERR_ALERT | ERR_FATAL;
677 }
678
679 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
680
681 if (newsrv->consecutive_errors_limit <= 0) {
682 memprintf(err, "%s has to be > 0.",
683 args[*cur_arg]);
684 return ERR_ALERT | ERR_FATAL;
685 }
686
687 return 0;
688}
689
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200690/* Parse the "ws" keyword */
691static int srv_parse_ws(char **args, int *cur_arg,
692 struct proxy *curproxy, struct server *newsrv, char **err)
693{
694 if (!args[*cur_arg + 1]) {
695 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
696 return ERR_ALERT | ERR_FATAL;
697 }
698
699 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
700 newsrv->ws = SRV_WS_H1;
701 }
702 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
703 newsrv->ws = SRV_WS_H2;
704 }
705 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
706 newsrv->ws = SRV_WS_AUTO;
707 }
708 else {
709 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
710 return ERR_ALERT | ERR_FATAL;
711 }
712
713
714 return 0;
715}
716
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100717/* Parse the "init-addr" server keyword */
718static int srv_parse_init_addr(char **args, int *cur_arg,
719 struct proxy *curproxy, struct server *newsrv, char **err)
720{
721 char *p, *end;
722 int done;
723 struct sockaddr_storage sa;
724
725 newsrv->init_addr_methods = 0;
726 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
727
728 for (p = args[*cur_arg + 1]; *p; p = end) {
729 /* cut on next comma */
730 for (end = p; *end && *end != ','; end++);
731 if (*end)
732 *(end++) = 0;
733
734 memset(&sa, 0, sizeof(sa));
735 if (strcmp(p, "libc") == 0) {
736 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
737 }
738 else if (strcmp(p, "last") == 0) {
739 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
740 }
741 else if (strcmp(p, "none") == 0) {
742 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
743 }
744 else if (str2ip2(p, &sa, 0)) {
745 if (is_addr(&newsrv->init_addr)) {
746 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
747 args[*cur_arg], p);
748 return ERR_ALERT | ERR_FATAL;
749 }
750 newsrv->init_addr = sa;
751 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
752 }
753 else {
754 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
755 args[*cur_arg], p);
756 return ERR_ALERT | ERR_FATAL;
757 }
758 if (!done) {
759 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
760 args[*cur_arg], p);
761 return ERR_ALERT | ERR_FATAL;
762 }
763 }
764
765 return 0;
766}
767
768/* Parse the "log-proto" server keyword */
769static int srv_parse_log_proto(char **args, int *cur_arg,
770 struct proxy *curproxy, struct server *newsrv, char **err)
771{
772 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
773 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
774 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
775 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
776 else {
777 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
778 args[*cur_arg], args[*cur_arg + 1]);
779 return ERR_ALERT | ERR_FATAL;
780 }
781
782 return 0;
783}
784
785/* Parse the "maxconn" server keyword */
786static int srv_parse_maxconn(char **args, int *cur_arg,
787 struct proxy *curproxy, struct server *newsrv, char **err)
788{
789 newsrv->maxconn = atol(args[*cur_arg + 1]);
790 return 0;
791}
792
793/* Parse the "maxqueue" server keyword */
794static int srv_parse_maxqueue(char **args, int *cur_arg,
795 struct proxy *curproxy, struct server *newsrv, char **err)
796{
797 newsrv->maxqueue = atol(args[*cur_arg + 1]);
798 return 0;
799}
800
801/* Parse the "minconn" server keyword */
802static int srv_parse_minconn(char **args, int *cur_arg,
803 struct proxy *curproxy, struct server *newsrv, char **err)
804{
805 newsrv->minconn = atol(args[*cur_arg + 1]);
806 return 0;
807}
808
Willy Tarreau9c538e02019-01-23 10:21:49 +0100809static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
810{
811 char *arg;
812
813 arg = args[*cur_arg + 1];
814 if (!*arg) {
815 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
816 return ERR_ALERT | ERR_FATAL;
817 }
818 newsrv->max_reuse = atoi(arg);
819
820 return 0;
821}
822
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100823static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100824{
825 const char *res;
826 char *arg;
827 unsigned int time;
828
829 arg = args[*cur_arg + 1];
830 if (!*arg) {
831 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
832 return ERR_ALERT | ERR_FATAL;
833 }
834 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200835 if (res == PARSE_TIME_OVER) {
836 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
837 args[*cur_arg+1], args[*cur_arg]);
838 return ERR_ALERT | ERR_FATAL;
839 }
840 else if (res == PARSE_TIME_UNDER) {
841 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
842 args[*cur_arg+1], args[*cur_arg]);
843 return ERR_ALERT | ERR_FATAL;
844 }
845 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100846 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
847 *res, args[*cur_arg]);
848 return ERR_ALERT | ERR_FATAL;
849 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100850 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100851
852 return 0;
853}
854
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200855static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
856{
857 char *arg;
858
859 arg = args[*cur_arg + 1];
860 if (!*arg) {
861 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
862 return ERR_ALERT | ERR_FATAL;
863 }
864
865 newsrv->low_idle_conns = atoi(arg);
866 return 0;
867}
868
Olivier Houchard006e3102018-12-10 18:30:32 +0100869static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
870{
871 char *arg;
872
873 arg = args[*cur_arg + 1];
874 if (!*arg) {
875 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
876 return ERR_ALERT | ERR_FATAL;
877 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100878
Olivier Houchard006e3102018-12-10 18:30:32 +0100879 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100880 if ((int)newsrv->max_idle_conns < -1) {
881 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
882 return ERR_ALERT | ERR_FATAL;
883 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100884
885 return 0;
886}
887
Willy Tarreaudff55432012-10-10 17:51:05 +0200888/* parse the "id" server keyword */
889static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
890{
891 struct eb32_node *node;
892
893 if (!*args[*cur_arg + 1]) {
894 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
895 return ERR_ALERT | ERR_FATAL;
896 }
897
898 newsrv->puid = atol(args[*cur_arg + 1]);
899 newsrv->conf.id.key = newsrv->puid;
900
901 if (newsrv->puid <= 0) {
902 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
903 return ERR_ALERT | ERR_FATAL;
904 }
905
906 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
907 if (node) {
908 struct server *target = container_of(node, struct server, conf.id);
909 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
910 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
911 target->id);
912 return ERR_ALERT | ERR_FATAL;
913 }
914
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200915 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200916 return 0;
917}
918
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100919/* Parse the "namespace" server keyword */
920static int srv_parse_namespace(char **args, int *cur_arg,
921 struct proxy *curproxy, struct server *newsrv, char **err)
922{
Willy Tarreaue5733232019-05-22 19:24:06 +0200923#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100924 char *arg;
925
926 arg = args[*cur_arg + 1];
927 if (!*arg) {
928 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
929 return ERR_ALERT | ERR_FATAL;
930 }
931
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100932 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100933 /* Use the namespace associated with the connection (if present). */
934 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
935 return 0;
936 }
937
938 /*
939 * As this parser may be called several times for the same 'default-server'
940 * object, or for a new 'server' instance deriving from a 'default-server'
941 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
942 */
943 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
944
945 newsrv->netns = netns_store_lookup(arg, strlen(arg));
946 if (!newsrv->netns)
947 newsrv->netns = netns_store_insert(arg);
948
949 if (!newsrv->netns) {
950 memprintf(err, "Cannot open namespace '%s'", arg);
951 return ERR_ALERT | ERR_FATAL;
952 }
953
954 return 0;
955#else
956 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
957 return ERR_ALERT | ERR_FATAL;
958#endif
959}
960
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100961/* Parse the "no-backup" server keyword */
962static int srv_parse_no_backup(char **args, int *cur_arg,
963 struct proxy *curproxy, struct server *newsrv, char **err)
964{
965 newsrv->flags &= ~SRV_F_BACKUP;
966 return 0;
967}
968
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100969
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100970/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200971static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100972{
973 srv->pp_opts &= ~flags;
974 return 0;
975}
976
977/* Parse the "no-send-proxy" server keyword */
978static int srv_parse_no_send_proxy(char **args, int *cur_arg,
979 struct proxy *curproxy, struct server *newsrv, char **err)
980{
981 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
982}
983
984/* Parse the "no-send-proxy-v2" server keyword */
985static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
986 struct proxy *curproxy, struct server *newsrv, char **err)
987{
988 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
989}
990
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200991/* Parse the "shard" server keyword */
992static int srv_parse_shard(char **args, int *cur_arg,
993 struct proxy *curproxy, struct server *newsrv, char **err)
994{
995 newsrv->shard = atol(args[*cur_arg + 1]);
996 return 0;
997}
998
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200999/* Parse the "no-tfo" server keyword */
1000static int srv_parse_no_tfo(char **args, int *cur_arg,
1001 struct proxy *curproxy, struct server *newsrv, char **err)
1002{
1003 newsrv->flags &= ~SRV_F_FASTOPEN;
1004 return 0;
1005}
1006
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001007/* Parse the "non-stick" server keyword */
1008static int srv_parse_non_stick(char **args, int *cur_arg,
1009 struct proxy *curproxy, struct server *newsrv, char **err)
1010{
1011 newsrv->flags |= SRV_F_NON_STICK;
1012 return 0;
1013}
1014
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001015/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +02001016static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001017{
1018 srv->pp_opts |= flags;
1019 return 0;
1020}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001021/* parse the "proto" server keyword */
1022static int srv_parse_proto(char **args, int *cur_arg,
1023 struct proxy *px, struct server *newsrv, char **err)
1024{
1025 struct ist proto;
1026
1027 if (!*args[*cur_arg + 1]) {
1028 memprintf(err, "'%s' : missing value", args[*cur_arg]);
1029 return ERR_ALERT | ERR_FATAL;
1030 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001031 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001032 newsrv->mux_proto = get_mux_proto(proto);
1033 if (!newsrv->mux_proto) {
1034 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
1035 return ERR_ALERT | ERR_FATAL;
1036 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001037 return 0;
1038}
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001039
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001040/* parse the "proxy-v2-options" */
1041static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
1042 struct proxy *px, struct server *newsrv, char **err)
1043{
1044 char *p, *n;
1045 for (p = args[*cur_arg+1]; p; p = n) {
1046 n = strchr(p, ',');
1047 if (n)
1048 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001049 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001050 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001051 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001052 newsrv->pp_opts |= SRV_PP_V2_SSL;
1053 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001054 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001055 newsrv->pp_opts |= SRV_PP_V2_SSL;
1056 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001057 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001058 newsrv->pp_opts |= SRV_PP_V2_SSL;
1059 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001060 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001061 newsrv->pp_opts |= SRV_PP_V2_SSL;
1062 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001063 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001064 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001065 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001066 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001067 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001068 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001069 } else
1070 goto fail;
1071 }
1072 return 0;
1073 fail:
1074 if (err)
1075 memprintf(err, "'%s' : proxy v2 option not implemented", p);
1076 return ERR_ALERT | ERR_FATAL;
1077}
1078
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001079/* Parse the "observe" server keyword */
1080static int srv_parse_observe(char **args, int *cur_arg,
1081 struct proxy *curproxy, struct server *newsrv, char **err)
1082{
1083 char *arg;
1084
1085 arg = args[*cur_arg + 1];
1086 if (!*arg) {
1087 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
1088 return ERR_ALERT | ERR_FATAL;
1089 }
1090
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001091 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001092 newsrv->observe = HANA_OBS_NONE;
1093 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001094 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001095 newsrv->observe = HANA_OBS_LAYER4;
1096 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001097 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001098 if (curproxy->mode != PR_MODE_HTTP) {
1099 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
1100 return ERR_ALERT;
1101 }
1102 newsrv->observe = HANA_OBS_LAYER7;
1103 }
1104 else {
1105 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
1106 "but got '%s'\n", args[*cur_arg], arg);
1107 return ERR_ALERT | ERR_FATAL;
1108 }
1109
1110 return 0;
1111}
1112
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001113/* Parse the "on-error" server keyword */
1114static int srv_parse_on_error(char **args, int *cur_arg,
1115 struct proxy *curproxy, struct server *newsrv, char **err)
1116{
1117 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
1118 newsrv->onerror = HANA_ONERR_FASTINTER;
1119 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
1120 newsrv->onerror = HANA_ONERR_FAILCHK;
1121 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
1122 newsrv->onerror = HANA_ONERR_SUDDTH;
1123 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
1124 newsrv->onerror = HANA_ONERR_MARKDWN;
1125 else {
1126 memprintf(err, "'%s' expects one of 'fastinter', "
1127 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1128 args[*cur_arg], args[*cur_arg + 1]);
1129 return ERR_ALERT | ERR_FATAL;
1130 }
1131
1132 return 0;
1133}
1134
1135/* Parse the "on-marked-down" server keyword */
1136static int srv_parse_on_marked_down(char **args, int *cur_arg,
1137 struct proxy *curproxy, struct server *newsrv, char **err)
1138{
1139 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1140 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1141 else {
1142 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1143 args[*cur_arg], args[*cur_arg + 1]);
1144 return ERR_ALERT | ERR_FATAL;
1145 }
1146
1147 return 0;
1148}
1149
1150/* Parse the "on-marked-up" server keyword */
1151static int srv_parse_on_marked_up(char **args, int *cur_arg,
1152 struct proxy *curproxy, struct server *newsrv, char **err)
1153{
1154 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1155 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1156 else {
1157 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1158 args[*cur_arg], args[*cur_arg + 1]);
1159 return ERR_ALERT | ERR_FATAL;
1160 }
1161
1162 return 0;
1163}
1164
Frédéric Lécaille16186232017-03-14 16:42:49 +01001165/* Parse the "redir" server keyword */
1166static int srv_parse_redir(char **args, int *cur_arg,
1167 struct proxy *curproxy, struct server *newsrv, char **err)
1168{
1169 char *arg;
1170
1171 arg = args[*cur_arg + 1];
1172 if (!*arg) {
1173 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1174 return ERR_ALERT | ERR_FATAL;
1175 }
1176
1177 free(newsrv->rdr_pfx);
1178 newsrv->rdr_pfx = strdup(arg);
1179 newsrv->rdr_len = strlen(arg);
1180
1181 return 0;
1182}
1183
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001184/* Parse the "resolvers" server keyword */
1185static int srv_parse_resolvers(char **args, int *cur_arg,
1186 struct proxy *curproxy, struct server *newsrv, char **err)
1187{
1188 free(newsrv->resolvers_id);
1189 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1190 return 0;
1191}
1192
1193/* Parse the "resolve-net" server keyword */
1194static int srv_parse_resolve_net(char **args, int *cur_arg,
1195 struct proxy *curproxy, struct server *newsrv, char **err)
1196{
1197 char *p, *e;
1198 unsigned char mask;
1199 struct resolv_options *opt;
1200
1201 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1202 memprintf(err, "'%s' expects a list of networks.",
1203 args[*cur_arg]);
1204 return ERR_ALERT | ERR_FATAL;
1205 }
1206
1207 opt = &newsrv->resolv_opts;
1208
1209 /* Split arguments by comma, and convert it from ipv4 or ipv6
1210 * string network in in_addr or in6_addr.
1211 */
1212 p = args[*cur_arg + 1];
1213 e = p;
1214 while (*p != '\0') {
1215 /* If no room available, return error. */
1216 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1217 memprintf(err, "'%s' exceed %d networks.",
1218 args[*cur_arg], SRV_MAX_PREF_NET);
1219 return ERR_ALERT | ERR_FATAL;
1220 }
1221 /* look for end or comma. */
1222 while (*e != ',' && *e != '\0')
1223 e++;
1224 if (*e == ',') {
1225 *e = '\0';
1226 e++;
1227 }
1228 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1229 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1230 /* Try to convert input string from ipv4 or ipv6 network. */
1231 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1232 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1233 &mask)) {
1234 /* Try to convert input string from ipv6 network. */
1235 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1236 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1237 } else {
1238 /* All network conversions fail, return error. */
1239 memprintf(err, "'%s' invalid network '%s'.",
1240 args[*cur_arg], p);
1241 return ERR_ALERT | ERR_FATAL;
1242 }
1243 opt->pref_net_nb++;
1244 p = e;
1245 }
1246
1247 return 0;
1248}
1249
1250/* Parse the "resolve-opts" server keyword */
1251static int srv_parse_resolve_opts(char **args, int *cur_arg,
1252 struct proxy *curproxy, struct server *newsrv, char **err)
1253{
1254 char *p, *end;
1255
1256 for (p = args[*cur_arg + 1]; *p; p = end) {
1257 /* cut on next comma */
1258 for (end = p; *end && *end != ','; end++);
1259 if (*end)
1260 *(end++) = 0;
1261
1262 if (strcmp(p, "allow-dup-ip") == 0) {
1263 newsrv->resolv_opts.accept_duplicate_ip = 1;
1264 }
1265 else if (strcmp(p, "ignore-weight") == 0) {
1266 newsrv->resolv_opts.ignore_weight = 1;
1267 }
1268 else if (strcmp(p, "prevent-dup-ip") == 0) {
1269 newsrv->resolv_opts.accept_duplicate_ip = 0;
1270 }
1271 else {
1272 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1273 args[*cur_arg], p);
1274 return ERR_ALERT | ERR_FATAL;
1275 }
1276 }
1277
1278 return 0;
1279}
1280
1281/* Parse the "resolve-prefer" server keyword */
1282static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1283 struct proxy *curproxy, struct server *newsrv, char **err)
1284{
1285 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1286 newsrv->resolv_opts.family_prio = AF_INET;
1287 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1288 newsrv->resolv_opts.family_prio = AF_INET6;
1289 else {
1290 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1291 args[*cur_arg]);
1292 return ERR_ALERT | ERR_FATAL;
1293 }
1294
1295 return 0;
1296}
1297
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001298/* Parse the "send-proxy" server keyword */
1299static int srv_parse_send_proxy(char **args, int *cur_arg,
1300 struct proxy *curproxy, struct server *newsrv, char **err)
1301{
1302 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1303}
1304
1305/* Parse the "send-proxy-v2" server keyword */
1306static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1307 struct proxy *curproxy, struct server *newsrv, char **err)
1308{
1309 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1310}
1311
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001312/* Parse the "slowstart" server keyword */
1313static int srv_parse_slowstart(char **args, int *cur_arg,
1314 struct proxy *curproxy, struct server *newsrv, char **err)
1315{
1316 /* slowstart is stored in seconds */
1317 unsigned int val;
1318 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1319
1320 if (time_err == PARSE_TIME_OVER) {
1321 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1322 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1323 return ERR_ALERT | ERR_FATAL;
1324 }
1325 else if (time_err == PARSE_TIME_UNDER) {
1326 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1327 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1328 return ERR_ALERT | ERR_FATAL;
1329 }
1330 else if (time_err) {
1331 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1332 *time_err, newsrv->id);
1333 return ERR_ALERT | ERR_FATAL;
1334 }
1335 newsrv->slowstart = (val + 999) / 1000;
1336
1337 return 0;
1338}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001339
1340/* Parse the "source" server keyword */
1341static int srv_parse_source(char **args, int *cur_arg,
1342 struct proxy *curproxy, struct server *newsrv, char **err)
1343{
1344 char *errmsg;
1345 int port_low, port_high;
1346 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001347
1348 errmsg = NULL;
1349
1350 if (!*args[*cur_arg + 1]) {
1351 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1352 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1353 goto err;
1354 }
1355
1356 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001357 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1358 &errmsg, NULL, NULL,
1359 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001360 if (!sk) {
1361 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1362 goto err;
1363 }
1364
Frédéric Lécailledba97072017-03-17 15:33:50 +01001365 newsrv->conn_src.opts |= CO_SRC_BIND;
1366 newsrv->conn_src.source_addr = *sk;
1367
1368 if (port_low != port_high) {
1369 int i;
1370
Frédéric Lécailledba97072017-03-17 15:33:50 +01001371 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001372 if (!newsrv->conn_src.sport_range) {
1373 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1374 goto err;
1375 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001376 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1377 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1378 }
1379
1380 *cur_arg += 2;
1381 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001382 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001383#if defined(CONFIG_HAP_TRANSPARENT)
1384 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001385 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1386 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001387 goto err;
1388 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001389 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001390 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1391 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1392 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001393 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001394 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1395 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1396 }
1397 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1398 char *name, *end;
1399
1400 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001401 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001402 name++;
1403
1404 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001405 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001406 end++;
1407
1408 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1409 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1410 free(newsrv->conn_src.bind_hdr_name);
1411 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001412 if (!newsrv->conn_src.bind_hdr_name) {
1413 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1414 goto err;
1415 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001416 newsrv->conn_src.bind_hdr_len = end - name;
1417 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1418 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1419 newsrv->conn_src.bind_hdr_occ = -1;
1420
1421 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001422 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001423 end++;
1424 if (*end == ',') {
1425 end++;
1426 name = end;
1427 if (*end == '-')
1428 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001429 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001430 end++;
1431 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1432 }
1433
1434 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001435 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1436 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001437 goto err;
1438 }
1439 }
1440 else {
1441 struct sockaddr_storage *sk;
1442 int port1, port2;
1443
1444 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001445 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1446 &errmsg, NULL, NULL,
1447 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001448 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001449 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001450 goto err;
1451 }
1452
Frédéric Lécailledba97072017-03-17 15:33:50 +01001453 newsrv->conn_src.tproxy_addr = *sk;
1454 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1455 }
1456 global.last_checks |= LSTCHK_NETADM;
1457 *cur_arg += 2;
1458 continue;
1459#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001460 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001461 goto err;
1462#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1463 } /* "usesrc" */
1464
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001465 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001466#ifdef SO_BINDTODEVICE
1467 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001468 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001469 goto err;
1470 }
1471 free(newsrv->conn_src.iface_name);
1472 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1473 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1474 global.last_checks |= LSTCHK_NETADM;
1475#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001476 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001477 goto err;
1478#endif
1479 *cur_arg += 2;
1480 continue;
1481 }
1482 /* this keyword in not an option of "source" */
1483 break;
1484 } /* while */
1485
1486 return 0;
1487
1488 err:
1489 free(errmsg);
1490 return ERR_ALERT | ERR_FATAL;
1491}
1492
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001493/* Parse the "stick" server keyword */
1494static int srv_parse_stick(char **args, int *cur_arg,
1495 struct proxy *curproxy, struct server *newsrv, char **err)
1496{
1497 newsrv->flags &= ~SRV_F_NON_STICK;
1498 return 0;
1499}
1500
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001501/* Parse the "track" server keyword */
1502static int srv_parse_track(char **args, int *cur_arg,
1503 struct proxy *curproxy, struct server *newsrv, char **err)
1504{
1505 char *arg;
1506
1507 arg = args[*cur_arg + 1];
1508 if (!*arg) {
1509 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1510 return ERR_ALERT | ERR_FATAL;
1511 }
1512
1513 free(newsrv->trackit);
1514 newsrv->trackit = strdup(arg);
1515
1516 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001517}
1518
1519/* Parse the "socks4" server keyword */
1520static int srv_parse_socks4(char **args, int *cur_arg,
1521 struct proxy *curproxy, struct server *newsrv, char **err)
1522{
1523 char *errmsg;
1524 int port_low, port_high;
1525 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001526
1527 errmsg = NULL;
1528
1529 if (!*args[*cur_arg + 1]) {
1530 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1531 goto err;
1532 }
1533
1534 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001535 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1536 &errmsg, NULL, NULL,
1537 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001538 if (!sk) {
1539 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1540 goto err;
1541 }
1542
Alexander Liu2a54bb72019-05-22 19:44:48 +08001543 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1544 newsrv->socks4_addr = *sk;
1545
Alexander Liu2a54bb72019-05-22 19:44:48 +08001546 return 0;
1547
1548 err:
1549 free(errmsg);
1550 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001551}
1552
Frédéric Lécailledba97072017-03-17 15:33:50 +01001553
Willy Tarreau034c88c2017-01-23 23:36:45 +01001554/* parse the "tfo" server keyword */
1555static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1556{
1557 newsrv->flags |= SRV_F_FASTOPEN;
1558 return 0;
1559}
1560
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001561/* parse the "usesrc" server keyword */
1562static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1563{
1564 memprintf(err, "'%s' only allowed after a '%s' statement.",
1565 "usesrc", "source");
1566 return ERR_ALERT | ERR_FATAL;
1567}
1568
1569/* parse the "weight" server keyword */
1570static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1571{
1572 int w;
1573
1574 w = atol(args[*cur_arg + 1]);
1575 if (w < 0 || w > SRV_UWGHT_MAX) {
1576 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1577 newsrv->id, SRV_UWGHT_MAX, w);
1578 return ERR_ALERT | ERR_FATAL;
1579 }
1580 newsrv->uweight = newsrv->iweight = w;
1581
1582 return 0;
1583}
1584
Aurelien DARRAGON4bc09092023-09-21 14:18:50 +02001585/* Returns 1 if the server has streams pointing to it, and 0 otherwise.
1586 *
1587 * Must be called with the server lock held.
1588 */
1589static int srv_has_streams(struct server *srv)
1590{
1591 int thr;
1592
1593 for (thr = 0; thr < global.nbthread; thr++)
1594 if (!MT_LIST_ISEMPTY(&srv->per_thr[thr].streams))
1595 return 1;
1596 return 0;
1597}
1598
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001599/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001600 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001601 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001602 *
1603 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001604 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001605void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001606{
Willy Tarreau751153e2021-02-17 13:33:24 +01001607 struct stream *stream;
1608 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001609 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001610
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001611 for (thr = 0; thr < global.nbthread; thr++)
1612 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1613 if (stream->srv_conn == srv)
1614 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001615}
1616
1617/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001618 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001619 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001620 *
1621 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001622 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001623void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001624{
1625 struct server *srv;
1626
1627 for (srv = px->srv; srv != NULL; srv = srv->next)
1628 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001629 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001630}
1631
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001632static void srv_append_op_chg_cause(struct buffer *msg, struct server *s, enum srv_op_st_chg_cause cause)
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001633{
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001634 switch (cause) {
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001635 case SRV_OP_STCHGC_NONE:
1636 break; /* do nothing */
1637 case SRV_OP_STCHGC_HEALTH:
1638 check_append_info(msg, &s->check);
1639 break;
1640 case SRV_OP_STCHGC_AGENT:
1641 check_append_info(msg, &s->agent);
1642 break;
1643 default:
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001644 chunk_appendf(msg, ", %s", srv_op_st_chg_cause(cause));
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001645 break;
1646 }
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001647}
1648
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001649static void srv_append_adm_chg_cause(struct buffer *msg, struct server *s, enum srv_adm_st_chg_cause cause)
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001650{
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001651 if (cause)
1652 chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(cause));
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001653}
1654
1655/* Appends some information to a message string related to a server tracking
1656 * or requeued connections info.
1657 *
1658 * If <forced> is null and the server tracks another one, a "via"
Emeric Brun5a133512017-10-19 14:42:30 +02001659 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001660 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001661 *
1662 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001663 */
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001664static void srv_append_more(struct buffer *msg, struct server *s,
1665 int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001666{
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001667 if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001668 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001669 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001670
1671 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001672 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001673 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1674 " %d sessions active, %d requeued, %d remaining in queue",
1675 s->proxy->srv_act, s->proxy->srv_bck,
1676 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001677 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001678 else
1679 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1680 " %d sessions requeued, %d total in queue",
1681 s->proxy->srv_act, s->proxy->srv_bck,
1682 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001683 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001684 }
1685}
1686
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001687/* Marks server <s> down, regardless of its checks' statuses. The server
1688 * transfers queued streams whenever possible to other servers at a sync
1689 * point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001690 *
1691 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001692 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001693void srv_set_stopped(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001694{
1695 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001696
Emeric Brun64cc49c2017-10-03 14:46:45 +02001697 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001698 return;
1699
Emeric Brun52a91d32017-08-31 14:41:55 +02001700 s->next_state = SRV_ST_STOPPED;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001701
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001702 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001703 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001704
Emeric Brun9f0b4582017-10-23 14:39:51 +02001705 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001706 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001707 srv_set_stopped(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001708 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001709 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001710}
1711
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001712/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001713 * in maintenance. The server tries to grab requests from the proxy at a sync
1714 * point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001715 *
1716 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001717 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001718void srv_set_running(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001719{
1720 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001721
Emeric Brun64cc49c2017-10-03 14:46:45 +02001722 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001723 return;
1724
Emeric Brun52a91d32017-08-31 14:41:55 +02001725 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001726 return;
1727
Emeric Brun52a91d32017-08-31 14:41:55 +02001728 s->next_state = SRV_ST_STARTING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001729
Emeric Brun64cc49c2017-10-03 14:46:45 +02001730 if (s->slowstart <= 0)
1731 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001732
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001733 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001734 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001735
Emeric Brun9f0b4582017-10-23 14:39:51 +02001736 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001737 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001738 srv_set_running(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001739 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001740 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001741}
1742
Willy Tarreau8eb77842014-05-21 13:54:57 +02001743/* Marks server <s> stopping regardless of its checks' statuses and provided it
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001744 * isn't in maintenance. The server tries to redispatch pending requests
1745 * to the proxy. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001746 *
1747 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001748 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001749void srv_set_stopping(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001750{
1751 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001752
Emeric Brun64cc49c2017-10-03 14:46:45 +02001753 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001754 return;
1755
Emeric Brun52a91d32017-08-31 14:41:55 +02001756 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001757 return;
1758
Emeric Brun52a91d32017-08-31 14:41:55 +02001759 s->next_state = SRV_ST_STOPPING;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001760
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001761 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001762 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001763
Emeric Brun9f0b4582017-10-23 14:39:51 +02001764 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001765 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001766 srv_set_stopping(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001767 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001768 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001769}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001770
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001771/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1772 * enforce either maint mode or drain mode. It is not allowed to set more than
1773 * one flag at once. The equivalent "inherited" flag is propagated to all
1774 * tracking servers. Maintenance mode disables health checks (but not agent
1775 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001776 * is done. If <cause> is non-null, it will be displayed at the end of the log
1777 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001778 *
1779 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001780 */
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001781void srv_set_admin_flag(struct server *s, enum srv_admin mode, enum srv_adm_st_chg_cause cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001782{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001783 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001784
1785 if (!mode)
1786 return;
1787
1788 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001789 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001790 return;
1791
Emeric Brun52a91d32017-08-31 14:41:55 +02001792 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001793
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001794 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001795 srv_update_status(s, 1, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001796
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001797 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001798 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1799 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001800 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001801
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001802 /* compute the inherited flag to propagate */
1803 if (mode & SRV_ADMF_MAINT)
1804 mode = SRV_ADMF_IMAINT;
1805 else if (mode & SRV_ADMF_DRAIN)
1806 mode = SRV_ADMF_IDRAIN;
1807
Emeric Brun9f0b4582017-10-23 14:39:51 +02001808 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001809 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001810 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001811 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001812 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001813}
1814
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001815/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1816 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1817 * than one flag at once. The equivalent "inherited" flag is propagated to all
1818 * tracking servers. Leaving maintenance mode re-enables health checks. When
1819 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001820 *
1821 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001822 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001823void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001824{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001825 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001826
1827 if (!mode)
1828 return;
1829
1830 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001831 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001832 return;
1833
Emeric Brun52a91d32017-08-31 14:41:55 +02001834 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001835
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001836 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001837 srv_update_status(s, 1, SRV_ADM_STCHGC_NONE);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001838
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001839 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001840 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1841 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001842 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001843
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001844 if (mode & SRV_ADMF_MAINT)
1845 mode = SRV_ADMF_IMAINT;
1846 else if (mode & SRV_ADMF_DRAIN)
1847 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001848
Emeric Brun9f0b4582017-10-23 14:39:51 +02001849 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001850 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001851 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001852 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001853 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001854}
1855
Willy Tarreau757478e2016-11-03 19:22:19 +01001856/* principle: propagate maint and drain to tracking servers. This is useful
1857 * upon startup so that inherited states are correct.
1858 */
1859static void srv_propagate_admin_state(struct server *srv)
1860{
1861 struct server *srv2;
1862
1863 if (!srv->trackers)
1864 return;
1865
1866 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001867 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001868 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001869 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau757478e2016-11-03 19:22:19 +01001870
Emeric Brun52a91d32017-08-31 14:41:55 +02001871 if (srv->next_admin & SRV_ADMF_DRAIN)
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001872 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001873 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001874 }
1875}
1876
1877/* Compute and propagate the admin states for all servers in proxy <px>.
1878 * Only servers *not* tracking another one are considered, because other
1879 * ones will be handled when the server they track is visited.
1880 */
1881void srv_compute_all_admin_states(struct proxy *px)
1882{
1883 struct server *srv;
1884
1885 for (srv = px->srv; srv; srv = srv->next) {
1886 if (srv->track)
1887 continue;
1888 srv_propagate_admin_state(srv);
1889 }
1890}
1891
Willy Tarreaudff55432012-10-10 17:51:05 +02001892/* Note: must not be declared <const> as its list will be overwritten.
1893 * Please take care of keeping this list alphabetically sorted, doing so helps
1894 * all code contributors.
1895 * Optional keywords are also declared with a NULL ->parse() function so that
1896 * the config parser can report an appropriate error when a known keyword was
1897 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001898 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001899 */
1900static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001901 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelleae8882d2024-03-27 10:50:21 +01001902 { "cookie", srv_parse_cookie, 1, 1, 1 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001903 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1904 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001905 { "error-limit", srv_parse_error_limit, 1, 1, 1 }, /* Configure the consecutive count of check failures to consider a server on error */
Amaury Denoyellef9d59572021-10-18 14:40:29 +02001906 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001907 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001908 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1909 { "log-proto", srv_parse_log_proto, 1, 1, 0 }, /* Set the protocol for event messages, only relevant in a ring section */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001910 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1911 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001912 { "max-reuse", srv_parse_max_reuse, 1, 1, 0 }, /* Set the max number of requests on a connection, -1 means unlimited */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001913 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001914 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001915 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1916 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1917 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1918 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001919 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001920 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1921 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1922 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1923 { "on-marked-up", srv_parse_on_marked_up, 1, 1, 1 }, /* Configure the action when a server is marked up */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001924 { "pool-low-conn", srv_parse_pool_low_conn, 1, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
1925 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1926 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Amaury Denoyelle30467232021-03-12 18:03:27 +01001927 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001928 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001929 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001930 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001931 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001932 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001933 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001934 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1935 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécaille36d15652022-10-17 14:58:19 +02001936 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001937 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001938 { "source", srv_parse_source, -1, 1, 1 }, /* Set the source address to be used to connect to the server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001939 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001940 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001941 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001942 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001943 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1944 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001945 { NULL, NULL, 0 },
1946}};
1947
Willy Tarreau0108d902018-11-25 19:14:37 +01001948INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001949
Willy Tarreau004e0452013-11-21 11:22:01 +01001950/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001951 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001952 * state is automatically disabled if the time is elapsed. If <must_update> is
1953 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001954 *
1955 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001956 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001957void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001958{
1959 struct proxy *px = sv->proxy;
1960 unsigned w;
1961
Willy Tarreau69530f52023-04-28 09:16:15 +02001962 if (ns_to_sec(now_ns) < sv->last_change || ns_to_sec(now_ns) >= sv->last_change + sv->slowstart) {
Damien Claisse2c2699e2024-04-09 15:37:07 +00001963 /* go to full throttle if the slowstart interval is reached unless server is currently down */
1964 if ((sv->cur_state != SRV_ST_STOPPED) && (sv->next_state == SRV_ST_STARTING))
Emeric Brun52a91d32017-08-31 14:41:55 +02001965 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001966 }
1967
1968 /* We must take care of not pushing the server to full throttle during slow starts.
1969 * It must also start immediately, at least at the minimal step when leaving maintenance.
1970 */
Damien Claisse2c2699e2024-04-09 15:37:07 +00001971 if ((sv->cur_state == SRV_ST_STOPPED) && (sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
1972 w = 1;
1973 else if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau69530f52023-04-28 09:16:15 +02001974 w = (px->lbprm.wdiv * (ns_to_sec(now_ns) - sv->last_change) + sv->slowstart) / sv->slowstart;
Willy Tarreau004e0452013-11-21 11:22:01 +01001975 else
1976 w = px->lbprm.wdiv;
1977
Emeric Brun52a91d32017-08-31 14:41:55 +02001978 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001979
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001980 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001981 if (must_update)
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001982 srv_update_status(sv, 0, SRV_OP_STCHGC_NONE);
Willy Tarreau004e0452013-11-21 11:22:01 +01001983}
1984
Willy Tarreaubaaee002006-06-26 02:48:02 +02001985/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001986 * Parses weight_str and configures sv accordingly.
1987 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001988 *
1989 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001990 */
1991const char *server_parse_weight_change_request(struct server *sv,
1992 const char *weight_str)
1993{
1994 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001995 long int w;
1996 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001997
1998 px = sv->proxy;
1999
2000 /* if the weight is terminated with '%', it is set relative to
2001 * the initial weight, otherwise it is absolute.
2002 */
2003 if (!*weight_str)
2004 return "Require <weight> or <weight%>.\n";
2005
Simon Hormanb796afa2013-02-12 10:45:53 +09002006 w = strtol(weight_str, &end, 10);
2007 if (end == weight_str)
2008 return "Empty weight string empty or preceded by garbage";
2009 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09002010 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09002011 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09002012 /* Avoid integer overflow */
2013 if (w > 25600)
2014 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09002015 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09002016 if (w > 256)
2017 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09002018 }
2019 else if (w < 0 || w > 256)
2020 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09002021 else if (end[0] != '\0')
2022 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09002023
2024 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
2025 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
2026
2027 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002028 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09002029
2030 return NULL;
2031}
2032
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002033/*
Thierry Fournier09a91782016-02-24 08:25:39 +01002034 * Parses <addr_str> and configures <sv> accordingly. <from> precise
2035 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002036 * Returns:
2037 * - error string on error
2038 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02002039 *
2040 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002041 */
2042const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01002043 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002044{
2045 unsigned char ip[INET6_ADDRSTRLEN];
2046
2047 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01002048 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002049 return NULL;
2050 }
2051 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01002052 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002053 return NULL;
2054 }
2055
2056 return "Could not understand IP address format.\n";
2057}
2058
Willy Tarreau46b7f532018-08-21 11:54:26 +02002059/*
2060 * Must be called with the server lock held.
2061 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002062const char *server_parse_maxconn_change_request(struct server *sv,
2063 const char *maxconn_str)
2064{
2065 long int v;
2066 char *end;
2067
2068 if (!*maxconn_str)
2069 return "Require <maxconn>.\n";
2070
2071 v = strtol(maxconn_str, &end, 10);
2072 if (end == maxconn_str)
2073 return "maxconn string empty or preceded by garbage";
2074 else if (end[0] != '\0')
2075 return "Trailing garbage in maxconn string";
2076
2077 if (sv->maxconn == sv->minconn) { // static maxconn
2078 sv->maxconn = sv->minconn = v;
2079 } else { // dynamic maxconn
2080 sv->maxconn = v;
2081 }
2082
2083 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002084 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002085
2086 return NULL;
2087}
2088
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002089static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
2090 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002091{
2092 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002093 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002094 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002095 NULL,
2096 };
2097
2098 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002099 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002100
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002101 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002102}
2103
William Lallemand0d058672022-03-16 15:44:42 +01002104int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002105{
2106 struct sample_expr *expr;
2107
2108 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002109 if (!expr) {
2110 memprintf(err, "error detected while parsing sni expression : %s", *err);
2111 return ERR_ALERT | ERR_FATAL;
2112 }
2113
2114 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2115 memprintf(err, "error detected while parsing sni expression : "
2116 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002117 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002118 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002119 return ERR_ALERT | ERR_FATAL;
2120 }
2121
2122 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2123 release_sample_expr(newsrv->ssl_ctx.sni);
2124 newsrv->ssl_ctx.sni = expr;
2125
2126 return 0;
2127}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002128
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002129static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002130{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002131 char *msg = "error encountered while processing ";
2132 char *quote = "'";
2133 char *token = args[cur_arg];
2134
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002135 if (err && *err) {
2136 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002137 msg = *err;
2138 quote = "";
2139 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002140 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002141
2142 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002143 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002144 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002145 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002146}
2147
Christopher Faulet0b365e32022-08-03 11:21:14 +02002148static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002149{
2150 int range_sz;
2151
2152 range_sz = src->conn_src.sport_range->size;
2153 if (range_sz > 0) {
2154 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2155 if (srv->conn_src.sport_range != NULL) {
2156 int i;
2157
2158 for (i = 0; i < range_sz; i++) {
2159 srv->conn_src.sport_range->ports[i] =
2160 src->conn_src.sport_range->ports[i];
2161 }
2162 }
2163 }
2164}
2165
2166/*
2167 * Copy <src> server connection source settings to <srv> server everything needed.
2168 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002169static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002170{
2171 srv->conn_src.opts = src->conn_src.opts;
2172 srv->conn_src.source_addr = src->conn_src.source_addr;
2173
2174 /* Source port range copy. */
2175 if (src->conn_src.sport_range != NULL)
2176 srv_conn_src_sport_range_cpy(srv, src);
2177
2178#ifdef CONFIG_HAP_TRANSPARENT
2179 if (src->conn_src.bind_hdr_name != NULL) {
2180 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2181 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2182 }
2183 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2184 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2185#endif
Aurelien DARRAGON92b935e2024-03-26 10:42:48 +01002186 if (src->conn_src.iface_name != NULL) {
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002187 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
Aurelien DARRAGON92b935e2024-03-26 10:42:48 +01002188 srv->conn_src.iface_len = src->conn_src.iface_len;
2189 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002190}
2191
2192/*
2193 * Copy <src> server SSL settings to <srv> server allocating
2194 * everything needed.
2195 */
2196#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002197static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002198{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002199 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002200 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2201
Christopher Faulet4ab26792021-12-01 09:50:41 +01002202 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2203 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2204
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002205 if (src->ssl_ctx.ca_file != NULL)
2206 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2207 if (src->ssl_ctx.crl_file != NULL)
2208 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002209 if (src->ssl_ctx.client_crt != NULL)
2210 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002211
2212 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2213
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002214
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002215 if (src->ssl_ctx.verify_host != NULL)
2216 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2217 if (src->ssl_ctx.ciphers != NULL)
2218 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002219 if (src->ssl_ctx.options)
2220 srv->ssl_ctx.options = src->ssl_ctx.options;
2221 if (src->ssl_ctx.methods.flags)
2222 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2223 if (src->ssl_ctx.methods.min)
2224 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2225 if (src->ssl_ctx.methods.max)
2226 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2227
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002228 if (src->ssl_ctx.ciphersuites != NULL)
2229 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002230 if (src->sni_expr != NULL)
2231 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002232
Olivier Houchardc7566002018-11-20 23:33:50 +01002233 if (src->ssl_ctx.alpn_str) {
2234 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2235 if (srv->ssl_ctx.alpn_str) {
2236 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2237 src->ssl_ctx.alpn_len);
2238 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2239 }
2240 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002241
Olivier Houchardc7566002018-11-20 23:33:50 +01002242 if (src->ssl_ctx.npn_str) {
2243 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2244 if (srv->ssl_ctx.npn_str) {
2245 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2246 src->ssl_ctx.npn_len);
2247 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2248 }
2249 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002250}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002251
2252/* Activate ssl on server <s>.
2253 * do nothing if there is no change to apply
2254 *
2255 * Must be called with the server lock held.
2256 */
2257void srv_set_ssl(struct server *s, int use_ssl)
2258{
2259 if (s->use_ssl == use_ssl)
2260 return;
2261
2262 s->use_ssl = use_ssl;
2263 if (s->use_ssl)
2264 s->xprt = xprt_get(XPRT_SSL);
2265 else
William Dauchya087f872022-01-06 16:57:15 +01002266 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002267}
2268
2269#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002270
2271/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002272 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002273 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002274 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002275 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002276int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002277{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002278 char *hostname_dn;
2279 int hostname_len, hostname_dn_len;
2280
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002281 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002282 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002283
Christopher Faulet67957bd2017-09-27 11:00:59 +02002284 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002285 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002286 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002287 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002288 if (hostname_dn_len == -1)
2289 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002290
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002291
Christopher Faulet67957bd2017-09-27 11:00:59 +02002292 free(srv->hostname);
2293 free(srv->hostname_dn);
2294 srv->hostname = strdup(hostname);
2295 srv->hostname_dn = strdup(hostname_dn);
2296 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002297 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002298 goto err;
2299
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002300 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002301
2302 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002303 ha_free(&srv->hostname);
2304 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002305 return -1;
2306}
2307
Amaury Denoyelle5cc599e2024-02-27 15:33:59 +01002308/* Initialize default values for <srv>. Used both for dynamic servers and
2309 * default servers. The latter are not initialized via new_server(), hence this
2310 * function purpose. For static servers, srv_settings_cpy() is used instead
2311 * reusing their default server instance.
2312 */
2313void srv_settings_init(struct server *srv)
2314{
2315 srv->check.inter = DEF_CHKINTR;
2316 srv->check.fastinter = 0;
2317 srv->check.downinter = 0;
2318 srv->check.rise = DEF_RISETIME;
2319 srv->check.fall = DEF_FALLTIME;
2320 srv->check.port = 0;
2321
2322 srv->agent.inter = DEF_CHKINTR;
2323 srv->agent.fastinter = 0;
2324 srv->agent.downinter = 0;
2325 srv->agent.rise = DEF_AGENT_RISETIME;
2326 srv->agent.fall = DEF_AGENT_FALLTIME;
2327 srv->agent.port = 0;
2328
2329 srv->maxqueue = 0;
2330 srv->minconn = 0;
2331 srv->maxconn = 0;
2332
2333 srv->max_reuse = -1;
2334 srv->max_idle_conns = -1;
2335 srv->pool_purge_delay = 5000;
2336
2337 srv->slowstart = 0;
2338
2339 srv->onerror = DEF_HANA_ONERR;
2340 srv->consecutive_errors_limit = DEF_HANA_ERRLIMIT;
2341
2342 srv->uweight = srv->iweight = 1;
2343}
2344
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002345/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002346 * Copy <src> server settings to <srv> server allocating
2347 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002348 * This function is not supposed to be called at any time, but only
2349 * during server settings parsing or during server allocations from
2350 * a server template, and just after having calloc()'ed a new server.
2351 * So, <src> may only be a default server (when parsing server settings)
2352 * or a server template (during server allocations from a server template).
2353 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2354 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002355 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002356void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002357{
2358 /* Connection source settings copy */
2359 srv_conn_src_cpy(srv, src);
2360
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002361 if (srv_tmpl) {
2362 srv->addr = src->addr;
2363 srv->svc_port = src->svc_port;
2364 }
2365
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002366 srv->pp_opts = src->pp_opts;
2367 if (src->rdr_pfx != NULL) {
2368 srv->rdr_pfx = strdup(src->rdr_pfx);
2369 srv->rdr_len = src->rdr_len;
2370 }
2371 if (src->cookie != NULL) {
2372 srv->cookie = strdup(src->cookie);
2373 srv->cklen = src->cklen;
2374 }
2375 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002376 srv->check.addr = src->check.addr;
2377 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002378 srv->check.use_ssl = src->check.use_ssl;
2379 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002380 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002381 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002382 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002383 /* Note: 'flags' field has potentially been already initialized. */
2384 srv->flags |= src->flags;
2385 srv->do_check = src->do_check;
2386 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002387 srv->check.inter = src->check.inter;
2388 srv->check.fastinter = src->check.fastinter;
2389 srv->check.downinter = src->check.downinter;
2390 srv->agent.use_ssl = src->agent.use_ssl;
2391 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002392
2393 if (src->agent.tcpcheck_rules) {
2394 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2395 if (srv->agent.tcpcheck_rules) {
2396 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2397 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2398 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2399 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2400 &src->agent.tcpcheck_rules->preset_vars);
2401 }
2402 }
2403
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002404 srv->agent.inter = src->agent.inter;
2405 srv->agent.fastinter = src->agent.fastinter;
2406 srv->agent.downinter = src->agent.downinter;
2407 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002408 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002409 srv->minconn = src->minconn;
2410 srv->maxconn = src->maxconn;
2411 srv->slowstart = src->slowstart;
2412 srv->observe = src->observe;
2413 srv->onerror = src->onerror;
2414 srv->onmarkeddown = src->onmarkeddown;
2415 srv->onmarkedup = src->onmarkedup;
2416 if (src->trackit != NULL)
2417 srv->trackit = strdup(src->trackit);
2418 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2419 srv->uweight = srv->iweight = src->iweight;
2420
2421 srv->check.send_proxy = src->check.send_proxy;
2422 /* health: up, but will fall down at first failure */
2423 srv->check.rise = srv->check.health = src->check.rise;
2424 srv->check.fall = src->check.fall;
2425
2426 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002427 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2428 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2429 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002430 srv->check.state |= CHK_ST_PAUSED;
2431 srv->check.health = 0;
2432 }
2433
2434 /* health: up but will fall down at first failure */
2435 srv->agent.rise = srv->agent.health = src->agent.rise;
2436 srv->agent.fall = src->agent.fall;
2437
2438 if (src->resolvers_id != NULL)
2439 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002440 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2441 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2442 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2443 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2444 srv->resolv_opts.family_prio = AF_INET6;
2445 memcpy(srv->resolv_opts.pref_net,
2446 src->resolv_opts.pref_net,
2447 sizeof srv->resolv_opts.pref_net);
2448 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002449
2450 srv->init_addr_methods = src->init_addr_methods;
2451 srv->init_addr = src->init_addr;
2452#if defined(USE_OPENSSL)
2453 srv_ssl_settings_cpy(srv, src);
2454#endif
2455#ifdef TCP_USER_TIMEOUT
2456 srv->tcp_ut = src->tcp_ut;
2457#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002458 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002459 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002460 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002461 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002462 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002463
Olivier Houchard8da5f982017-08-04 18:35:36 +02002464 if (srv_tmpl)
2465 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002466
Aurelien DARRAGON60d185d2023-06-14 09:53:32 +02002467 srv->netns = src->netns;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002468 srv->check.via_socks4 = src->check.via_socks4;
2469 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002470}
2471
Willy Tarreau198e92a2021-03-05 10:23:32 +01002472/* allocate a server and attach it to the global servers_list. Returns
2473 * the server on success, otherwise NULL.
2474 */
William Lallemand313bfd12018-10-26 14:47:32 +02002475struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002476{
2477 struct server *srv;
2478
2479 srv = calloc(1, sizeof *srv);
2480 if (!srv)
2481 return NULL;
2482
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002483 srv_take(srv);
2484
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002485 srv->obj_type = OBJ_TYPE_SERVER;
2486 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002487 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002488 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002489 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002490 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002491 MT_LIST_INIT(&srv->prev_deleted);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002492 event_hdl_sub_list_init(&srv->e_subs);
Aurelien DARRAGON9a55efb2023-07-07 15:19:36 +02002493 srv->rid = 0; /* rid defaults to 0 */
Christopher Faulet40a007c2017-07-03 15:41:01 +02002494
Emeric Brun52a91d32017-08-31 14:41:55 +02002495 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau69530f52023-04-28 09:16:15 +02002496 srv->last_change = ns_to_sec(now_ns);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002497
Christopher Faulet38290462020-04-21 11:46:40 +02002498 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002499 srv->check.status = HCHK_STATUS_INI;
2500 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002501 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002502 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002503
Christopher Faulet38290462020-04-21 11:46:40 +02002504 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002505 srv->agent.status = HCHK_STATUS_INI;
2506 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002507 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002508 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
2509
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002510 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002511#ifdef USE_OPENSSL
2512 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2513#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002514
Willy Tarreau975b1552019-06-06 16:25:55 +02002515 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002516 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002517 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002518 return srv;
2519}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002520
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002521/* Increment the server refcount. */
2522void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002523{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002524 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002525}
2526
Aurelien DARRAGON899b5472023-06-01 12:07:43 +02002527/* deallocate common server parameters (may be used by default-servers) */
2528void srv_free_params(struct server *srv)
2529{
2530 free(srv->cookie);
Aurelien DARRAGONd2d7fbd2023-09-15 00:42:55 +02002531 free(srv->rdr_pfx);
Aurelien DARRAGON899b5472023-06-01 12:07:43 +02002532 free(srv->hostname);
2533 free(srv->hostname_dn);
2534 free((char*)srv->conf.file);
2535 free(srv->per_thr);
2536 free(srv->per_tgrp);
2537 free(srv->curr_idle_thr);
2538 free(srv->resolvers_id);
2539 free(srv->addr_node.key);
2540 free(srv->lb_nodes);
2541
2542 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2543 xprt_get(XPRT_SSL)->destroy_srv(srv);
2544}
2545
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002546/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2547 * dynamic servers, its refcount is decremented first. The free operations are
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002548 * conducted only if the refcount is nul.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002549 *
2550 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002551 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002552 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002553struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002554{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002555 struct server *next = NULL;
2556
William Lallemand4c395fc2021-08-20 10:10:15 +02002557 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002558 goto end;
2559
2560 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002561
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002562 /* For dynamic servers, decrement the reference counter. Only free the
2563 * server when reaching zero.
2564 */
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002565 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2566 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002567
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002568 /* make sure we are removed from our 'next->prev_deleted' list
2569 * This doesn't require full thread isolation as we're using mt lists
2570 * However this could easily be turned into regular list if required
2571 * (with the proper use of thread isolation)
2572 */
2573 MT_LIST_DELETE(&srv->prev_deleted);
2574
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002575 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002576 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002577
2578 free(srv->id);
Aurelien DARRAGON899b5472023-06-01 12:07:43 +02002579 srv_free_params(srv);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002580
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002581 HA_SPIN_DESTROY(&srv->lock);
2582
Willy Tarreau2b718102021-04-21 07:32:39 +02002583 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002584 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002585
2586 EXTRA_COUNTERS_FREE(srv->extra_counters);
2587
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002588 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002589
2590 end:
2591 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002592}
2593
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002594/* Remove a server <srv> from a tracking list if <srv> is tracking another
2595 * server. No special care is taken if <srv> is tracked itself by another one :
2596 * this situation should be avoided by the caller.
2597 *
2598 * Not thread-safe.
2599 */
2600static void release_server_track(struct server *srv)
2601{
2602 struct server *strack = srv->track;
2603 struct server **base;
2604
2605 if (!strack)
2606 return;
2607
2608 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2609 if (*base == srv) {
2610 *base = srv->tracknext;
2611 return;
2612 }
2613 }
2614
2615 /* srv not found on the tracking list, this should never happen */
2616 BUG_ON(!*base);
2617}
2618
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002619/*
2620 * Parse as much as possible such a range string argument: low[-high]
2621 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2622 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2623 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2624 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002625 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002626static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2627 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002628{
2629 char *nb_high_arg;
2630
2631 *nb_high = 0;
2632 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002633 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002634
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002635 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002636 *nb_high_arg++ = '\0';
2637 *nb_high = atoi(nb_high_arg);
2638 }
2639 else {
2640 *nb_high += *nb_low;
2641 *nb_low = 1;
2642 }
2643
2644 if (*nb_low < 0 || *nb_high < *nb_low)
2645 return -1;
2646
2647 return 0;
2648}
2649
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002650/* Parse as much as possible such a range string argument: low[-high]
2651 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2652 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2653 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002654 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002655 * initialize a new server on startup.
2656 *
2657 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2658 * Returns 0 if succeeded, -1 if not.
2659 */
2660static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2661 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002662{
2663 chunk_printf(&trash, "%s%d", prefix, nb);
2664 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002665 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002666}
2667
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002668/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002669 * Note that a server template is a special server with
2670 * a few different parameters than a server which has
2671 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002672 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002673 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002674 * initialize a new server on startup.
2675 *
Joseph Herlant44466822018-11-15 08:57:51 -08002676 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002677 * 'srv' template included.
2678 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002679static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002680{
2681 int i;
2682 struct server *newsrv;
2683
2684 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002685 newsrv = new_server(px);
2686 if (!newsrv)
2687 goto err;
2688
Christopher Faulet75bef002020-11-02 22:04:55 +01002689 newsrv->conf.file = strdup(srv->conf.file);
2690 newsrv->conf.line = srv->conf.line;
2691
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002692 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002693 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002694
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002695 if (newsrv->sni_expr) {
2696 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2697 if (!newsrv->ssl_ctx.sni)
2698 goto err;
2699 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002700
Emeric Brun34067662021-06-11 10:48:45 +02002701 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002702 if (newsrv->srvrq)
2703 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002704
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002705 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002706 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002707
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002708 /* Linked backwards first. This will be restablished after parsing. */
2709 newsrv->next = px->srv;
2710 px->srv = newsrv;
2711 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002712 _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002713
2714 return i - srv->tmpl_info.nb_low;
2715
2716 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002717 _srv_parse_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002718 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002719 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002720 free_check(&newsrv->agent);
2721 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002722 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002723 }
2724 free(newsrv);
2725 return i - srv->tmpl_info.nb_low;
2726}
2727
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002728/* Allocate a new server pointed by <srv> and try to parse the first arguments
2729 * in <args> as an address for a server or an address-range for a template or
2730 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2731 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002732 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002733 * initialize a new server on startup.
2734 *
2735 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2736 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2737 * <srv> will be set to NULL.
2738 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002739static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2740 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002741 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002742{
2743 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002744 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002745 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002746 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002747 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002748 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002749
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002750 *srv = NULL;
2751
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002752 /* There is no mandatory first arguments for default server. */
2753 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2754 if (parse_flags & SRV_PARSE_TEMPLATE) {
2755 if (!*args[3]) {
2756 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002757 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2758 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002759 err_code |= ERR_ALERT | ERR_FATAL;
2760 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002761 }
2762
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002763 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002764 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002765 else {
2766 if (!*args[2]) {
2767 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002768 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2769 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002770 err_code |= ERR_ALERT | ERR_FATAL;
2771 goto out;
2772 }
2773
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002774 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002775 }
2776
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002777 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002778 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2779 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002780 err_code |= ERR_ALERT | ERR_FATAL;
2781 goto out;
2782 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002783 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002784
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002785 *cur_arg = 2;
2786 if (parse_flags & SRV_PARSE_TEMPLATE) {
2787 /* Parse server-template <nb | range> arg. */
2788 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002789 ha_alert("Wrong %s number or range arg '%s'.\n",
2790 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002791 err_code |= ERR_ALERT | ERR_FATAL;
2792 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002793 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002794 (*cur_arg)++;
2795 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002796
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002797 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2798 struct sockaddr_storage *sk;
2799 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002800
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002801 *srv = newsrv = new_server(curproxy);
2802 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002803 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002804 err_code |= ERR_ALERT | ERR_ABORT;
2805 goto out;
2806 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002807 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002808
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002809 if (parse_flags & SRV_PARSE_TEMPLATE) {
2810 newsrv->tmpl_info.nb_low = tmpl_range_low;
2811 newsrv->tmpl_info.nb_high = tmpl_range_high;
2812 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002813
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002814 if (parse_flags & SRV_PARSE_DYNAMIC)
2815 newsrv->flags |= SRV_F_DYNAMIC;
2816
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002817 /* Note: for a server template, its id is its prefix.
2818 * This is a temporary id which will be used for server allocations to come
2819 * after parsing.
2820 */
2821 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2822 newsrv->id = strdup(args[1]);
2823 else
2824 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002825
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002826 /* several ways to check the port component :
2827 * - IP => port=+0, relative (IPv4 only)
2828 * - IP: => port=+0, relative
2829 * - IP:N => port=N, absolute
2830 * - IP:+N => port=+N, relative
2831 * - IP:-N => port=-N, relative
2832 */
2833 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2834 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002835
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002836 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002837 &errmsg, NULL, &fqdn,
Christopher Fauletaf27ada2023-06-02 14:10:36 +02002838 (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK |
2839 (parse_flags & SRV_PARSE_IN_PEER_SECTION ? PA_O_PORT_MAND : PA_O_PORT_OFS) |
2840 PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002841 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002842 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002843 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002844 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002845 goto out;
2846 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002847
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002848 if (!port1 || !port2) {
2849 /* no port specified, +offset, -offset */
2850 newsrv->flags |= SRV_F_MAPPORTS;
2851 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002852
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002853 /* save hostname and create associated name resolution */
2854 if (fqdn) {
2855 if (fqdn[0] == '_') { /* SRV record */
2856 /* Check if a SRV request already exists, and if not, create it */
2857 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2858 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2859 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002860 err_code |= ERR_ALERT | ERR_FATAL;
2861 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002862 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002863 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002864 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002865 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002866 ha_alert("Can't create DNS resolution for server '%s'\n",
2867 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002868 err_code |= ERR_ALERT | ERR_FATAL;
2869 goto out;
2870 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002871 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002872
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002873 newsrv->addr = *sk;
2874 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002875 /*
2876 * we don't need to lock the server here, because
2877 * we are in the process of initializing.
2878 *
2879 * Note that the server is not attached into the proxy tree if
2880 * this is a dynamic server.
2881 */
2882 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002883
Willy Tarreau14e7f292021-10-27 17:41:07 +02002884 if (!newsrv->srvrq && !newsrv->hostname &&
2885 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002886 ha_alert("Unknown protocol family %d '%s'\n",
2887 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002888 err_code |= ERR_ALERT | ERR_FATAL;
2889 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002890 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002891
2892 (*cur_arg)++;
2893 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002894 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2895 /* Copy default server settings to new server */
2896 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2897 } else {
Amaury Denoyelle5cc599e2024-02-27 15:33:59 +01002898 srv_settings_init(newsrv);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002899
2900 /* A dynamic server is disabled on startup */
2901 newsrv->next_admin = SRV_ADMF_FMAINT;
2902 newsrv->next_state = SRV_ST_STOPPED;
2903 server_recalc_eweight(newsrv, 0);
2904 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002905 HA_SPIN_INIT(&newsrv->lock);
2906 }
2907 else {
2908 *srv = newsrv = &curproxy->defsrv;
2909 *cur_arg = 1;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002910 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002911
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002912 free(fqdn);
2913 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002914
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002915out:
2916 free(fqdn);
2917 return err_code;
2918}
Willy Tarreau272adea2014-03-31 10:39:59 +02002919
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002920/* Parse the server keyword in <args>.
2921 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2922 * might not be the case if an error is reported.
2923 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002924 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002925 * initialize a new server on startup.
2926 *
2927 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2928 * interrupted.
2929 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002930static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2931 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002932 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002933{
2934 int err_code = 0;
2935 struct srv_kw *kw;
2936 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002937 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002938
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002939 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002940 if (!kw) {
2941 best = srv_find_best_kw(args[*cur_arg]);
2942 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002943 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2944 args[*cur_arg], best,
2945 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2946 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002947 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002948 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2949 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2950 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002951
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002952 return ERR_ALERT | ERR_FATAL;
2953 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002954
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002955 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002956 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2957 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002958 err_code = ERR_ALERT | ERR_FATAL;
2959 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002960 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002961
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002962 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002963 ha_alert("'%s' option is not accepted in default-server sections\n",
2964 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002965 err_code = ERR_ALERT;
2966 goto out;
2967 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002968 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002969 ha_alert("'%s' option is not accepted for dynamic server\n",
2970 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002971 err_code |= ERR_ALERT;
2972 goto out;
2973 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002974
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002975 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2976 if (err_code) {
2977 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2978 free(errmsg);
2979 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002980
2981out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002982 if (kw->skip != -1)
2983 *cur_arg += 1 + kw->skip;
2984
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002985 return err_code;
2986}
2987
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002988/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002989 * initialize a new server on startup.
2990 */
2991static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2992 struct server *srv, struct proxy *proxy,
2993 char **errmsg)
2994{
2995 int ret;
2996
2997 if (!srv->sni_expr)
2998 return 0;
2999
3000 ret = server_parse_sni_expr(srv, proxy, errmsg);
3001 if (!ret)
3002 return 0;
3003
3004 return ret;
3005}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003006
3007/* Server initializations finalization.
3008 * Initialize health check, agent check and SNI expression if enabled.
3009 * Must not be called for a default server instance.
3010 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05003011 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003012 * initialize a new server on startup.
3013 */
3014static int _srv_parse_finalize(char **args, int cur_arg,
3015 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003016 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003017{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003018 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003019 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003020
3021 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003022 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003023 return ERR_ALERT | ERR_FATAL;
3024 }
3025
3026 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003027 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
3028 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003029 return ERR_ALERT | ERR_FATAL;
3030 }
3031
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003032 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
3033 if (errmsg) {
3034 ha_alert("%s\n", errmsg);
3035 free(errmsg);
3036 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003037 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003038 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003039
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01003040 /* A dynamic server is disabled on startup. It must not be counted as
3041 * an active backend entry.
3042 */
3043 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
3044 if (srv->flags & SRV_F_BACKUP)
3045 px->srv_bck++;
3046 else
3047 px->srv_act++;
3048 }
3049
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003050 srv_lb_commit_status(srv);
3051
3052 return 0;
3053}
3054
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003055int parse_server(const char *file, int linenum, char **args,
3056 struct proxy *curproxy, const struct proxy *defproxy,
3057 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01003058{
3059 struct server *newsrv = NULL;
3060 int err_code = 0;
3061
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003062 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02003063
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003064 set_usermsgs_ctx(file, linenum, NULL);
3065
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003066 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003067 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003068 err_code |= ERR_ALERT | ERR_FATAL;
3069 goto out;
3070 }
3071 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
3072 err_code |= ERR_ALERT | ERR_FATAL;
3073 goto out;
3074 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01003075
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003076 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
3077 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
3078 if (!*args[2])
3079 return 0;
3080 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003081
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003082 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003083 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01003084
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003085 /* the servers are linked backwards first */
3086 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
3087 newsrv->next = curproxy->srv;
3088 curproxy->srv = newsrv;
3089 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01003090
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003091 if (err_code & ERR_CODE)
3092 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003093
Willy Tarreau8bb771a2023-11-23 14:28:14 +01003094 if (!newsrv->conf.file) // note: do it only once for default-server
3095 newsrv->conf.file = strdup(file);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003096 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003097
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003098 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003099 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003100 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003101 if (err_code & ERR_FATAL)
3102 goto out;
3103 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003104
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003105 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003106 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003107 if (err_code & ERR_FATAL)
3108 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02003109 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01003110
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003111 if (parse_flags & SRV_PARSE_TEMPLATE)
3112 _srv_parse_tmpl_init(newsrv, curproxy);
3113
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02003114 /* If the server id is fixed, insert it in the proxy used_id tree.
3115 * This is needed to detect a later duplicate id via srv_parse_id.
3116 *
3117 * If no is specified, a dynamic one is generated in
3118 * check_config_validity.
3119 */
3120 if (newsrv->flags & SRV_F_FORCED_ID)
3121 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3122
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02003123 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003124 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02003125
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003126 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003127 return 0;
3128
3129 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003130 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003131 return err_code;
3132}
3133
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003134/* Returns a pointer to the first server matching either id <id>.
3135 * NULL is returned if no match is found.
3136 * the lookup is performed in the backend <bk>
3137 */
3138struct server *server_find_by_id(struct proxy *bk, int id)
3139{
3140 struct eb32_node *eb32;
3141 struct server *curserver;
3142
3143 if (!bk || (id ==0))
3144 return NULL;
3145
3146 /* <bk> has no backend capabilities, so it can't have a server */
3147 if (!(bk->cap & PR_CAP_BE))
3148 return NULL;
3149
3150 curserver = NULL;
3151
3152 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3153 if (eb32)
3154 curserver = container_of(eb32, struct server, conf.id);
3155
3156 return curserver;
3157}
3158
3159/* Returns a pointer to the first server matching either name <name>, or id
3160 * if <name> starts with a '#'. NULL is returned if no match is found.
3161 * the lookup is performed in the backend <bk>
3162 */
3163struct server *server_find_by_name(struct proxy *bk, const char *name)
3164{
3165 struct server *curserver;
3166
3167 if (!bk || !name)
3168 return NULL;
3169
3170 /* <bk> has no backend capabilities, so it can't have a server */
3171 if (!(bk->cap & PR_CAP_BE))
3172 return NULL;
3173
3174 curserver = NULL;
3175 if (*name == '#') {
3176 curserver = server_find_by_id(bk, atoi(name + 1));
3177 if (curserver)
3178 return curserver;
3179 }
3180 else {
3181 curserver = bk->srv;
3182
3183 while (curserver && (strcmp(curserver->id, name) != 0))
3184 curserver = curserver->next;
3185
3186 if (curserver)
3187 return curserver;
3188 }
3189
3190 return NULL;
3191}
3192
3193struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3194{
3195 struct server *byname;
3196 struct server *byid;
3197
3198 if (!name && !id)
3199 return NULL;
3200
3201 if (diff)
3202 *diff = 0;
3203
3204 byname = byid = NULL;
3205
3206 if (name) {
3207 byname = server_find_by_name(bk, name);
3208 if (byname && (!id || byname->puid == id))
3209 return byname;
3210 }
3211
3212 /* remaining possibilities :
3213 * - name not set
3214 * - name set but not found
3215 * - name found but ID doesn't match
3216 */
3217 if (id) {
3218 byid = server_find_by_id(bk, id);
3219 if (byid) {
3220 if (byname) {
3221 /* use id only if forced by configuration */
3222 if (byid->flags & SRV_F_FORCED_ID) {
3223 if (diff)
3224 *diff |= 2;
3225 return byid;
3226 }
3227 else {
3228 if (diff)
3229 *diff |= 1;
3230 return byname;
3231 }
3232 }
3233
3234 /* remaining possibilities:
3235 * - name not set
3236 * - name set but not found
3237 */
3238 if (name && diff)
3239 *diff |= 2;
3240 return byid;
3241 }
3242
3243 /* id bot found */
3244 if (byname) {
3245 if (diff)
3246 *diff |= 1;
3247 return byname;
3248 }
3249 }
3250
3251 return NULL;
3252}
3253
Simon Horman7d09b9a2013-02-12 10:45:51 +09003254/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003255 * update a server's current IP address.
3256 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3257 * ip is in network format.
3258 * updater is a string which contains an information about the requester of the update.
3259 * updater is used if not NULL.
3260 *
3261 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003262 *
3263 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003264 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003265int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003266{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003267 /* save the new IP family & address if necessary */
3268 switch (ip_sin_family) {
3269 case AF_INET:
3270 if (s->addr.ss_family == ip_sin_family &&
3271 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3272 return 0;
3273 break;
3274 case AF_INET6:
3275 if (s->addr.ss_family == ip_sin_family &&
3276 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3277 return 0;
3278 break;
3279 };
3280
Baptiste Assmann14e40142015-04-14 01:13:07 +02003281 /* generates a log line and a warning on stderr */
3282 if (1) {
3283 /* book enough space for both IPv4 and IPv6 */
3284 char oldip[INET6_ADDRSTRLEN];
3285 char newip[INET6_ADDRSTRLEN];
3286
3287 memset(oldip, '\0', INET6_ADDRSTRLEN);
3288 memset(newip, '\0', INET6_ADDRSTRLEN);
3289
3290 /* copy old IP address in a string */
3291 switch (s->addr.ss_family) {
3292 case AF_INET:
3293 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3294 break;
3295 case AF_INET6:
3296 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3297 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003298 default:
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003299 strlcpy2(oldip, "(none)", sizeof(oldip));
Christopher Fauletb0b76072020-09-08 10:38:40 +02003300 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003301 };
3302
3303 /* copy new IP address in a string */
3304 switch (ip_sin_family) {
3305 case AF_INET:
3306 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3307 break;
3308 case AF_INET6:
3309 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3310 break;
3311 };
3312
3313 /* save log line into a buffer */
3314 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3315 s->proxy->id, s->id, oldip, newip, updater);
3316
3317 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003318 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003319
3320 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003321 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003322 }
3323
3324 /* save the new IP family */
3325 s->addr.ss_family = ip_sin_family;
3326 /* save the new IP address */
3327 switch (ip_sin_family) {
3328 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003329 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003330 break;
3331 case AF_INET6:
3332 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3333 break;
3334 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003335 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003336 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003337
3338 return 0;
3339}
3340
William Dauchy7cabc062021-02-11 22:51:24 +01003341/* update agent health check address and port
3342 * addr can be ip4/ip6 or a hostname
3343 * if one error occurs, don't apply anything
3344 * must be called with the server lock held.
3345 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003346const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003347{
3348 struct sockaddr_storage sk;
3349 struct buffer *msg;
3350 int new_port;
3351
3352 msg = get_trash_chunk();
3353 chunk_reset(msg);
3354
3355 if (!(s->agent.state & CHK_ST_ENABLED)) {
3356 chunk_strcat(msg, "agent checks are not enabled on this server");
3357 goto out;
3358 }
3359 if (addr) {
3360 memset(&sk, 0, sizeof(struct sockaddr_storage));
3361 if (str2ip(addr, &sk) == NULL) {
3362 chunk_appendf(msg, "invalid addr '%s'", addr);
3363 goto out;
3364 }
3365 }
3366 if (port) {
3367 if (strl2irc(port, strlen(port), &new_port) != 0) {
3368 chunk_appendf(msg, "provided port is not an integer");
3369 goto out;
3370 }
3371 if (new_port < 0 || new_port > 65535) {
3372 chunk_appendf(msg, "provided port is invalid");
3373 goto out;
3374 }
3375 }
3376out:
3377 if (msg->data)
3378 return msg->area;
3379 else {
3380 if (addr)
3381 set_srv_agent_addr(s, &sk);
3382 if (port)
3383 set_srv_agent_port(s, new_port);
3384 }
3385 return NULL;
3386}
3387
William Dauchyb456e1f2021-02-11 22:51:23 +01003388/* update server health check address and port
3389 * addr must be ip4 or ip6, it won't be resolved
3390 * if one error occurs, don't apply anything
3391 * must be called with the server lock held.
3392 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003393const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003394{
3395 struct sockaddr_storage sk;
3396 struct buffer *msg;
3397 int new_port;
3398
3399 msg = get_trash_chunk();
3400 chunk_reset(msg);
3401
3402 if (!(s->check.state & CHK_ST_ENABLED)) {
3403 chunk_strcat(msg, "health checks are not enabled on this server");
3404 goto out;
3405 }
3406 if (addr) {
3407 memset(&sk, 0, sizeof(struct sockaddr_storage));
3408 if (str2ip2(addr, &sk, 0) == NULL) {
3409 chunk_appendf(msg, "invalid addr '%s'", addr);
3410 goto out;
3411 }
3412 }
3413 if (port) {
3414 if (strl2irc(port, strlen(port), &new_port) != 0) {
3415 chunk_appendf(msg, "provided port is not an integer");
3416 goto out;
3417 }
3418 if (new_port < 0 || new_port > 65535) {
3419 chunk_appendf(msg, "provided port is invalid");
3420 goto out;
3421 }
3422 /* prevent the update of port to 0 if MAPPORTS are in use */
3423 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3424 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3425 goto out;
3426 }
3427 }
3428out:
3429 if (msg->data)
3430 return msg->area;
3431 else {
3432 if (addr)
3433 s->check.addr = sk;
3434 if (port)
3435 s->check.port = new_port;
3436 }
3437 return NULL;
3438}
3439
Baptiste Assmann14e40142015-04-14 01:13:07 +02003440/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003441 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3442 *
3443 * Caller can pass its name through <updater> to get it integrated in the response message
3444 * returned by the function.
3445 *
3446 * The function first does the following, in that order:
3447 * - validates the new addr and/or port
3448 * - checks if an update is required (new IP or port is different than current ones)
3449 * - checks the update is allowed:
3450 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3451 * - allow all changes if no CHECKS are configured
3452 * - if CHECK is configured:
3453 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3454 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3455 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003456 *
3457 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003458 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003459const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003460{
3461 struct sockaddr_storage sa;
3462 int ret, port_change_required;
3463 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003464 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003465 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003466 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003467
3468 msg = get_trash_chunk();
3469 chunk_reset(msg);
3470
3471 if (addr) {
3472 memset(&sa, 0, sizeof(struct sockaddr_storage));
3473 if (str2ip2(addr, &sa, 0) == NULL) {
3474 chunk_printf(msg, "Invalid addr '%s'", addr);
3475 goto out;
3476 }
3477
3478 /* changes are allowed on AF_INET* families only */
3479 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3480 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3481 goto out;
3482 }
3483
3484 /* collecting data currently setup */
3485 memset(current_addr, '\0', sizeof(current_addr));
3486 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3487 /* changes are allowed on AF_INET* families only */
3488 if ((ret != AF_INET) && (ret != AF_INET6)) {
3489 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3490 goto out;
3491 }
3492
3493 /* applying ADDR changes if required and allowed
3494 * ipcmp returns 0 when both ADDR are the same
3495 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003496 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003497 chunk_appendf(msg, "no need to change the addr");
3498 goto port;
3499 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003500 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003501 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003502
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003503 /* update report for caller */
3504 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3505 }
3506
3507 port:
3508 if (port) {
3509 char sign = '\0';
3510 char *endptr;
3511
3512 if (addr)
3513 chunk_appendf(msg, ", ");
3514
3515 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003516 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003517
3518 /* check if PORT change is required */
3519 port_change_required = 0;
3520
3521 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003522 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003523 new_port = strtol(port, &endptr, 10);
3524 if ((errno != 0) || (port == endptr)) {
3525 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3526 goto out;
3527 }
3528
3529 /* check if caller triggers a port mapped or offset */
3530 if (sign == '-' || (sign == '+')) {
3531 /* check if server currently uses port map */
3532 if (!(s->flags & SRV_F_MAPPORTS)) {
3533 /* switch from fixed port to port map mandatorily triggers
3534 * a port change */
3535 port_change_required = 1;
3536 /* check is configured
3537 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3538 * prevent PORT change if check doesn't have it's dedicated port while switching
3539 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003540 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003541 chunk_appendf(msg, "can't change <port> to port map because it is incompatible with current health check port configuration (use 'port' statement from the 'server' directive.");
3542 goto out;
3543 }
3544 }
3545 /* we're already using port maps */
3546 else {
3547 port_change_required = current_port != new_port;
3548 }
3549 }
3550 /* fixed port */
3551 else {
3552 port_change_required = current_port != new_port;
3553 }
3554
3555 /* applying PORT changes if required and update response message */
3556 if (port_change_required) {
3557 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003558 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003559 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003560
3561 /* prepare message */
3562 chunk_appendf(msg, "port changed from '");
3563 if (s->flags & SRV_F_MAPPORTS)
3564 chunk_appendf(msg, "+");
3565 chunk_appendf(msg, "%d' to '", current_port);
3566
3567 if (sign == '-') {
3568 s->flags |= SRV_F_MAPPORTS;
3569 chunk_appendf(msg, "%c", sign);
3570 /* just use for result output */
3571 new_port = -new_port;
3572 }
3573 else if (sign == '+') {
3574 s->flags |= SRV_F_MAPPORTS;
3575 chunk_appendf(msg, "%c", sign);
3576 }
3577 else {
3578 s->flags &= ~SRV_F_MAPPORTS;
3579 }
3580
3581 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003582 }
3583 else {
3584 chunk_appendf(msg, "no need to change the port");
3585 }
3586 }
3587
3588out:
William Dauchy6318d332020-05-02 21:52:36 +02003589 if (changed) {
3590 /* force connection cleanup on the given server */
3591 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003592 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003593 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003594 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003595 if (updater)
3596 chunk_appendf(msg, " by '%s'", updater);
3597 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003598 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003599}
3600
Christopher Faulet5efdef22021-03-11 18:03:21 +01003601/*
3602 * update server status based on result of SRV resolution
3603 * returns:
3604 * 0 if server status is updated
3605 * 1 if server status has not changed
3606 *
3607 * Must be called with the server lock held.
3608 */
3609int srvrq_update_srv_status(struct server *s, int has_no_ip)
3610{
3611 if (!s->srvrq)
3612 return 1;
3613
3614 /* since this server has an IP, it can go back in production */
3615 if (has_no_ip == 0) {
3616 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3617 return 1;
3618 }
3619
3620 if (s->next_admin & SRV_ADMF_RMAINT)
3621 return 1;
3622
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003623 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOENT);
Christopher Faulet5efdef22021-03-11 18:03:21 +01003624 return 0;
3625}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003626
3627/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003628 * update server status based on result of name resolution
3629 * returns:
3630 * 0 if server status is updated
3631 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003632 *
3633 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003634 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003635int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003636{
Emeric Brun750fe792020-12-23 16:51:12 +01003637 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003638 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003639 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003640
Jerome Magnin012261a2020-07-28 13:38:22 +02003641 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003642 if (resolution == NULL)
3643 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003644
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003645 switch (resolution->status) {
3646 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003647 /* status when HAProxy has just (re)started.
3648 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003649 break;
3650
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003651 case RSLV_STATUS_VALID:
3652 /*
3653 * resume health checks
3654 * server will be turned back on if health check is safe
3655 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003656 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003657 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003658 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003659 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOIP);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003660 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003661 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003662
Emeric Brun52a91d32017-08-31 14:41:55 +02003663 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003664 return 1;
3665 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3666 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3667 s->proxy->id, s->id);
3668
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003669 ha_warning("%s.\n", trash.area);
3670 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003671 return 0;
3672
3673 case RSLV_STATUS_NX:
3674 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003675 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3676 if (!tick_is_expired(exp, now_ms))
3677 break;
3678
3679 if (s->next_admin & SRV_ADMF_RMAINT)
3680 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003681 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NX);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003682 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003683
3684 case RSLV_STATUS_TIMEOUT:
3685 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003686 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3687 if (!tick_is_expired(exp, now_ms))
3688 break;
3689
3690 if (s->next_admin & SRV_ADMF_RMAINT)
3691 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003692 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_TIMEOUT);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003693 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003694
3695 case RSLV_STATUS_REFUSED:
3696 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003697 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3698 if (!tick_is_expired(exp, now_ms))
3699 break;
3700
3701 if (s->next_admin & SRV_ADMF_RMAINT)
3702 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003703 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_REFUSED);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003704 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003705
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003706 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003707 /* stop server if resolution failed for a long enough period */
3708 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3709 if (!tick_is_expired(exp, now_ms))
3710 break;
3711
3712 if (s->next_admin & SRV_ADMF_RMAINT)
3713 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003714 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_UNSPEC);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003715 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716 }
3717
3718 return 1;
3719}
3720
3721/*
3722 * Server Name Resolution valid response callback
3723 * It expects:
3724 * - <nameserver>: the name server which answered the valid response
3725 * - <response>: buffer containing a valid DNS response
3726 * - <response_len>: size of <response>
3727 * It performs the following actions:
3728 * - ignore response if current ip found and server family not met
3729 * - update with first new ip found if family is met and current IP is not found
3730 * returns:
3731 * 0 on error
3732 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003733 *
3734 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003735 */
Emeric Brun08622d32020-12-23 17:41:43 +01003736int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003737{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003738 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003739 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003740 void *serverip, *firstip;
3741 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003742 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003743 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003744 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003745
Christopher Faulet67957bd2017-09-27 11:00:59 +02003746 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003747 if (!s)
3748 return 1;
3749
Christopher Faulet49531e82021-03-10 15:07:27 +01003750 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003751 /* If DNS resolution is disabled ignore it.
3752 * This is the case if the server was associated to
3753 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003754 */
Emeric Brun34067662021-06-11 10:48:45 +02003755 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003756 return 1;
3757 }
3758
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003759 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003760 if (!resolution)
3761 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003762
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003763 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003764 firstip = NULL; /* pointer to the first valid response found */
3765 /* it will be used as the new IP if a change is required */
3766 firstip_sin_family = AF_UNSPEC;
3767 serverip = NULL; /* current server IP address */
3768
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003769 /* initializing server IP pointer */
3770 server_sin_family = s->addr.ss_family;
3771 switch (server_sin_family) {
3772 case AF_INET:
3773 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3774 break;
3775
3776 case AF_INET6:
3777 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3778 break;
3779
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003780 case AF_UNSPEC:
3781 break;
3782
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003783 default:
3784 goto invalid;
3785 }
3786
Emeric Brund30e9a12020-12-23 18:49:16 +01003787 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3788 serverip, server_sin_family, &firstip,
3789 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003790
3791 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003792 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003793 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003794
Emeric Brun456de772020-12-23 18:17:31 +01003795 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003796 goto save_ip;
3797
Emeric Brun456de772020-12-23 18:17:31 +01003798 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003799 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003800 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003801
Emeric Brun456de772020-12-23 18:17:31 +01003802 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003803 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003804 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003805 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003806 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003807
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003809 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003810 goto invalid;
3811
3812 }
3813
3814 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003815 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003816 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003817 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003818 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003819 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003820 else
3821 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003822 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003823
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003824 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003825 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3826 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003827 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003828
3829 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003830 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003831 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003832 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003833 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003834 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3835 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003836 return 0;
3837}
3838
3839/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003840 * SRV record error management callback
3841 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003842 * 0 if we can trash answser items.
3843 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003844 *
3845 * Grabs the server's lock.
3846 */
3847int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3848{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003849 struct resolv_srvrq *srvrq;
3850 struct resolv_resolution *res;
3851 struct resolvers *resolvers;
3852 int exp;
3853
3854 /* SRV records */
3855 srvrq = objt_resolv_srvrq(requester->owner);
3856 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003857 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003858
3859 resolvers = srvrq->resolvers;
3860 res = requester->resolution;
3861
3862 switch (res->status) {
3863
3864 case RSLV_STATUS_NX:
3865 /* stop server if resolution is NX for a long enough period */
3866 exp = tick_add(res->last_valid, resolvers->hold.nx);
3867 if (!tick_is_expired(exp, now_ms))
3868 return 1;
3869 break;
3870
3871 case RSLV_STATUS_TIMEOUT:
3872 /* stop server if resolution is TIMEOUT for a long enough period */
3873 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3874 if (!tick_is_expired(exp, now_ms))
3875 return 1;
3876 break;
3877
3878 case RSLV_STATUS_REFUSED:
3879 /* stop server if resolution is REFUSED for a long enough period */
3880 exp = tick_add(res->last_valid, resolvers->hold.refused);
3881 if (!tick_is_expired(exp, now_ms))
3882 return 1;
3883 break;
3884
3885 default:
3886 /* stop server if resolution failed for a long enough period */
3887 exp = tick_add(res->last_valid, resolvers->hold.other);
3888 if (!tick_is_expired(exp, now_ms))
3889 return 1;
3890 }
3891
Emeric Brun34067662021-06-11 10:48:45 +02003892 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003893 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003894
Emeric Brun12ca6582021-06-10 15:25:25 +02003895 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003896}
3897
3898/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003899 * Server Name Resolution error management callback
3900 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003901 * 0 if we can trash answser items.
3902 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003903 *
3904 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003905 */
Emeric Brun08622d32020-12-23 17:41:43 +01003906int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003908 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003909
Christopher Faulet67957bd2017-09-27 11:00:59 +02003910 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003911 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003912 return 0;
3913
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003914 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003915 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003916 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003917 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003918 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003919 return 0;
3920 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003921 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003922
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003923 return 1;
3924}
3925
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003926/*
3927 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003928 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003929 * It returns a pointer to the first server found or NULL if <ip> is not yet
3930 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003931 *
3932 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003933 */
3934struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3935{
3936 struct server *tmpsrv;
3937 struct proxy *be;
3938
3939 if (!srv)
3940 return NULL;
3941
3942 be = srv->proxy;
3943 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003944 /* we found the current server is the same, ignore it */
3945 if (srv == tmpsrv)
3946 continue;
3947
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003948 /* We want to compare the IP in the record with the IP of the servers in the
3949 * same backend, only if:
3950 * * DNS resolution is enabled on the server
3951 * * the hostname used for the resolution by our server is the same than the
3952 * one used for the server found in the backend
3953 * * the server found in the backend is not our current server
3954 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003955 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003956 if ((tmpsrv->hostname_dn == NULL) ||
3957 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003958 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003959 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003960 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003961 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003962 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003963
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003964 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003965 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003966 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003967 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003968 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003969
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003970 /* At this point, we have 2 different servers using the same DNS hostname
3971 * for their respective resolution.
3972 */
3973 if (*ip_family == tmpsrv->addr.ss_family &&
3974 ((tmpsrv->addr.ss_family == AF_INET &&
3975 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3976 (tmpsrv->addr.ss_family == AF_INET6 &&
3977 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003978 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003979 return tmpsrv;
3980 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003981 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003982 }
3983
Emeric Brune9fd6b52017-11-02 17:20:39 +01003984
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003985 return NULL;
3986}
3987
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003988/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3989 * resolver. This is suited for initial address configuration. Returns 0 on
3990 * success otherwise a non-zero error code. In case of error, *err_code, if
3991 * not NULL, is filled up.
3992 */
3993int srv_set_addr_via_libc(struct server *srv, int *err_code)
3994{
3995 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003996 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003997 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003998 return 1;
3999 }
4000 return 0;
4001}
4002
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004003/* Set the server's FDQN (->hostname) from <hostname>.
4004 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004005 *
4006 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004007 */
Emeric Brun08622d32020-12-23 17:41:43 +01004008int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004009{
Emeric Brun08622d32020-12-23 17:41:43 +01004010 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004011 char *hostname_dn;
4012 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004013
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004014 /* Note that the server lock is already held. */
4015 if (!srv->resolvers)
4016 return -1;
4017
Emeric Brun08622d32020-12-23 17:41:43 +01004018 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004019 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01004020 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004021 * and we can't enable it at run time for now.
4022 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01004023 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004024 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004025
4026 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004027 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004028 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02004029 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01004030 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004031 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004032 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004033
Christopher Fauletd83a6df2021-03-12 10:23:05 +01004034 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004035 if (resolution &&
4036 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01004037 resolution->hostname_dn_len == hostname_dn_len &&
4038 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004039 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004040
Willy Tarreau6878f802021-10-20 14:07:31 +02004041 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004042
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004043 free(srv->hostname);
4044 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004045 srv->hostname = strdup(hostname);
4046 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004047 srv->hostname_dn_len = hostname_dn_len;
4048 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004049 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004050
Baptiste Assmann13a92322019-06-07 09:40:55 +02004051 if (srv->flags & SRV_F_NO_RESOLUTION)
4052 goto end;
4053
Emeric Brund30e9a12020-12-23 18:49:16 +01004054 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004055 goto err;
4056
4057 end:
Emeric Brun08622d32020-12-23 17:41:43 +01004058 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004059 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004060 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004061
4062 err:
Emeric Brun08622d32020-12-23 17:41:43 +01004063 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004064 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004065 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004066}
4067
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004068/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4069 * from the state file. This is suited for initial address configuration.
4070 * Returns 0 on success otherwise a non-zero error code. In case of error,
4071 * *err_code, if not NULL, is filled up.
4072 */
4073static int srv_apply_lastaddr(struct server *srv, int *err_code)
4074{
4075 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4076 if (err_code)
4077 *err_code |= ERR_WARN;
4078 return 1;
4079 }
4080 return 0;
4081}
4082
Willy Tarreau25e51522016-11-04 15:10:17 +01004083/* returns 0 if no error, otherwise a combination of ERR_* flags */
4084static int srv_iterate_initaddr(struct server *srv)
4085{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004086 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01004087 int return_code = 0;
4088 int err_code;
4089 unsigned int methods;
4090
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004091 /* If no addr and no hostname set, get the name from the DNS SRV request */
4092 if (!name && srv->srvrq)
4093 name = srv->srvrq->name;
4094
Willy Tarreau25e51522016-11-04 15:10:17 +01004095 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004096 if (!methods) {
4097 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01004098 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4099 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004100 if (srv->resolvers_id) {
4101 /* dns resolution is configured, add "none" to not fail on startup */
4102 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4103 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004104 }
4105
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004106 /* "-dr" : always append "none" so that server addresses resolution
4107 * failures are silently ignored, this is convenient to validate some
4108 * configs out of their environment.
4109 */
4110 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4111 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4112
Willy Tarreau25e51522016-11-04 15:10:17 +01004113 while (methods) {
4114 err_code = 0;
4115 switch (srv_get_next_initaddr(&methods)) {
4116 case SRV_IADDR_LAST:
4117 if (!srv->lastaddr)
4118 continue;
4119 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004120 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004121 return_code |= err_code;
4122 break;
4123
4124 case SRV_IADDR_LIBC:
4125 if (!srv->hostname)
4126 continue;
4127 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004128 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004129 return_code |= err_code;
4130 break;
4131
Willy Tarreau37ebe122016-11-04 15:17:58 +01004132 case SRV_IADDR_NONE:
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004133 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004134 if (return_code) {
Christopher Faulet521d99f2023-07-20 17:25:54 +02004135 ha_notice("could not resolve address '%s', disabling server.\n",
4136 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004137 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004138 return return_code;
4139
Willy Tarreau4310d362016-11-02 15:05:56 +01004140 case SRV_IADDR_IP:
4141 ipcpy(&srv->init_addr, &srv->addr);
4142 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004143 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4144 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004145 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004146 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004147
Willy Tarreau25e51522016-11-04 15:10:17 +01004148 default: /* unhandled method */
4149 break;
4150 }
4151 }
4152
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004153 if (!return_code)
4154 ha_alert("no method found to resolve address '%s'.\n", name);
4155 else
4156 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004157
4158 return_code |= ERR_ALERT | ERR_FATAL;
4159 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004160out:
4161 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004162 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004163 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004164}
4165
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004166/*
4167 * This function parses all backends and all servers within each backend
4168 * and performs servers' addr resolution based on information provided by:
4169 * - configuration file
4170 * - server-state file (states provided by an 'old' haproxy process)
4171 *
4172 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4173 */
4174int srv_init_addr(void)
4175{
4176 struct proxy *curproxy;
4177 int return_code = 0;
4178
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004179 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004180 while (curproxy) {
4181 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004182
4183 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004184 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004185 goto srv_init_addr_next;
4186
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004187 for (srv = curproxy->srv; srv; srv = srv->next) {
4188 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004189 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004190 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004191 reset_usermsgs_ctx();
4192 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004193
4194 srv_init_addr_next:
4195 curproxy = curproxy->next;
4196 }
4197
4198 return return_code;
4199}
4200
Willy Tarreau46b7f532018-08-21 11:54:26 +02004201/*
4202 * Must be called with the server lock held.
4203 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004204const char *srv_update_fqdn(struct server *server, const char *fqdn, const char *updater, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004205{
4206
Willy Tarreau83061a82018-07-13 11:56:34 +02004207 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004208
4209 msg = get_trash_chunk();
4210 chunk_reset(msg);
4211
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004212 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004213 chunk_appendf(msg, "no need to change the FDQN");
4214 goto out;
4215 }
4216
4217 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4218 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4219 goto out;
4220 }
4221
4222 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4223 server->proxy->id, server->id, server->hostname, fqdn);
4224
Emeric Brun08622d32020-12-23 17:41:43 +01004225 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004226 chunk_reset(msg);
4227 chunk_appendf(msg, "could not update %s/%s FQDN",
4228 server->proxy->id, server->id);
4229 goto out;
4230 }
4231
4232 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004233 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004234
4235 out:
4236 if (updater)
4237 chunk_appendf(msg, " by '%s'", updater);
4238 chunk_appendf(msg, "\n");
4239
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004240 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004241}
4242
4243
Willy Tarreau21b069d2016-11-23 17:15:08 +01004244/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4245 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004246 * on the CLI, sets the CLI's state to CLI_ST_PRINT and returns NULL. This is only
Willy Tarreau21b069d2016-11-23 17:15:08 +01004247 * used for CLI commands requiring a server name.
4248 * Important: the <arg> is modified to remove the '/'.
4249 */
4250struct server *cli_find_server(struct appctx *appctx, char *arg)
4251{
4252 struct proxy *px;
4253 struct server *sv;
4254 char *line;
4255
4256 /* split "backend/server" and make <line> point to server */
4257 for (line = arg; *line; line++)
4258 if (*line == '/') {
4259 *line++ = '\0';
4260 break;
4261 }
4262
4263 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004264 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004265 return NULL;
4266 }
4267
4268 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004269 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004270 return NULL;
4271 }
4272
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004273 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004274 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004275 return NULL;
4276 }
4277
4278 return sv;
4279}
4280
William Lallemand222baf22016-11-19 02:00:33 +01004281
Willy Tarreau46b7f532018-08-21 11:54:26 +02004282/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004283static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004284{
4285 struct server *sv;
4286 const char *warning;
4287
4288 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4289 return 1;
4290
4291 sv = cli_find_server(appctx, args[2]);
4292 if (!sv)
4293 return 1;
4294
4295 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004296 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004297 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004298 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004299 if (warning)
4300 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004301 }
4302 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004303 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004304 if (strcmp(args[4], "ready") == 0)
4305 srv_adm_set_ready(sv);
4306 else if (strcmp(args[4], "drain") == 0)
4307 srv_adm_set_drain(sv);
4308 else if (strcmp(args[4], "maint") == 0)
4309 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004310 else
4311 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004312 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004313 }
4314 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004315 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004316 if (sv->track)
4317 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004318 else if (strcmp(args[4], "up") == 0) {
4319 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004320 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004321 }
4322 else if (strcmp(args[4], "stopping") == 0) {
4323 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004324 srv_set_stopping(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004325 }
4326 else if (strcmp(args[4], "down") == 0) {
4327 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004328 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004329 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004330 else
4331 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004332 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004333 }
4334 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004335 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004336 if (!(sv->agent.state & CHK_ST_ENABLED))
4337 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004338 else if (strcmp(args[4], "up") == 0) {
4339 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004340 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004341 }
4342 else if (strcmp(args[4], "down") == 0) {
4343 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004344 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004345 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004346 else
4347 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004348 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004349 }
Misiek2da082d2017-01-09 09:40:42 +01004350 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004351 char *addr = NULL;
4352 char *port = NULL;
4353 if (strlen(args[4]) == 0) {
4354 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4355 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004356 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004357 }
4358 addr = args[4];
4359 if (strcmp(args[5], "port") == 0)
4360 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004361 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004362 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004363 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004364 if (warning)
4365 cli_msg(appctx, LOG_WARNING, warning);
4366 }
4367 else if (strcmp(args[3], "agent-port") == 0) {
4368 char *port = NULL;
4369 if (strlen(args[4]) == 0) {
4370 cli_err(appctx, "set server <b>/<s> agent-port requires"
4371 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004372 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004373 }
4374 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004375 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004376 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004377 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004378 if (warning)
4379 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004380 }
4381 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004382 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004383 if (!(sv->agent.state & CHK_ST_ENABLED))
4384 cli_err(appctx, "agent checks are not enabled on this server.\n");
4385 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004386 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004387 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004388 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004389 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004390 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004391 else if (strcmp(args[3], "check-addr") == 0) {
4392 char *addr = NULL;
4393 char *port = NULL;
4394 if (strlen(args[4]) == 0) {
4395 cli_err(appctx, "set server <b>/<s> check-addr requires"
4396 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004397 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004398 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004399 addr = args[4];
4400 if (strcmp(args[5], "port") == 0)
4401 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004402 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004403 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004404 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004405 if (warning)
4406 cli_msg(appctx, LOG_WARNING, warning);
4407 }
4408 else if (strcmp(args[3], "check-port") == 0) {
4409 char *port = NULL;
4410 if (strlen(args[4]) == 0) {
4411 cli_err(appctx, "set server <b>/<s> check-port requires"
4412 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004413 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004414 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004415 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004416 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004417 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004418 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004419 if (warning)
4420 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004421 }
4422 else if (strcmp(args[3], "addr") == 0) {
4423 char *addr = NULL;
4424 char *port = NULL;
4425 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004426 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004427 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004428 }
4429 else {
4430 addr = args[4];
4431 }
4432 if (strcmp(args[5], "port") == 0) {
4433 port = args[6];
4434 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004435 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004436 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004437 if (warning)
4438 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004439 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004440 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004441 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004442 else if (strcmp(args[3], "fqdn") == 0) {
4443 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004444 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004445 goto out;
4446 }
4447 if (!sv->resolvers) {
4448 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4449 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004450 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004451 if (sv->srvrq) {
4452 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004453 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004454 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004455 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4456 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004457 /* ensure runtime resolver will process this new fqdn */
4458 if (sv->flags & SRV_F_NO_RESOLUTION) {
4459 sv->flags &= ~SRV_F_NO_RESOLUTION;
4460 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004461 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004462 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004463 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004464 if (warning)
4465 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004466 }
William Dauchyf6370442020-11-14 19:25:33 +01004467 else if (strcmp(args[3], "ssl") == 0) {
4468#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004469 if (sv->flags & SRV_F_DYNAMIC) {
4470 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4471 goto out;
4472 }
4473
William Dauchyf6370442020-11-14 19:25:33 +01004474 if (sv->ssl_ctx.ctx == NULL) {
4475 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4476 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004477 goto out;
4478 }
4479
4480 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4481 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004482 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004483 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004484 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004485 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004486 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004487 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004488 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004489 }
4490 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004491 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004492 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4493#else
4494 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4495#endif
4496 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004497 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004498 "usage: set server <backend>/<server> "
4499 "addr | agent | agent-addr | agent-port | agent-send | "
4500 "check-addr | check-port | fqdn | health | ssl | "
4501 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004502 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004503 out:
William Lallemand222baf22016-11-19 02:00:33 +01004504 return 1;
4505}
4506
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004507static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004508{
William Lallemand6b160942016-11-22 12:34:35 +01004509 struct proxy *px;
4510 struct server *sv;
4511 char *line;
4512
4513
4514 /* split "backend/server" and make <line> point to server */
4515 for (line = args[2]; *line; line++)
4516 if (*line == '/') {
4517 *line++ = '\0';
4518 break;
4519 }
4520
Willy Tarreau9d008692019-08-09 11:21:01 +02004521 if (!*line)
4522 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004523
Willy Tarreau9d008692019-08-09 11:21:01 +02004524 if (!get_backend_server(args[2], line, &px, &sv))
4525 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004526
4527 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004528 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4529 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004530 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004531 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004532 return 1;
4533}
4534
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004535/* Parse a "set weight" command.
4536 *
4537 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004538 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004539static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004540{
4541 struct server *sv;
4542 const char *warning;
4543
4544 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4545 return 1;
4546
4547 sv = cli_find_server(appctx, args[2]);
4548 if (!sv)
4549 return 1;
4550
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004551 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4552
William Lallemand6b160942016-11-22 12:34:35 +01004553 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004554 if (warning)
4555 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004556
4557 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4558
William Lallemand6b160942016-11-22 12:34:35 +01004559 return 1;
4560}
4561
Willy Tarreau46b7f532018-08-21 11:54:26 +02004562/* parse a "set maxconn server" command. It always returns 1.
4563 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004564 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004565 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004566static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004567{
4568 struct server *sv;
4569 const char *warning;
4570
4571 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4572 return 1;
4573
4574 sv = cli_find_server(appctx, args[3]);
4575 if (!sv)
4576 return 1;
4577
Amaury Denoyelle02742862021-06-18 11:11:36 +02004578 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4579
Willy Tarreaub8026272016-11-23 11:26:56 +01004580 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004581 if (warning)
4582 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004583
Amaury Denoyelle02742862021-06-18 11:11:36 +02004584 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4585
Willy Tarreaub8026272016-11-23 11:26:56 +01004586 return 1;
4587}
William Lallemand6b160942016-11-22 12:34:35 +01004588
Willy Tarreau46b7f532018-08-21 11:54:26 +02004589/* parse a "disable agent" command. It always returns 1.
4590 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004591 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004593static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004594{
4595 struct server *sv;
4596
4597 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4598 return 1;
4599
4600 sv = cli_find_server(appctx, args[2]);
4601 if (!sv)
4602 return 1;
4603
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004604 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004605 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004606 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004607 return 1;
4608}
4609
Willy Tarreau46b7f532018-08-21 11:54:26 +02004610/* parse a "disable health" command. It always returns 1.
4611 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004612 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004613 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004614static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004615{
4616 struct server *sv;
4617
4618 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4619 return 1;
4620
4621 sv = cli_find_server(appctx, args[2]);
4622 if (!sv)
4623 return 1;
4624
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004625 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004626 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004627 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004628 return 1;
4629}
4630
Willy Tarreau46b7f532018-08-21 11:54:26 +02004631/* parse a "disable server" command. It always returns 1.
4632 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004633 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004634 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004635static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004636{
4637 struct server *sv;
4638
4639 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4640 return 1;
4641
4642 sv = cli_find_server(appctx, args[2]);
4643 if (!sv)
4644 return 1;
4645
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004646 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004647 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004648 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004649 return 1;
4650}
4651
Willy Tarreau46b7f532018-08-21 11:54:26 +02004652/* parse a "enable agent" command. It always returns 1.
4653 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004654 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004655 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004656static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004657{
4658 struct server *sv;
4659
4660 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4661 return 1;
4662
4663 sv = cli_find_server(appctx, args[2]);
4664 if (!sv)
4665 return 1;
4666
Willy Tarreau9d008692019-08-09 11:21:01 +02004667 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4668 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004669
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004670 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004671 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004672 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004673 return 1;
4674}
4675
Willy Tarreau46b7f532018-08-21 11:54:26 +02004676/* parse a "enable health" command. It always returns 1.
4677 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004678 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004679 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004680static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004681{
4682 struct server *sv;
4683
4684 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4685 return 1;
4686
4687 sv = cli_find_server(appctx, args[2]);
4688 if (!sv)
4689 return 1;
4690
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004691 if (!(sv->check.state & CHK_ST_CONFIGURED))
4692 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4693
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004694 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004695 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004696 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004697 return 1;
4698}
4699
Willy Tarreau46b7f532018-08-21 11:54:26 +02004700/* parse a "enable server" command. It always returns 1.
4701 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004702 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004703 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004704static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004705{
4706 struct server *sv;
4707
4708 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4709 return 1;
4710
4711 sv = cli_find_server(appctx, args[2]);
4712 if (!sv)
4713 return 1;
4714
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004715 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004716 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004717 if (!(sv->flags & SRV_F_COOKIESET)
4718 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4719 sv->cookie)
4720 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004721 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004722 return 1;
4723}
4724
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004725/* Allocates data structure related to load balancing for the server <sv>. It
4726 * is only required for dynamic servers.
4727 *
4728 * At the moment, the server lock is not used as this function is only called
4729 * for a dynamic server not yet registered.
4730 *
4731 * Returns 1 on success, 0 on allocation failure.
4732 */
4733static int srv_alloc_lb(struct server *sv, struct proxy *be)
4734{
4735 int node;
4736
4737 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4738 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4739 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4740 sv->lb_nodes_now = 0;
4741
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004742 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4743 ((be->lbprm.algo & (BE_LB_KIND | BE_LB_HASH_TYPE)) == (BE_LB_KIND_HI | BE_LB_HASH_CONS))) {
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004744 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4745
4746 if (!sv->lb_nodes)
4747 return 0;
4748
4749 for (node = 0; node < sv->lb_nodes_tot; node++) {
4750 sv->lb_nodes[node].server = sv;
4751 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4752 }
4753 }
4754
4755 return 1;
4756}
4757
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004758/* updates the server's weight during a warmup stage. Once the final weight is
4759 * reached, the task automatically stops. Note that any server status change
4760 * must have updated s->last_change accordingly.
4761 */
4762static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4763{
4764 struct server *s = context;
4765
4766 /* by default, plan on stopping the task */
4767 t->expire = TICK_ETERNITY;
4768 if ((s->next_admin & SRV_ADMF_MAINT) ||
4769 (s->next_state != SRV_ST_STARTING))
4770 return t;
4771
4772 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4773
4774 /* recalculate the weights and update the state */
4775 server_recalc_eweight(s, 1);
4776
4777 /* probably that we can refill this server with a bit more connections */
4778 pendconn_grab_from_px(s);
4779
4780 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4781
4782 /* get back there in 1 second or 1/20th of the slowstart interval,
4783 * whichever is greater, resulting in small 5% steps.
4784 */
4785 if (s->next_state == SRV_ST_STARTING)
4786 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4787 return t;
4788}
4789
4790/* Allocate the slowstart task if the server is configured with a slowstart
4791 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4792 *
4793 * Returns 0 on success else non-zero.
4794 */
4795static int init_srv_slowstart(struct server *srv)
4796{
4797 struct task *t;
4798
4799 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004800 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004801 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4802 return ERR_ALERT | ERR_FATAL;
4803 }
4804
4805 /* We need a warmup task that will be called when the server
4806 * state switches from down to up.
4807 */
4808 srv->warmup = t;
4809 t->process = server_warmup;
4810 t->context = srv;
4811
4812 /* server can be in this state only because of */
4813 if (srv->next_state == SRV_ST_STARTING) {
4814 task_schedule(srv->warmup,
4815 tick_add(now_ms,
Willy Tarreau69530f52023-04-28 09:16:15 +02004816 MS_TO_TICKS(MAX(1000, (ns_to_sec(now_ns) - srv->last_change)) / 20)));
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004817 }
4818 }
4819
4820 return ERR_NONE;
4821}
4822REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4823
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004824/* Memory allocation and initialization of the per_thr field.
4825 * Returns 0 if the field has been successfully initialized, -1 on failure.
4826 */
4827int srv_init_per_thr(struct server *srv)
4828{
4829 int i;
4830
4831 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004832 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4833 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004834 return -1;
4835
4836 for (i = 0; i < global.nbthread; i++) {
4837 srv->per_thr[i].idle_conns = EB_ROOT;
4838 srv->per_thr[i].safe_conns = EB_ROOT;
4839 srv->per_thr[i].avail_conns = EB_ROOT;
4840 MT_LIST_INIT(&srv->per_thr[i].streams);
4841 }
4842
4843 return 0;
4844}
4845
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004846/* Parse a "add server" command
4847 * Returns 0 if the server has been successfully initialized, 1 on failure.
4848 */
4849static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4850{
4851 struct proxy *be;
4852 struct server *srv;
4853 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004854 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004855 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004856 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4857
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004858 usermsgs_clr("CLI");
4859
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004860 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4861 return 1;
4862
4863 ++args;
4864
4865 sv_name = be_name = args[1];
4866 /* split backend/server arg */
4867 while (*sv_name && *(++sv_name)) {
4868 if (*sv_name == '/') {
4869 *sv_name = '\0';
4870 ++sv_name;
4871 break;
4872 }
4873 }
4874
4875 if (!*sv_name)
4876 return cli_err(appctx, "Require 'backend/server'.");
4877
Amaury Denoyellecece9182021-04-20 17:09:08 +02004878 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004879 if (!be)
4880 return cli_err(appctx, "No such backend.");
4881
4882 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004883 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004884 return 1;
4885 }
4886
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004887 /* At this point, some operations might not be thread-safe anymore. This
4888 * might be the case for parsing handlers which were designed to run
4889 * only at the starting stage on single-thread mode.
4890 *
4891 * Activate thread isolation to ensure thread-safety.
4892 */
4893 thread_isolate();
4894
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004895 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004896 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4897 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004898 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004899
4900 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004901 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004902
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004903 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004904 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004905 }
4906
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004907 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4908 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004909 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004910
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004911 /* A dynamic server does not currently support resolution.
4912 *
4913 * Initialize it explicitly to the "none" method to ensure no
4914 * resolution will ever be executed.
4915 */
4916 srv->init_addr_methods = SRV_IADDR_NONE;
4917
Amaury Denoyelle30467232021-03-12 18:03:27 +01004918 if (srv->mux_proto) {
Aurelien DARRAGONd0086f72023-10-19 16:15:50 +02004919 int proto_mode = conn_pr_mode_to_proto_mode(be->mode);
4920 const struct mux_proto_list *mux_ent;
4921
4922 mux_ent = conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, proto_mode);
4923
4924 if (!mux_ent || !isteq(mux_ent->token, srv->mux_proto->token)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004925 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004926 goto out;
4927 }
4928 }
4929
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004930 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004931 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004932 goto out;
4933 }
4934
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004935 if (srv->max_idle_conns != 0) {
4936 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4937 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004938 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004939 goto out;
4940 }
4941 }
4942
4943 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004944 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004945 goto out;
4946 }
4947
4948 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4949 COUNTERS_SV,
4950 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004951 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004952 goto out;
4953 }
4954
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004955 /* ensure minconn/maxconn consistency */
4956 srv_minmax_conn_apply(srv);
4957
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004958 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4959 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004960 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4961 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4962 goto out;
4963 }
4964 }
4965
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004966 if (srv->trackit) {
4967 if (srv_apply_track(srv, be))
4968 goto out;
4969 }
4970
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004971 /* Init check/agent if configured. The check is manually disabled
4972 * because a dynamic server is started in a disable state. It must be
4973 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004974 */
4975 if (srv->do_check) {
4976 if (init_srv_check(srv))
4977 goto out;
4978
4979 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004980 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004981
4982 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004983 if (init_srv_agent_check(srv))
4984 goto out;
4985
4986 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004987 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004988
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004989 /* Init slowstart if needed. */
4990 if (init_srv_slowstart(srv))
4991 goto out;
4992
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004993 /* Attach the server to the end of the proxy linked list. Note that this
4994 * operation is not thread-safe so this is executed under thread
4995 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004996 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004997 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004998 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004999
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005000 /* TODO use a double-linked list for px->srv */
5001 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02005002 struct server *next = be->srv;
5003
5004 while (1) {
5005 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02005006 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005007 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02005008 goto out;
5009 }
5010
5011 if (!next->next)
5012 break;
5013
5014 next = next->next;
5015 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005016
5017 next->next = srv;
5018 }
5019 else {
5020 srv->next = be->srv;
5021 be->srv = srv;
5022 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02005023
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02005024 /* generate the server id if not manually specified */
5025 if (!srv->puid) {
5026 next_id = get_next_id(&be->conf.used_server_id, 1);
5027 if (!next_id) {
5028 ha_alert("Cannot attach server : no id left in proxy\n");
5029 goto out;
5030 }
5031
5032 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02005033 }
Christopher Faulet70f89482021-12-07 18:49:44 +01005034 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02005035
5036 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02005037 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
5038 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005039 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
5040 if (srv->addr_node.key)
5041 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02005042
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005043 /* check if LSB bit (odd bit) is set for reuse_cnt */
5044 if (srv_id_reuse_cnt & 1) {
5045 /* cnt must be increased */
5046 srv_id_reuse_cnt++;
5047 }
5048 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
5049 * save some space
5050 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
5051 */
5052 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
5053
Damien Claisse8ed726b2024-03-27 14:34:25 +00005054 /* generate new server's dynamic cookie if enabled on backend */
5055 if (be->ck_opts & PR_CK_DYNAMIC) {
5056 srv_set_dyncookie(srv);
5057 }
5058
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005059 /* adding server cannot fail when we reach this:
5060 * publishing EVENT_HDL_SUB_SERVER_ADD
5061 */
5062 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
5063
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005064 thread_release();
5065
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005066 /* Start the check task. The server must be fully initialized.
5067 *
5068 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
5069 * need to randomly spread the task interval for dynamic servers.
5070 */
5071 if (srv->check.state & CHK_ST_CONFIGURED) {
5072 if (!start_check_task(&srv->check, 0, 1, 1))
5073 ha_alert("System might be unstable, consider to execute a reload");
5074 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005075 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005076 if (!start_check_task(&srv->agent, 0, 1, 1))
5077 ha_alert("System might be unstable, consider to execute a reload");
5078 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005079
Amaury Denoyelleae8882d2024-03-27 10:50:21 +01005080 if (srv->cklen && be->mode != PR_MODE_HTTP)
5081 ha_warning("Ignoring cookie as HTTP mode is disabled.\n");
5082
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005083 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01005084 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005085
5086 return 0;
5087
5088out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02005089 if (srv) {
5090 if (srv->track)
5091 release_server_track(srv);
5092
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005093 if (srv->check.state & CHK_ST_CONFIGURED)
5094 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005095 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005096 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005097
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02005098 /* remove the server from the proxy linked list */
5099 if (be->srv == srv) {
5100 be->srv = srv->next;
5101 }
5102 else {
5103 struct server *prev;
5104 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
5105 ;
5106 if (prev)
5107 prev->next = srv->next;
5108 }
5109
5110 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02005111
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02005112 thread_release();
5113
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005114 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01005115 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005116
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02005117 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005118 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005119
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005120 return 1;
5121}
5122
Amaury Denoyellee5580432021-04-15 14:41:20 +02005123/* Parse a "del server" command
5124 * Returns 0 if the server has been successfully initialized, 1 on failure.
5125 */
5126static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
5127{
5128 struct proxy *be;
5129 struct server *srv;
5130 char *be_name, *sv_name;
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005131 struct server *prev_del;
Amaury Denoyellee5580432021-04-15 14:41:20 +02005132
5133 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5134 return 1;
5135
5136 ++args;
5137
5138 sv_name = be_name = args[1];
5139 /* split backend/server arg */
5140 while (*sv_name && *(++sv_name)) {
5141 if (*sv_name == '/') {
5142 *sv_name = '\0';
5143 ++sv_name;
5144 break;
5145 }
5146 }
5147
5148 if (!*sv_name)
5149 return cli_err(appctx, "Require 'backend/server'.");
5150
5151 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005152 * requires thread isolation. In addition, any place referencing the
5153 * server about to be deleted would be unsafe after our operation, so
5154 * we must be certain to be alone so that no other thread has even
5155 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005156 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005157 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005158
5159 get_backend_server(be_name, sv_name, &be, &srv);
5160 if (!be) {
5161 cli_err(appctx, "No such backend.");
5162 goto out;
5163 }
5164
5165 if (!srv) {
5166 cli_err(appctx, "No such server.");
5167 goto out;
5168 }
5169
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005170 if (srv->flags & SRV_F_NON_PURGEABLE) {
5171 cli_err(appctx, "This server cannot be removed at runtime due to other configuration elements pointing to it.");
Amaury Denoyellee5580432021-04-15 14:41:20 +02005172 goto out;
5173 }
5174
5175 /* Only servers in maintenance can be deleted. This ensures that the
5176 * server is not present anymore in the lb structures (through
5177 * lbprm.set_server_status_down).
5178 */
5179 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5180 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5181 goto out;
5182 }
5183
5184 /* Ensure that there is no active/idle/pending connection on the server.
5185 *
5186 * TODO idle connections should not prevent server deletion. A proper
5187 * cleanup function should be implemented to be used here.
5188 */
Amaury Denoyelled8514b92024-02-21 15:54:11 +01005189 if (srv->curr_used_conns || srv->curr_idle_conns ||
Aurelien DARRAGON4bc09092023-09-21 14:18:50 +02005190 !eb_is_empty(&srv->queue.head) || srv_has_streams(srv)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005191 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5192 goto out;
5193 }
5194
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005195 /* removing cannot fail anymore when we reach this:
5196 * publishing EVENT_HDL_SUB_SERVER_DEL
5197 */
5198 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5199
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005200 /* remove srv from tracking list */
5201 if (srv->track)
5202 release_server_track(srv);
5203
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005204 /* stop the check task if running */
5205 if (srv->check.state & CHK_ST_CONFIGURED)
5206 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005207 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005208 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005209
5210 /* detach the server from the proxy linked list
5211 * The proxy servers list is currently not protected by a lock, so this
5212 * requires thread_isolate/release.
5213 */
5214
5215 /* be->srv cannot be empty since we have already found the server with
5216 * get_backend_server */
5217 BUG_ON(!be->srv);
5218 if (be->srv == srv) {
5219 be->srv = srv->next;
5220 }
5221 else {
5222 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005223 for (next = be->srv; srv != next->next; next = next->next) {
5224 /* srv cannot be not found since we have already found
5225 * it with get_backend_server */
5226 BUG_ON(!next);
5227 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005228
Amaury Denoyellee5580432021-04-15 14:41:20 +02005229 next->next = srv->next;
5230 }
5231
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005232 /* Some deleted servers could still point to us using their 'next',
5233 * update them as needed
5234 * Please note the small race between the POP and APPEND, although in
5235 * this situation this is not an issue as we are under full thread
5236 * isolation
5237 */
5238 while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) {
5239 /* update its 'next' ptr */
5240 prev_del->next = srv->next;
5241 if (srv->next) {
5242 /* now it is our 'next' responsibility */
5243 MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted);
5244 }
5245 }
5246
5247 /* we ourselves need to inform our 'next' that we will still point it */
5248 if (srv->next)
5249 MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted);
5250
Amaury Denoyellee5580432021-04-15 14:41:20 +02005251 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005252 eb32_delete(&srv->conf.id);
5253 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005254 if (srv->addr_node.key)
5255 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005256
5257 /* remove srv from idle_node tree for idle conn cleanup */
5258 eb32_delete(&srv->idle_node);
5259
Aurelien DARRAGON75b9d1c2023-01-24 14:40:01 +01005260 /* flag the server as deleted
5261 * (despite the server being removed from primary server list,
5262 * one could still access the server data from a valid ptr)
5263 * Deleted flag helps detecting when a server is in transient removal
5264 * state.
5265 * ie: removed from the list but not yet freed/purged from memory.
5266 */
5267 srv->flags |= SRV_F_DELETED;
5268
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005269 /* set LSB bit (odd bit) for reuse_cnt */
5270 srv_id_reuse_cnt |= 1;
5271
Amaury Denoyellee5580432021-04-15 14:41:20 +02005272 thread_release();
5273
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005274 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005275 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005276
5277 cli_msg(appctx, LOG_INFO, "Server deleted.");
5278
5279 return 0;
5280
5281out:
5282 thread_release();
5283
5284 return 1;
5285}
5286
William Lallemand222baf22016-11-19 02:00:33 +01005287/* register cli keywords */
5288static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005289 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5290 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005291 { { "disable", "server", NULL }, "disable server (DEPRECATED) : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL },
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005292 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5293 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005294 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5295 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5296 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5297 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5298 { { "set", "weight", NULL }, "set weight <bk>/<srv> (DEPRECATED) : change a server's weight (use 'set server' instead)", cli_parse_set_weight },
Amaury Denoyelle76e8b702022-03-09 15:07:31 +01005299 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5300 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005301 {{},}
5302}};
5303
Willy Tarreau0108d902018-11-25 19:14:37 +01005304INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005305
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005306/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5307 * field is used to retrieve the identifier of the tracked server, either with
5308 * the format "proxy/server" or just "server". <curproxy> must point to the
5309 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5310 * to find the tracked server.
5311 *
5312 * Returns 0 if the server track has been activated else non-zero.
5313 *
5314 * Not thread-safe.
5315 */
5316int srv_apply_track(struct server *srv, struct proxy *curproxy)
5317{
5318 struct proxy *px;
5319 struct server *strack, *loop;
5320 char *pname, *sname;
5321
5322 if (!srv->trackit)
5323 return 1;
5324
5325 pname = srv->trackit;
5326 sname = strrchr(pname, '/');
5327
5328 if (sname) {
5329 *sname++ = '\0';
5330 }
5331 else {
5332 sname = pname;
5333 pname = NULL;
5334 }
5335
5336 if (pname) {
5337 px = proxy_be_by_name(pname);
5338 if (!px) {
5339 ha_alert("unable to find required proxy '%s' for tracking.\n",
5340 pname);
5341 return 1;
5342 }
5343 }
5344 else {
5345 px = curproxy;
5346 }
5347
5348 strack = findserver(px, sname);
5349 if (!strack) {
5350 ha_alert("unable to find required server '%s' for tracking.\n",
5351 sname);
5352 return 1;
5353 }
5354
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005355 if (strack->flags & SRV_F_DYNAMIC) {
5356 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5357 px->id, strack->id);
5358 return 1;
5359 }
5360
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005361 if (!strack->do_check && !strack->do_agent && !strack->track &&
5362 !strack->trackit) {
5363 ha_alert("unable to use %s/%s for "
5364 "tracking as it does not have any check nor agent enabled.\n",
5365 px->id, strack->id);
5366 return 1;
5367 }
5368
5369 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5370 ;
5371
5372 if (srv == strack || loop) {
5373 ha_alert("unable to track %s/%s as it "
5374 "belongs to a tracking chain looping back to %s/%s.\n",
5375 px->id, strack->id, px->id,
5376 srv == strack ? strack->id : loop->id);
5377 return 1;
5378 }
5379
5380 if (curproxy != px &&
5381 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5382 ha_alert("unable to use %s/%s for"
5383 "tracking: disable-on-404 option inconsistency.\n",
5384 px->id, strack->id);
5385 return 1;
5386 }
5387
5388 srv->track = strack;
5389 srv->tracknext = strack->trackers;
5390 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005391 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005392
5393 ha_free(&srv->trackit);
5394
5395 return 0;
5396}
5397
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005398/* This function propagates srv state change to lb algorithms */
5399static void srv_lb_propagate(struct server *s)
5400{
5401 struct proxy *px = s->proxy;
5402
5403 if (px->lbprm.update_server_eweight)
5404 px->lbprm.update_server_eweight(s);
5405 else if (srv_willbe_usable(s)) {
5406 if (px->lbprm.set_server_status_up)
5407 px->lbprm.set_server_status_up(s);
5408 }
5409 else {
5410 if (px->lbprm.set_server_status_down)
5411 px->lbprm.set_server_status_down(s);
5412 }
5413}
5414
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005415/* directly update server state based on an operational change
5416 * (compare current and next state to know which transition to apply)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005417 *
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005418 * The function returns the number of requeued sessions (either taken by
5419 * the server or redispatched to others servers) due to the server state
5420 * change.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005421 */
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005422static int _srv_update_status_op(struct server *s, enum srv_op_st_chg_cause cause)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005423{
Willy Tarreau83061a82018-07-13 11:56:34 +02005424 struct buffer *tmptrash = NULL;
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005425 int log_level;
5426 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5427 int xferred = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005428
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005429 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5430 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005431
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005432 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5433 srv_shutdown_streams(s, SF_ERR_DOWN);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005434
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005435 /* we might have streams queued on this server and waiting for
5436 * a connection. Those which are redispatchable will be queued
5437 * to another server or to the proxy itself.
5438 */
5439 xferred = pendconn_redistribute(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005440
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005441 tmptrash = alloc_trash_chunk();
5442 if (tmptrash) {
5443 chunk_printf(tmptrash,
5444 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5445 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005446
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005447 srv_append_op_chg_cause(tmptrash, s, cause);
5448 srv_append_more(tmptrash, s, xferred, 0);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005449
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005450 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005451
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005452 /* we don't send an alert if the server was previously paused */
5453 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
5454 send_log(s->proxy, log_level, "%s.\n",
5455 tmptrash->area);
5456 send_email_alert(s, log_level, "%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005457 tmptrash->area);
Aurelien DARRAGON0d2f1ac2023-05-15 18:03:35 +02005458 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005459 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005460 }
5461 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5462 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005463
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005464 /* we might have streams queued on this server and waiting for
5465 * a connection. Those which are redispatchable will be queued
5466 * to another server or to the proxy itself.
5467 */
5468 xferred = pendconn_redistribute(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005469
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005470 tmptrash = alloc_trash_chunk();
5471 if (tmptrash) {
5472 chunk_printf(tmptrash,
5473 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5474 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005475
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005476 srv_append_op_chg_cause(tmptrash, s, cause);
5477 srv_append_more(tmptrash, s, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005478
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005479 ha_warning("%s.\n", tmptrash->area);
5480 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5481 tmptrash->area);
5482 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005483 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005484 }
5485 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5486 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005487
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005488 if (s->next_state == SRV_ST_STARTING && s->warmup)
5489 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005490
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005491 server_recalc_eweight(s, 0);
5492 /* now propagate the status change to any LB algorithms */
5493 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005494
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005495 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5496 * and it's not a backup server and its effective weight is > 0,
5497 * then it can accept new connections, so we shut down all streams
5498 * on all backup servers.
5499 */
5500 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5501 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5502 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005503
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005504 /* check if we can handle some connections queued at the proxy. We
5505 * will take as many as we can handle.
5506 */
5507 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005508
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005509 tmptrash = alloc_trash_chunk();
5510 if (tmptrash) {
5511 chunk_printf(tmptrash,
5512 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5513 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005514
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005515 srv_append_op_chg_cause(tmptrash, s, cause);
5516 srv_append_more(tmptrash, s, xferred, 0);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005517
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005518 ha_warning("%s.\n", tmptrash->area);
5519 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5520 tmptrash->area);
5521 send_email_alert(s, LOG_NOTICE, "%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005522 tmptrash->area);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005523 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005524 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005525 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005526 else if (s->cur_eweight != s->next_eweight) {
5527 /* now propagate the status change to any LB algorithms */
5528 srv_lb_propagate(s);
5529 }
5530 return xferred;
5531}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005532
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005533/* deduct and update server state from an administrative change
5534 * (use current and next admin to deduct the administrative transition that
5535 * may result in server state update)
5536 *
5537 * The function returns the number of requeued sessions (either taken by
5538 * the server or redispatched to others servers) due to the server state
5539 * change.
5540 */
5541static int _srv_update_status_adm(struct server *s, enum srv_adm_st_chg_cause cause)
5542{
5543 struct buffer *tmptrash = NULL;
5544 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5545 int xferred = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005546
5547 /* Maintenance must also disable health checks */
5548 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5549 if (s->check.state & CHK_ST_ENABLED) {
5550 s->check.state |= CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005551 s->check.health = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005552 }
5553
5554 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005555 tmptrash = alloc_trash_chunk();
5556 if (tmptrash) {
5557 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005558 "%sServer %s/%s was DOWN and now enters maintenance",
5559 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005560 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005561 srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005562
Olivier Houchard796a2b32017-10-24 17:42:47 +02005563 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005564 ha_warning("%s.\n", tmptrash->area);
5565 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5566 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005567 }
5568 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005569 }
5570 }
5571 else { /* server was still running */
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005572 s->check.health = 0; /* failure */
Emeric Brune3114802017-12-21 14:42:26 +01005573
5574 s->next_state = SRV_ST_STOPPED;
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005575 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005576
Emeric Brun64cc49c2017-10-03 14:46:45 +02005577 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5578 srv_shutdown_streams(s, SF_ERR_DOWN);
5579
William Dauchy6318d332020-05-02 21:52:36 +02005580 /* force connection cleanup on the given server */
5581 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005582 /* we might have streams queued on this server and waiting for
5583 * a connection. Those which are redispatchable will be queued
5584 * to another server or to the proxy itself.
5585 */
5586 xferred = pendconn_redistribute(s);
5587
5588 tmptrash = alloc_trash_chunk();
5589 if (tmptrash) {
5590 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005591 "%sServer %s/%s is going DOWN for maintenance",
Emeric Brun64cc49c2017-10-03 14:46:45 +02005592 s->flags & SRV_F_BACKUP ? "Backup " : "",
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005593 s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005594 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005595 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005596
5597 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005598 ha_warning("%s.\n", tmptrash->area);
5599 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5600 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005601 }
5602 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005603 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005604 }
5605 }
5606 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5607 /* OK here we're leaving maintenance, we have many things to check,
5608 * because the server might possibly be coming back up depending on
5609 * its state. In practice, leaving maintenance means that we should
5610 * immediately turn to UP (more or less the slowstart) under the
5611 * following conditions :
5612 * - server is neither checked nor tracked
5613 * - server tracks another server which is not checked
5614 * - server tracks another server which is already up
5615 * Which sums up as something simpler :
5616 * "either the tracking server is up or the server's checks are disabled
5617 * or up". Otherwise we only re-enable health checks. There's a special
5618 * case associated to the stopping state which can be inherited. Note
5619 * that the server might still be in drain mode, which is naturally dealt
5620 * with by the lower level functions.
5621 */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005622 if (s->check.state & CHK_ST_ENABLED) {
5623 s->check.state &= ~CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005624 s->check.health = s->check.rise; /* start OK but check immediately */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005625 }
5626
5627 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5628 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5629 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5630 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5631 s->next_state = SRV_ST_STOPPING;
5632 }
5633 else {
5634 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005635 if (s->slowstart > 0) {
5636 if (s->warmup)
5637 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5638 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005639 else
5640 s->next_state = SRV_ST_RUNNING;
5641 }
5642
5643 }
5644
5645 tmptrash = alloc_trash_chunk();
5646 if (tmptrash) {
5647 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5648 chunk_printf(tmptrash,
5649 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5650 s->flags & SRV_F_BACKUP ? "Backup " : "",
5651 s->proxy->id, s->id,
5652 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5653 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5654 }
5655 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5656 chunk_printf(tmptrash,
5657 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5658 s->flags & SRV_F_BACKUP ? "Backup " : "",
5659 s->proxy->id, s->id, s->hostname,
5660 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5661 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5662 }
5663 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5664 chunk_printf(tmptrash,
5665 "%sServer %s/%s is %s/%s (leaving maintenance)",
5666 s->flags & SRV_F_BACKUP ? "Backup " : "",
5667 s->proxy->id, s->id,
5668 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5669 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5670 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005671 ha_warning("%s.\n", tmptrash->area);
5672 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5673 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005674 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005675 }
5676
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005677 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005678 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005679 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005680
Willy Tarreau6a78e612018-08-07 10:14:53 +02005681 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5682 * and it's not a backup server and its effective weight is > 0,
5683 * then it can accept new connections, so we shut down all streams
5684 * on all backup servers.
5685 */
5686 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5687 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5688 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5689
5690 /* check if we can handle some connections queued at the proxy. We
5691 * will take as many as we can handle.
5692 */
5693 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005694 }
5695 else if (s->next_admin & SRV_ADMF_MAINT) {
5696 /* remaining in maintenance mode, let's inform precisely about the
5697 * situation.
5698 */
5699 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5700 tmptrash = alloc_trash_chunk();
5701 if (tmptrash) {
5702 chunk_printf(tmptrash,
5703 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5704 s->flags & SRV_F_BACKUP ? "Backup " : "",
5705 s->proxy->id, s->id);
5706
5707 if (s->track) /* normally it's mandatory here */
5708 chunk_appendf(tmptrash, " via %s/%s",
5709 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005710 ha_warning("%s.\n", tmptrash->area);
5711 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5712 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005713 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005714 }
5715 }
5716 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5717 tmptrash = alloc_trash_chunk();
5718 if (tmptrash) {
5719 chunk_printf(tmptrash,
5720 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5721 s->flags & SRV_F_BACKUP ? "Backup " : "",
5722 s->proxy->id, s->id, s->hostname);
5723
5724 if (s->track) /* normally it's mandatory here */
5725 chunk_appendf(tmptrash, " via %s/%s",
5726 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005727 ha_warning("%s.\n", tmptrash->area);
5728 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5729 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005730 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005731 }
5732 }
5733 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5734 tmptrash = alloc_trash_chunk();
5735 if (tmptrash) {
5736 chunk_printf(tmptrash,
5737 "%sServer %s/%s remains in forced maintenance",
5738 s->flags & SRV_F_BACKUP ? "Backup " : "",
5739 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005740 ha_warning("%s.\n", tmptrash->area);
5741 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5742 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005743 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005744 }
5745 }
5746 /* don't report anything when leaving drain mode and remaining in maintenance */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005747 }
5748
5749 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5750 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5751 /* drain state is applied only if not yet in maint */
5752
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005753 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005754
5755 /* we might have streams queued on this server and waiting for
5756 * a connection. Those which are redispatchable will be queued
5757 * to another server or to the proxy itself.
5758 */
5759 xferred = pendconn_redistribute(s);
5760
5761 tmptrash = alloc_trash_chunk();
5762 if (tmptrash) {
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005763 chunk_printf(tmptrash, "%sServer %s/%s enters drain state",
5764 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005765 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005766 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005767
5768 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005769 ha_warning("%s.\n", tmptrash->area);
5770 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5771 tmptrash->area);
5772 send_email_alert(s, LOG_NOTICE, "%s",
5773 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005774 }
5775 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005776 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005777 }
5778 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5779 /* OK completely leaving drain mode */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005780 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005781
5782 tmptrash = alloc_trash_chunk();
5783 if (tmptrash) {
Aurelien DARRAGON9a1df022023-03-27 10:17:31 +02005784 if (s->cur_admin & SRV_ADMF_FDRAIN) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005785 chunk_printf(tmptrash,
5786 "%sServer %s/%s is %s (leaving forced drain)",
5787 s->flags & SRV_F_BACKUP ? "Backup " : "",
5788 s->proxy->id, s->id,
5789 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5790 }
5791 else {
5792 chunk_printf(tmptrash,
5793 "%sServer %s/%s is %s (leaving drain)",
5794 s->flags & SRV_F_BACKUP ? "Backup " : "",
5795 s->proxy->id, s->id,
5796 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5797 if (s->track) /* normally it's mandatory here */
5798 chunk_appendf(tmptrash, " via %s/%s",
5799 s->track->proxy->id, s->track->id);
5800 }
5801
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005802 ha_warning("%s.\n", tmptrash->area);
5803 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5804 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005805 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005806 }
5807
5808 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005809 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005810 }
5811 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5812 /* remaining in drain mode after removing one of its flags */
5813
5814 tmptrash = alloc_trash_chunk();
5815 if (tmptrash) {
5816 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5817 chunk_printf(tmptrash,
Aurelien DARRAGON977688b2023-05-03 10:36:54 +02005818 "%sServer %s/%s remains in drain mode",
Emeric Brun64cc49c2017-10-03 14:46:45 +02005819 s->flags & SRV_F_BACKUP ? "Backup " : "",
5820 s->proxy->id, s->id);
5821
5822 if (s->track) /* normally it's mandatory here */
5823 chunk_appendf(tmptrash, " via %s/%s",
5824 s->track->proxy->id, s->track->id);
5825 }
5826 else {
5827 chunk_printf(tmptrash,
5828 "%sServer %s/%s remains in forced drain mode",
5829 s->flags & SRV_F_BACKUP ? "Backup " : "",
5830 s->proxy->id, s->id);
5831 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005832 ha_warning("%s.\n", tmptrash->area);
5833 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5834 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005835 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005836 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005837 }
5838 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005839 return xferred;
5840}
5841
5842/*
5843 * This function applies server's status changes.
5844 *
5845 * Must be called with the server lock held. This may also be called at init
5846 * time as the result of parsing the state file, in which case no lock will be
5847 * held, and the server's warmup task can be null.
5848 * <type> should be 0 for operational and 1 for administrative
5849 * <cause> must be srv_op_st_chg_cause enum for operational and
5850 * srv_adm_st_chg_cause enum for administrative
5851 */
5852static void srv_update_status(struct server *s, int type, int cause)
5853{
5854 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5855 enum srv_state srv_prev_state = s->cur_state;
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005856 union {
5857 struct event_hdl_cb_data_server_state state;
Aurelien DARRAGONa163d652023-04-21 18:06:58 +02005858 struct event_hdl_cb_data_server_admin admin;
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005859 struct event_hdl_cb_data_server common;
5860 } cb_data;
5861 int requeued;
5862
5863 /* prepare common server event data */
5864 _srv_event_hdl_prepare(&cb_data.common, s, 0);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005865
Aurelien DARRAGONa163d652023-04-21 18:06:58 +02005866 if (type) {
5867 cb_data.admin.safe.cause = cause;
5868 cb_data.admin.safe.old_admin = s->cur_admin;
5869 cb_data.admin.safe.new_admin = s->next_admin;
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005870 requeued = _srv_update_status_adm(s, cause);
Aurelien DARRAGONa163d652023-04-21 18:06:58 +02005871 cb_data.admin.safe.requeued = requeued;
5872 /* publish admin change */
5873 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADMIN, cb_data.admin, s);
5874 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005875 else
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005876 requeued = _srv_update_status_op(s, cause);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005877
Aurelien DARRAGON22151c72023-04-17 17:45:58 +02005878 /* explicitly commit state changes (even if it was already applied implicitly
5879 * by some lb state change function), so we don't miss anything
5880 */
5881 srv_lb_commit_status(s);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005882
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005883 /* check if server stats must be updated due the the server state change */
5884 if (srv_prev_state != s->cur_state) {
5885 if (srv_prev_state == SRV_ST_STOPPED) {
5886 /* server was down and no longer is */
Willy Tarreau69530f52023-04-28 09:16:15 +02005887 if (s->last_change < ns_to_sec(now_ns)) // ignore negative times
5888 s->down_time += ns_to_sec(now_ns) - s->last_change;
Aurelien DARRAGONc249f6d2023-04-19 22:34:01 +02005889 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, cb_data.common, s);
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005890 }
Aurelien DARRAGON5f80f8b2023-04-19 18:22:21 +02005891 else if (s->cur_state == SRV_ST_STOPPED) {
5892 /* server was up and is currently down */
5893 s->counters.down_trans++;
Aurelien DARRAGONc249f6d2023-04-19 22:34:01 +02005894 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, cb_data.common, s);
Aurelien DARRAGON5f80f8b2023-04-19 18:22:21 +02005895 }
Willy Tarreau69530f52023-04-28 09:16:15 +02005896 s->last_change = ns_to_sec(now_ns);
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005897
5898 /* publish the state change */
5899 _srv_event_hdl_prepare_state(&cb_data.state,
5900 s, type, cause, srv_prev_state, requeued);
5901 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_STATE, cb_data.state, s);
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005902 }
5903
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005904 /* check if backend stats must be updated due to the server state change */
5905 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5906 set_backend_down(s->proxy); /* backend going down */
5907 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) {
5908 /* backend was down and is back up again:
5909 * no helper function, updating last_change and backend downtime stats
5910 */
Willy Tarreau69530f52023-04-28 09:16:15 +02005911 if (s->proxy->last_change < ns_to_sec(now_ns)) // ignore negative times
5912 s->proxy->down_time += ns_to_sec(now_ns) - s->proxy->last_change;
5913 s->proxy->last_change = ns_to_sec(now_ns);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005914 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005915}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005916
Willy Tarreau144f84a2021-03-02 16:09:26 +01005917struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005918{
5919 struct connection *conn;
5920
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005921 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005922 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005923 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005924 }
5925
5926 return task;
5927}
5928
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005929/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005930 * moving them all.
5931 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005932 *
5933 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005934 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005935static int srv_migrate_conns_to_remove(struct eb_root *idle_tree, struct mt_list *toremove_list, int toremove_nb)
Olivier Houchardff1d0922020-06-29 20:15:59 +02005936{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005937 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005938 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005939 int i = 0;
5940
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005941 node = eb_first(idle_tree);
5942 while (node) {
5943 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005944 if (toremove_nb != -1 && i >= toremove_nb)
5945 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005946
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005947 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005948 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005949 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005950 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005951
5952 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005953 }
5954 return i;
5955}
William Dauchy6318d332020-05-02 21:52:36 +02005956/* cleanup connections for a given server
5957 * might be useful when going on forced maintenance or live changing ip/port
5958 */
William Dauchy707ad322020-05-04 13:52:40 +02005959static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005960{
William Dauchy6318d332020-05-02 21:52:36 +02005961 int did_remove;
5962 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005963
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005964 /* nothing to do if pool-max-conn is null */
5965 if (!srv->max_idle_conns)
5966 return;
5967
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005968 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005969 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005970 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005971 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005972 if (srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02005973 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005974 if (srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, -1) > 0)
Olivier Houchardff1d0922020-06-29 20:15:59 +02005975 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005976 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005977 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005978 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005979
5980 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5981 break;
William Dauchy6318d332020-05-02 21:52:36 +02005982 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005983}
5984
5985/* removes an idle conn after updating the server idle conns counters */
5986void srv_release_conn(struct server *srv, struct connection *conn)
5987{
5988 if (conn->flags & CO_FL_LIST_MASK) {
5989 /* The connection is currently in the server's idle list, so tell it
5990 * there's one less connection available in that list.
5991 */
5992 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5993 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5994 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5995 }
5996 else {
5997 /* The connection is not private and not in any server's idle
5998 * list, so decrement the current number of used connections
5999 */
6000 _HA_ATOMIC_DEC(&srv->curr_used_conns);
6001 }
6002
6003 /* Remove the connection from any tree (safe, idle or available) */
6004 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6005 conn_delete_from_tree(&conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01006006 conn->flags &= ~CO_FL_LIST_MASK;
Willy Tarreau260f3242021-10-06 18:30:04 +02006007 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6008}
6009
6010/* retrieve a connection from its <hash> in <tree>
6011 * returns NULL if no connection found
6012 */
6013struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
6014{
Willy Tarreau85223482022-09-29 20:32:43 +02006015 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02006016 struct connection *conn = NULL;
6017 struct conn_hash_node *hash_node = NULL;
6018
Willy Tarreau85223482022-09-29 20:32:43 +02006019 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02006020 if (node) {
6021 hash_node = ebmb_entry(node, struct conn_hash_node, node);
6022 conn = hash_node->conn;
6023 }
6024
6025 return conn;
6026}
6027
6028/* retrieve the next connection sharing the same hash as <conn>
6029 * returns NULL if no connection found
6030 */
6031struct connection *srv_lookup_conn_next(struct connection *conn)
6032{
Willy Tarreau85223482022-09-29 20:32:43 +02006033 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02006034 struct connection *next_conn = NULL;
6035 struct conn_hash_node *hash_node = NULL;
6036
Willy Tarreau85223482022-09-29 20:32:43 +02006037 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006038 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02006039 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006040 next_conn = hash_node->conn;
6041 }
6042
6043 return next_conn;
6044}
6045
6046/* This adds an idle connection to the server's list if the connection is
6047 * reusable, not held by any owner anymore, but still has available streams.
6048 */
6049int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
6050{
6051 /* we try to keep the connection in the server's idle list
6052 * if we don't have too many FD in use, and if the number of
6053 * idle+current conns is lower than what was observed before
6054 * last purge, or if we already don't have idle conns for the
6055 * current thread and we don't exceed last count by global.nbthread.
6056 */
6057 if (!(conn->flags & CO_FL_PRIVATE) &&
6058 srv && srv->pool_purge_delay > 0 &&
6059 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
6060 ha_used_fds < global.tune.pool_high_count &&
6061 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
6062 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
6063 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
6064 (ha_used_fds < global.tune.pool_low_count &&
6065 (srv->curr_used_conns + srv->curr_idle_conns <=
6066 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
6067 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
6068 int retadd;
6069
6070 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
6071 if (retadd > srv->max_idle_conns) {
6072 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
6073 return 0;
6074 }
6075 _HA_ATOMIC_DEC(&srv->curr_used_conns);
6076
6077 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6078 conn_delete_from_tree(&conn->hash_node->node);
6079
6080 if (is_safe) {
6081 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02006082 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006083 _HA_ATOMIC_INC(&srv->curr_safe_nb);
6084 } else {
6085 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02006086 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006087 _HA_ATOMIC_INC(&srv->curr_idle_nb);
6088 }
6089 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6090 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
6091
6092 __ha_barrier_full();
6093 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
6094 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6095 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
6096 srv->idle_node.key = tick_add(srv->pool_purge_delay,
6097 now_ms);
6098 eb32_insert(&idle_conn_srv, &srv->idle_node);
6099 if (!task_in_wq(idle_conn_task) && !
6100 task_in_rq(idle_conn_task)) {
6101 task_schedule(idle_conn_task,
6102 srv->idle_node.key);
6103 }
6104
6105 }
6106 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6107 }
6108 return 1;
6109 }
6110 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02006111}
6112
Willy Tarreau144f84a2021-03-02 16:09:26 +01006113struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006114{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006115 struct server *srv;
6116 struct eb32_node *eb;
6117 int i;
6118 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006119
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006120 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006121 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6122 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006123 int exceed_conns;
6124 int to_kill;
6125 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006126
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006127 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
6128 if (!eb) {
6129 /* we might have reached the end of the tree, typically because
6130 * <now_ms> is in the first half and we're first scanning the last
6131 * half. Let's loop back to the beginning of the tree now.
6132 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006133
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006134 eb = eb32_first(&idle_conn_srv);
6135 if (likely(!eb))
6136 break;
6137 }
6138 if (tick_is_lt(now_ms, eb->key)) {
6139 /* timer not expired yet, revisit it later */
6140 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006141 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006142 }
6143 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006144
6145 /* Calculate how many idle connections we want to kill :
6146 * we want to remove half the difference between the total
6147 * of established connections (used or idle) and the max
6148 * number of used connections.
6149 */
6150 curr_idle = srv->curr_idle_conns;
6151 if (curr_idle == 0)
6152 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006153 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006154 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006155
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006156 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006157 if (srv->est_need_conns < srv->max_used_conns)
6158 srv->est_need_conns = srv->max_used_conns;
6159
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006160 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006161
Willy Tarreau18ed7892020-07-02 19:05:30 +02006162 if (exceed_conns <= 0)
6163 goto remove;
6164
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006165 /* check all threads starting with ours */
6166 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006167 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006168 int j;
6169 int did_remove = 0;
6170
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006171 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6172 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006173
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006174 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006175 j = srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, max_conn);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006176 if (j > 0)
6177 did_remove = 1;
6178 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006179 srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, max_conn - j) > 0)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006180 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006181 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006182
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006183 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006184 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006185
6186 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6187 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006188 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006189remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006190 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006191
6192 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006193 /* There are still more idle connections, add the
6194 * server back in the tree.
6195 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006196 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006197 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006198 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006199 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006200 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006201 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6202
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006203 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006204 return task;
6205}
Olivier Houchard88698d92019-04-16 19:07:22 +02006206
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006207/* Close remaining idle connections. This functions is designed to be run on
6208 * process shutdown. This guarantees a proper socket shutdown to avoid
6209 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6210 * bypassed.
6211 *
6212 * This function is not thread-safe so it must only be called via a global
6213 * deinit function.
6214 */
6215static void srv_close_idle_conns(struct server *srv)
6216{
6217 struct eb_root **cleaned_tree;
6218 int i;
6219
6220 for (i = 0; i < global.nbthread; ++i) {
6221 struct eb_root *conn_trees[] = {
6222 &srv->per_thr[i].idle_conns,
6223 &srv->per_thr[i].safe_conns,
6224 &srv->per_thr[i].avail_conns,
6225 NULL
6226 };
6227
6228 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6229 while (!eb_is_empty(*cleaned_tree)) {
6230 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6231 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6232 struct connection *conn = conn_hash_node->conn;
6233
6234 if (conn->ctrl->ctrl_close)
6235 conn->ctrl->ctrl_close(conn);
6236 ebmb_delete(node);
6237 }
6238 }
6239 }
6240}
6241
6242REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6243
Willy Tarreau76cc6992020-07-01 18:49:24 +02006244/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6245static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006246 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006247 char **err)
6248{
6249 if (too_many_args(1, args, err, NULL))
6250 return -1;
6251
6252 if (strcmp(args[1], "on") == 0)
6253 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6254 else if (strcmp(args[1], "off") == 0)
6255 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6256 else {
6257 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6258 return -1;
6259 }
6260 return 0;
6261}
6262
Olivier Houchard88698d92019-04-16 19:07:22 +02006263/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6264static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006265 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006266 char **err)
6267{
6268 int arg = -1;
6269
6270 if (too_many_args(1, args, err, NULL))
6271 return -1;
6272
6273 if (*(args[1]) != 0)
6274 arg = atoi(args[1]);
6275
6276 if (arg < 0 || arg > 100) {
6277 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6278 return -1;
6279 }
6280
6281 if (args[0][10] == 'h')
6282 global.tune.pool_high_ratio = arg;
6283 else
6284 global.tune.pool_low_ratio = arg;
6285 return 0;
6286}
6287
6288/* config keyword parsers */
6289static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006290 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006291 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6292 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6293 { 0, NULL, NULL }
6294}};
6295
6296INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6297
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006298/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006299 * Local variables:
6300 * c-indent-level: 8
6301 * c-basic-offset: 8
6302 * End:
6303 */