blob: fc6df63e7380d8d25ae888cd1204de1dfdf5d93d [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{
Emeric Brun52a91d32017-08-31 14:41:55 +0200657 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
658 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100659 newsrv->check.state &= ~CHK_ST_PAUSED;
660 newsrv->check.health = newsrv->check.rise;
661 return 0;
662}
663
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100664/* Parse the "error-limit" server keyword */
665static int srv_parse_error_limit(char **args, int *cur_arg,
666 struct proxy *curproxy, struct server *newsrv, char **err)
667{
668 if (!*args[*cur_arg + 1]) {
669 memprintf(err, "'%s' expects an integer argument.",
670 args[*cur_arg]);
671 return ERR_ALERT | ERR_FATAL;
672 }
673
674 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
675
676 if (newsrv->consecutive_errors_limit <= 0) {
677 memprintf(err, "%s has to be > 0.",
678 args[*cur_arg]);
679 return ERR_ALERT | ERR_FATAL;
680 }
681
682 return 0;
683}
684
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200685/* Parse the "ws" keyword */
686static int srv_parse_ws(char **args, int *cur_arg,
687 struct proxy *curproxy, struct server *newsrv, char **err)
688{
689 if (!args[*cur_arg + 1]) {
690 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
691 return ERR_ALERT | ERR_FATAL;
692 }
693
694 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
695 newsrv->ws = SRV_WS_H1;
696 }
697 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
698 newsrv->ws = SRV_WS_H2;
699 }
700 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
701 newsrv->ws = SRV_WS_AUTO;
702 }
703 else {
704 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
705 return ERR_ALERT | ERR_FATAL;
706 }
707
708
709 return 0;
710}
711
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100712/* Parse the "init-addr" server keyword */
713static int srv_parse_init_addr(char **args, int *cur_arg,
714 struct proxy *curproxy, struct server *newsrv, char **err)
715{
716 char *p, *end;
717 int done;
718 struct sockaddr_storage sa;
719
720 newsrv->init_addr_methods = 0;
721 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
722
723 for (p = args[*cur_arg + 1]; *p; p = end) {
724 /* cut on next comma */
725 for (end = p; *end && *end != ','; end++);
726 if (*end)
727 *(end++) = 0;
728
729 memset(&sa, 0, sizeof(sa));
730 if (strcmp(p, "libc") == 0) {
731 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
732 }
733 else if (strcmp(p, "last") == 0) {
734 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
735 }
736 else if (strcmp(p, "none") == 0) {
737 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
738 }
739 else if (str2ip2(p, &sa, 0)) {
740 if (is_addr(&newsrv->init_addr)) {
741 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
742 args[*cur_arg], p);
743 return ERR_ALERT | ERR_FATAL;
744 }
745 newsrv->init_addr = sa;
746 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
747 }
748 else {
749 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
750 args[*cur_arg], p);
751 return ERR_ALERT | ERR_FATAL;
752 }
753 if (!done) {
754 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
755 args[*cur_arg], p);
756 return ERR_ALERT | ERR_FATAL;
757 }
758 }
759
760 return 0;
761}
762
763/* Parse the "log-proto" server keyword */
764static int srv_parse_log_proto(char **args, int *cur_arg,
765 struct proxy *curproxy, struct server *newsrv, char **err)
766{
767 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
768 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
769 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
770 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
771 else {
772 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
773 args[*cur_arg], args[*cur_arg + 1]);
774 return ERR_ALERT | ERR_FATAL;
775 }
776
777 return 0;
778}
779
780/* Parse the "maxconn" server keyword */
781static int srv_parse_maxconn(char **args, int *cur_arg,
782 struct proxy *curproxy, struct server *newsrv, char **err)
783{
784 newsrv->maxconn = atol(args[*cur_arg + 1]);
785 return 0;
786}
787
788/* Parse the "maxqueue" server keyword */
789static int srv_parse_maxqueue(char **args, int *cur_arg,
790 struct proxy *curproxy, struct server *newsrv, char **err)
791{
792 newsrv->maxqueue = atol(args[*cur_arg + 1]);
793 return 0;
794}
795
796/* Parse the "minconn" server keyword */
797static int srv_parse_minconn(char **args, int *cur_arg,
798 struct proxy *curproxy, struct server *newsrv, char **err)
799{
800 newsrv->minconn = atol(args[*cur_arg + 1]);
801 return 0;
802}
803
Willy Tarreau9c538e02019-01-23 10:21:49 +0100804static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
805{
806 char *arg;
807
808 arg = args[*cur_arg + 1];
809 if (!*arg) {
810 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
811 return ERR_ALERT | ERR_FATAL;
812 }
813 newsrv->max_reuse = atoi(arg);
814
815 return 0;
816}
817
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100818static 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 +0100819{
820 const char *res;
821 char *arg;
822 unsigned int time;
823
824 arg = args[*cur_arg + 1];
825 if (!*arg) {
826 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
827 return ERR_ALERT | ERR_FATAL;
828 }
829 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200830 if (res == PARSE_TIME_OVER) {
831 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
832 args[*cur_arg+1], args[*cur_arg]);
833 return ERR_ALERT | ERR_FATAL;
834 }
835 else if (res == PARSE_TIME_UNDER) {
836 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
837 args[*cur_arg+1], args[*cur_arg]);
838 return ERR_ALERT | ERR_FATAL;
839 }
840 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100841 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
842 *res, args[*cur_arg]);
843 return ERR_ALERT | ERR_FATAL;
844 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100845 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100846
847 return 0;
848}
849
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200850static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
851{
852 char *arg;
853
854 arg = args[*cur_arg + 1];
855 if (!*arg) {
856 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
857 return ERR_ALERT | ERR_FATAL;
858 }
859
860 newsrv->low_idle_conns = atoi(arg);
861 return 0;
862}
863
Olivier Houchard006e3102018-12-10 18:30:32 +0100864static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
865{
866 char *arg;
867
868 arg = args[*cur_arg + 1];
869 if (!*arg) {
870 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
871 return ERR_ALERT | ERR_FATAL;
872 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100873
Olivier Houchard006e3102018-12-10 18:30:32 +0100874 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100875 if ((int)newsrv->max_idle_conns < -1) {
876 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
877 return ERR_ALERT | ERR_FATAL;
878 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100879
880 return 0;
881}
882
Willy Tarreaudff55432012-10-10 17:51:05 +0200883/* parse the "id" server keyword */
884static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
885{
886 struct eb32_node *node;
887
888 if (!*args[*cur_arg + 1]) {
889 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
890 return ERR_ALERT | ERR_FATAL;
891 }
892
893 newsrv->puid = atol(args[*cur_arg + 1]);
894 newsrv->conf.id.key = newsrv->puid;
895
896 if (newsrv->puid <= 0) {
897 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
898 return ERR_ALERT | ERR_FATAL;
899 }
900
901 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
902 if (node) {
903 struct server *target = container_of(node, struct server, conf.id);
904 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
905 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
906 target->id);
907 return ERR_ALERT | ERR_FATAL;
908 }
909
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200910 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200911 return 0;
912}
913
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100914/* Parse the "namespace" server keyword */
915static int srv_parse_namespace(char **args, int *cur_arg,
916 struct proxy *curproxy, struct server *newsrv, char **err)
917{
Willy Tarreaue5733232019-05-22 19:24:06 +0200918#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100919 char *arg;
920
921 arg = args[*cur_arg + 1];
922 if (!*arg) {
923 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
924 return ERR_ALERT | ERR_FATAL;
925 }
926
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100927 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100928 /* Use the namespace associated with the connection (if present). */
929 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
930 return 0;
931 }
932
933 /*
934 * As this parser may be called several times for the same 'default-server'
935 * object, or for a new 'server' instance deriving from a 'default-server'
936 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
937 */
938 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
939
940 newsrv->netns = netns_store_lookup(arg, strlen(arg));
941 if (!newsrv->netns)
942 newsrv->netns = netns_store_insert(arg);
943
944 if (!newsrv->netns) {
945 memprintf(err, "Cannot open namespace '%s'", arg);
946 return ERR_ALERT | ERR_FATAL;
947 }
948
949 return 0;
950#else
951 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
952 return ERR_ALERT | ERR_FATAL;
953#endif
954}
955
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100956/* Parse the "no-backup" server keyword */
957static int srv_parse_no_backup(char **args, int *cur_arg,
958 struct proxy *curproxy, struct server *newsrv, char **err)
959{
960 newsrv->flags &= ~SRV_F_BACKUP;
961 return 0;
962}
963
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100964
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100965/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200966static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100967{
968 srv->pp_opts &= ~flags;
969 return 0;
970}
971
972/* Parse the "no-send-proxy" server keyword */
973static int srv_parse_no_send_proxy(char **args, int *cur_arg,
974 struct proxy *curproxy, struct server *newsrv, char **err)
975{
976 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
977}
978
979/* Parse the "no-send-proxy-v2" server keyword */
980static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
981 struct proxy *curproxy, struct server *newsrv, char **err)
982{
983 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
984}
985
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200986/* Parse the "shard" server keyword */
987static int srv_parse_shard(char **args, int *cur_arg,
988 struct proxy *curproxy, struct server *newsrv, char **err)
989{
990 newsrv->shard = atol(args[*cur_arg + 1]);
991 return 0;
992}
993
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200994/* Parse the "no-tfo" server keyword */
995static int srv_parse_no_tfo(char **args, int *cur_arg,
996 struct proxy *curproxy, struct server *newsrv, char **err)
997{
998 newsrv->flags &= ~SRV_F_FASTOPEN;
999 return 0;
1000}
1001
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001002/* Parse the "non-stick" server keyword */
1003static int srv_parse_non_stick(char **args, int *cur_arg,
1004 struct proxy *curproxy, struct server *newsrv, char **err)
1005{
1006 newsrv->flags |= SRV_F_NON_STICK;
1007 return 0;
1008}
1009
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001010/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +02001011static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001012{
1013 srv->pp_opts |= flags;
1014 return 0;
1015}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001016/* parse the "proto" server keyword */
1017static int srv_parse_proto(char **args, int *cur_arg,
1018 struct proxy *px, struct server *newsrv, char **err)
1019{
1020 struct ist proto;
1021
1022 if (!*args[*cur_arg + 1]) {
1023 memprintf(err, "'%s' : missing value", args[*cur_arg]);
1024 return ERR_ALERT | ERR_FATAL;
1025 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +01001026 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001027 newsrv->mux_proto = get_mux_proto(proto);
1028 if (!newsrv->mux_proto) {
1029 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
1030 return ERR_ALERT | ERR_FATAL;
1031 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001032 return 0;
1033}
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001034
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001035/* parse the "proxy-v2-options" */
1036static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
1037 struct proxy *px, struct server *newsrv, char **err)
1038{
1039 char *p, *n;
1040 for (p = args[*cur_arg+1]; p; p = n) {
1041 n = strchr(p, ',');
1042 if (n)
1043 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001044 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001045 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001046 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001047 newsrv->pp_opts |= SRV_PP_V2_SSL;
1048 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001049 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001050 newsrv->pp_opts |= SRV_PP_V2_SSL;
1051 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001052 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001053 newsrv->pp_opts |= SRV_PP_V2_SSL;
1054 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001055 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +01001056 newsrv->pp_opts |= SRV_PP_V2_SSL;
1057 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001058 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +01001059 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001060 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +01001061 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001062 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +01001063 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001064 } else
1065 goto fail;
1066 }
1067 return 0;
1068 fail:
1069 if (err)
1070 memprintf(err, "'%s' : proxy v2 option not implemented", p);
1071 return ERR_ALERT | ERR_FATAL;
1072}
1073
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001074/* Parse the "observe" server keyword */
1075static int srv_parse_observe(char **args, int *cur_arg,
1076 struct proxy *curproxy, struct server *newsrv, char **err)
1077{
1078 char *arg;
1079
1080 arg = args[*cur_arg + 1];
1081 if (!*arg) {
1082 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
1083 return ERR_ALERT | ERR_FATAL;
1084 }
1085
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001086 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001087 newsrv->observe = HANA_OBS_NONE;
1088 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001089 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001090 newsrv->observe = HANA_OBS_LAYER4;
1091 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001092 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001093 if (curproxy->mode != PR_MODE_HTTP) {
1094 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
1095 return ERR_ALERT;
1096 }
1097 newsrv->observe = HANA_OBS_LAYER7;
1098 }
1099 else {
1100 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
1101 "but got '%s'\n", args[*cur_arg], arg);
1102 return ERR_ALERT | ERR_FATAL;
1103 }
1104
1105 return 0;
1106}
1107
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001108/* Parse the "on-error" server keyword */
1109static int srv_parse_on_error(char **args, int *cur_arg,
1110 struct proxy *curproxy, struct server *newsrv, char **err)
1111{
1112 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
1113 newsrv->onerror = HANA_ONERR_FASTINTER;
1114 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
1115 newsrv->onerror = HANA_ONERR_FAILCHK;
1116 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
1117 newsrv->onerror = HANA_ONERR_SUDDTH;
1118 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
1119 newsrv->onerror = HANA_ONERR_MARKDWN;
1120 else {
1121 memprintf(err, "'%s' expects one of 'fastinter', "
1122 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1123 args[*cur_arg], args[*cur_arg + 1]);
1124 return ERR_ALERT | ERR_FATAL;
1125 }
1126
1127 return 0;
1128}
1129
1130/* Parse the "on-marked-down" server keyword */
1131static int srv_parse_on_marked_down(char **args, int *cur_arg,
1132 struct proxy *curproxy, struct server *newsrv, char **err)
1133{
1134 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1135 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1136 else {
1137 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1138 args[*cur_arg], args[*cur_arg + 1]);
1139 return ERR_ALERT | ERR_FATAL;
1140 }
1141
1142 return 0;
1143}
1144
1145/* Parse the "on-marked-up" server keyword */
1146static int srv_parse_on_marked_up(char **args, int *cur_arg,
1147 struct proxy *curproxy, struct server *newsrv, char **err)
1148{
1149 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1150 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1151 else {
1152 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1153 args[*cur_arg], args[*cur_arg + 1]);
1154 return ERR_ALERT | ERR_FATAL;
1155 }
1156
1157 return 0;
1158}
1159
Frédéric Lécaille16186232017-03-14 16:42:49 +01001160/* Parse the "redir" server keyword */
1161static int srv_parse_redir(char **args, int *cur_arg,
1162 struct proxy *curproxy, struct server *newsrv, char **err)
1163{
1164 char *arg;
1165
1166 arg = args[*cur_arg + 1];
1167 if (!*arg) {
1168 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1169 return ERR_ALERT | ERR_FATAL;
1170 }
1171
1172 free(newsrv->rdr_pfx);
1173 newsrv->rdr_pfx = strdup(arg);
1174 newsrv->rdr_len = strlen(arg);
1175
1176 return 0;
1177}
1178
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001179/* Parse the "resolvers" server keyword */
1180static int srv_parse_resolvers(char **args, int *cur_arg,
1181 struct proxy *curproxy, struct server *newsrv, char **err)
1182{
1183 free(newsrv->resolvers_id);
1184 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1185 return 0;
1186}
1187
1188/* Parse the "resolve-net" server keyword */
1189static int srv_parse_resolve_net(char **args, int *cur_arg,
1190 struct proxy *curproxy, struct server *newsrv, char **err)
1191{
1192 char *p, *e;
1193 unsigned char mask;
1194 struct resolv_options *opt;
1195
1196 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1197 memprintf(err, "'%s' expects a list of networks.",
1198 args[*cur_arg]);
1199 return ERR_ALERT | ERR_FATAL;
1200 }
1201
1202 opt = &newsrv->resolv_opts;
1203
1204 /* Split arguments by comma, and convert it from ipv4 or ipv6
1205 * string network in in_addr or in6_addr.
1206 */
1207 p = args[*cur_arg + 1];
1208 e = p;
1209 while (*p != '\0') {
1210 /* If no room available, return error. */
1211 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1212 memprintf(err, "'%s' exceed %d networks.",
1213 args[*cur_arg], SRV_MAX_PREF_NET);
1214 return ERR_ALERT | ERR_FATAL;
1215 }
1216 /* look for end or comma. */
1217 while (*e != ',' && *e != '\0')
1218 e++;
1219 if (*e == ',') {
1220 *e = '\0';
1221 e++;
1222 }
1223 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1224 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1225 /* Try to convert input string from ipv4 or ipv6 network. */
1226 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1227 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1228 &mask)) {
1229 /* Try to convert input string from ipv6 network. */
1230 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1231 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1232 } else {
1233 /* All network conversions fail, return error. */
1234 memprintf(err, "'%s' invalid network '%s'.",
1235 args[*cur_arg], p);
1236 return ERR_ALERT | ERR_FATAL;
1237 }
1238 opt->pref_net_nb++;
1239 p = e;
1240 }
1241
1242 return 0;
1243}
1244
1245/* Parse the "resolve-opts" server keyword */
1246static int srv_parse_resolve_opts(char **args, int *cur_arg,
1247 struct proxy *curproxy, struct server *newsrv, char **err)
1248{
1249 char *p, *end;
1250
1251 for (p = args[*cur_arg + 1]; *p; p = end) {
1252 /* cut on next comma */
1253 for (end = p; *end && *end != ','; end++);
1254 if (*end)
1255 *(end++) = 0;
1256
1257 if (strcmp(p, "allow-dup-ip") == 0) {
1258 newsrv->resolv_opts.accept_duplicate_ip = 1;
1259 }
1260 else if (strcmp(p, "ignore-weight") == 0) {
1261 newsrv->resolv_opts.ignore_weight = 1;
1262 }
1263 else if (strcmp(p, "prevent-dup-ip") == 0) {
1264 newsrv->resolv_opts.accept_duplicate_ip = 0;
1265 }
1266 else {
1267 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1268 args[*cur_arg], p);
1269 return ERR_ALERT | ERR_FATAL;
1270 }
1271 }
1272
1273 return 0;
1274}
1275
1276/* Parse the "resolve-prefer" server keyword */
1277static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1278 struct proxy *curproxy, struct server *newsrv, char **err)
1279{
1280 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1281 newsrv->resolv_opts.family_prio = AF_INET;
1282 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1283 newsrv->resolv_opts.family_prio = AF_INET6;
1284 else {
1285 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1286 args[*cur_arg]);
1287 return ERR_ALERT | ERR_FATAL;
1288 }
1289
1290 return 0;
1291}
1292
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001293/* Parse the "send-proxy" server keyword */
1294static int srv_parse_send_proxy(char **args, int *cur_arg,
1295 struct proxy *curproxy, struct server *newsrv, char **err)
1296{
1297 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1298}
1299
1300/* Parse the "send-proxy-v2" server keyword */
1301static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1302 struct proxy *curproxy, struct server *newsrv, char **err)
1303{
1304 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1305}
1306
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001307/* Parse the "slowstart" server keyword */
1308static int srv_parse_slowstart(char **args, int *cur_arg,
1309 struct proxy *curproxy, struct server *newsrv, char **err)
1310{
1311 /* slowstart is stored in seconds */
1312 unsigned int val;
1313 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1314
1315 if (time_err == PARSE_TIME_OVER) {
1316 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1317 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1318 return ERR_ALERT | ERR_FATAL;
1319 }
1320 else if (time_err == PARSE_TIME_UNDER) {
1321 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1322 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1323 return ERR_ALERT | ERR_FATAL;
1324 }
1325 else if (time_err) {
1326 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1327 *time_err, newsrv->id);
1328 return ERR_ALERT | ERR_FATAL;
1329 }
1330 newsrv->slowstart = (val + 999) / 1000;
1331
1332 return 0;
1333}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001334
1335/* Parse the "source" server keyword */
1336static int srv_parse_source(char **args, int *cur_arg,
1337 struct proxy *curproxy, struct server *newsrv, char **err)
1338{
1339 char *errmsg;
1340 int port_low, port_high;
1341 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001342
1343 errmsg = NULL;
1344
1345 if (!*args[*cur_arg + 1]) {
1346 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1347 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1348 goto err;
1349 }
1350
1351 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001352 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1353 &errmsg, NULL, NULL,
1354 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 +01001355 if (!sk) {
1356 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1357 goto err;
1358 }
1359
Frédéric Lécailledba97072017-03-17 15:33:50 +01001360 newsrv->conn_src.opts |= CO_SRC_BIND;
1361 newsrv->conn_src.source_addr = *sk;
1362
1363 if (port_low != port_high) {
1364 int i;
1365
Frédéric Lécailledba97072017-03-17 15:33:50 +01001366 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001367 if (!newsrv->conn_src.sport_range) {
1368 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1369 goto err;
1370 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001371 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1372 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1373 }
1374
1375 *cur_arg += 2;
1376 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001377 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001378#if defined(CONFIG_HAP_TRANSPARENT)
1379 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001380 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1381 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001382 goto err;
1383 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001384 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001385 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1386 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1387 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001388 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001389 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1390 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1391 }
1392 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1393 char *name, *end;
1394
1395 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001396 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001397 name++;
1398
1399 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001400 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001401 end++;
1402
1403 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1404 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1405 free(newsrv->conn_src.bind_hdr_name);
1406 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001407 if (!newsrv->conn_src.bind_hdr_name) {
1408 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1409 goto err;
1410 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001411 newsrv->conn_src.bind_hdr_len = end - name;
1412 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1413 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1414 newsrv->conn_src.bind_hdr_occ = -1;
1415
1416 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001417 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001418 end++;
1419 if (*end == ',') {
1420 end++;
1421 name = end;
1422 if (*end == '-')
1423 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001424 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001425 end++;
1426 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1427 }
1428
1429 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001430 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1431 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001432 goto err;
1433 }
1434 }
1435 else {
1436 struct sockaddr_storage *sk;
1437 int port1, port2;
1438
1439 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001440 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1441 &errmsg, NULL, NULL,
1442 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001443 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001444 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001445 goto err;
1446 }
1447
Frédéric Lécailledba97072017-03-17 15:33:50 +01001448 newsrv->conn_src.tproxy_addr = *sk;
1449 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1450 }
1451 global.last_checks |= LSTCHK_NETADM;
1452 *cur_arg += 2;
1453 continue;
1454#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001455 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 +01001456 goto err;
1457#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1458 } /* "usesrc" */
1459
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001460 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001461#ifdef SO_BINDTODEVICE
1462 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001463 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001464 goto err;
1465 }
1466 free(newsrv->conn_src.iface_name);
1467 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1468 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1469 global.last_checks |= LSTCHK_NETADM;
1470#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001471 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001472 goto err;
1473#endif
1474 *cur_arg += 2;
1475 continue;
1476 }
1477 /* this keyword in not an option of "source" */
1478 break;
1479 } /* while */
1480
1481 return 0;
1482
1483 err:
1484 free(errmsg);
1485 return ERR_ALERT | ERR_FATAL;
1486}
1487
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001488/* Parse the "stick" server keyword */
1489static int srv_parse_stick(char **args, int *cur_arg,
1490 struct proxy *curproxy, struct server *newsrv, char **err)
1491{
1492 newsrv->flags &= ~SRV_F_NON_STICK;
1493 return 0;
1494}
1495
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001496/* Parse the "track" server keyword */
1497static int srv_parse_track(char **args, int *cur_arg,
1498 struct proxy *curproxy, struct server *newsrv, char **err)
1499{
1500 char *arg;
1501
1502 arg = args[*cur_arg + 1];
1503 if (!*arg) {
1504 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1505 return ERR_ALERT | ERR_FATAL;
1506 }
1507
1508 free(newsrv->trackit);
1509 newsrv->trackit = strdup(arg);
1510
1511 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001512}
1513
1514/* Parse the "socks4" server keyword */
1515static int srv_parse_socks4(char **args, int *cur_arg,
1516 struct proxy *curproxy, struct server *newsrv, char **err)
1517{
1518 char *errmsg;
1519 int port_low, port_high;
1520 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001521
1522 errmsg = NULL;
1523
1524 if (!*args[*cur_arg + 1]) {
1525 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1526 goto err;
1527 }
1528
1529 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001530 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1531 &errmsg, NULL, NULL,
1532 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001533 if (!sk) {
1534 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1535 goto err;
1536 }
1537
Alexander Liu2a54bb72019-05-22 19:44:48 +08001538 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1539 newsrv->socks4_addr = *sk;
1540
Alexander Liu2a54bb72019-05-22 19:44:48 +08001541 return 0;
1542
1543 err:
1544 free(errmsg);
1545 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001546}
1547
Frédéric Lécailledba97072017-03-17 15:33:50 +01001548
Willy Tarreau034c88c2017-01-23 23:36:45 +01001549/* parse the "tfo" server keyword */
1550static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1551{
1552 newsrv->flags |= SRV_F_FASTOPEN;
1553 return 0;
1554}
1555
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001556/* parse the "usesrc" server keyword */
1557static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1558{
1559 memprintf(err, "'%s' only allowed after a '%s' statement.",
1560 "usesrc", "source");
1561 return ERR_ALERT | ERR_FATAL;
1562}
1563
1564/* parse the "weight" server keyword */
1565static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1566{
1567 int w;
1568
1569 w = atol(args[*cur_arg + 1]);
1570 if (w < 0 || w > SRV_UWGHT_MAX) {
1571 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1572 newsrv->id, SRV_UWGHT_MAX, w);
1573 return ERR_ALERT | ERR_FATAL;
1574 }
1575 newsrv->uweight = newsrv->iweight = w;
1576
1577 return 0;
1578}
1579
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001580/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001581 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001582 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001583 *
1584 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001585 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001586void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001587{
Willy Tarreau751153e2021-02-17 13:33:24 +01001588 struct stream *stream;
1589 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001590 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001591
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001592 for (thr = 0; thr < global.nbthread; thr++)
1593 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1594 if (stream->srv_conn == srv)
1595 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001596}
1597
1598/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001599 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001600 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001601 *
1602 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001603 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001604void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001605{
1606 struct server *srv;
1607
1608 for (srv = px->srv; srv != NULL; srv = srv->next)
1609 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001610 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001611}
1612
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001613static 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 +02001614{
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001615 switch (cause) {
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001616 case SRV_OP_STCHGC_NONE:
1617 break; /* do nothing */
1618 case SRV_OP_STCHGC_HEALTH:
1619 check_append_info(msg, &s->check);
1620 break;
1621 case SRV_OP_STCHGC_AGENT:
1622 check_append_info(msg, &s->agent);
1623 break;
1624 default:
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001625 chunk_appendf(msg, ", %s", srv_op_st_chg_cause(cause));
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001626 break;
1627 }
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001628}
1629
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001630static 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 +02001631{
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001632 if (cause)
1633 chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(cause));
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001634}
1635
1636/* Appends some information to a message string related to a server tracking
1637 * or requeued connections info.
1638 *
1639 * If <forced> is null and the server tracks another one, a "via"
Emeric Brun5a133512017-10-19 14:42:30 +02001640 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001641 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001642 *
1643 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001644 */
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001645static void srv_append_more(struct buffer *msg, struct server *s,
1646 int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001647{
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001648 if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001649 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001650 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001651
1652 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001653 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001654 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1655 " %d sessions active, %d requeued, %d remaining in queue",
1656 s->proxy->srv_act, s->proxy->srv_bck,
1657 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001658 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001659 else
1660 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1661 " %d sessions requeued, %d total in queue",
1662 s->proxy->srv_act, s->proxy->srv_bck,
1663 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001664 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001665 }
1666}
1667
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001668/* Marks server <s> down, regardless of its checks' statuses. The server
1669 * transfers queued streams whenever possible to other servers at a sync
1670 * point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001671 *
1672 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001673 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001674void srv_set_stopped(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001675{
1676 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001677
Emeric Brun64cc49c2017-10-03 14:46:45 +02001678 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001679 return;
1680
Emeric Brun52a91d32017-08-31 14:41:55 +02001681 s->next_state = SRV_ST_STOPPED;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001682
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001683 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001684 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001685
Emeric Brun9f0b4582017-10-23 14:39:51 +02001686 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001687 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001688 srv_set_stopped(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001689 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001690 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001691}
1692
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001693/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001694 * in maintenance. The server tries to grab requests from the proxy at a sync
1695 * point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001696 *
1697 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001698 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001699void srv_set_running(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001700{
1701 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001702
Emeric Brun64cc49c2017-10-03 14:46:45 +02001703 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001704 return;
1705
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001707 return;
1708
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 s->next_state = SRV_ST_STARTING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001710
Emeric Brun64cc49c2017-10-03 14:46:45 +02001711 if (s->slowstart <= 0)
1712 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001713
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001714 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001715 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001716
Emeric Brun9f0b4582017-10-23 14:39:51 +02001717 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001718 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001719 srv_set_running(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001720 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001721 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001722}
1723
Willy Tarreau8eb77842014-05-21 13:54:57 +02001724/* Marks server <s> stopping regardless of its checks' statuses and provided it
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001725 * isn't in maintenance. The server tries to redispatch pending requests
1726 * to the proxy. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001727 *
1728 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001729 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001730void srv_set_stopping(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001731{
1732 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001733
Emeric Brun64cc49c2017-10-03 14:46:45 +02001734 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001735 return;
1736
Emeric Brun52a91d32017-08-31 14:41:55 +02001737 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001738 return;
1739
Emeric Brun52a91d32017-08-31 14:41:55 +02001740 s->next_state = SRV_ST_STOPPING;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001741
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001742 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001743 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001744
Emeric Brun9f0b4582017-10-23 14:39:51 +02001745 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001746 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001747 srv_set_stopping(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001748 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001749 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001750}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001751
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001752/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1753 * enforce either maint mode or drain mode. It is not allowed to set more than
1754 * one flag at once. The equivalent "inherited" flag is propagated to all
1755 * tracking servers. Maintenance mode disables health checks (but not agent
1756 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001757 * is done. If <cause> is non-null, it will be displayed at the end of the log
1758 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001759 *
1760 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001761 */
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001762void 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 +02001763{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001764 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001765
1766 if (!mode)
1767 return;
1768
1769 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001770 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001771 return;
1772
Emeric Brun52a91d32017-08-31 14:41:55 +02001773 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001774
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001775 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001776 srv_update_status(s, 1, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001777
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001778 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001779 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1780 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001781 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001782
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001783 /* compute the inherited flag to propagate */
1784 if (mode & SRV_ADMF_MAINT)
1785 mode = SRV_ADMF_IMAINT;
1786 else if (mode & SRV_ADMF_DRAIN)
1787 mode = SRV_ADMF_IDRAIN;
1788
Emeric Brun9f0b4582017-10-23 14:39:51 +02001789 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001790 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001791 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001792 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001793 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001794}
1795
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001796/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1797 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1798 * than one flag at once. The equivalent "inherited" flag is propagated to all
1799 * tracking servers. Leaving maintenance mode re-enables health checks. When
1800 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001801 *
1802 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001803 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001804void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001805{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001806 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001807
1808 if (!mode)
1809 return;
1810
1811 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001812 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001813 return;
1814
Emeric Brun52a91d32017-08-31 14:41:55 +02001815 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001816
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001817 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001818 srv_update_status(s, 1, SRV_ADM_STCHGC_NONE);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001819
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001820 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001821 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1822 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001823 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001824
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001825 if (mode & SRV_ADMF_MAINT)
1826 mode = SRV_ADMF_IMAINT;
1827 else if (mode & SRV_ADMF_DRAIN)
1828 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001829
Emeric Brun9f0b4582017-10-23 14:39:51 +02001830 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001831 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001832 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001833 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001834 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001835}
1836
Willy Tarreau757478e2016-11-03 19:22:19 +01001837/* principle: propagate maint and drain to tracking servers. This is useful
1838 * upon startup so that inherited states are correct.
1839 */
1840static void srv_propagate_admin_state(struct server *srv)
1841{
1842 struct server *srv2;
1843
1844 if (!srv->trackers)
1845 return;
1846
1847 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001848 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001849 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001850 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau757478e2016-11-03 19:22:19 +01001851
Emeric Brun52a91d32017-08-31 14:41:55 +02001852 if (srv->next_admin & SRV_ADMF_DRAIN)
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001853 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001854 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001855 }
1856}
1857
1858/* Compute and propagate the admin states for all servers in proxy <px>.
1859 * Only servers *not* tracking another one are considered, because other
1860 * ones will be handled when the server they track is visited.
1861 */
1862void srv_compute_all_admin_states(struct proxy *px)
1863{
1864 struct server *srv;
1865
1866 for (srv = px->srv; srv; srv = srv->next) {
1867 if (srv->track)
1868 continue;
1869 srv_propagate_admin_state(srv);
1870 }
1871}
1872
Willy Tarreaudff55432012-10-10 17:51:05 +02001873/* Note: must not be declared <const> as its list will be overwritten.
1874 * Please take care of keeping this list alphabetically sorted, doing so helps
1875 * all code contributors.
1876 * Optional keywords are also declared with a NULL ->parse() function so that
1877 * the config parser can report an appropriate error when a known keyword was
1878 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001879 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001880 */
1881static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001882 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001883 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001884 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1885 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001886 { "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 +02001887 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001888 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001889 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1890 { "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 +01001891 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1892 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001893 { "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 +01001894 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001895 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001896 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1897 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1898 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1899 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001900 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001901 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1902 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1903 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1904 { "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 +01001905 { "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 */
1906 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1907 { "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 +01001908 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001909 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001910 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001911 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001912 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001913 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001914 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001915 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1916 { "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 +02001917 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001918 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001919 { "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 +01001920 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001921 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001922 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001923 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001924 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1925 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001926 { NULL, NULL, 0 },
1927}};
1928
Willy Tarreau0108d902018-11-25 19:14:37 +01001929INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001930
Willy Tarreau004e0452013-11-21 11:22:01 +01001931/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001932 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001933 * state is automatically disabled if the time is elapsed. If <must_update> is
1934 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001935 *
1936 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001937 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001938void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001939{
1940 struct proxy *px = sv->proxy;
1941 unsigned w;
1942
Willy Tarreau69530f52023-04-28 09:16:15 +02001943 if (ns_to_sec(now_ns) < sv->last_change || ns_to_sec(now_ns) >= sv->last_change + sv->slowstart) {
Willy Tarreau004e0452013-11-21 11:22:01 +01001944 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001945 if (sv->next_state == SRV_ST_STARTING)
1946 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001947 }
1948
1949 /* We must take care of not pushing the server to full throttle during slow starts.
1950 * It must also start immediately, at least at the minimal step when leaving maintenance.
1951 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001952 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau69530f52023-04-28 09:16:15 +02001953 w = (px->lbprm.wdiv * (ns_to_sec(now_ns) - sv->last_change) + sv->slowstart) / sv->slowstart;
Willy Tarreau004e0452013-11-21 11:22:01 +01001954 else
1955 w = px->lbprm.wdiv;
1956
Emeric Brun52a91d32017-08-31 14:41:55 +02001957 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001958
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001959 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001960 if (must_update)
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001961 srv_update_status(sv, 0, SRV_OP_STCHGC_NONE);
Willy Tarreau004e0452013-11-21 11:22:01 +01001962}
1963
Willy Tarreaubaaee002006-06-26 02:48:02 +02001964/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001965 * Parses weight_str and configures sv accordingly.
1966 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001967 *
1968 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001969 */
1970const char *server_parse_weight_change_request(struct server *sv,
1971 const char *weight_str)
1972{
1973 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001974 long int w;
1975 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001976
1977 px = sv->proxy;
1978
1979 /* if the weight is terminated with '%', it is set relative to
1980 * the initial weight, otherwise it is absolute.
1981 */
1982 if (!*weight_str)
1983 return "Require <weight> or <weight%>.\n";
1984
Simon Hormanb796afa2013-02-12 10:45:53 +09001985 w = strtol(weight_str, &end, 10);
1986 if (end == weight_str)
1987 return "Empty weight string empty or preceded by garbage";
1988 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001989 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001990 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001991 /* Avoid integer overflow */
1992 if (w > 25600)
1993 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001994 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001995 if (w > 256)
1996 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001997 }
1998 else if (w < 0 || w > 256)
1999 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09002000 else if (end[0] != '\0')
2001 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09002002
2003 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
2004 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
2005
2006 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002007 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09002008
2009 return NULL;
2010}
2011
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002012/*
Thierry Fournier09a91782016-02-24 08:25:39 +01002013 * Parses <addr_str> and configures <sv> accordingly. <from> precise
2014 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002015 * Returns:
2016 * - error string on error
2017 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02002018 *
2019 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002020 */
2021const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01002022 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002023{
2024 unsigned char ip[INET6_ADDRSTRLEN];
2025
2026 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01002027 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002028 return NULL;
2029 }
2030 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01002031 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02002032 return NULL;
2033 }
2034
2035 return "Could not understand IP address format.\n";
2036}
2037
Willy Tarreau46b7f532018-08-21 11:54:26 +02002038/*
2039 * Must be called with the server lock held.
2040 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002041const char *server_parse_maxconn_change_request(struct server *sv,
2042 const char *maxconn_str)
2043{
2044 long int v;
2045 char *end;
2046
2047 if (!*maxconn_str)
2048 return "Require <maxconn>.\n";
2049
2050 v = strtol(maxconn_str, &end, 10);
2051 if (end == maxconn_str)
2052 return "maxconn string empty or preceded by garbage";
2053 else if (end[0] != '\0')
2054 return "Trailing garbage in maxconn string";
2055
2056 if (sv->maxconn == sv->minconn) { // static maxconn
2057 sv->maxconn = sv->minconn = v;
2058 } else { // dynamic maxconn
2059 sv->maxconn = v;
2060 }
2061
2062 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002063 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002064
2065 return NULL;
2066}
2067
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002068static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
2069 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002070{
2071 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002072 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002073 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002074 NULL,
2075 };
2076
2077 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002078 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002079
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002080 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002081}
2082
William Lallemand0d058672022-03-16 15:44:42 +01002083int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002084{
2085 struct sample_expr *expr;
2086
2087 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 +01002088 if (!expr) {
2089 memprintf(err, "error detected while parsing sni expression : %s", *err);
2090 return ERR_ALERT | ERR_FATAL;
2091 }
2092
2093 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2094 memprintf(err, "error detected while parsing sni expression : "
2095 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002096 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002097 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002098 return ERR_ALERT | ERR_FATAL;
2099 }
2100
2101 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2102 release_sample_expr(newsrv->ssl_ctx.sni);
2103 newsrv->ssl_ctx.sni = expr;
2104
2105 return 0;
2106}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002107
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002108static 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 +01002109{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002110 char *msg = "error encountered while processing ";
2111 char *quote = "'";
2112 char *token = args[cur_arg];
2113
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002114 if (err && *err) {
2115 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002116 msg = *err;
2117 quote = "";
2118 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002119 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002120
2121 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002122 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002123 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002124 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002125}
2126
Christopher Faulet0b365e32022-08-03 11:21:14 +02002127static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002128{
2129 int range_sz;
2130
2131 range_sz = src->conn_src.sport_range->size;
2132 if (range_sz > 0) {
2133 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2134 if (srv->conn_src.sport_range != NULL) {
2135 int i;
2136
2137 for (i = 0; i < range_sz; i++) {
2138 srv->conn_src.sport_range->ports[i] =
2139 src->conn_src.sport_range->ports[i];
2140 }
2141 }
2142 }
2143}
2144
2145/*
2146 * Copy <src> server connection source settings to <srv> server everything needed.
2147 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002148static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002149{
2150 srv->conn_src.opts = src->conn_src.opts;
2151 srv->conn_src.source_addr = src->conn_src.source_addr;
2152
2153 /* Source port range copy. */
2154 if (src->conn_src.sport_range != NULL)
2155 srv_conn_src_sport_range_cpy(srv, src);
2156
2157#ifdef CONFIG_HAP_TRANSPARENT
2158 if (src->conn_src.bind_hdr_name != NULL) {
2159 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2160 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2161 }
2162 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2163 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2164#endif
2165 if (src->conn_src.iface_name != NULL)
2166 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2167}
2168
2169/*
2170 * Copy <src> server SSL settings to <srv> server allocating
2171 * everything needed.
2172 */
2173#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002174static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002175{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002176 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002177 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2178
Christopher Faulet4ab26792021-12-01 09:50:41 +01002179 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2180 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2181
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002182 if (src->ssl_ctx.ca_file != NULL)
2183 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2184 if (src->ssl_ctx.crl_file != NULL)
2185 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002186 if (src->ssl_ctx.client_crt != NULL)
2187 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002188
2189 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2190
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002191
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002192 if (src->ssl_ctx.verify_host != NULL)
2193 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2194 if (src->ssl_ctx.ciphers != NULL)
2195 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002196 if (src->ssl_ctx.options)
2197 srv->ssl_ctx.options = src->ssl_ctx.options;
2198 if (src->ssl_ctx.methods.flags)
2199 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2200 if (src->ssl_ctx.methods.min)
2201 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2202 if (src->ssl_ctx.methods.max)
2203 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2204
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002205 if (src->ssl_ctx.ciphersuites != NULL)
2206 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002207 if (src->sni_expr != NULL)
2208 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002209
Olivier Houchardc7566002018-11-20 23:33:50 +01002210 if (src->ssl_ctx.alpn_str) {
2211 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2212 if (srv->ssl_ctx.alpn_str) {
2213 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2214 src->ssl_ctx.alpn_len);
2215 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2216 }
2217 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002218
Olivier Houchardc7566002018-11-20 23:33:50 +01002219 if (src->ssl_ctx.npn_str) {
2220 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2221 if (srv->ssl_ctx.npn_str) {
2222 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2223 src->ssl_ctx.npn_len);
2224 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2225 }
2226 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002227}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002228
2229/* Activate ssl on server <s>.
2230 * do nothing if there is no change to apply
2231 *
2232 * Must be called with the server lock held.
2233 */
2234void srv_set_ssl(struct server *s, int use_ssl)
2235{
2236 if (s->use_ssl == use_ssl)
2237 return;
2238
2239 s->use_ssl = use_ssl;
2240 if (s->use_ssl)
2241 s->xprt = xprt_get(XPRT_SSL);
2242 else
William Dauchya087f872022-01-06 16:57:15 +01002243 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002244}
2245
2246#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002247
2248/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002249 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002250 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002251 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002252 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002253int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002254{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002255 char *hostname_dn;
2256 int hostname_len, hostname_dn_len;
2257
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002258 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002259 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002260
Christopher Faulet67957bd2017-09-27 11:00:59 +02002261 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002262 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002263 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002264 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002265 if (hostname_dn_len == -1)
2266 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002267
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002268
Christopher Faulet67957bd2017-09-27 11:00:59 +02002269 free(srv->hostname);
2270 free(srv->hostname_dn);
2271 srv->hostname = strdup(hostname);
2272 srv->hostname_dn = strdup(hostname_dn);
2273 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002274 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002275 goto err;
2276
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002277 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002278
2279 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002280 ha_free(&srv->hostname);
2281 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002282 return -1;
2283}
2284
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002285/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002286 * Copy <src> server settings to <srv> server allocating
2287 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002288 * This function is not supposed to be called at any time, but only
2289 * during server settings parsing or during server allocations from
2290 * a server template, and just after having calloc()'ed a new server.
2291 * So, <src> may only be a default server (when parsing server settings)
2292 * or a server template (during server allocations from a server template).
2293 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2294 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002295 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002296void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002297{
2298 /* Connection source settings copy */
2299 srv_conn_src_cpy(srv, src);
2300
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002301 if (srv_tmpl) {
2302 srv->addr = src->addr;
2303 srv->svc_port = src->svc_port;
2304 }
2305
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002306 srv->pp_opts = src->pp_opts;
2307 if (src->rdr_pfx != NULL) {
2308 srv->rdr_pfx = strdup(src->rdr_pfx);
2309 srv->rdr_len = src->rdr_len;
2310 }
2311 if (src->cookie != NULL) {
2312 srv->cookie = strdup(src->cookie);
2313 srv->cklen = src->cklen;
2314 }
2315 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002316 srv->check.addr = src->check.addr;
2317 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002318 srv->check.use_ssl = src->check.use_ssl;
2319 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002320 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002321 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002322 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002323 /* Note: 'flags' field has potentially been already initialized. */
2324 srv->flags |= src->flags;
2325 srv->do_check = src->do_check;
2326 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002327 srv->check.inter = src->check.inter;
2328 srv->check.fastinter = src->check.fastinter;
2329 srv->check.downinter = src->check.downinter;
2330 srv->agent.use_ssl = src->agent.use_ssl;
2331 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002332
2333 if (src->agent.tcpcheck_rules) {
2334 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2335 if (srv->agent.tcpcheck_rules) {
2336 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2337 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2338 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2339 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2340 &src->agent.tcpcheck_rules->preset_vars);
2341 }
2342 }
2343
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002344 srv->agent.inter = src->agent.inter;
2345 srv->agent.fastinter = src->agent.fastinter;
2346 srv->agent.downinter = src->agent.downinter;
2347 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002348 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002349 srv->minconn = src->minconn;
2350 srv->maxconn = src->maxconn;
2351 srv->slowstart = src->slowstart;
2352 srv->observe = src->observe;
2353 srv->onerror = src->onerror;
2354 srv->onmarkeddown = src->onmarkeddown;
2355 srv->onmarkedup = src->onmarkedup;
2356 if (src->trackit != NULL)
2357 srv->trackit = strdup(src->trackit);
2358 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2359 srv->uweight = srv->iweight = src->iweight;
2360
2361 srv->check.send_proxy = src->check.send_proxy;
2362 /* health: up, but will fall down at first failure */
2363 srv->check.rise = srv->check.health = src->check.rise;
2364 srv->check.fall = src->check.fall;
2365
2366 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002367 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2368 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2369 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002370 srv->check.state |= CHK_ST_PAUSED;
2371 srv->check.health = 0;
2372 }
2373
2374 /* health: up but will fall down at first failure */
2375 srv->agent.rise = srv->agent.health = src->agent.rise;
2376 srv->agent.fall = src->agent.fall;
2377
2378 if (src->resolvers_id != NULL)
2379 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002380 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2381 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2382 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2383 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2384 srv->resolv_opts.family_prio = AF_INET6;
2385 memcpy(srv->resolv_opts.pref_net,
2386 src->resolv_opts.pref_net,
2387 sizeof srv->resolv_opts.pref_net);
2388 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002389
2390 srv->init_addr_methods = src->init_addr_methods;
2391 srv->init_addr = src->init_addr;
2392#if defined(USE_OPENSSL)
2393 srv_ssl_settings_cpy(srv, src);
2394#endif
2395#ifdef TCP_USER_TIMEOUT
2396 srv->tcp_ut = src->tcp_ut;
2397#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002398 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002399 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002400 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002401 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002402 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002403
Olivier Houchard8da5f982017-08-04 18:35:36 +02002404 if (srv_tmpl)
2405 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002406
Aurelien DARRAGON60d185d2023-06-14 09:53:32 +02002407 srv->netns = src->netns;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002408 srv->check.via_socks4 = src->check.via_socks4;
2409 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002410}
2411
Willy Tarreau198e92a2021-03-05 10:23:32 +01002412/* allocate a server and attach it to the global servers_list. Returns
2413 * the server on success, otherwise NULL.
2414 */
William Lallemand313bfd12018-10-26 14:47:32 +02002415struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002416{
2417 struct server *srv;
2418
2419 srv = calloc(1, sizeof *srv);
2420 if (!srv)
2421 return NULL;
2422
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002423 srv_take(srv);
2424
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002425 srv->obj_type = OBJ_TYPE_SERVER;
2426 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002427 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002428 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002429 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002430 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002431 MT_LIST_INIT(&srv->prev_deleted);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002432 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002433
Emeric Brun52a91d32017-08-31 14:41:55 +02002434 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau69530f52023-04-28 09:16:15 +02002435 srv->last_change = ns_to_sec(now_ns);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002436
Christopher Faulet38290462020-04-21 11:46:40 +02002437 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002438 srv->check.status = HCHK_STATUS_INI;
2439 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002440 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002441 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002442
Christopher Faulet38290462020-04-21 11:46:40 +02002443 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002444 srv->agent.status = HCHK_STATUS_INI;
2445 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002446 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002447 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002448#if defined(USE_QUIC)
2449 srv->cids = EB_ROOT_UNIQUE;
2450#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002451
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002452 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002453#ifdef USE_OPENSSL
2454 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2455#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002456
Willy Tarreau975b1552019-06-06 16:25:55 +02002457 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002458 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002459 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002460 return srv;
2461}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002462
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002463/* Increment the server refcount. */
2464void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002465{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002466 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002467}
2468
Aurelien DARRAGON899b5472023-06-01 12:07:43 +02002469/* deallocate common server parameters (may be used by default-servers) */
2470void srv_free_params(struct server *srv)
2471{
2472 free(srv->cookie);
2473 free(srv->hostname);
2474 free(srv->hostname_dn);
2475 free((char*)srv->conf.file);
2476 free(srv->per_thr);
2477 free(srv->per_tgrp);
2478 free(srv->curr_idle_thr);
2479 free(srv->resolvers_id);
2480 free(srv->addr_node.key);
2481 free(srv->lb_nodes);
2482
2483 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2484 xprt_get(XPRT_SSL)->destroy_srv(srv);
2485}
2486
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002487/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2488 * dynamic servers, its refcount is decremented first. The free operations are
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002489 * conducted only if the refcount is nul.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002490 *
2491 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002492 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002493 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002494struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002495{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002496 struct server *next = NULL;
2497
William Lallemand4c395fc2021-08-20 10:10:15 +02002498 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002499 goto end;
2500
2501 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002502
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002503 /* For dynamic servers, decrement the reference counter. Only free the
2504 * server when reaching zero.
2505 */
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002506 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2507 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002508
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002509 /* make sure we are removed from our 'next->prev_deleted' list
2510 * This doesn't require full thread isolation as we're using mt lists
2511 * However this could easily be turned into regular list if required
2512 * (with the proper use of thread isolation)
2513 */
2514 MT_LIST_DELETE(&srv->prev_deleted);
2515
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002516 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002517 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002518
2519 free(srv->id);
Aurelien DARRAGON899b5472023-06-01 12:07:43 +02002520 srv_free_params(srv);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002521
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002522 HA_SPIN_DESTROY(&srv->lock);
2523
Willy Tarreau2b718102021-04-21 07:32:39 +02002524 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002525 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002526
2527 EXTRA_COUNTERS_FREE(srv->extra_counters);
2528
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002529 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002530
2531 end:
2532 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002533}
2534
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002535/* Remove a server <srv> from a tracking list if <srv> is tracking another
2536 * server. No special care is taken if <srv> is tracked itself by another one :
2537 * this situation should be avoided by the caller.
2538 *
2539 * Not thread-safe.
2540 */
2541static void release_server_track(struct server *srv)
2542{
2543 struct server *strack = srv->track;
2544 struct server **base;
2545
2546 if (!strack)
2547 return;
2548
2549 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2550 if (*base == srv) {
2551 *base = srv->tracknext;
2552 return;
2553 }
2554 }
2555
2556 /* srv not found on the tracking list, this should never happen */
2557 BUG_ON(!*base);
2558}
2559
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002560/*
2561 * Parse as much as possible such a range string argument: low[-high]
2562 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2563 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2564 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2565 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002566 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002567static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2568 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002569{
2570 char *nb_high_arg;
2571
2572 *nb_high = 0;
2573 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002574 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002575
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002576 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002577 *nb_high_arg++ = '\0';
2578 *nb_high = atoi(nb_high_arg);
2579 }
2580 else {
2581 *nb_high += *nb_low;
2582 *nb_low = 1;
2583 }
2584
2585 if (*nb_low < 0 || *nb_high < *nb_low)
2586 return -1;
2587
2588 return 0;
2589}
2590
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002591/* Parse as much as possible such a range string argument: low[-high]
2592 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2593 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2594 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002595 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002596 * initialize a new server on startup.
2597 *
2598 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2599 * Returns 0 if succeeded, -1 if not.
2600 */
2601static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2602 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002603{
2604 chunk_printf(&trash, "%s%d", prefix, nb);
2605 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002606 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002607}
2608
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002609/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002610 * Note that a server template is a special server with
2611 * a few different parameters than a server which has
2612 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002613 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002614 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002615 * initialize a new server on startup.
2616 *
Joseph Herlant44466822018-11-15 08:57:51 -08002617 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002618 * 'srv' template included.
2619 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002620static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002621{
2622 int i;
2623 struct server *newsrv;
2624
2625 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 +02002626 newsrv = new_server(px);
2627 if (!newsrv)
2628 goto err;
2629
Christopher Faulet75bef002020-11-02 22:04:55 +01002630 newsrv->conf.file = strdup(srv->conf.file);
2631 newsrv->conf.line = srv->conf.line;
2632
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002633 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002634 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002635
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002636 if (newsrv->sni_expr) {
2637 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2638 if (!newsrv->ssl_ctx.sni)
2639 goto err;
2640 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002641
Emeric Brun34067662021-06-11 10:48:45 +02002642 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002643 if (newsrv->srvrq)
2644 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002645
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002646 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002647 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002648
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002649 /* Linked backwards first. This will be restablished after parsing. */
2650 newsrv->next = px->srv;
2651 px->srv = newsrv;
2652 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002653 _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 +02002654
2655 return i - srv->tmpl_info.nb_low;
2656
2657 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002658 _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 +02002659 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002660 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002661 free_check(&newsrv->agent);
2662 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002663 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002664 }
2665 free(newsrv);
2666 return i - srv->tmpl_info.nb_low;
2667}
2668
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002669/* Allocate a new server pointed by <srv> and try to parse the first arguments
2670 * in <args> as an address for a server or an address-range for a template or
2671 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2672 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002673 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002674 * initialize a new server on startup.
2675 *
2676 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2677 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2678 * <srv> will be set to NULL.
2679 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002680static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2681 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002682 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002683{
2684 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002685 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002686 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002687 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002688 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002689 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002690
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002691 *srv = NULL;
2692
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002693 /* There is no mandatory first arguments for default server. */
2694 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2695 if (parse_flags & SRV_PARSE_TEMPLATE) {
2696 if (!*args[3]) {
2697 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002698 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2699 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002700 err_code |= ERR_ALERT | ERR_FATAL;
2701 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002702 }
2703
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002704 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002705 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002706 else {
2707 if (!*args[2]) {
2708 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002709 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2710 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002711 err_code |= ERR_ALERT | ERR_FATAL;
2712 goto out;
2713 }
2714
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002715 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002716 }
2717
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002718 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002719 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2720 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002721 err_code |= ERR_ALERT | ERR_FATAL;
2722 goto out;
2723 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002724 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002725
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002726 *cur_arg = 2;
2727 if (parse_flags & SRV_PARSE_TEMPLATE) {
2728 /* Parse server-template <nb | range> arg. */
2729 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002730 ha_alert("Wrong %s number or range arg '%s'.\n",
2731 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002732 err_code |= ERR_ALERT | ERR_FATAL;
2733 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002734 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002735 (*cur_arg)++;
2736 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002737
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002738 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2739 struct sockaddr_storage *sk;
2740 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002741
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002742 *srv = newsrv = new_server(curproxy);
2743 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002744 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002745 err_code |= ERR_ALERT | ERR_ABORT;
2746 goto out;
2747 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002748 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002749
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002750 if (parse_flags & SRV_PARSE_TEMPLATE) {
2751 newsrv->tmpl_info.nb_low = tmpl_range_low;
2752 newsrv->tmpl_info.nb_high = tmpl_range_high;
2753 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002754
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002755 if (parse_flags & SRV_PARSE_DYNAMIC)
2756 newsrv->flags |= SRV_F_DYNAMIC;
2757
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002758 /* Note: for a server template, its id is its prefix.
2759 * This is a temporary id which will be used for server allocations to come
2760 * after parsing.
2761 */
2762 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2763 newsrv->id = strdup(args[1]);
2764 else
2765 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002766
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002767 /* revision defaults to 0 */
2768 newsrv->rid = 0;
2769
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002770 /* several ways to check the port component :
2771 * - IP => port=+0, relative (IPv4 only)
2772 * - IP: => port=+0, relative
2773 * - IP:N => port=N, absolute
2774 * - IP:+N => port=+N, relative
2775 * - IP:-N => port=-N, relative
2776 */
2777 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2778 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002779
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002780 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002781 &errmsg, NULL, &fqdn,
Christopher Fauletaf27ada2023-06-02 14:10:36 +02002782 (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK |
2783 (parse_flags & SRV_PARSE_IN_PEER_SECTION ? PA_O_PORT_MAND : PA_O_PORT_OFS) |
2784 PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002785 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002786 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002787 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002788 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002789 goto out;
2790 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002791
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002792 if (!port1 || !port2) {
2793 /* no port specified, +offset, -offset */
2794 newsrv->flags |= SRV_F_MAPPORTS;
2795 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002796
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002797 /* save hostname and create associated name resolution */
2798 if (fqdn) {
2799 if (fqdn[0] == '_') { /* SRV record */
2800 /* Check if a SRV request already exists, and if not, create it */
2801 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2802 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2803 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002804 err_code |= ERR_ALERT | ERR_FATAL;
2805 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002806 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002807 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002808 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002809 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002810 ha_alert("Can't create DNS resolution for server '%s'\n",
2811 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002812 err_code |= ERR_ALERT | ERR_FATAL;
2813 goto out;
2814 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002815 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002816
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002817 newsrv->addr = *sk;
2818 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002819 /*
2820 * we don't need to lock the server here, because
2821 * we are in the process of initializing.
2822 *
2823 * Note that the server is not attached into the proxy tree if
2824 * this is a dynamic server.
2825 */
2826 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002827
Willy Tarreau14e7f292021-10-27 17:41:07 +02002828 if (!newsrv->srvrq && !newsrv->hostname &&
2829 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002830 ha_alert("Unknown protocol family %d '%s'\n",
2831 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002832 err_code |= ERR_ALERT | ERR_FATAL;
2833 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002834 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002835
2836 (*cur_arg)++;
2837 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002838 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2839 /* Copy default server settings to new server */
2840 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2841 } else {
2842 /* Initialize dynamic server weight to 1 */
2843 newsrv->uweight = newsrv->iweight = 1;
2844
2845 /* A dynamic server is disabled on startup */
2846 newsrv->next_admin = SRV_ADMF_FMAINT;
2847 newsrv->next_state = SRV_ST_STOPPED;
2848 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002849
2850 /* Set default values for checks */
2851 newsrv->check.inter = DEF_CHKINTR;
2852 newsrv->check.rise = DEF_RISETIME;
2853 newsrv->check.fall = DEF_FALLTIME;
2854
2855 newsrv->agent.inter = DEF_CHKINTR;
2856 newsrv->agent.rise = DEF_AGENT_RISETIME;
2857 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002858 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002859 HA_SPIN_INIT(&newsrv->lock);
2860 }
2861 else {
2862 *srv = newsrv = &curproxy->defsrv;
2863 *cur_arg = 1;
2864 newsrv->resolv_opts.family_prio = AF_INET6;
2865 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002866 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002867
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002868 free(fqdn);
2869 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002870
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002871out:
2872 free(fqdn);
2873 return err_code;
2874}
Willy Tarreau272adea2014-03-31 10:39:59 +02002875
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002876/* Parse the server keyword in <args>.
2877 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2878 * might not be the case if an error is reported.
2879 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002880 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002881 * initialize a new server on startup.
2882 *
2883 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2884 * interrupted.
2885 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002886static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2887 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002888 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002889{
2890 int err_code = 0;
2891 struct srv_kw *kw;
2892 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002893 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002894
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002895 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002896 if (!kw) {
2897 best = srv_find_best_kw(args[*cur_arg]);
2898 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002899 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2900 args[*cur_arg], best,
2901 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2902 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002903 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002904 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2905 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2906 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002907
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002908 return ERR_ALERT | ERR_FATAL;
2909 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002910
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002911 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002912 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2913 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002914 err_code = ERR_ALERT | ERR_FATAL;
2915 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002916 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002917
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002918 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002919 ha_alert("'%s' option is not accepted in default-server sections\n",
2920 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002921 err_code = ERR_ALERT;
2922 goto out;
2923 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002924 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002925 ha_alert("'%s' option is not accepted for dynamic server\n",
2926 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002927 err_code |= ERR_ALERT;
2928 goto out;
2929 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002930
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002931 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2932 if (err_code) {
2933 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2934 free(errmsg);
2935 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002936
2937out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002938 if (kw->skip != -1)
2939 *cur_arg += 1 + kw->skip;
2940
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002941 return err_code;
2942}
2943
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002944/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002945 * initialize a new server on startup.
2946 */
2947static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2948 struct server *srv, struct proxy *proxy,
2949 char **errmsg)
2950{
2951 int ret;
2952
2953 if (!srv->sni_expr)
2954 return 0;
2955
2956 ret = server_parse_sni_expr(srv, proxy, errmsg);
2957 if (!ret)
2958 return 0;
2959
2960 return ret;
2961}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002962
2963/* Server initializations finalization.
2964 * Initialize health check, agent check and SNI expression if enabled.
2965 * Must not be called for a default server instance.
2966 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002967 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002968 * initialize a new server on startup.
2969 */
2970static int _srv_parse_finalize(char **args, int cur_arg,
2971 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002972 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002973{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002974 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002975 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002976
2977 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002978 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002979 return ERR_ALERT | ERR_FATAL;
2980 }
2981
2982 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002983 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2984 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002985 return ERR_ALERT | ERR_FATAL;
2986 }
2987
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002988 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2989 if (errmsg) {
2990 ha_alert("%s\n", errmsg);
2991 free(errmsg);
2992 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002993 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002994 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002995
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002996 /* A dynamic server is disabled on startup. It must not be counted as
2997 * an active backend entry.
2998 */
2999 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
3000 if (srv->flags & SRV_F_BACKUP)
3001 px->srv_bck++;
3002 else
3003 px->srv_act++;
3004 }
3005
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01003006 srv_lb_commit_status(srv);
3007
3008 return 0;
3009}
3010
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003011int parse_server(const char *file, int linenum, char **args,
3012 struct proxy *curproxy, const struct proxy *defproxy,
3013 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01003014{
3015 struct server *newsrv = NULL;
3016 int err_code = 0;
3017
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003018 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02003019
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003020 set_usermsgs_ctx(file, linenum, NULL);
3021
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003022 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003023 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003024 err_code |= ERR_ALERT | ERR_FATAL;
3025 goto out;
3026 }
3027 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
3028 err_code |= ERR_ALERT | ERR_FATAL;
3029 goto out;
3030 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01003031
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003032 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
3033 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
3034 if (!*args[2])
3035 return 0;
3036 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003037
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003038 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003039 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01003040
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003041 /* the servers are linked backwards first */
3042 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
3043 newsrv->next = curproxy->srv;
3044 curproxy->srv = newsrv;
3045 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01003046
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003047 if (err_code & ERR_CODE)
3048 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003049
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003050 newsrv->conf.file = strdup(file);
3051 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003052
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003053 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003054 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003055 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003056 if (err_code & ERR_FATAL)
3057 goto out;
3058 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01003059
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003060 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02003061 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003062 if (err_code & ERR_FATAL)
3063 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02003064 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01003065
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003066 if (parse_flags & SRV_PARSE_TEMPLATE)
3067 _srv_parse_tmpl_init(newsrv, curproxy);
3068
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02003069 /* If the server id is fixed, insert it in the proxy used_id tree.
3070 * This is needed to detect a later duplicate id via srv_parse_id.
3071 *
3072 * If no is specified, a dynamic one is generated in
3073 * check_config_validity.
3074 */
3075 if (newsrv->flags & SRV_F_FORCED_ID)
3076 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3077
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02003078 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003079 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02003080
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003081 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003082 return 0;
3083
3084 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003085 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003086 return err_code;
3087}
3088
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003089/* Returns a pointer to the first server matching either id <id>.
3090 * NULL is returned if no match is found.
3091 * the lookup is performed in the backend <bk>
3092 */
3093struct server *server_find_by_id(struct proxy *bk, int id)
3094{
3095 struct eb32_node *eb32;
3096 struct server *curserver;
3097
3098 if (!bk || (id ==0))
3099 return NULL;
3100
3101 /* <bk> has no backend capabilities, so it can't have a server */
3102 if (!(bk->cap & PR_CAP_BE))
3103 return NULL;
3104
3105 curserver = NULL;
3106
3107 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3108 if (eb32)
3109 curserver = container_of(eb32, struct server, conf.id);
3110
3111 return curserver;
3112}
3113
3114/* Returns a pointer to the first server matching either name <name>, or id
3115 * if <name> starts with a '#'. NULL is returned if no match is found.
3116 * the lookup is performed in the backend <bk>
3117 */
3118struct server *server_find_by_name(struct proxy *bk, const char *name)
3119{
3120 struct server *curserver;
3121
3122 if (!bk || !name)
3123 return NULL;
3124
3125 /* <bk> has no backend capabilities, so it can't have a server */
3126 if (!(bk->cap & PR_CAP_BE))
3127 return NULL;
3128
3129 curserver = NULL;
3130 if (*name == '#') {
3131 curserver = server_find_by_id(bk, atoi(name + 1));
3132 if (curserver)
3133 return curserver;
3134 }
3135 else {
3136 curserver = bk->srv;
3137
3138 while (curserver && (strcmp(curserver->id, name) != 0))
3139 curserver = curserver->next;
3140
3141 if (curserver)
3142 return curserver;
3143 }
3144
3145 return NULL;
3146}
3147
3148struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3149{
3150 struct server *byname;
3151 struct server *byid;
3152
3153 if (!name && !id)
3154 return NULL;
3155
3156 if (diff)
3157 *diff = 0;
3158
3159 byname = byid = NULL;
3160
3161 if (name) {
3162 byname = server_find_by_name(bk, name);
3163 if (byname && (!id || byname->puid == id))
3164 return byname;
3165 }
3166
3167 /* remaining possibilities :
3168 * - name not set
3169 * - name set but not found
3170 * - name found but ID doesn't match
3171 */
3172 if (id) {
3173 byid = server_find_by_id(bk, id);
3174 if (byid) {
3175 if (byname) {
3176 /* use id only if forced by configuration */
3177 if (byid->flags & SRV_F_FORCED_ID) {
3178 if (diff)
3179 *diff |= 2;
3180 return byid;
3181 }
3182 else {
3183 if (diff)
3184 *diff |= 1;
3185 return byname;
3186 }
3187 }
3188
3189 /* remaining possibilities:
3190 * - name not set
3191 * - name set but not found
3192 */
3193 if (name && diff)
3194 *diff |= 2;
3195 return byid;
3196 }
3197
3198 /* id bot found */
3199 if (byname) {
3200 if (diff)
3201 *diff |= 1;
3202 return byname;
3203 }
3204 }
3205
3206 return NULL;
3207}
3208
Simon Horman7d09b9a2013-02-12 10:45:51 +09003209/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003210 * update a server's current IP address.
3211 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3212 * ip is in network format.
3213 * updater is a string which contains an information about the requester of the update.
3214 * updater is used if not NULL.
3215 *
3216 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003217 *
3218 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003219 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003220int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003221{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003222 /* save the new IP family & address if necessary */
3223 switch (ip_sin_family) {
3224 case AF_INET:
3225 if (s->addr.ss_family == ip_sin_family &&
3226 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3227 return 0;
3228 break;
3229 case AF_INET6:
3230 if (s->addr.ss_family == ip_sin_family &&
3231 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3232 return 0;
3233 break;
3234 };
3235
Baptiste Assmann14e40142015-04-14 01:13:07 +02003236 /* generates a log line and a warning on stderr */
3237 if (1) {
3238 /* book enough space for both IPv4 and IPv6 */
3239 char oldip[INET6_ADDRSTRLEN];
3240 char newip[INET6_ADDRSTRLEN];
3241
3242 memset(oldip, '\0', INET6_ADDRSTRLEN);
3243 memset(newip, '\0', INET6_ADDRSTRLEN);
3244
3245 /* copy old IP address in a string */
3246 switch (s->addr.ss_family) {
3247 case AF_INET:
3248 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3249 break;
3250 case AF_INET6:
3251 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3252 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003253 default:
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003254 strlcpy2(oldip, "(none)", sizeof(oldip));
Christopher Fauletb0b76072020-09-08 10:38:40 +02003255 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003256 };
3257
3258 /* copy new IP address in a string */
3259 switch (ip_sin_family) {
3260 case AF_INET:
3261 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3262 break;
3263 case AF_INET6:
3264 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3265 break;
3266 };
3267
3268 /* save log line into a buffer */
3269 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3270 s->proxy->id, s->id, oldip, newip, updater);
3271
3272 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003273 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003274
3275 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003276 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003277 }
3278
3279 /* save the new IP family */
3280 s->addr.ss_family = ip_sin_family;
3281 /* save the new IP address */
3282 switch (ip_sin_family) {
3283 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003284 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003285 break;
3286 case AF_INET6:
3287 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3288 break;
3289 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003290 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003291 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003292
3293 return 0;
3294}
3295
William Dauchy7cabc062021-02-11 22:51:24 +01003296/* update agent health check address and port
3297 * addr can be ip4/ip6 or a hostname
3298 * if one error occurs, don't apply anything
3299 * must be called with the server lock held.
3300 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003301const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003302{
3303 struct sockaddr_storage sk;
3304 struct buffer *msg;
3305 int new_port;
3306
3307 msg = get_trash_chunk();
3308 chunk_reset(msg);
3309
3310 if (!(s->agent.state & CHK_ST_ENABLED)) {
3311 chunk_strcat(msg, "agent checks are not enabled on this server");
3312 goto out;
3313 }
3314 if (addr) {
3315 memset(&sk, 0, sizeof(struct sockaddr_storage));
3316 if (str2ip(addr, &sk) == NULL) {
3317 chunk_appendf(msg, "invalid addr '%s'", addr);
3318 goto out;
3319 }
3320 }
3321 if (port) {
3322 if (strl2irc(port, strlen(port), &new_port) != 0) {
3323 chunk_appendf(msg, "provided port is not an integer");
3324 goto out;
3325 }
3326 if (new_port < 0 || new_port > 65535) {
3327 chunk_appendf(msg, "provided port is invalid");
3328 goto out;
3329 }
3330 }
3331out:
3332 if (msg->data)
3333 return msg->area;
3334 else {
3335 if (addr)
3336 set_srv_agent_addr(s, &sk);
3337 if (port)
3338 set_srv_agent_port(s, new_port);
3339 }
3340 return NULL;
3341}
3342
William Dauchyb456e1f2021-02-11 22:51:23 +01003343/* update server health check address and port
3344 * addr must be ip4 or ip6, it won't be resolved
3345 * if one error occurs, don't apply anything
3346 * must be called with the server lock held.
3347 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003348const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003349{
3350 struct sockaddr_storage sk;
3351 struct buffer *msg;
3352 int new_port;
3353
3354 msg = get_trash_chunk();
3355 chunk_reset(msg);
3356
3357 if (!(s->check.state & CHK_ST_ENABLED)) {
3358 chunk_strcat(msg, "health checks are not enabled on this server");
3359 goto out;
3360 }
3361 if (addr) {
3362 memset(&sk, 0, sizeof(struct sockaddr_storage));
3363 if (str2ip2(addr, &sk, 0) == NULL) {
3364 chunk_appendf(msg, "invalid addr '%s'", addr);
3365 goto out;
3366 }
3367 }
3368 if (port) {
3369 if (strl2irc(port, strlen(port), &new_port) != 0) {
3370 chunk_appendf(msg, "provided port is not an integer");
3371 goto out;
3372 }
3373 if (new_port < 0 || new_port > 65535) {
3374 chunk_appendf(msg, "provided port is invalid");
3375 goto out;
3376 }
3377 /* prevent the update of port to 0 if MAPPORTS are in use */
3378 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3379 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3380 goto out;
3381 }
3382 }
3383out:
3384 if (msg->data)
3385 return msg->area;
3386 else {
3387 if (addr)
3388 s->check.addr = sk;
3389 if (port)
3390 s->check.port = new_port;
3391 }
3392 return NULL;
3393}
3394
Baptiste Assmann14e40142015-04-14 01:13:07 +02003395/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003396 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3397 *
3398 * Caller can pass its name through <updater> to get it integrated in the response message
3399 * returned by the function.
3400 *
3401 * The function first does the following, in that order:
3402 * - validates the new addr and/or port
3403 * - checks if an update is required (new IP or port is different than current ones)
3404 * - checks the update is allowed:
3405 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3406 * - allow all changes if no CHECKS are configured
3407 * - if CHECK is configured:
3408 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3409 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3410 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003411 *
3412 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003413 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003414const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003415{
3416 struct sockaddr_storage sa;
3417 int ret, port_change_required;
3418 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003419 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003420 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003421 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003422
3423 msg = get_trash_chunk();
3424 chunk_reset(msg);
3425
3426 if (addr) {
3427 memset(&sa, 0, sizeof(struct sockaddr_storage));
3428 if (str2ip2(addr, &sa, 0) == NULL) {
3429 chunk_printf(msg, "Invalid addr '%s'", addr);
3430 goto out;
3431 }
3432
3433 /* changes are allowed on AF_INET* families only */
3434 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3435 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3436 goto out;
3437 }
3438
3439 /* collecting data currently setup */
3440 memset(current_addr, '\0', sizeof(current_addr));
3441 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3442 /* changes are allowed on AF_INET* families only */
3443 if ((ret != AF_INET) && (ret != AF_INET6)) {
3444 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3445 goto out;
3446 }
3447
3448 /* applying ADDR changes if required and allowed
3449 * ipcmp returns 0 when both ADDR are the same
3450 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003451 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003452 chunk_appendf(msg, "no need to change the addr");
3453 goto port;
3454 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003455 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003456 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003457
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003458 /* update report for caller */
3459 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3460 }
3461
3462 port:
3463 if (port) {
3464 char sign = '\0';
3465 char *endptr;
3466
3467 if (addr)
3468 chunk_appendf(msg, ", ");
3469
3470 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003471 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003472
3473 /* check if PORT change is required */
3474 port_change_required = 0;
3475
3476 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003477 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003478 new_port = strtol(port, &endptr, 10);
3479 if ((errno != 0) || (port == endptr)) {
3480 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3481 goto out;
3482 }
3483
3484 /* check if caller triggers a port mapped or offset */
3485 if (sign == '-' || (sign == '+')) {
3486 /* check if server currently uses port map */
3487 if (!(s->flags & SRV_F_MAPPORTS)) {
3488 /* switch from fixed port to port map mandatorily triggers
3489 * a port change */
3490 port_change_required = 1;
3491 /* check is configured
3492 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3493 * prevent PORT change if check doesn't have it's dedicated port while switching
3494 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003495 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003496 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.");
3497 goto out;
3498 }
3499 }
3500 /* we're already using port maps */
3501 else {
3502 port_change_required = current_port != new_port;
3503 }
3504 }
3505 /* fixed port */
3506 else {
3507 port_change_required = current_port != new_port;
3508 }
3509
3510 /* applying PORT changes if required and update response message */
3511 if (port_change_required) {
3512 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003513 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003514 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003515
3516 /* prepare message */
3517 chunk_appendf(msg, "port changed from '");
3518 if (s->flags & SRV_F_MAPPORTS)
3519 chunk_appendf(msg, "+");
3520 chunk_appendf(msg, "%d' to '", current_port);
3521
3522 if (sign == '-') {
3523 s->flags |= SRV_F_MAPPORTS;
3524 chunk_appendf(msg, "%c", sign);
3525 /* just use for result output */
3526 new_port = -new_port;
3527 }
3528 else if (sign == '+') {
3529 s->flags |= SRV_F_MAPPORTS;
3530 chunk_appendf(msg, "%c", sign);
3531 }
3532 else {
3533 s->flags &= ~SRV_F_MAPPORTS;
3534 }
3535
3536 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003537 }
3538 else {
3539 chunk_appendf(msg, "no need to change the port");
3540 }
3541 }
3542
3543out:
William Dauchy6318d332020-05-02 21:52:36 +02003544 if (changed) {
3545 /* force connection cleanup on the given server */
3546 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003547 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003548 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003549 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003550 if (updater)
3551 chunk_appendf(msg, " by '%s'", updater);
3552 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003553 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003554}
3555
Christopher Faulet5efdef22021-03-11 18:03:21 +01003556/*
3557 * update server status based on result of SRV resolution
3558 * returns:
3559 * 0 if server status is updated
3560 * 1 if server status has not changed
3561 *
3562 * Must be called with the server lock held.
3563 */
3564int srvrq_update_srv_status(struct server *s, int has_no_ip)
3565{
3566 if (!s->srvrq)
3567 return 1;
3568
3569 /* since this server has an IP, it can go back in production */
3570 if (has_no_ip == 0) {
3571 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3572 return 1;
3573 }
3574
3575 if (s->next_admin & SRV_ADMF_RMAINT)
3576 return 1;
3577
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003578 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOENT);
Christopher Faulet5efdef22021-03-11 18:03:21 +01003579 return 0;
3580}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003581
3582/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003583 * update server status based on result of name resolution
3584 * returns:
3585 * 0 if server status is updated
3586 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003587 *
3588 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003589 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003590int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003591{
Emeric Brun750fe792020-12-23 16:51:12 +01003592 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003593 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003594 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003595
Jerome Magnin012261a2020-07-28 13:38:22 +02003596 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003597 if (resolution == NULL)
3598 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003599
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003600 switch (resolution->status) {
3601 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003602 /* status when HAProxy has just (re)started.
3603 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003604 break;
3605
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003606 case RSLV_STATUS_VALID:
3607 /*
3608 * resume health checks
3609 * server will be turned back on if health check is safe
3610 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003611 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003612 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003613 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003614 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOIP);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003615 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003616 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003617
Emeric Brun52a91d32017-08-31 14:41:55 +02003618 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003619 return 1;
3620 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3621 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3622 s->proxy->id, s->id);
3623
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003624 ha_warning("%s.\n", trash.area);
3625 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003626 return 0;
3627
3628 case RSLV_STATUS_NX:
3629 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003630 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3631 if (!tick_is_expired(exp, now_ms))
3632 break;
3633
3634 if (s->next_admin & SRV_ADMF_RMAINT)
3635 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003636 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NX);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003637 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003638
3639 case RSLV_STATUS_TIMEOUT:
3640 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003641 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3642 if (!tick_is_expired(exp, now_ms))
3643 break;
3644
3645 if (s->next_admin & SRV_ADMF_RMAINT)
3646 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003647 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_TIMEOUT);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003648 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003649
3650 case RSLV_STATUS_REFUSED:
3651 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003652 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3653 if (!tick_is_expired(exp, now_ms))
3654 break;
3655
3656 if (s->next_admin & SRV_ADMF_RMAINT)
3657 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003658 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_REFUSED);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003659 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003660
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003661 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003662 /* stop server if resolution failed for a long enough period */
3663 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3664 if (!tick_is_expired(exp, now_ms))
3665 break;
3666
3667 if (s->next_admin & SRV_ADMF_RMAINT)
3668 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003669 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_UNSPEC);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003670 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003671 }
3672
3673 return 1;
3674}
3675
3676/*
3677 * Server Name Resolution valid response callback
3678 * It expects:
3679 * - <nameserver>: the name server which answered the valid response
3680 * - <response>: buffer containing a valid DNS response
3681 * - <response_len>: size of <response>
3682 * It performs the following actions:
3683 * - ignore response if current ip found and server family not met
3684 * - update with first new ip found if family is met and current IP is not found
3685 * returns:
3686 * 0 on error
3687 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003688 *
3689 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003690 */
Emeric Brun08622d32020-12-23 17:41:43 +01003691int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003692{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003693 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003694 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003695 void *serverip, *firstip;
3696 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003697 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003698 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003699 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003700
Christopher Faulet67957bd2017-09-27 11:00:59 +02003701 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003702 if (!s)
3703 return 1;
3704
Christopher Faulet49531e82021-03-10 15:07:27 +01003705 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003706 /* If DNS resolution is disabled ignore it.
3707 * This is the case if the server was associated to
3708 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003709 */
Emeric Brun34067662021-06-11 10:48:45 +02003710 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003711 return 1;
3712 }
3713
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003714 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003715 if (!resolution)
3716 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003717
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003718 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003719 firstip = NULL; /* pointer to the first valid response found */
3720 /* it will be used as the new IP if a change is required */
3721 firstip_sin_family = AF_UNSPEC;
3722 serverip = NULL; /* current server IP address */
3723
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003724 /* initializing server IP pointer */
3725 server_sin_family = s->addr.ss_family;
3726 switch (server_sin_family) {
3727 case AF_INET:
3728 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3729 break;
3730
3731 case AF_INET6:
3732 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3733 break;
3734
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003735 case AF_UNSPEC:
3736 break;
3737
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003738 default:
3739 goto invalid;
3740 }
3741
Emeric Brund30e9a12020-12-23 18:49:16 +01003742 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3743 serverip, server_sin_family, &firstip,
3744 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003745
3746 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003747 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003748 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003749
Emeric Brun456de772020-12-23 18:17:31 +01003750 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003751 goto save_ip;
3752
Emeric Brun456de772020-12-23 18:17:31 +01003753 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003754 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003755 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003756
Emeric Brun456de772020-12-23 18:17:31 +01003757 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003758 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003759 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003760 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003761 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003762
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003763 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003764 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003765 goto invalid;
3766
3767 }
3768
3769 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003770 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003771 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003772 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003773 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003774 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003775 else
3776 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003777 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003778
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003779 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003780 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3781 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003782 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003783
3784 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003785 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003786 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003787 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003788 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003789 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3790 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003791 return 0;
3792}
3793
3794/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003795 * SRV record error management callback
3796 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003797 * 0 if we can trash answser items.
3798 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003799 *
3800 * Grabs the server's lock.
3801 */
3802int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3803{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003804 struct resolv_srvrq *srvrq;
3805 struct resolv_resolution *res;
3806 struct resolvers *resolvers;
3807 int exp;
3808
3809 /* SRV records */
3810 srvrq = objt_resolv_srvrq(requester->owner);
3811 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003812 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003813
3814 resolvers = srvrq->resolvers;
3815 res = requester->resolution;
3816
3817 switch (res->status) {
3818
3819 case RSLV_STATUS_NX:
3820 /* stop server if resolution is NX for a long enough period */
3821 exp = tick_add(res->last_valid, resolvers->hold.nx);
3822 if (!tick_is_expired(exp, now_ms))
3823 return 1;
3824 break;
3825
3826 case RSLV_STATUS_TIMEOUT:
3827 /* stop server if resolution is TIMEOUT for a long enough period */
3828 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3829 if (!tick_is_expired(exp, now_ms))
3830 return 1;
3831 break;
3832
3833 case RSLV_STATUS_REFUSED:
3834 /* stop server if resolution is REFUSED for a long enough period */
3835 exp = tick_add(res->last_valid, resolvers->hold.refused);
3836 if (!tick_is_expired(exp, now_ms))
3837 return 1;
3838 break;
3839
3840 default:
3841 /* stop server if resolution failed for a long enough period */
3842 exp = tick_add(res->last_valid, resolvers->hold.other);
3843 if (!tick_is_expired(exp, now_ms))
3844 return 1;
3845 }
3846
Emeric Brun34067662021-06-11 10:48:45 +02003847 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003848 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003849
Emeric Brun12ca6582021-06-10 15:25:25 +02003850 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003851}
3852
3853/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003854 * Server Name Resolution error management callback
3855 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003856 * 0 if we can trash answser items.
3857 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003858 *
3859 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003860 */
Emeric Brun08622d32020-12-23 17:41:43 +01003861int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003862{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003863 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864
Christopher Faulet67957bd2017-09-27 11:00:59 +02003865 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003866 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003867 return 0;
3868
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003869 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003870 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003871 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003872 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003873 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003874 return 0;
3875 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003876 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003877
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003878 return 1;
3879}
3880
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003881/*
3882 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003883 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003884 * It returns a pointer to the first server found or NULL if <ip> is not yet
3885 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003886 *
3887 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003888 */
3889struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3890{
3891 struct server *tmpsrv;
3892 struct proxy *be;
3893
3894 if (!srv)
3895 return NULL;
3896
3897 be = srv->proxy;
3898 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003899 /* we found the current server is the same, ignore it */
3900 if (srv == tmpsrv)
3901 continue;
3902
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003903 /* We want to compare the IP in the record with the IP of the servers in the
3904 * same backend, only if:
3905 * * DNS resolution is enabled on the server
3906 * * the hostname used for the resolution by our server is the same than the
3907 * one used for the server found in the backend
3908 * * the server found in the backend is not our current server
3909 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003910 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003911 if ((tmpsrv->hostname_dn == NULL) ||
3912 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003913 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003914 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003915 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003916 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003917 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003918
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003919 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003920 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003921 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003922 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003923 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003924
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003925 /* At this point, we have 2 different servers using the same DNS hostname
3926 * for their respective resolution.
3927 */
3928 if (*ip_family == tmpsrv->addr.ss_family &&
3929 ((tmpsrv->addr.ss_family == AF_INET &&
3930 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3931 (tmpsrv->addr.ss_family == AF_INET6 &&
3932 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003933 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003934 return tmpsrv;
3935 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003936 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003937 }
3938
Emeric Brune9fd6b52017-11-02 17:20:39 +01003939
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003940 return NULL;
3941}
3942
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003943/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3944 * resolver. This is suited for initial address configuration. Returns 0 on
3945 * success otherwise a non-zero error code. In case of error, *err_code, if
3946 * not NULL, is filled up.
3947 */
3948int srv_set_addr_via_libc(struct server *srv, int *err_code)
3949{
3950 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003951 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003952 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003953 return 1;
3954 }
3955 return 0;
3956}
3957
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003958/* Set the server's FDQN (->hostname) from <hostname>.
3959 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003960 *
3961 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003962 */
Emeric Brun08622d32020-12-23 17:41:43 +01003963int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003964{
Emeric Brun08622d32020-12-23 17:41:43 +01003965 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003966 char *hostname_dn;
3967 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003968
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003969 /* Note that the server lock is already held. */
3970 if (!srv->resolvers)
3971 return -1;
3972
Emeric Brun08622d32020-12-23 17:41:43 +01003973 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003974 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003975 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003976 * and we can't enable it at run time for now.
3977 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003978 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003979 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003980
3981 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003982 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003983 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003984 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003985 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003986 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003987 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003988
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003989 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003990 if (resolution &&
3991 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003992 resolution->hostname_dn_len == hostname_dn_len &&
3993 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003994 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003995
Willy Tarreau6878f802021-10-20 14:07:31 +02003996 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003998 free(srv->hostname);
3999 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004000 srv->hostname = strdup(hostname);
4001 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004002 srv->hostname_dn_len = hostname_dn_len;
4003 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004004 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004005
Baptiste Assmann13a92322019-06-07 09:40:55 +02004006 if (srv->flags & SRV_F_NO_RESOLUTION)
4007 goto end;
4008
Emeric Brund30e9a12020-12-23 18:49:16 +01004009 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004010 goto err;
4011
4012 end:
Emeric Brun08622d32020-12-23 17:41:43 +01004013 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004014 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004015 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004016
4017 err:
Emeric Brun08622d32020-12-23 17:41:43 +01004018 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004019 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004020 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004021}
4022
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004023/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4024 * from the state file. This is suited for initial address configuration.
4025 * Returns 0 on success otherwise a non-zero error code. In case of error,
4026 * *err_code, if not NULL, is filled up.
4027 */
4028static int srv_apply_lastaddr(struct server *srv, int *err_code)
4029{
4030 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4031 if (err_code)
4032 *err_code |= ERR_WARN;
4033 return 1;
4034 }
4035 return 0;
4036}
4037
Willy Tarreau25e51522016-11-04 15:10:17 +01004038/* returns 0 if no error, otherwise a combination of ERR_* flags */
4039static int srv_iterate_initaddr(struct server *srv)
4040{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004041 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01004042 int return_code = 0;
4043 int err_code;
4044 unsigned int methods;
4045
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004046 /* If no addr and no hostname set, get the name from the DNS SRV request */
4047 if (!name && srv->srvrq)
4048 name = srv->srvrq->name;
4049
Willy Tarreau25e51522016-11-04 15:10:17 +01004050 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004051 if (!methods) {
4052 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01004053 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4054 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004055 if (srv->resolvers_id) {
4056 /* dns resolution is configured, add "none" to not fail on startup */
4057 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4058 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004059 }
4060
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004061 /* "-dr" : always append "none" so that server addresses resolution
4062 * failures are silently ignored, this is convenient to validate some
4063 * configs out of their environment.
4064 */
4065 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4066 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4067
Willy Tarreau25e51522016-11-04 15:10:17 +01004068 while (methods) {
4069 err_code = 0;
4070 switch (srv_get_next_initaddr(&methods)) {
4071 case SRV_IADDR_LAST:
4072 if (!srv->lastaddr)
4073 continue;
4074 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004075 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004076 return_code |= err_code;
4077 break;
4078
4079 case SRV_IADDR_LIBC:
4080 if (!srv->hostname)
4081 continue;
4082 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004083 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004084 return_code |= err_code;
4085 break;
4086
Willy Tarreau37ebe122016-11-04 15:17:58 +01004087 case SRV_IADDR_NONE:
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004088 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004089 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004090 ha_warning("could not resolve address '%s', disabling server.\n",
4091 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004092 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004093 return return_code;
4094
Willy Tarreau4310d362016-11-02 15:05:56 +01004095 case SRV_IADDR_IP:
4096 ipcpy(&srv->init_addr, &srv->addr);
4097 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004098 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4099 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004100 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004101 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004102
Willy Tarreau25e51522016-11-04 15:10:17 +01004103 default: /* unhandled method */
4104 break;
4105 }
4106 }
4107
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004108 if (!return_code)
4109 ha_alert("no method found to resolve address '%s'.\n", name);
4110 else
4111 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004112
4113 return_code |= ERR_ALERT | ERR_FATAL;
4114 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004115out:
4116 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004117 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004118 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004119}
4120
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004121/*
4122 * This function parses all backends and all servers within each backend
4123 * and performs servers' addr resolution based on information provided by:
4124 * - configuration file
4125 * - server-state file (states provided by an 'old' haproxy process)
4126 *
4127 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4128 */
4129int srv_init_addr(void)
4130{
4131 struct proxy *curproxy;
4132 int return_code = 0;
4133
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004134 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004135 while (curproxy) {
4136 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004137
4138 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004139 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004140 goto srv_init_addr_next;
4141
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004142 for (srv = curproxy->srv; srv; srv = srv->next) {
4143 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004144 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004145 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004146 reset_usermsgs_ctx();
4147 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004148
4149 srv_init_addr_next:
4150 curproxy = curproxy->next;
4151 }
4152
4153 return return_code;
4154}
4155
Willy Tarreau46b7f532018-08-21 11:54:26 +02004156/*
4157 * Must be called with the server lock held.
4158 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004159const 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 +02004160{
4161
Willy Tarreau83061a82018-07-13 11:56:34 +02004162 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004163
4164 msg = get_trash_chunk();
4165 chunk_reset(msg);
4166
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004167 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004168 chunk_appendf(msg, "no need to change the FDQN");
4169 goto out;
4170 }
4171
4172 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4173 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4174 goto out;
4175 }
4176
4177 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4178 server->proxy->id, server->id, server->hostname, fqdn);
4179
Emeric Brun08622d32020-12-23 17:41:43 +01004180 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004181 chunk_reset(msg);
4182 chunk_appendf(msg, "could not update %s/%s FQDN",
4183 server->proxy->id, server->id);
4184 goto out;
4185 }
4186
4187 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004188 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004189
4190 out:
4191 if (updater)
4192 chunk_appendf(msg, " by '%s'", updater);
4193 chunk_appendf(msg, "\n");
4194
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004195 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004196}
4197
4198
Willy Tarreau21b069d2016-11-23 17:15:08 +01004199/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4200 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004201 * 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 +01004202 * used for CLI commands requiring a server name.
4203 * Important: the <arg> is modified to remove the '/'.
4204 */
4205struct server *cli_find_server(struct appctx *appctx, char *arg)
4206{
4207 struct proxy *px;
4208 struct server *sv;
4209 char *line;
4210
4211 /* split "backend/server" and make <line> point to server */
4212 for (line = arg; *line; line++)
4213 if (*line == '/') {
4214 *line++ = '\0';
4215 break;
4216 }
4217
4218 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004219 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004220 return NULL;
4221 }
4222
4223 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004224 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004225 return NULL;
4226 }
4227
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004228 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004229 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004230 return NULL;
4231 }
4232
4233 return sv;
4234}
4235
William Lallemand222baf22016-11-19 02:00:33 +01004236
Willy Tarreau46b7f532018-08-21 11:54:26 +02004237/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004238static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004239{
4240 struct server *sv;
4241 const char *warning;
4242
4243 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4244 return 1;
4245
4246 sv = cli_find_server(appctx, args[2]);
4247 if (!sv)
4248 return 1;
4249
4250 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004251 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004252 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004253 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004254 if (warning)
4255 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004256 }
4257 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004258 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004259 if (strcmp(args[4], "ready") == 0)
4260 srv_adm_set_ready(sv);
4261 else if (strcmp(args[4], "drain") == 0)
4262 srv_adm_set_drain(sv);
4263 else if (strcmp(args[4], "maint") == 0)
4264 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004265 else
4266 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004267 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004268 }
4269 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004270 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004271 if (sv->track)
4272 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004273 else if (strcmp(args[4], "up") == 0) {
4274 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004275 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004276 }
4277 else if (strcmp(args[4], "stopping") == 0) {
4278 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004279 srv_set_stopping(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004280 }
4281 else if (strcmp(args[4], "down") == 0) {
4282 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004283 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004284 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004285 else
4286 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004287 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004288 }
4289 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004290 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004291 if (!(sv->agent.state & CHK_ST_ENABLED))
4292 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004293 else if (strcmp(args[4], "up") == 0) {
4294 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004295 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004296 }
4297 else if (strcmp(args[4], "down") == 0) {
4298 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004299 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004300 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004301 else
4302 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004303 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004304 }
Misiek2da082d2017-01-09 09:40:42 +01004305 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004306 char *addr = NULL;
4307 char *port = NULL;
4308 if (strlen(args[4]) == 0) {
4309 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4310 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004311 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004312 }
4313 addr = args[4];
4314 if (strcmp(args[5], "port") == 0)
4315 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004316 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004317 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004318 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004319 if (warning)
4320 cli_msg(appctx, LOG_WARNING, warning);
4321 }
4322 else if (strcmp(args[3], "agent-port") == 0) {
4323 char *port = NULL;
4324 if (strlen(args[4]) == 0) {
4325 cli_err(appctx, "set server <b>/<s> agent-port requires"
4326 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004327 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004328 }
4329 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004330 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004331 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004332 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004333 if (warning)
4334 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004335 }
4336 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004337 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004338 if (!(sv->agent.state & CHK_ST_ENABLED))
4339 cli_err(appctx, "agent checks are not enabled on this server.\n");
4340 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004341 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004342 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004343 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004344 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004345 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004346 else if (strcmp(args[3], "check-addr") == 0) {
4347 char *addr = NULL;
4348 char *port = NULL;
4349 if (strlen(args[4]) == 0) {
4350 cli_err(appctx, "set server <b>/<s> check-addr requires"
4351 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004352 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004353 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004354 addr = args[4];
4355 if (strcmp(args[5], "port") == 0)
4356 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004357 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004358 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004359 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004360 if (warning)
4361 cli_msg(appctx, LOG_WARNING, warning);
4362 }
4363 else if (strcmp(args[3], "check-port") == 0) {
4364 char *port = NULL;
4365 if (strlen(args[4]) == 0) {
4366 cli_err(appctx, "set server <b>/<s> check-port requires"
4367 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004368 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004369 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004370 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004371 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004372 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004373 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004374 if (warning)
4375 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004376 }
4377 else if (strcmp(args[3], "addr") == 0) {
4378 char *addr = NULL;
4379 char *port = NULL;
4380 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004381 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004382 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004383 }
4384 else {
4385 addr = args[4];
4386 }
4387 if (strcmp(args[5], "port") == 0) {
4388 port = args[6];
4389 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004390 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004391 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004392 if (warning)
4393 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004394 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004395 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004396 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004397 else if (strcmp(args[3], "fqdn") == 0) {
4398 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004399 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004400 goto out;
4401 }
4402 if (!sv->resolvers) {
4403 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4404 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004405 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004406 if (sv->srvrq) {
4407 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004408 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004409 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004410 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4411 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004412 /* ensure runtime resolver will process this new fqdn */
4413 if (sv->flags & SRV_F_NO_RESOLUTION) {
4414 sv->flags &= ~SRV_F_NO_RESOLUTION;
4415 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004416 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004417 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004418 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004419 if (warning)
4420 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004421 }
William Dauchyf6370442020-11-14 19:25:33 +01004422 else if (strcmp(args[3], "ssl") == 0) {
4423#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004424 if (sv->flags & SRV_F_DYNAMIC) {
4425 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4426 goto out;
4427 }
4428
William Dauchyf6370442020-11-14 19:25:33 +01004429 if (sv->ssl_ctx.ctx == NULL) {
4430 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4431 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004432 goto out;
4433 }
4434
4435 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4436 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004437 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004438 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004439 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004440 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004441 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004442 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004443 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004444 }
4445 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004446 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004447 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4448#else
4449 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4450#endif
4451 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004452 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004453 "usage: set server <backend>/<server> "
4454 "addr | agent | agent-addr | agent-port | agent-send | "
4455 "check-addr | check-port | fqdn | health | ssl | "
4456 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004457 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004458 out:
William Lallemand222baf22016-11-19 02:00:33 +01004459 return 1;
4460}
4461
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004462static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004463{
William Lallemand6b160942016-11-22 12:34:35 +01004464 struct proxy *px;
4465 struct server *sv;
4466 char *line;
4467
4468
4469 /* split "backend/server" and make <line> point to server */
4470 for (line = args[2]; *line; line++)
4471 if (*line == '/') {
4472 *line++ = '\0';
4473 break;
4474 }
4475
Willy Tarreau9d008692019-08-09 11:21:01 +02004476 if (!*line)
4477 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004478
Willy Tarreau9d008692019-08-09 11:21:01 +02004479 if (!get_backend_server(args[2], line, &px, &sv))
4480 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004481
4482 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004483 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4484 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004485 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004486 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004487 return 1;
4488}
4489
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004490/* Parse a "set weight" command.
4491 *
4492 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004493 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004494static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004495{
4496 struct server *sv;
4497 const char *warning;
4498
4499 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4500 return 1;
4501
4502 sv = cli_find_server(appctx, args[2]);
4503 if (!sv)
4504 return 1;
4505
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004506 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4507
William Lallemand6b160942016-11-22 12:34:35 +01004508 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004509 if (warning)
4510 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004511
4512 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4513
William Lallemand6b160942016-11-22 12:34:35 +01004514 return 1;
4515}
4516
Willy Tarreau46b7f532018-08-21 11:54:26 +02004517/* parse a "set maxconn server" command. It always returns 1.
4518 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004519 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004520 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004521static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004522{
4523 struct server *sv;
4524 const char *warning;
4525
4526 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4527 return 1;
4528
4529 sv = cli_find_server(appctx, args[3]);
4530 if (!sv)
4531 return 1;
4532
Amaury Denoyelle02742862021-06-18 11:11:36 +02004533 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4534
Willy Tarreaub8026272016-11-23 11:26:56 +01004535 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004536 if (warning)
4537 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004538
Amaury Denoyelle02742862021-06-18 11:11:36 +02004539 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4540
Willy Tarreaub8026272016-11-23 11:26:56 +01004541 return 1;
4542}
William Lallemand6b160942016-11-22 12:34:35 +01004543
Willy Tarreau46b7f532018-08-21 11:54:26 +02004544/* parse a "disable agent" command. It always returns 1.
4545 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004546 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004547 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004548static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004549{
4550 struct server *sv;
4551
4552 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4553 return 1;
4554
4555 sv = cli_find_server(appctx, args[2]);
4556 if (!sv)
4557 return 1;
4558
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004559 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004560 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004561 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004562 return 1;
4563}
4564
Willy Tarreau46b7f532018-08-21 11:54:26 +02004565/* parse a "disable health" command. It always returns 1.
4566 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004567 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004568 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004569static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004570{
4571 struct server *sv;
4572
4573 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4574 return 1;
4575
4576 sv = cli_find_server(appctx, args[2]);
4577 if (!sv)
4578 return 1;
4579
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004580 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004581 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004582 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004583 return 1;
4584}
4585
Willy Tarreau46b7f532018-08-21 11:54:26 +02004586/* parse a "disable server" command. It always returns 1.
4587 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004588 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004589 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004590static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004591{
4592 struct server *sv;
4593
4594 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4595 return 1;
4596
4597 sv = cli_find_server(appctx, args[2]);
4598 if (!sv)
4599 return 1;
4600
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004601 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004602 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004603 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004604 return 1;
4605}
4606
Willy Tarreau46b7f532018-08-21 11:54:26 +02004607/* parse a "enable agent" command. It always returns 1.
4608 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004609 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004610 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004611static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004612{
4613 struct server *sv;
4614
4615 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4616 return 1;
4617
4618 sv = cli_find_server(appctx, args[2]);
4619 if (!sv)
4620 return 1;
4621
Willy Tarreau9d008692019-08-09 11:21:01 +02004622 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4623 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004624
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004625 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004626 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004627 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004628 return 1;
4629}
4630
Willy Tarreau46b7f532018-08-21 11:54:26 +02004631/* parse a "enable health" 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_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +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
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004646 if (!(sv->check.state & CHK_ST_CONFIGURED))
4647 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4648
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004649 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004650 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004651 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004652 return 1;
4653}
4654
Willy Tarreau46b7f532018-08-21 11:54:26 +02004655/* parse a "enable server" command. It always returns 1.
4656 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004657 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004658 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004659static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004660{
4661 struct server *sv;
4662
4663 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4664 return 1;
4665
4666 sv = cli_find_server(appctx, args[2]);
4667 if (!sv)
4668 return 1;
4669
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004670 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004671 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004672 if (!(sv->flags & SRV_F_COOKIESET)
4673 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4674 sv->cookie)
4675 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004676 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004677 return 1;
4678}
4679
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004680/* Allocates data structure related to load balancing for the server <sv>. It
4681 * is only required for dynamic servers.
4682 *
4683 * At the moment, the server lock is not used as this function is only called
4684 * for a dynamic server not yet registered.
4685 *
4686 * Returns 1 on success, 0 on allocation failure.
4687 */
4688static int srv_alloc_lb(struct server *sv, struct proxy *be)
4689{
4690 int node;
4691
4692 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4693 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4694 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4695 sv->lb_nodes_now = 0;
4696
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004697 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4698 ((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 +01004699 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4700
4701 if (!sv->lb_nodes)
4702 return 0;
4703
4704 for (node = 0; node < sv->lb_nodes_tot; node++) {
4705 sv->lb_nodes[node].server = sv;
4706 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4707 }
4708 }
4709
4710 return 1;
4711}
4712
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004713/* updates the server's weight during a warmup stage. Once the final weight is
4714 * reached, the task automatically stops. Note that any server status change
4715 * must have updated s->last_change accordingly.
4716 */
4717static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4718{
4719 struct server *s = context;
4720
4721 /* by default, plan on stopping the task */
4722 t->expire = TICK_ETERNITY;
4723 if ((s->next_admin & SRV_ADMF_MAINT) ||
4724 (s->next_state != SRV_ST_STARTING))
4725 return t;
4726
4727 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4728
4729 /* recalculate the weights and update the state */
4730 server_recalc_eweight(s, 1);
4731
4732 /* probably that we can refill this server with a bit more connections */
4733 pendconn_grab_from_px(s);
4734
4735 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4736
4737 /* get back there in 1 second or 1/20th of the slowstart interval,
4738 * whichever is greater, resulting in small 5% steps.
4739 */
4740 if (s->next_state == SRV_ST_STARTING)
4741 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4742 return t;
4743}
4744
4745/* Allocate the slowstart task if the server is configured with a slowstart
4746 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4747 *
4748 * Returns 0 on success else non-zero.
4749 */
4750static int init_srv_slowstart(struct server *srv)
4751{
4752 struct task *t;
4753
4754 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004755 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004756 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4757 return ERR_ALERT | ERR_FATAL;
4758 }
4759
4760 /* We need a warmup task that will be called when the server
4761 * state switches from down to up.
4762 */
4763 srv->warmup = t;
4764 t->process = server_warmup;
4765 t->context = srv;
4766
4767 /* server can be in this state only because of */
4768 if (srv->next_state == SRV_ST_STARTING) {
4769 task_schedule(srv->warmup,
4770 tick_add(now_ms,
Willy Tarreau69530f52023-04-28 09:16:15 +02004771 MS_TO_TICKS(MAX(1000, (ns_to_sec(now_ns) - srv->last_change)) / 20)));
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004772 }
4773 }
4774
4775 return ERR_NONE;
4776}
4777REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4778
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004779/* Memory allocation and initialization of the per_thr field.
4780 * Returns 0 if the field has been successfully initialized, -1 on failure.
4781 */
4782int srv_init_per_thr(struct server *srv)
4783{
4784 int i;
4785
4786 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004787 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4788 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004789 return -1;
4790
4791 for (i = 0; i < global.nbthread; i++) {
4792 srv->per_thr[i].idle_conns = EB_ROOT;
4793 srv->per_thr[i].safe_conns = EB_ROOT;
4794 srv->per_thr[i].avail_conns = EB_ROOT;
4795 MT_LIST_INIT(&srv->per_thr[i].streams);
4796 }
4797
4798 return 0;
4799}
4800
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004801/* Parse a "add server" command
4802 * Returns 0 if the server has been successfully initialized, 1 on failure.
4803 */
4804static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4805{
4806 struct proxy *be;
4807 struct server *srv;
4808 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004809 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004810 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004811 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4812
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004813 usermsgs_clr("CLI");
4814
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004815 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4816 return 1;
4817
4818 ++args;
4819
4820 sv_name = be_name = args[1];
4821 /* split backend/server arg */
4822 while (*sv_name && *(++sv_name)) {
4823 if (*sv_name == '/') {
4824 *sv_name = '\0';
4825 ++sv_name;
4826 break;
4827 }
4828 }
4829
4830 if (!*sv_name)
4831 return cli_err(appctx, "Require 'backend/server'.");
4832
Amaury Denoyellecece9182021-04-20 17:09:08 +02004833 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004834 if (!be)
4835 return cli_err(appctx, "No such backend.");
4836
4837 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004838 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004839 return 1;
4840 }
4841
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004842 /* At this point, some operations might not be thread-safe anymore. This
4843 * might be the case for parsing handlers which were designed to run
4844 * only at the starting stage on single-thread mode.
4845 *
4846 * Activate thread isolation to ensure thread-safety.
4847 */
4848 thread_isolate();
4849
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004850 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004851 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4852 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004853 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004854
4855 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004856 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004857
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004858 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004859 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004860 }
4861
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004862 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4863 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004864 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004865
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004866 /* A dynamic server does not currently support resolution.
4867 *
4868 * Initialize it explicitly to the "none" method to ensure no
4869 * resolution will ever be executed.
4870 */
4871 srv->init_addr_methods = SRV_IADDR_NONE;
4872
Amaury Denoyelle30467232021-03-12 18:03:27 +01004873 if (srv->mux_proto) {
4874 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004875 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004876 goto out;
4877 }
4878 }
4879
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004880 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004881 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004882 goto out;
4883 }
4884
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004885 if (srv->max_idle_conns != 0) {
4886 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4887 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004888 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004889 goto out;
4890 }
4891 }
4892
4893 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004894 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004895 goto out;
4896 }
4897
4898 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4899 COUNTERS_SV,
4900 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004901 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004902 goto out;
4903 }
4904
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004905 /* ensure minconn/maxconn consistency */
4906 srv_minmax_conn_apply(srv);
4907
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004908 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4909 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004910 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4911 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4912 goto out;
4913 }
4914 }
4915
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004916 if (srv->trackit) {
4917 if (srv_apply_track(srv, be))
4918 goto out;
4919 }
4920
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004921 /* Init check/agent if configured. The check is manually disabled
4922 * because a dynamic server is started in a disable state. It must be
4923 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004924 */
4925 if (srv->do_check) {
4926 if (init_srv_check(srv))
4927 goto out;
4928
4929 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004930 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004931
4932 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004933 if (init_srv_agent_check(srv))
4934 goto out;
4935
4936 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004937 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004938
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004939 /* Init slowstart if needed. */
4940 if (init_srv_slowstart(srv))
4941 goto out;
4942
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004943 /* Attach the server to the end of the proxy linked list. Note that this
4944 * operation is not thread-safe so this is executed under thread
4945 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004946 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004947 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004948 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004949
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004950 /* TODO use a double-linked list for px->srv */
4951 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004952 struct server *next = be->srv;
4953
4954 while (1) {
4955 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004956 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004957 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004958 goto out;
4959 }
4960
4961 if (!next->next)
4962 break;
4963
4964 next = next->next;
4965 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004966
4967 next->next = srv;
4968 }
4969 else {
4970 srv->next = be->srv;
4971 be->srv = srv;
4972 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004973
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004974 /* generate the server id if not manually specified */
4975 if (!srv->puid) {
4976 next_id = get_next_id(&be->conf.used_server_id, 1);
4977 if (!next_id) {
4978 ha_alert("Cannot attach server : no id left in proxy\n");
4979 goto out;
4980 }
4981
4982 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004983 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004984 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004985
4986 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004987 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4988 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004989 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4990 if (srv->addr_node.key)
4991 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004992
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004993 /* check if LSB bit (odd bit) is set for reuse_cnt */
4994 if (srv_id_reuse_cnt & 1) {
4995 /* cnt must be increased */
4996 srv_id_reuse_cnt++;
4997 }
4998 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4999 * save some space
5000 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
5001 */
5002 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
5003
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005004 /* adding server cannot fail when we reach this:
5005 * publishing EVENT_HDL_SUB_SERVER_ADD
5006 */
5007 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
5008
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005009 thread_release();
5010
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005011 /* Start the check task. The server must be fully initialized.
5012 *
5013 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
5014 * need to randomly spread the task interval for dynamic servers.
5015 */
5016 if (srv->check.state & CHK_ST_CONFIGURED) {
5017 if (!start_check_task(&srv->check, 0, 1, 1))
5018 ha_alert("System might be unstable, consider to execute a reload");
5019 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005020 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005021 if (!start_check_task(&srv->agent, 0, 1, 1))
5022 ha_alert("System might be unstable, consider to execute a reload");
5023 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005024
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005025 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01005026 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005027
5028 return 0;
5029
5030out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02005031 if (srv) {
5032 if (srv->track)
5033 release_server_track(srv);
5034
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005035 if (srv->check.state & CHK_ST_CONFIGURED)
5036 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005037 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005038 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005039
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02005040 /* remove the server from the proxy linked list */
5041 if (be->srv == srv) {
5042 be->srv = srv->next;
5043 }
5044 else {
5045 struct server *prev;
5046 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
5047 ;
5048 if (prev)
5049 prev->next = srv->next;
5050 }
5051
5052 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02005053
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02005054 thread_release();
5055
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005056 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01005057 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005058
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02005059 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005060 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005061
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01005062 return 1;
5063}
5064
Amaury Denoyellee5580432021-04-15 14:41:20 +02005065/* Parse a "del server" command
5066 * Returns 0 if the server has been successfully initialized, 1 on failure.
5067 */
5068static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
5069{
5070 struct proxy *be;
5071 struct server *srv;
5072 char *be_name, *sv_name;
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005073 struct server *prev_del;
Amaury Denoyellee5580432021-04-15 14:41:20 +02005074
5075 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5076 return 1;
5077
5078 ++args;
5079
5080 sv_name = be_name = args[1];
5081 /* split backend/server arg */
5082 while (*sv_name && *(++sv_name)) {
5083 if (*sv_name == '/') {
5084 *sv_name = '\0';
5085 ++sv_name;
5086 break;
5087 }
5088 }
5089
5090 if (!*sv_name)
5091 return cli_err(appctx, "Require 'backend/server'.");
5092
5093 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005094 * requires thread isolation. In addition, any place referencing the
5095 * server about to be deleted would be unsafe after our operation, so
5096 * we must be certain to be alone so that no other thread has even
5097 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005098 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005099 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005100
5101 get_backend_server(be_name, sv_name, &be, &srv);
5102 if (!be) {
5103 cli_err(appctx, "No such backend.");
5104 goto out;
5105 }
5106
5107 if (!srv) {
5108 cli_err(appctx, "No such server.");
5109 goto out;
5110 }
5111
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005112 if (srv->flags & SRV_F_NON_PURGEABLE) {
5113 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 +02005114 goto out;
5115 }
5116
5117 /* Only servers in maintenance can be deleted. This ensures that the
5118 * server is not present anymore in the lb structures (through
5119 * lbprm.set_server_status_down).
5120 */
5121 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5122 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5123 goto out;
5124 }
5125
5126 /* Ensure that there is no active/idle/pending connection on the server.
5127 *
5128 * TODO idle connections should not prevent server deletion. A proper
5129 * cleanup function should be implemented to be used here.
5130 */
5131 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005132 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005133 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5134 goto out;
5135 }
5136
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005137 /* removing cannot fail anymore when we reach this:
5138 * publishing EVENT_HDL_SUB_SERVER_DEL
5139 */
5140 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5141
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005142 /* remove srv from tracking list */
5143 if (srv->track)
5144 release_server_track(srv);
5145
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005146 /* stop the check task if running */
5147 if (srv->check.state & CHK_ST_CONFIGURED)
5148 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005149 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005150 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005151
5152 /* detach the server from the proxy linked list
5153 * The proxy servers list is currently not protected by a lock, so this
5154 * requires thread_isolate/release.
5155 */
5156
5157 /* be->srv cannot be empty since we have already found the server with
5158 * get_backend_server */
5159 BUG_ON(!be->srv);
5160 if (be->srv == srv) {
5161 be->srv = srv->next;
5162 }
5163 else {
5164 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005165 for (next = be->srv; srv != next->next; next = next->next) {
5166 /* srv cannot be not found since we have already found
5167 * it with get_backend_server */
5168 BUG_ON(!next);
5169 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005170
Amaury Denoyellee5580432021-04-15 14:41:20 +02005171 next->next = srv->next;
5172 }
5173
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005174 /* Some deleted servers could still point to us using their 'next',
5175 * update them as needed
5176 * Please note the small race between the POP and APPEND, although in
5177 * this situation this is not an issue as we are under full thread
5178 * isolation
5179 */
5180 while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) {
5181 /* update its 'next' ptr */
5182 prev_del->next = srv->next;
5183 if (srv->next) {
5184 /* now it is our 'next' responsibility */
5185 MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted);
5186 }
5187 }
5188
5189 /* we ourselves need to inform our 'next' that we will still point it */
5190 if (srv->next)
5191 MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted);
5192
Amaury Denoyellee5580432021-04-15 14:41:20 +02005193 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005194 eb32_delete(&srv->conf.id);
5195 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005196 if (srv->addr_node.key)
5197 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005198
5199 /* remove srv from idle_node tree for idle conn cleanup */
5200 eb32_delete(&srv->idle_node);
5201
Aurelien DARRAGON75b9d1c2023-01-24 14:40:01 +01005202 /* flag the server as deleted
5203 * (despite the server being removed from primary server list,
5204 * one could still access the server data from a valid ptr)
5205 * Deleted flag helps detecting when a server is in transient removal
5206 * state.
5207 * ie: removed from the list but not yet freed/purged from memory.
5208 */
5209 srv->flags |= SRV_F_DELETED;
5210
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005211 /* set LSB bit (odd bit) for reuse_cnt */
5212 srv_id_reuse_cnt |= 1;
5213
Amaury Denoyellee5580432021-04-15 14:41:20 +02005214 thread_release();
5215
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005216 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005217 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005218
5219 cli_msg(appctx, LOG_INFO, "Server deleted.");
5220
5221 return 0;
5222
5223out:
5224 thread_release();
5225
5226 return 1;
5227}
5228
William Lallemand222baf22016-11-19 02:00:33 +01005229/* register cli keywords */
5230static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005231 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5232 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005233 { { "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 +02005234 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5235 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005236 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5237 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5238 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5239 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5240 { { "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 +01005241 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5242 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005243 {{},}
5244}};
5245
Willy Tarreau0108d902018-11-25 19:14:37 +01005246INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005247
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005248/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5249 * field is used to retrieve the identifier of the tracked server, either with
5250 * the format "proxy/server" or just "server". <curproxy> must point to the
5251 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5252 * to find the tracked server.
5253 *
5254 * Returns 0 if the server track has been activated else non-zero.
5255 *
5256 * Not thread-safe.
5257 */
5258int srv_apply_track(struct server *srv, struct proxy *curproxy)
5259{
5260 struct proxy *px;
5261 struct server *strack, *loop;
5262 char *pname, *sname;
5263
5264 if (!srv->trackit)
5265 return 1;
5266
5267 pname = srv->trackit;
5268 sname = strrchr(pname, '/');
5269
5270 if (sname) {
5271 *sname++ = '\0';
5272 }
5273 else {
5274 sname = pname;
5275 pname = NULL;
5276 }
5277
5278 if (pname) {
5279 px = proxy_be_by_name(pname);
5280 if (!px) {
5281 ha_alert("unable to find required proxy '%s' for tracking.\n",
5282 pname);
5283 return 1;
5284 }
5285 }
5286 else {
5287 px = curproxy;
5288 }
5289
5290 strack = findserver(px, sname);
5291 if (!strack) {
5292 ha_alert("unable to find required server '%s' for tracking.\n",
5293 sname);
5294 return 1;
5295 }
5296
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005297 if (strack->flags & SRV_F_DYNAMIC) {
5298 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5299 px->id, strack->id);
5300 return 1;
5301 }
5302
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005303 if (!strack->do_check && !strack->do_agent && !strack->track &&
5304 !strack->trackit) {
5305 ha_alert("unable to use %s/%s for "
5306 "tracking as it does not have any check nor agent enabled.\n",
5307 px->id, strack->id);
5308 return 1;
5309 }
5310
5311 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5312 ;
5313
5314 if (srv == strack || loop) {
5315 ha_alert("unable to track %s/%s as it "
5316 "belongs to a tracking chain looping back to %s/%s.\n",
5317 px->id, strack->id, px->id,
5318 srv == strack ? strack->id : loop->id);
5319 return 1;
5320 }
5321
5322 if (curproxy != px &&
5323 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5324 ha_alert("unable to use %s/%s for"
5325 "tracking: disable-on-404 option inconsistency.\n",
5326 px->id, strack->id);
5327 return 1;
5328 }
5329
5330 srv->track = strack;
5331 srv->tracknext = strack->trackers;
5332 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005333 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005334
5335 ha_free(&srv->trackit);
5336
5337 return 0;
5338}
5339
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005340/* This function propagates srv state change to lb algorithms */
5341static void srv_lb_propagate(struct server *s)
5342{
5343 struct proxy *px = s->proxy;
5344
5345 if (px->lbprm.update_server_eweight)
5346 px->lbprm.update_server_eweight(s);
5347 else if (srv_willbe_usable(s)) {
5348 if (px->lbprm.set_server_status_up)
5349 px->lbprm.set_server_status_up(s);
5350 }
5351 else {
5352 if (px->lbprm.set_server_status_down)
5353 px->lbprm.set_server_status_down(s);
5354 }
5355}
5356
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005357/* directly update server state based on an operational change
5358 * (compare current and next state to know which transition to apply)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005359 *
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005360 * The function returns the number of requeued sessions (either taken by
5361 * the server or redispatched to others servers) due to the server state
5362 * change.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005363 */
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005364static int _srv_update_status_op(struct server *s, enum srv_op_st_chg_cause cause)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005365{
Willy Tarreau83061a82018-07-13 11:56:34 +02005366 struct buffer *tmptrash = NULL;
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005367 int log_level;
5368 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5369 int xferred = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005370
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005371 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5372 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005373
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005374 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5375 srv_shutdown_streams(s, SF_ERR_DOWN);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005376
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005377 /* we might have streams queued on this server and waiting for
5378 * a connection. Those which are redispatchable will be queued
5379 * to another server or to the proxy itself.
5380 */
5381 xferred = pendconn_redistribute(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005382
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005383 tmptrash = alloc_trash_chunk();
5384 if (tmptrash) {
5385 chunk_printf(tmptrash,
5386 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5387 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005388
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005389 srv_append_op_chg_cause(tmptrash, s, cause);
5390 srv_append_more(tmptrash, s, xferred, 0);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005391
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005392 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005393
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005394 /* we don't send an alert if the server was previously paused */
5395 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
5396 send_log(s->proxy, log_level, "%s.\n",
5397 tmptrash->area);
5398 send_email_alert(s, log_level, "%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005399 tmptrash->area);
Aurelien DARRAGON0d2f1ac2023-05-15 18:03:35 +02005400 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005401 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005402 }
5403 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5404 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005405
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005406 /* we might have streams queued on this server and waiting for
5407 * a connection. Those which are redispatchable will be queued
5408 * to another server or to the proxy itself.
5409 */
5410 xferred = pendconn_redistribute(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005411
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005412 tmptrash = alloc_trash_chunk();
5413 if (tmptrash) {
5414 chunk_printf(tmptrash,
5415 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5416 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005417
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005418 srv_append_op_chg_cause(tmptrash, s, cause);
5419 srv_append_more(tmptrash, s, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005420
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005421 ha_warning("%s.\n", tmptrash->area);
5422 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5423 tmptrash->area);
5424 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005425 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005426 }
5427 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5428 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005429
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005430 if (s->next_state == SRV_ST_STARTING && s->warmup)
5431 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005432
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005433 server_recalc_eweight(s, 0);
5434 /* now propagate the status change to any LB algorithms */
5435 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005436
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005437 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5438 * and it's not a backup server and its effective weight is > 0,
5439 * then it can accept new connections, so we shut down all streams
5440 * on all backup servers.
5441 */
5442 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5443 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5444 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005445
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005446 /* check if we can handle some connections queued at the proxy. We
5447 * will take as many as we can handle.
5448 */
5449 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005450
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005451 tmptrash = alloc_trash_chunk();
5452 if (tmptrash) {
5453 chunk_printf(tmptrash,
5454 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5455 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005456
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005457 srv_append_op_chg_cause(tmptrash, s, cause);
5458 srv_append_more(tmptrash, s, xferred, 0);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005459
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005460 ha_warning("%s.\n", tmptrash->area);
5461 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5462 tmptrash->area);
5463 send_email_alert(s, LOG_NOTICE, "%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005464 tmptrash->area);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005465 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005466 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005467 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005468 else if (s->cur_eweight != s->next_eweight) {
5469 /* now propagate the status change to any LB algorithms */
5470 srv_lb_propagate(s);
5471 }
5472 return xferred;
5473}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005474
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005475/* deduct and update server state from an administrative change
5476 * (use current and next admin to deduct the administrative transition that
5477 * may result in server state update)
5478 *
5479 * The function returns the number of requeued sessions (either taken by
5480 * the server or redispatched to others servers) due to the server state
5481 * change.
5482 */
5483static int _srv_update_status_adm(struct server *s, enum srv_adm_st_chg_cause cause)
5484{
5485 struct buffer *tmptrash = NULL;
5486 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5487 int xferred = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005488
5489 /* Maintenance must also disable health checks */
5490 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5491 if (s->check.state & CHK_ST_ENABLED) {
5492 s->check.state |= CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005493 s->check.health = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005494 }
5495
5496 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005497 tmptrash = alloc_trash_chunk();
5498 if (tmptrash) {
5499 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005500 "%sServer %s/%s was DOWN and now enters maintenance",
5501 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005502 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005503 srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005504
Olivier Houchard796a2b32017-10-24 17:42:47 +02005505 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005506 ha_warning("%s.\n", tmptrash->area);
5507 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5508 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005509 }
5510 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005511 }
5512 }
5513 else { /* server was still running */
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005514 s->check.health = 0; /* failure */
Emeric Brune3114802017-12-21 14:42:26 +01005515
5516 s->next_state = SRV_ST_STOPPED;
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005517 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005518
Emeric Brun64cc49c2017-10-03 14:46:45 +02005519 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5520 srv_shutdown_streams(s, SF_ERR_DOWN);
5521
William Dauchy6318d332020-05-02 21:52:36 +02005522 /* force connection cleanup on the given server */
5523 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005524 /* we might have streams queued on this server and waiting for
5525 * a connection. Those which are redispatchable will be queued
5526 * to another server or to the proxy itself.
5527 */
5528 xferred = pendconn_redistribute(s);
5529
5530 tmptrash = alloc_trash_chunk();
5531 if (tmptrash) {
5532 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005533 "%sServer %s/%s is going DOWN for maintenance",
Emeric Brun64cc49c2017-10-03 14:46:45 +02005534 s->flags & SRV_F_BACKUP ? "Backup " : "",
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005535 s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005536 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005537 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005538
5539 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005540 ha_warning("%s.\n", tmptrash->area);
5541 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5542 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005543 }
5544 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005545 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005546 }
5547 }
5548 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5549 /* OK here we're leaving maintenance, we have many things to check,
5550 * because the server might possibly be coming back up depending on
5551 * its state. In practice, leaving maintenance means that we should
5552 * immediately turn to UP (more or less the slowstart) under the
5553 * following conditions :
5554 * - server is neither checked nor tracked
5555 * - server tracks another server which is not checked
5556 * - server tracks another server which is already up
5557 * Which sums up as something simpler :
5558 * "either the tracking server is up or the server's checks are disabled
5559 * or up". Otherwise we only re-enable health checks. There's a special
5560 * case associated to the stopping state which can be inherited. Note
5561 * that the server might still be in drain mode, which is naturally dealt
5562 * with by the lower level functions.
5563 */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005564 if (s->check.state & CHK_ST_ENABLED) {
5565 s->check.state &= ~CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005566 s->check.health = s->check.rise; /* start OK but check immediately */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005567 }
5568
5569 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5570 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5571 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5572 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5573 s->next_state = SRV_ST_STOPPING;
5574 }
5575 else {
5576 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005577 if (s->slowstart > 0) {
5578 if (s->warmup)
5579 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5580 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005581 else
5582 s->next_state = SRV_ST_RUNNING;
5583 }
5584
5585 }
5586
5587 tmptrash = alloc_trash_chunk();
5588 if (tmptrash) {
5589 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5590 chunk_printf(tmptrash,
5591 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5592 s->flags & SRV_F_BACKUP ? "Backup " : "",
5593 s->proxy->id, s->id,
5594 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5595 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5596 }
5597 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5598 chunk_printf(tmptrash,
5599 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5600 s->flags & SRV_F_BACKUP ? "Backup " : "",
5601 s->proxy->id, s->id, s->hostname,
5602 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5603 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5604 }
5605 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5606 chunk_printf(tmptrash,
5607 "%sServer %s/%s is %s/%s (leaving maintenance)",
5608 s->flags & SRV_F_BACKUP ? "Backup " : "",
5609 s->proxy->id, s->id,
5610 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5611 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5612 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005613 ha_warning("%s.\n", tmptrash->area);
5614 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5615 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005616 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005617 }
5618
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005619 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005620 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005621 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005622
Willy Tarreau6a78e612018-08-07 10:14:53 +02005623 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5624 * and it's not a backup server and its effective weight is > 0,
5625 * then it can accept new connections, so we shut down all streams
5626 * on all backup servers.
5627 */
5628 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5629 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5630 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5631
5632 /* check if we can handle some connections queued at the proxy. We
5633 * will take as many as we can handle.
5634 */
5635 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005636 }
5637 else if (s->next_admin & SRV_ADMF_MAINT) {
5638 /* remaining in maintenance mode, let's inform precisely about the
5639 * situation.
5640 */
5641 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5642 tmptrash = alloc_trash_chunk();
5643 if (tmptrash) {
5644 chunk_printf(tmptrash,
5645 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5646 s->flags & SRV_F_BACKUP ? "Backup " : "",
5647 s->proxy->id, s->id);
5648
5649 if (s->track) /* normally it's mandatory here */
5650 chunk_appendf(tmptrash, " via %s/%s",
5651 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005652 ha_warning("%s.\n", tmptrash->area);
5653 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5654 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005655 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005656 }
5657 }
5658 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5659 tmptrash = alloc_trash_chunk();
5660 if (tmptrash) {
5661 chunk_printf(tmptrash,
5662 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5663 s->flags & SRV_F_BACKUP ? "Backup " : "",
5664 s->proxy->id, s->id, s->hostname);
5665
5666 if (s->track) /* normally it's mandatory here */
5667 chunk_appendf(tmptrash, " via %s/%s",
5668 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005669 ha_warning("%s.\n", tmptrash->area);
5670 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5671 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005672 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005673 }
5674 }
5675 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5676 tmptrash = alloc_trash_chunk();
5677 if (tmptrash) {
5678 chunk_printf(tmptrash,
5679 "%sServer %s/%s remains in forced maintenance",
5680 s->flags & SRV_F_BACKUP ? "Backup " : "",
5681 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005682 ha_warning("%s.\n", tmptrash->area);
5683 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5684 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005685 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005686 }
5687 }
5688 /* don't report anything when leaving drain mode and remaining in maintenance */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005689 }
5690
5691 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5692 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5693 /* drain state is applied only if not yet in maint */
5694
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005695 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005696
5697 /* we might have streams queued on this server and waiting for
5698 * a connection. Those which are redispatchable will be queued
5699 * to another server or to the proxy itself.
5700 */
5701 xferred = pendconn_redistribute(s);
5702
5703 tmptrash = alloc_trash_chunk();
5704 if (tmptrash) {
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005705 chunk_printf(tmptrash, "%sServer %s/%s enters drain state",
5706 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005707 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005708 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005709
5710 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005711 ha_warning("%s.\n", tmptrash->area);
5712 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5713 tmptrash->area);
5714 send_email_alert(s, LOG_NOTICE, "%s",
5715 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005716 }
5717 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005718 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005719 }
5720 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5721 /* OK completely leaving drain mode */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005722 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005723
5724 tmptrash = alloc_trash_chunk();
5725 if (tmptrash) {
Aurelien DARRAGON9a1df022023-03-27 10:17:31 +02005726 if (s->cur_admin & SRV_ADMF_FDRAIN) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005727 chunk_printf(tmptrash,
5728 "%sServer %s/%s is %s (leaving forced drain)",
5729 s->flags & SRV_F_BACKUP ? "Backup " : "",
5730 s->proxy->id, s->id,
5731 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5732 }
5733 else {
5734 chunk_printf(tmptrash,
5735 "%sServer %s/%s is %s (leaving drain)",
5736 s->flags & SRV_F_BACKUP ? "Backup " : "",
5737 s->proxy->id, s->id,
5738 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5739 if (s->track) /* normally it's mandatory here */
5740 chunk_appendf(tmptrash, " via %s/%s",
5741 s->track->proxy->id, s->track->id);
5742 }
5743
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005744 ha_warning("%s.\n", tmptrash->area);
5745 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5746 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005747 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005748 }
5749
5750 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005751 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005752 }
5753 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5754 /* remaining in drain mode after removing one of its flags */
5755
5756 tmptrash = alloc_trash_chunk();
5757 if (tmptrash) {
5758 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5759 chunk_printf(tmptrash,
Aurelien DARRAGON977688b2023-05-03 10:36:54 +02005760 "%sServer %s/%s remains in drain mode",
Emeric Brun64cc49c2017-10-03 14:46:45 +02005761 s->flags & SRV_F_BACKUP ? "Backup " : "",
5762 s->proxy->id, s->id);
5763
5764 if (s->track) /* normally it's mandatory here */
5765 chunk_appendf(tmptrash, " via %s/%s",
5766 s->track->proxy->id, s->track->id);
5767 }
5768 else {
5769 chunk_printf(tmptrash,
5770 "%sServer %s/%s remains in forced drain mode",
5771 s->flags & SRV_F_BACKUP ? "Backup " : "",
5772 s->proxy->id, s->id);
5773 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005774 ha_warning("%s.\n", tmptrash->area);
5775 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5776 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005777 free_trash_chunk(tmptrash);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005778 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005779 }
5780 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005781 return xferred;
5782}
5783
5784/*
5785 * This function applies server's status changes.
5786 *
5787 * Must be called with the server lock held. This may also be called at init
5788 * time as the result of parsing the state file, in which case no lock will be
5789 * held, and the server's warmup task can be null.
5790 * <type> should be 0 for operational and 1 for administrative
5791 * <cause> must be srv_op_st_chg_cause enum for operational and
5792 * srv_adm_st_chg_cause enum for administrative
5793 */
5794static void srv_update_status(struct server *s, int type, int cause)
5795{
5796 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5797 enum srv_state srv_prev_state = s->cur_state;
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005798 union {
5799 struct event_hdl_cb_data_server_state state;
Aurelien DARRAGONa163d652023-04-21 18:06:58 +02005800 struct event_hdl_cb_data_server_admin admin;
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005801 struct event_hdl_cb_data_server common;
5802 } cb_data;
5803 int requeued;
5804
5805 /* prepare common server event data */
5806 _srv_event_hdl_prepare(&cb_data.common, s, 0);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005807
Aurelien DARRAGONa163d652023-04-21 18:06:58 +02005808 if (type) {
5809 cb_data.admin.safe.cause = cause;
5810 cb_data.admin.safe.old_admin = s->cur_admin;
5811 cb_data.admin.safe.new_admin = s->next_admin;
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005812 requeued = _srv_update_status_adm(s, cause);
Aurelien DARRAGONa163d652023-04-21 18:06:58 +02005813 cb_data.admin.safe.requeued = requeued;
5814 /* publish admin change */
5815 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADMIN, cb_data.admin, s);
5816 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005817 else
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005818 requeued = _srv_update_status_op(s, cause);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005819
Aurelien DARRAGON22151c72023-04-17 17:45:58 +02005820 /* explicitly commit state changes (even if it was already applied implicitly
5821 * by some lb state change function), so we don't miss anything
5822 */
5823 srv_lb_commit_status(s);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005824
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005825 /* check if server stats must be updated due the the server state change */
5826 if (srv_prev_state != s->cur_state) {
5827 if (srv_prev_state == SRV_ST_STOPPED) {
5828 /* server was down and no longer is */
Willy Tarreau69530f52023-04-28 09:16:15 +02005829 if (s->last_change < ns_to_sec(now_ns)) // ignore negative times
5830 s->down_time += ns_to_sec(now_ns) - s->last_change;
Aurelien DARRAGONc249f6d2023-04-19 22:34:01 +02005831 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, cb_data.common, s);
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005832 }
Aurelien DARRAGON5f80f8b2023-04-19 18:22:21 +02005833 else if (s->cur_state == SRV_ST_STOPPED) {
5834 /* server was up and is currently down */
5835 s->counters.down_trans++;
Aurelien DARRAGONc249f6d2023-04-19 22:34:01 +02005836 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, cb_data.common, s);
Aurelien DARRAGON5f80f8b2023-04-19 18:22:21 +02005837 }
Willy Tarreau69530f52023-04-28 09:16:15 +02005838 s->last_change = ns_to_sec(now_ns);
Aurelien DARRAGONe3eea292023-04-04 21:28:07 +02005839
5840 /* publish the state change */
5841 _srv_event_hdl_prepare_state(&cb_data.state,
5842 s, type, cause, srv_prev_state, requeued);
5843 _srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_STATE, cb_data.state, s);
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005844 }
5845
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005846 /* check if backend stats must be updated due to the server state change */
5847 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5848 set_backend_down(s->proxy); /* backend going down */
5849 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) {
5850 /* backend was down and is back up again:
5851 * no helper function, updating last_change and backend downtime stats
5852 */
Willy Tarreau69530f52023-04-28 09:16:15 +02005853 if (s->proxy->last_change < ns_to_sec(now_ns)) // ignore negative times
5854 s->proxy->down_time += ns_to_sec(now_ns) - s->proxy->last_change;
5855 s->proxy->last_change = ns_to_sec(now_ns);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005856 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005857}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005858
Willy Tarreau144f84a2021-03-02 16:09:26 +01005859struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005860{
5861 struct connection *conn;
5862
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005863 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005864 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005865 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005866 }
5867
5868 return task;
5869}
5870
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005871/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005872 * moving them all.
5873 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005874 *
5875 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005876 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005877static 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 +02005878{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005879 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005880 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005881 int i = 0;
5882
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005883 node = eb_first(idle_tree);
5884 while (node) {
5885 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005886 if (toremove_nb != -1 && i >= toremove_nb)
5887 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005888
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005889 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005890 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005891 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005892 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005893
5894 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005895 }
5896 return i;
5897}
William Dauchy6318d332020-05-02 21:52:36 +02005898/* cleanup connections for a given server
5899 * might be useful when going on forced maintenance or live changing ip/port
5900 */
William Dauchy707ad322020-05-04 13:52:40 +02005901static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005902{
William Dauchy6318d332020-05-02 21:52:36 +02005903 int did_remove;
5904 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005905
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005906 /* nothing to do if pool-max-conn is null */
5907 if (!srv->max_idle_conns)
5908 return;
5909
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005910 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005911 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005912 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005913 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005914 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 +02005915 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005916 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 +02005917 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005918 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005919 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005920 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005921
5922 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5923 break;
William Dauchy6318d332020-05-02 21:52:36 +02005924 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005925}
5926
5927/* removes an idle conn after updating the server idle conns counters */
5928void srv_release_conn(struct server *srv, struct connection *conn)
5929{
5930 if (conn->flags & CO_FL_LIST_MASK) {
5931 /* The connection is currently in the server's idle list, so tell it
5932 * there's one less connection available in that list.
5933 */
5934 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5935 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5936 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5937 }
5938 else {
5939 /* The connection is not private and not in any server's idle
5940 * list, so decrement the current number of used connections
5941 */
5942 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5943 }
5944
5945 /* Remove the connection from any tree (safe, idle or available) */
5946 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5947 conn_delete_from_tree(&conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01005948 conn->flags &= ~CO_FL_LIST_MASK;
Willy Tarreau260f3242021-10-06 18:30:04 +02005949 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5950}
5951
5952/* retrieve a connection from its <hash> in <tree>
5953 * returns NULL if no connection found
5954 */
5955struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5956{
Willy Tarreau85223482022-09-29 20:32:43 +02005957 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005958 struct connection *conn = NULL;
5959 struct conn_hash_node *hash_node = NULL;
5960
Willy Tarreau85223482022-09-29 20:32:43 +02005961 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005962 if (node) {
5963 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5964 conn = hash_node->conn;
5965 }
5966
5967 return conn;
5968}
5969
5970/* retrieve the next connection sharing the same hash as <conn>
5971 * returns NULL if no connection found
5972 */
5973struct connection *srv_lookup_conn_next(struct connection *conn)
5974{
Willy Tarreau85223482022-09-29 20:32:43 +02005975 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005976 struct connection *next_conn = NULL;
5977 struct conn_hash_node *hash_node = NULL;
5978
Willy Tarreau85223482022-09-29 20:32:43 +02005979 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005980 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005981 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005982 next_conn = hash_node->conn;
5983 }
5984
5985 return next_conn;
5986}
5987
5988/* This adds an idle connection to the server's list if the connection is
5989 * reusable, not held by any owner anymore, but still has available streams.
5990 */
5991int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5992{
5993 /* we try to keep the connection in the server's idle list
5994 * if we don't have too many FD in use, and if the number of
5995 * idle+current conns is lower than what was observed before
5996 * last purge, or if we already don't have idle conns for the
5997 * current thread and we don't exceed last count by global.nbthread.
5998 */
5999 if (!(conn->flags & CO_FL_PRIVATE) &&
6000 srv && srv->pool_purge_delay > 0 &&
6001 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
6002 ha_used_fds < global.tune.pool_high_count &&
6003 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
6004 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
6005 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
6006 (ha_used_fds < global.tune.pool_low_count &&
6007 (srv->curr_used_conns + srv->curr_idle_conns <=
6008 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
6009 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
6010 int retadd;
6011
6012 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
6013 if (retadd > srv->max_idle_conns) {
6014 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
6015 return 0;
6016 }
6017 _HA_ATOMIC_DEC(&srv->curr_used_conns);
6018
6019 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6020 conn_delete_from_tree(&conn->hash_node->node);
6021
6022 if (is_safe) {
6023 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02006024 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006025 _HA_ATOMIC_INC(&srv->curr_safe_nb);
6026 } else {
6027 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02006028 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006029 _HA_ATOMIC_INC(&srv->curr_idle_nb);
6030 }
6031 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6032 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
6033
6034 __ha_barrier_full();
6035 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
6036 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6037 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
6038 srv->idle_node.key = tick_add(srv->pool_purge_delay,
6039 now_ms);
6040 eb32_insert(&idle_conn_srv, &srv->idle_node);
6041 if (!task_in_wq(idle_conn_task) && !
6042 task_in_rq(idle_conn_task)) {
6043 task_schedule(idle_conn_task,
6044 srv->idle_node.key);
6045 }
6046
6047 }
6048 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6049 }
6050 return 1;
6051 }
6052 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02006053}
6054
Willy Tarreau144f84a2021-03-02 16:09:26 +01006055struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006056{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006057 struct server *srv;
6058 struct eb32_node *eb;
6059 int i;
6060 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006061
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006062 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006063 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6064 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006065 int exceed_conns;
6066 int to_kill;
6067 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006068
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006069 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
6070 if (!eb) {
6071 /* we might have reached the end of the tree, typically because
6072 * <now_ms> is in the first half and we're first scanning the last
6073 * half. Let's loop back to the beginning of the tree now.
6074 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006075
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006076 eb = eb32_first(&idle_conn_srv);
6077 if (likely(!eb))
6078 break;
6079 }
6080 if (tick_is_lt(now_ms, eb->key)) {
6081 /* timer not expired yet, revisit it later */
6082 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006083 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006084 }
6085 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006086
6087 /* Calculate how many idle connections we want to kill :
6088 * we want to remove half the difference between the total
6089 * of established connections (used or idle) and the max
6090 * number of used connections.
6091 */
6092 curr_idle = srv->curr_idle_conns;
6093 if (curr_idle == 0)
6094 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006095 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006096 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006097
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006098 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006099 if (srv->est_need_conns < srv->max_used_conns)
6100 srv->est_need_conns = srv->max_used_conns;
6101
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006102 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006103
Willy Tarreau18ed7892020-07-02 19:05:30 +02006104 if (exceed_conns <= 0)
6105 goto remove;
6106
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006107 /* check all threads starting with ours */
6108 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006109 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006110 int j;
6111 int did_remove = 0;
6112
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006113 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6114 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006115
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006116 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006117 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 +02006118 if (j > 0)
6119 did_remove = 1;
6120 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006121 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 +01006122 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006123 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006124
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006125 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006126 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006127
6128 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6129 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006130 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006131remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006132 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006133
6134 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006135 /* There are still more idle connections, add the
6136 * server back in the tree.
6137 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006138 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006139 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006140 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006141 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006142 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006143 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6144
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006145 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006146 return task;
6147}
Olivier Houchard88698d92019-04-16 19:07:22 +02006148
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006149/* Close remaining idle connections. This functions is designed to be run on
6150 * process shutdown. This guarantees a proper socket shutdown to avoid
6151 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6152 * bypassed.
6153 *
6154 * This function is not thread-safe so it must only be called via a global
6155 * deinit function.
6156 */
6157static void srv_close_idle_conns(struct server *srv)
6158{
6159 struct eb_root **cleaned_tree;
6160 int i;
6161
6162 for (i = 0; i < global.nbthread; ++i) {
6163 struct eb_root *conn_trees[] = {
6164 &srv->per_thr[i].idle_conns,
6165 &srv->per_thr[i].safe_conns,
6166 &srv->per_thr[i].avail_conns,
6167 NULL
6168 };
6169
6170 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6171 while (!eb_is_empty(*cleaned_tree)) {
6172 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6173 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6174 struct connection *conn = conn_hash_node->conn;
6175
6176 if (conn->ctrl->ctrl_close)
6177 conn->ctrl->ctrl_close(conn);
6178 ebmb_delete(node);
6179 }
6180 }
6181 }
6182}
6183
6184REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6185
Willy Tarreau76cc6992020-07-01 18:49:24 +02006186/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6187static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006188 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006189 char **err)
6190{
6191 if (too_many_args(1, args, err, NULL))
6192 return -1;
6193
6194 if (strcmp(args[1], "on") == 0)
6195 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6196 else if (strcmp(args[1], "off") == 0)
6197 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6198 else {
6199 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6200 return -1;
6201 }
6202 return 0;
6203}
6204
Olivier Houchard88698d92019-04-16 19:07:22 +02006205/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6206static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006207 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006208 char **err)
6209{
6210 int arg = -1;
6211
6212 if (too_many_args(1, args, err, NULL))
6213 return -1;
6214
6215 if (*(args[1]) != 0)
6216 arg = atoi(args[1]);
6217
6218 if (arg < 0 || arg > 100) {
6219 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6220 return -1;
6221 }
6222
6223 if (args[0][10] == 'h')
6224 global.tune.pool_high_ratio = arg;
6225 else
6226 global.tune.pool_low_ratio = arg;
6227 return 0;
6228}
6229
6230/* config keyword parsers */
6231static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006232 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006233 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6234 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6235 { 0, NULL, NULL }
6236}};
6237
6238INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6239
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006240/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006241 * Local variables:
6242 * c-indent-level: 8
6243 * c-basic-offset: 8
6244 * End:
6245 */