blob: bcb3018d0230db4d51e2fd8077b82240c3a235ba [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
192/* general server event publishing:
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100193 * Use this to publish EVENT_HDL_SUB_SERVER family type event
194 * from srv facility
195 * Event will be published in both global subscription list and
196 * server dedicated subscription list
197 * server ptr must be valid
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100198 * must be called with srv lock or under thread_isolate
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100199 */
Aurelien DARRAGON23f352f2023-03-24 17:02:53 +0100200static void srv_event_hdl_publish(struct event_hdl_sub_type event,
201 struct server *srv, uint8_t thread_isolate)
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100202{
203 struct event_hdl_cb_data_server cb_data;
204
Aurelien DARRAGON23f352f2023-03-24 17:02:53 +0100205 /* prepare event data */
206 _srv_event_hdl_prepare(&cb_data, srv, thread_isolate);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100207 /* publish in server dedicated sub list */
208 event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data));
209 /* publish in global subscription list */
210 event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data));
211}
212
213/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100214 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200215 * Unlikely, but it can happen. The server's proxy must be at least
216 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100217 */
218static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100219{
220 struct proxy *p = s->proxy;
221 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100222
223 for (tmpserv = p->srv; tmpserv != NULL;
224 tmpserv = tmpserv->next) {
225 if (tmpserv == s)
226 continue;
227 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
228 continue;
229 if (tmpserv->cookie &&
230 strcmp(tmpserv->cookie, s->cookie) == 0) {
231 ha_warning("We generated two equal cookies for two different servers.\n"
232 "Please change the secret key for '%s'.\n",
233 s->proxy->id);
234 }
235 }
236
237}
238
Willy Tarreau46b7f532018-08-21 11:54:26 +0200239/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200240 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200241 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100242void srv_set_dyncookie(struct server *s)
243{
244 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100245 char *tmpbuf;
246 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100247 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100248 size_t buffer_len;
249 int addr_len;
250 int port;
251
Willy Tarreau595e7672020-10-20 17:30:08 +0200252 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200253
Olivier Houchard4e694042017-03-14 20:01:29 +0100254 if ((s->flags & SRV_F_COOKIESET) ||
255 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
256 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200257 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100258 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100259
260 if (s->addr.ss_family != AF_INET &&
261 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200262 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100263 /*
264 * Buffer to calculate the cookie value.
265 * The buffer contains the secret key + the server IP address
266 * + the TCP port.
267 */
268 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
269 /*
270 * The TCP port should use only 2 bytes, but is stored in
271 * an unsigned int in struct server, so let's use 4, to be
272 * on the safe side.
273 */
274 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200275 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100276 memcpy(tmpbuf, p->dyncookie_key, key_len);
277 memcpy(&(tmpbuf[key_len]),
278 s->addr.ss_family == AF_INET ?
279 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
280 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
281 addr_len);
282 /*
283 * Make sure it's the same across all the load balancers,
284 * no matter their endianness.
285 */
286 port = htonl(s->svc_port);
287 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
288 hash_value = XXH64(tmpbuf, buffer_len, 0);
289 memprintf(&s->cookie, "%016llx", hash_value);
290 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200291 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100292 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100293
294 /* Don't bother checking if the dyncookie is duplicated if
295 * the server is marked as "disabled", maybe it doesn't have
296 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100297 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100298 if (!(s->next_admin & SRV_ADMF_FMAINT))
299 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200300 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200301 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100302}
303
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200304/* Returns true if it's possible to reuse an idle connection from server <srv>
305 * for a websocket stream. This is the case if server is configured to use the
306 * same protocol for both HTTP and websocket streams. This depends on the value
307 * of "proto", "alpn" and "ws" keywords.
308 */
309int srv_check_reuse_ws(struct server *srv)
310{
311 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
312 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
313 * for mux selection.
314 */
315 const struct ist srv_mux = srv->mux_proto ?
316 srv->mux_proto->token : IST_NULL;
317
318 switch (srv->ws) {
319 /* "auto" means use the same protocol : reuse is possible. */
320 case SRV_WS_AUTO:
321 return 1;
322
323 /* "h2" means use h2 for websocket : reuse is possible if
324 * server mux is h2.
325 */
326 case SRV_WS_H2:
327 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
328 return 1;
329 break;
330
331 /* "h1" means use h1 for websocket : reuse is possible if
332 * server mux is h1.
333 */
334 case SRV_WS_H1:
335 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
336 return 1;
337 break;
338 }
339 }
340 else {
341 /* ALPN selection.
342 * Based on the assumption that only "h2" and "http/1.1" token
343 * are used on server ALPN.
344 */
345 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
346 srv->ssl_ctx.alpn_len);
347
348 switch (srv->ws) {
349 case SRV_WS_AUTO:
350 /* for auto mode, consider reuse as possible if the
351 * server uses a single protocol ALPN
352 */
353 if (!istchr(alpn, ','))
354 return 1;
355 break;
356
357 case SRV_WS_H2:
358 return isteq(alpn, ist("\x02h2"));
359
360 case SRV_WS_H1:
361 return isteq(alpn, ist("\x08http/1.1"));
362 }
363 }
364
365 return 0;
366}
367
368/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
369 * is a valid value indicating to use the fallback mux.
370 */
371const struct mux_ops *srv_get_ws_proto(struct server *srv)
372{
373 const struct mux_proto_list *mux = NULL;
374
375 switch (srv->ws) {
376 case SRV_WS_AUTO:
377 mux = srv->mux_proto;
378 break;
379
380 case SRV_WS_H1:
381 mux = get_mux_proto(ist("h1"));
382 break;
383
384 case SRV_WS_H2:
385 mux = get_mux_proto(ist("h2"));
386 break;
387 }
388
389 return mux ? mux->mux : NULL;
390}
391
Willy Tarreau21faa912012-10-10 08:27:36 +0200392/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200393 * Must be called with the server lock held. The server is first removed from
394 * the proxy tree if it was already attached. If <reattach> is true, the server
395 * will then be attached in the proxy tree. The proxy lock is held to
396 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700397 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200398static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700399{
400 struct proxy *p = s->proxy;
401 char *key;
402
403 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
404
405 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700406 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700407 free(key);
408 return;
409 }
410
411 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
412 ebpt_delete(&s->addr_node);
413 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
414
415 free(s->addr_node.key);
416 }
417
418 s->addr_node.key = key;
419
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200420 if (reattach) {
421 if (s->addr_node.key) {
422 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
423 ebis_insert(&p->used_server_addr, &s->addr_node);
424 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
425 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700426 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700427}
428
429/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200430 * Registers the server keyword list <kwl> as a list of valid keywords for next
431 * parsing sessions.
432 */
433void srv_register_keywords(struct srv_kw_list *kwl)
434{
Willy Tarreau2b718102021-04-21 07:32:39 +0200435 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200436}
437
438/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
439 * keyword is found with a NULL ->parse() function, then an attempt is made to
440 * find one with a valid ->parse() function. This way it is possible to declare
441 * platform-dependant, known keywords as NULL, then only declare them as valid
442 * if some options are met. Note that if the requested keyword contains an
443 * opening parenthesis, everything from this point is ignored.
444 */
445struct srv_kw *srv_find_kw(const char *kw)
446{
447 int index;
448 const char *kwend;
449 struct srv_kw_list *kwl;
450 struct srv_kw *ret = NULL;
451
452 kwend = strchr(kw, '(');
453 if (!kwend)
454 kwend = kw + strlen(kw);
455
456 list_for_each_entry(kwl, &srv_keywords.list, list) {
457 for (index = 0; kwl->kw[index].kw != NULL; index++) {
458 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
459 kwl->kw[index].kw[kwend-kw] == 0) {
460 if (kwl->kw[index].parse)
461 return &kwl->kw[index]; /* found it !*/
462 else
463 ret = &kwl->kw[index]; /* may be OK */
464 }
465 }
466 }
467 return ret;
468}
469
470/* Dumps all registered "server" keywords to the <out> string pointer. The
471 * unsupported keywords are only dumped if their supported form was not
472 * found.
473 */
474void srv_dump_kws(char **out)
475{
476 struct srv_kw_list *kwl;
477 int index;
478
Christopher Faulet784063e2020-05-18 12:14:18 +0200479 if (!out)
480 return;
481
Willy Tarreau21faa912012-10-10 08:27:36 +0200482 *out = NULL;
483 list_for_each_entry(kwl, &srv_keywords.list, list) {
484 for (index = 0; kwl->kw[index].kw != NULL; index++) {
485 if (kwl->kw[index].parse ||
486 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
487 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
488 kwl->scope,
489 kwl->kw[index].kw,
490 kwl->kw[index].skip ? " <arg>" : "",
491 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
492 kwl->kw[index].parse ? "" : " (not supported)");
493 }
494 }
495 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100496}
497
498/* Try to find in srv_keyword the word that looks closest to <word> by counting
499 * transitions between letters, digits and other characters. Will return the
500 * best matching word if found, otherwise NULL. An optional array of extra
501 * words to compare may be passed in <extra>, but it must then be terminated
502 * by a NULL entry. If unused it may be NULL.
503 */
504static const char *srv_find_best_kw(const char *word)
505{
506 uint8_t word_sig[1024];
507 uint8_t list_sig[1024];
508 const struct srv_kw_list *kwl;
509 const char *best_ptr = NULL;
510 int dist, best_dist = INT_MAX;
511 const char **extra;
512 int index;
513
514 make_word_fingerprint(word_sig, word);
515 list_for_each_entry(kwl, &srv_keywords.list, list) {
516 for (index = 0; kwl->kw[index].kw != NULL; index++) {
517 make_word_fingerprint(list_sig, kwl->kw[index].kw);
518 dist = word_fingerprint_distance(word_sig, list_sig);
519 if (dist < best_dist) {
520 best_dist = dist;
521 best_ptr = kwl->kw[index].kw;
522 }
523 }
524 }
525
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100526 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100527 make_word_fingerprint(list_sig, *extra);
528 dist = word_fingerprint_distance(word_sig, list_sig);
529 if (dist < best_dist) {
530 best_dist = dist;
531 best_ptr = *extra;
532 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100533 }
534
535 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
536 best_ptr = NULL;
537
538 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200539}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100540
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100541/* Parse the "backup" server keyword */
542static int srv_parse_backup(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 newsrv->flags |= SRV_F_BACKUP;
546 return 0;
547}
548
Alexander Liu2a54bb72019-05-22 19:44:48 +0800549
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100550/* Parse the "cookie" server keyword */
551static int srv_parse_cookie(char **args, int *cur_arg,
552 struct proxy *curproxy, struct server *newsrv, char **err)
553{
554 char *arg;
555
556 arg = args[*cur_arg + 1];
557 if (!*arg) {
558 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
559 return ERR_ALERT | ERR_FATAL;
560 }
561
562 free(newsrv->cookie);
563 newsrv->cookie = strdup(arg);
564 newsrv->cklen = strlen(arg);
565 newsrv->flags |= SRV_F_COOKIESET;
566 return 0;
567}
568
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100569/* Parse the "disabled" server keyword */
570static int srv_parse_disabled(char **args, int *cur_arg,
571 struct proxy *curproxy, struct server *newsrv, char **err)
572{
Emeric Brun52a91d32017-08-31 14:41:55 +0200573 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
574 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100575 newsrv->check.state |= CHK_ST_PAUSED;
576 newsrv->check.health = 0;
577 return 0;
578}
579
580/* Parse the "enabled" server keyword */
581static int srv_parse_enabled(char **args, int *cur_arg,
582 struct proxy *curproxy, struct server *newsrv, char **err)
583{
Emeric Brun52a91d32017-08-31 14:41:55 +0200584 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
585 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100586 newsrv->check.state &= ~CHK_ST_PAUSED;
587 newsrv->check.health = newsrv->check.rise;
588 return 0;
589}
590
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100591/* Parse the "error-limit" server keyword */
592static int srv_parse_error_limit(char **args, int *cur_arg,
593 struct proxy *curproxy, struct server *newsrv, char **err)
594{
595 if (!*args[*cur_arg + 1]) {
596 memprintf(err, "'%s' expects an integer argument.",
597 args[*cur_arg]);
598 return ERR_ALERT | ERR_FATAL;
599 }
600
601 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
602
603 if (newsrv->consecutive_errors_limit <= 0) {
604 memprintf(err, "%s has to be > 0.",
605 args[*cur_arg]);
606 return ERR_ALERT | ERR_FATAL;
607 }
608
609 return 0;
610}
611
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200612/* Parse the "ws" keyword */
613static int srv_parse_ws(char **args, int *cur_arg,
614 struct proxy *curproxy, struct server *newsrv, char **err)
615{
616 if (!args[*cur_arg + 1]) {
617 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
618 return ERR_ALERT | ERR_FATAL;
619 }
620
621 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
622 newsrv->ws = SRV_WS_H1;
623 }
624 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
625 newsrv->ws = SRV_WS_H2;
626 }
627 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
628 newsrv->ws = SRV_WS_AUTO;
629 }
630 else {
631 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
632 return ERR_ALERT | ERR_FATAL;
633 }
634
635
636 return 0;
637}
638
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100639/* Parse the "init-addr" server keyword */
640static int srv_parse_init_addr(char **args, int *cur_arg,
641 struct proxy *curproxy, struct server *newsrv, char **err)
642{
643 char *p, *end;
644 int done;
645 struct sockaddr_storage sa;
646
647 newsrv->init_addr_methods = 0;
648 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
649
650 for (p = args[*cur_arg + 1]; *p; p = end) {
651 /* cut on next comma */
652 for (end = p; *end && *end != ','; end++);
653 if (*end)
654 *(end++) = 0;
655
656 memset(&sa, 0, sizeof(sa));
657 if (strcmp(p, "libc") == 0) {
658 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
659 }
660 else if (strcmp(p, "last") == 0) {
661 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
662 }
663 else if (strcmp(p, "none") == 0) {
664 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
665 }
666 else if (str2ip2(p, &sa, 0)) {
667 if (is_addr(&newsrv->init_addr)) {
668 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
669 args[*cur_arg], p);
670 return ERR_ALERT | ERR_FATAL;
671 }
672 newsrv->init_addr = sa;
673 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
674 }
675 else {
676 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
677 args[*cur_arg], p);
678 return ERR_ALERT | ERR_FATAL;
679 }
680 if (!done) {
681 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
682 args[*cur_arg], p);
683 return ERR_ALERT | ERR_FATAL;
684 }
685 }
686
687 return 0;
688}
689
690/* Parse the "log-proto" server keyword */
691static int srv_parse_log_proto(char **args, int *cur_arg,
692 struct proxy *curproxy, struct server *newsrv, char **err)
693{
694 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
695 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
696 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
697 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
698 else {
699 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
700 args[*cur_arg], args[*cur_arg + 1]);
701 return ERR_ALERT | ERR_FATAL;
702 }
703
704 return 0;
705}
706
707/* Parse the "maxconn" server keyword */
708static int srv_parse_maxconn(char **args, int *cur_arg,
709 struct proxy *curproxy, struct server *newsrv, char **err)
710{
711 newsrv->maxconn = atol(args[*cur_arg + 1]);
712 return 0;
713}
714
715/* Parse the "maxqueue" server keyword */
716static int srv_parse_maxqueue(char **args, int *cur_arg,
717 struct proxy *curproxy, struct server *newsrv, char **err)
718{
719 newsrv->maxqueue = atol(args[*cur_arg + 1]);
720 return 0;
721}
722
723/* Parse the "minconn" server keyword */
724static int srv_parse_minconn(char **args, int *cur_arg,
725 struct proxy *curproxy, struct server *newsrv, char **err)
726{
727 newsrv->minconn = atol(args[*cur_arg + 1]);
728 return 0;
729}
730
Willy Tarreau9c538e02019-01-23 10:21:49 +0100731static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
732{
733 char *arg;
734
735 arg = args[*cur_arg + 1];
736 if (!*arg) {
737 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
738 return ERR_ALERT | ERR_FATAL;
739 }
740 newsrv->max_reuse = atoi(arg);
741
742 return 0;
743}
744
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100745static 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 +0100746{
747 const char *res;
748 char *arg;
749 unsigned int time;
750
751 arg = args[*cur_arg + 1];
752 if (!*arg) {
753 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
754 return ERR_ALERT | ERR_FATAL;
755 }
756 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200757 if (res == PARSE_TIME_OVER) {
758 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
759 args[*cur_arg+1], args[*cur_arg]);
760 return ERR_ALERT | ERR_FATAL;
761 }
762 else if (res == PARSE_TIME_UNDER) {
763 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
764 args[*cur_arg+1], args[*cur_arg]);
765 return ERR_ALERT | ERR_FATAL;
766 }
767 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100768 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
769 *res, args[*cur_arg]);
770 return ERR_ALERT | ERR_FATAL;
771 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100772 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100773
774 return 0;
775}
776
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200777static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
778{
779 char *arg;
780
781 arg = args[*cur_arg + 1];
782 if (!*arg) {
783 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
784 return ERR_ALERT | ERR_FATAL;
785 }
786
787 newsrv->low_idle_conns = atoi(arg);
788 return 0;
789}
790
Olivier Houchard006e3102018-12-10 18:30:32 +0100791static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
792{
793 char *arg;
794
795 arg = args[*cur_arg + 1];
796 if (!*arg) {
797 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
798 return ERR_ALERT | ERR_FATAL;
799 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100800
Olivier Houchard006e3102018-12-10 18:30:32 +0100801 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100802 if ((int)newsrv->max_idle_conns < -1) {
803 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
804 return ERR_ALERT | ERR_FATAL;
805 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100806
807 return 0;
808}
809
Willy Tarreaudff55432012-10-10 17:51:05 +0200810/* parse the "id" server keyword */
811static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
812{
813 struct eb32_node *node;
814
815 if (!*args[*cur_arg + 1]) {
816 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
817 return ERR_ALERT | ERR_FATAL;
818 }
819
820 newsrv->puid = atol(args[*cur_arg + 1]);
821 newsrv->conf.id.key = newsrv->puid;
822
823 if (newsrv->puid <= 0) {
824 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
825 return ERR_ALERT | ERR_FATAL;
826 }
827
828 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
829 if (node) {
830 struct server *target = container_of(node, struct server, conf.id);
831 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
832 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
833 target->id);
834 return ERR_ALERT | ERR_FATAL;
835 }
836
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200837 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200838 return 0;
839}
840
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100841/* Parse the "namespace" server keyword */
842static int srv_parse_namespace(char **args, int *cur_arg,
843 struct proxy *curproxy, struct server *newsrv, char **err)
844{
Willy Tarreaue5733232019-05-22 19:24:06 +0200845#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100846 char *arg;
847
848 arg = args[*cur_arg + 1];
849 if (!*arg) {
850 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
851 return ERR_ALERT | ERR_FATAL;
852 }
853
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100854 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100855 /* Use the namespace associated with the connection (if present). */
856 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
857 return 0;
858 }
859
860 /*
861 * As this parser may be called several times for the same 'default-server'
862 * object, or for a new 'server' instance deriving from a 'default-server'
863 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
864 */
865 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
866
867 newsrv->netns = netns_store_lookup(arg, strlen(arg));
868 if (!newsrv->netns)
869 newsrv->netns = netns_store_insert(arg);
870
871 if (!newsrv->netns) {
872 memprintf(err, "Cannot open namespace '%s'", arg);
873 return ERR_ALERT | ERR_FATAL;
874 }
875
876 return 0;
877#else
878 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
879 return ERR_ALERT | ERR_FATAL;
880#endif
881}
882
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100883/* Parse the "no-backup" server keyword */
884static int srv_parse_no_backup(char **args, int *cur_arg,
885 struct proxy *curproxy, struct server *newsrv, char **err)
886{
887 newsrv->flags &= ~SRV_F_BACKUP;
888 return 0;
889}
890
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100891
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100892/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200893static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100894{
895 srv->pp_opts &= ~flags;
896 return 0;
897}
898
899/* Parse the "no-send-proxy" server keyword */
900static int srv_parse_no_send_proxy(char **args, int *cur_arg,
901 struct proxy *curproxy, struct server *newsrv, char **err)
902{
903 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
904}
905
906/* Parse the "no-send-proxy-v2" server keyword */
907static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
908 struct proxy *curproxy, struct server *newsrv, char **err)
909{
910 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
911}
912
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200913/* Parse the "shard" server keyword */
914static int srv_parse_shard(char **args, int *cur_arg,
915 struct proxy *curproxy, struct server *newsrv, char **err)
916{
917 newsrv->shard = atol(args[*cur_arg + 1]);
918 return 0;
919}
920
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200921/* Parse the "no-tfo" server keyword */
922static int srv_parse_no_tfo(char **args, int *cur_arg,
923 struct proxy *curproxy, struct server *newsrv, char **err)
924{
925 newsrv->flags &= ~SRV_F_FASTOPEN;
926 return 0;
927}
928
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100929/* Parse the "non-stick" server keyword */
930static int srv_parse_non_stick(char **args, int *cur_arg,
931 struct proxy *curproxy, struct server *newsrv, char **err)
932{
933 newsrv->flags |= SRV_F_NON_STICK;
934 return 0;
935}
936
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100937/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200938static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100939{
940 srv->pp_opts |= flags;
941 return 0;
942}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200943/* parse the "proto" server keyword */
944static int srv_parse_proto(char **args, int *cur_arg,
945 struct proxy *px, struct server *newsrv, char **err)
946{
947 struct ist proto;
948
949 if (!*args[*cur_arg + 1]) {
950 memprintf(err, "'%s' : missing value", args[*cur_arg]);
951 return ERR_ALERT | ERR_FATAL;
952 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100953 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200954 newsrv->mux_proto = get_mux_proto(proto);
955 if (!newsrv->mux_proto) {
956 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
957 return ERR_ALERT | ERR_FATAL;
958 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200959 return 0;
960}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100961
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100962/* parse the "proxy-v2-options" */
963static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
964 struct proxy *px, struct server *newsrv, char **err)
965{
966 char *p, *n;
967 for (p = args[*cur_arg+1]; p; p = n) {
968 n = strchr(p, ',');
969 if (n)
970 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100971 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100972 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100973 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100974 newsrv->pp_opts |= SRV_PP_V2_SSL;
975 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100976 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100977 newsrv->pp_opts |= SRV_PP_V2_SSL;
978 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100979 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100980 newsrv->pp_opts |= SRV_PP_V2_SSL;
981 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100982 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100983 newsrv->pp_opts |= SRV_PP_V2_SSL;
984 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100985 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100986 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100987 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100988 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100989 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100990 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100991 } else
992 goto fail;
993 }
994 return 0;
995 fail:
996 if (err)
997 memprintf(err, "'%s' : proxy v2 option not implemented", p);
998 return ERR_ALERT | ERR_FATAL;
999}
1000
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001001/* Parse the "observe" server keyword */
1002static int srv_parse_observe(char **args, int *cur_arg,
1003 struct proxy *curproxy, struct server *newsrv, char **err)
1004{
1005 char *arg;
1006
1007 arg = args[*cur_arg + 1];
1008 if (!*arg) {
1009 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
1010 return ERR_ALERT | ERR_FATAL;
1011 }
1012
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001013 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001014 newsrv->observe = HANA_OBS_NONE;
1015 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001016 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001017 newsrv->observe = HANA_OBS_LAYER4;
1018 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001019 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001020 if (curproxy->mode != PR_MODE_HTTP) {
1021 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
1022 return ERR_ALERT;
1023 }
1024 newsrv->observe = HANA_OBS_LAYER7;
1025 }
1026 else {
1027 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
1028 "but got '%s'\n", args[*cur_arg], arg);
1029 return ERR_ALERT | ERR_FATAL;
1030 }
1031
1032 return 0;
1033}
1034
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001035/* Parse the "on-error" server keyword */
1036static int srv_parse_on_error(char **args, int *cur_arg,
1037 struct proxy *curproxy, struct server *newsrv, char **err)
1038{
1039 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
1040 newsrv->onerror = HANA_ONERR_FASTINTER;
1041 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
1042 newsrv->onerror = HANA_ONERR_FAILCHK;
1043 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
1044 newsrv->onerror = HANA_ONERR_SUDDTH;
1045 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
1046 newsrv->onerror = HANA_ONERR_MARKDWN;
1047 else {
1048 memprintf(err, "'%s' expects one of 'fastinter', "
1049 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1050 args[*cur_arg], args[*cur_arg + 1]);
1051 return ERR_ALERT | ERR_FATAL;
1052 }
1053
1054 return 0;
1055}
1056
1057/* Parse the "on-marked-down" server keyword */
1058static int srv_parse_on_marked_down(char **args, int *cur_arg,
1059 struct proxy *curproxy, struct server *newsrv, char **err)
1060{
1061 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1062 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1063 else {
1064 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1065 args[*cur_arg], args[*cur_arg + 1]);
1066 return ERR_ALERT | ERR_FATAL;
1067 }
1068
1069 return 0;
1070}
1071
1072/* Parse the "on-marked-up" server keyword */
1073static int srv_parse_on_marked_up(char **args, int *cur_arg,
1074 struct proxy *curproxy, struct server *newsrv, char **err)
1075{
1076 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1077 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1078 else {
1079 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1080 args[*cur_arg], args[*cur_arg + 1]);
1081 return ERR_ALERT | ERR_FATAL;
1082 }
1083
1084 return 0;
1085}
1086
Frédéric Lécaille16186232017-03-14 16:42:49 +01001087/* Parse the "redir" server keyword */
1088static int srv_parse_redir(char **args, int *cur_arg,
1089 struct proxy *curproxy, struct server *newsrv, char **err)
1090{
1091 char *arg;
1092
1093 arg = args[*cur_arg + 1];
1094 if (!*arg) {
1095 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1096 return ERR_ALERT | ERR_FATAL;
1097 }
1098
1099 free(newsrv->rdr_pfx);
1100 newsrv->rdr_pfx = strdup(arg);
1101 newsrv->rdr_len = strlen(arg);
1102
1103 return 0;
1104}
1105
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001106/* Parse the "resolvers" server keyword */
1107static int srv_parse_resolvers(char **args, int *cur_arg,
1108 struct proxy *curproxy, struct server *newsrv, char **err)
1109{
1110 free(newsrv->resolvers_id);
1111 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1112 return 0;
1113}
1114
1115/* Parse the "resolve-net" server keyword */
1116static int srv_parse_resolve_net(char **args, int *cur_arg,
1117 struct proxy *curproxy, struct server *newsrv, char **err)
1118{
1119 char *p, *e;
1120 unsigned char mask;
1121 struct resolv_options *opt;
1122
1123 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1124 memprintf(err, "'%s' expects a list of networks.",
1125 args[*cur_arg]);
1126 return ERR_ALERT | ERR_FATAL;
1127 }
1128
1129 opt = &newsrv->resolv_opts;
1130
1131 /* Split arguments by comma, and convert it from ipv4 or ipv6
1132 * string network in in_addr or in6_addr.
1133 */
1134 p = args[*cur_arg + 1];
1135 e = p;
1136 while (*p != '\0') {
1137 /* If no room available, return error. */
1138 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1139 memprintf(err, "'%s' exceed %d networks.",
1140 args[*cur_arg], SRV_MAX_PREF_NET);
1141 return ERR_ALERT | ERR_FATAL;
1142 }
1143 /* look for end or comma. */
1144 while (*e != ',' && *e != '\0')
1145 e++;
1146 if (*e == ',') {
1147 *e = '\0';
1148 e++;
1149 }
1150 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1151 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1152 /* Try to convert input string from ipv4 or ipv6 network. */
1153 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1154 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1155 &mask)) {
1156 /* Try to convert input string from ipv6 network. */
1157 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1158 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1159 } else {
1160 /* All network conversions fail, return error. */
1161 memprintf(err, "'%s' invalid network '%s'.",
1162 args[*cur_arg], p);
1163 return ERR_ALERT | ERR_FATAL;
1164 }
1165 opt->pref_net_nb++;
1166 p = e;
1167 }
1168
1169 return 0;
1170}
1171
1172/* Parse the "resolve-opts" server keyword */
1173static int srv_parse_resolve_opts(char **args, int *cur_arg,
1174 struct proxy *curproxy, struct server *newsrv, char **err)
1175{
1176 char *p, *end;
1177
1178 for (p = args[*cur_arg + 1]; *p; p = end) {
1179 /* cut on next comma */
1180 for (end = p; *end && *end != ','; end++);
1181 if (*end)
1182 *(end++) = 0;
1183
1184 if (strcmp(p, "allow-dup-ip") == 0) {
1185 newsrv->resolv_opts.accept_duplicate_ip = 1;
1186 }
1187 else if (strcmp(p, "ignore-weight") == 0) {
1188 newsrv->resolv_opts.ignore_weight = 1;
1189 }
1190 else if (strcmp(p, "prevent-dup-ip") == 0) {
1191 newsrv->resolv_opts.accept_duplicate_ip = 0;
1192 }
1193 else {
1194 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1195 args[*cur_arg], p);
1196 return ERR_ALERT | ERR_FATAL;
1197 }
1198 }
1199
1200 return 0;
1201}
1202
1203/* Parse the "resolve-prefer" server keyword */
1204static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1205 struct proxy *curproxy, struct server *newsrv, char **err)
1206{
1207 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1208 newsrv->resolv_opts.family_prio = AF_INET;
1209 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1210 newsrv->resolv_opts.family_prio = AF_INET6;
1211 else {
1212 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1213 args[*cur_arg]);
1214 return ERR_ALERT | ERR_FATAL;
1215 }
1216
1217 return 0;
1218}
1219
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001220/* Parse the "send-proxy" server keyword */
1221static int srv_parse_send_proxy(char **args, int *cur_arg,
1222 struct proxy *curproxy, struct server *newsrv, char **err)
1223{
1224 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1225}
1226
1227/* Parse the "send-proxy-v2" server keyword */
1228static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1229 struct proxy *curproxy, struct server *newsrv, char **err)
1230{
1231 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1232}
1233
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001234/* Parse the "slowstart" server keyword */
1235static int srv_parse_slowstart(char **args, int *cur_arg,
1236 struct proxy *curproxy, struct server *newsrv, char **err)
1237{
1238 /* slowstart is stored in seconds */
1239 unsigned int val;
1240 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1241
1242 if (time_err == PARSE_TIME_OVER) {
1243 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1244 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1245 return ERR_ALERT | ERR_FATAL;
1246 }
1247 else if (time_err == PARSE_TIME_UNDER) {
1248 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1249 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1250 return ERR_ALERT | ERR_FATAL;
1251 }
1252 else if (time_err) {
1253 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1254 *time_err, newsrv->id);
1255 return ERR_ALERT | ERR_FATAL;
1256 }
1257 newsrv->slowstart = (val + 999) / 1000;
1258
1259 return 0;
1260}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001261
1262/* Parse the "source" server keyword */
1263static int srv_parse_source(char **args, int *cur_arg,
1264 struct proxy *curproxy, struct server *newsrv, char **err)
1265{
1266 char *errmsg;
1267 int port_low, port_high;
1268 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001269
1270 errmsg = NULL;
1271
1272 if (!*args[*cur_arg + 1]) {
1273 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1274 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1275 goto err;
1276 }
1277
1278 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001279 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1280 &errmsg, NULL, NULL,
1281 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 +01001282 if (!sk) {
1283 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1284 goto err;
1285 }
1286
Frédéric Lécailledba97072017-03-17 15:33:50 +01001287 newsrv->conn_src.opts |= CO_SRC_BIND;
1288 newsrv->conn_src.source_addr = *sk;
1289
1290 if (port_low != port_high) {
1291 int i;
1292
Frédéric Lécailledba97072017-03-17 15:33:50 +01001293 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001294 if (!newsrv->conn_src.sport_range) {
1295 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1296 goto err;
1297 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001298 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1299 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1300 }
1301
1302 *cur_arg += 2;
1303 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001304 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001305#if defined(CONFIG_HAP_TRANSPARENT)
1306 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001307 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1308 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001309 goto err;
1310 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001311 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001312 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1313 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1314 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001315 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001316 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1317 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1318 }
1319 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1320 char *name, *end;
1321
1322 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001323 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001324 name++;
1325
1326 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001327 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001328 end++;
1329
1330 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1331 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1332 free(newsrv->conn_src.bind_hdr_name);
1333 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001334 if (!newsrv->conn_src.bind_hdr_name) {
1335 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1336 goto err;
1337 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001338 newsrv->conn_src.bind_hdr_len = end - name;
1339 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1340 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1341 newsrv->conn_src.bind_hdr_occ = -1;
1342
1343 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001344 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001345 end++;
1346 if (*end == ',') {
1347 end++;
1348 name = end;
1349 if (*end == '-')
1350 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001351 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001352 end++;
1353 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1354 }
1355
1356 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001357 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1358 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001359 goto err;
1360 }
1361 }
1362 else {
1363 struct sockaddr_storage *sk;
1364 int port1, port2;
1365
1366 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001367 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1368 &errmsg, NULL, NULL,
1369 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001370 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001371 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001372 goto err;
1373 }
1374
Frédéric Lécailledba97072017-03-17 15:33:50 +01001375 newsrv->conn_src.tproxy_addr = *sk;
1376 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1377 }
1378 global.last_checks |= LSTCHK_NETADM;
1379 *cur_arg += 2;
1380 continue;
1381#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001382 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 +01001383 goto err;
1384#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1385 } /* "usesrc" */
1386
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001387 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001388#ifdef SO_BINDTODEVICE
1389 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001390 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001391 goto err;
1392 }
1393 free(newsrv->conn_src.iface_name);
1394 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1395 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1396 global.last_checks |= LSTCHK_NETADM;
1397#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001398 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001399 goto err;
1400#endif
1401 *cur_arg += 2;
1402 continue;
1403 }
1404 /* this keyword in not an option of "source" */
1405 break;
1406 } /* while */
1407
1408 return 0;
1409
1410 err:
1411 free(errmsg);
1412 return ERR_ALERT | ERR_FATAL;
1413}
1414
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001415/* Parse the "stick" server keyword */
1416static int srv_parse_stick(char **args, int *cur_arg,
1417 struct proxy *curproxy, struct server *newsrv, char **err)
1418{
1419 newsrv->flags &= ~SRV_F_NON_STICK;
1420 return 0;
1421}
1422
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001423/* Parse the "track" server keyword */
1424static int srv_parse_track(char **args, int *cur_arg,
1425 struct proxy *curproxy, struct server *newsrv, char **err)
1426{
1427 char *arg;
1428
1429 arg = args[*cur_arg + 1];
1430 if (!*arg) {
1431 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1432 return ERR_ALERT | ERR_FATAL;
1433 }
1434
1435 free(newsrv->trackit);
1436 newsrv->trackit = strdup(arg);
1437
1438 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001439}
1440
1441/* Parse the "socks4" server keyword */
1442static int srv_parse_socks4(char **args, int *cur_arg,
1443 struct proxy *curproxy, struct server *newsrv, char **err)
1444{
1445 char *errmsg;
1446 int port_low, port_high;
1447 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001448
1449 errmsg = NULL;
1450
1451 if (!*args[*cur_arg + 1]) {
1452 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1453 goto err;
1454 }
1455
1456 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001457 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1458 &errmsg, NULL, NULL,
1459 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001460 if (!sk) {
1461 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1462 goto err;
1463 }
1464
Alexander Liu2a54bb72019-05-22 19:44:48 +08001465 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1466 newsrv->socks4_addr = *sk;
1467
Alexander Liu2a54bb72019-05-22 19:44:48 +08001468 return 0;
1469
1470 err:
1471 free(errmsg);
1472 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001473}
1474
Frédéric Lécailledba97072017-03-17 15:33:50 +01001475
Willy Tarreau034c88c2017-01-23 23:36:45 +01001476/* parse the "tfo" server keyword */
1477static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1478{
1479 newsrv->flags |= SRV_F_FASTOPEN;
1480 return 0;
1481}
1482
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001483/* parse the "usesrc" server keyword */
1484static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1485{
1486 memprintf(err, "'%s' only allowed after a '%s' statement.",
1487 "usesrc", "source");
1488 return ERR_ALERT | ERR_FATAL;
1489}
1490
1491/* parse the "weight" server keyword */
1492static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1493{
1494 int w;
1495
1496 w = atol(args[*cur_arg + 1]);
1497 if (w < 0 || w > SRV_UWGHT_MAX) {
1498 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1499 newsrv->id, SRV_UWGHT_MAX, w);
1500 return ERR_ALERT | ERR_FATAL;
1501 }
1502 newsrv->uweight = newsrv->iweight = w;
1503
1504 return 0;
1505}
1506
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001507/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001508 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001509 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001510 *
1511 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001512 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001513void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001514{
Willy Tarreau751153e2021-02-17 13:33:24 +01001515 struct stream *stream;
1516 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001517 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001518
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001519 for (thr = 0; thr < global.nbthread; thr++)
1520 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1521 if (stream->srv_conn == srv)
1522 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001523}
1524
1525/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001526 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001527 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001528 *
1529 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001530 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001531void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001532{
1533 struct server *srv;
1534
1535 for (srv = px->srv; srv != NULL; srv = srv->next)
1536 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001537 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001538}
1539
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001540static 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 +02001541{
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001542 switch (cause) {
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001543 case SRV_OP_STCHGC_NONE:
1544 break; /* do nothing */
1545 case SRV_OP_STCHGC_HEALTH:
1546 check_append_info(msg, &s->check);
1547 break;
1548 case SRV_OP_STCHGC_AGENT:
1549 check_append_info(msg, &s->agent);
1550 break;
1551 default:
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001552 chunk_appendf(msg, ", %s", srv_op_st_chg_cause(cause));
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001553 break;
1554 }
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001555}
1556
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001557static 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 +02001558{
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001559 if (cause)
1560 chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(cause));
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001561}
1562
1563/* Appends some information to a message string related to a server tracking
1564 * or requeued connections info.
1565 *
1566 * If <forced> is null and the server tracks another one, a "via"
Emeric Brun5a133512017-10-19 14:42:30 +02001567 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001568 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001569 *
1570 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001571 */
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001572static void srv_append_more(struct buffer *msg, struct server *s,
1573 int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001574{
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001575 if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001576 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001577 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001578
1579 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001580 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001581 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1582 " %d sessions active, %d requeued, %d remaining in queue",
1583 s->proxy->srv_act, s->proxy->srv_bck,
1584 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001585 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001586 else
1587 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1588 " %d sessions requeued, %d total in queue",
1589 s->proxy->srv_act, s->proxy->srv_bck,
1590 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001591 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001592 }
1593}
1594
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001595/* Marks server <s> down, regardless of its checks' statuses. The server
1596 * transfers queued streams whenever possible to other servers at a sync
1597 * point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001598 *
1599 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001600 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001601void srv_set_stopped(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001602{
1603 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001604
Emeric Brun64cc49c2017-10-03 14:46:45 +02001605 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001606 return;
1607
Emeric Brun52a91d32017-08-31 14:41:55 +02001608 s->next_state = SRV_ST_STOPPED;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001609
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001610 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001611 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001612
Emeric Brun9f0b4582017-10-23 14:39:51 +02001613 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001614 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001615 srv_set_stopped(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001616 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001617 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001618}
1619
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001620/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001621 * in maintenance. The server tries to grab requests from the proxy at a sync
1622 * point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001623 *
1624 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001625 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001626void srv_set_running(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001627{
1628 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001629
Emeric Brun64cc49c2017-10-03 14:46:45 +02001630 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001631 return;
1632
Emeric Brun52a91d32017-08-31 14:41:55 +02001633 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001634 return;
1635
Emeric Brun52a91d32017-08-31 14:41:55 +02001636 s->next_state = SRV_ST_STARTING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001637
Emeric Brun64cc49c2017-10-03 14:46:45 +02001638 if (s->slowstart <= 0)
1639 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001640
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001641 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001642 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001643
Emeric Brun9f0b4582017-10-23 14:39:51 +02001644 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001645 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001646 srv_set_running(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001647 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001648 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001649}
1650
Willy Tarreau8eb77842014-05-21 13:54:57 +02001651/* Marks server <s> stopping regardless of its checks' statuses and provided it
Aurelien DARRAGON10518c02023-04-19 10:33:02 +02001652 * isn't in maintenance. The server tries to redispatch pending requests
1653 * to the proxy. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001654 *
1655 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001656 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001657void srv_set_stopping(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001658{
1659 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001660
Emeric Brun64cc49c2017-10-03 14:46:45 +02001661 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001662 return;
1663
Emeric Brun52a91d32017-08-31 14:41:55 +02001664 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001665 return;
1666
Emeric Brun52a91d32017-08-31 14:41:55 +02001667 s->next_state = SRV_ST_STOPPING;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001668
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001669 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001670 srv_update_status(s, 0, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001671
Emeric Brun9f0b4582017-10-23 14:39:51 +02001672 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001673 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001674 srv_set_stopping(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001675 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001676 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001677}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001678
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001679/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1680 * enforce either maint mode or drain mode. It is not allowed to set more than
1681 * one flag at once. The equivalent "inherited" flag is propagated to all
1682 * tracking servers. Maintenance mode disables health checks (but not agent
1683 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001684 * is done. If <cause> is non-null, it will be displayed at the end of the log
1685 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001686 *
1687 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001688 */
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001689void 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 +02001690{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001691 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001692
1693 if (!mode)
1694 return;
1695
1696 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001697 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001698 return;
1699
Emeric Brun52a91d32017-08-31 14:41:55 +02001700 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001701
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001702 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001703 srv_update_status(s, 1, cause);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001704
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001705 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1707 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001708 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001709
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001710 /* compute the inherited flag to propagate */
1711 if (mode & SRV_ADMF_MAINT)
1712 mode = SRV_ADMF_IMAINT;
1713 else if (mode & SRV_ADMF_DRAIN)
1714 mode = SRV_ADMF_IDRAIN;
1715
Emeric Brun9f0b4582017-10-23 14:39:51 +02001716 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001717 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001718 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001719 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001720 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001721}
1722
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001723/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1724 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1725 * than one flag at once. The equivalent "inherited" flag is propagated to all
1726 * tracking servers. Leaving maintenance mode re-enables health checks. When
1727 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001728 *
1729 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001730 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001731void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001732{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001733 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001734
1735 if (!mode)
1736 return;
1737
1738 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001739 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001740 return;
1741
Emeric Brun52a91d32017-08-31 14:41:55 +02001742 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001743
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001744 /* propagate changes */
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001745 srv_update_status(s, 1, SRV_ADM_STCHGC_NONE);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001746
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001747 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001748 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1749 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001750 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001751
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001752 if (mode & SRV_ADMF_MAINT)
1753 mode = SRV_ADMF_IMAINT;
1754 else if (mode & SRV_ADMF_DRAIN)
1755 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001756
Emeric Brun9f0b4582017-10-23 14:39:51 +02001757 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001758 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001759 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001760 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001761 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001762}
1763
Willy Tarreau757478e2016-11-03 19:22:19 +01001764/* principle: propagate maint and drain to tracking servers. This is useful
1765 * upon startup so that inherited states are correct.
1766 */
1767static void srv_propagate_admin_state(struct server *srv)
1768{
1769 struct server *srv2;
1770
1771 if (!srv->trackers)
1772 return;
1773
1774 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001775 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001776 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001777 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau757478e2016-11-03 19:22:19 +01001778
Emeric Brun52a91d32017-08-31 14:41:55 +02001779 if (srv->next_admin & SRV_ADMF_DRAIN)
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001780 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001781 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001782 }
1783}
1784
1785/* Compute and propagate the admin states for all servers in proxy <px>.
1786 * Only servers *not* tracking another one are considered, because other
1787 * ones will be handled when the server they track is visited.
1788 */
1789void srv_compute_all_admin_states(struct proxy *px)
1790{
1791 struct server *srv;
1792
1793 for (srv = px->srv; srv; srv = srv->next) {
1794 if (srv->track)
1795 continue;
1796 srv_propagate_admin_state(srv);
1797 }
1798}
1799
Willy Tarreaudff55432012-10-10 17:51:05 +02001800/* Note: must not be declared <const> as its list will be overwritten.
1801 * Please take care of keeping this list alphabetically sorted, doing so helps
1802 * all code contributors.
1803 * Optional keywords are also declared with a NULL ->parse() function so that
1804 * the config parser can report an appropriate error when a known keyword was
1805 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001806 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001807 */
1808static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001809 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001810 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001811 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1812 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001813 { "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 +02001814 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001815 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001816 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1817 { "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 +01001818 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1819 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001820 { "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 +01001821 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001822 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001823 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1824 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1825 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1826 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001827 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001828 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1829 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1830 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1831 { "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 +01001832 { "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 */
1833 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1834 { "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 +01001835 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001836 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001837 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001838 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001839 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001840 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001841 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001842 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1843 { "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 +02001844 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001845 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001846 { "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 +01001847 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001848 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001849 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001850 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001851 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1852 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001853 { NULL, NULL, 0 },
1854}};
1855
Willy Tarreau0108d902018-11-25 19:14:37 +01001856INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001857
Willy Tarreau004e0452013-11-21 11:22:01 +01001858/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001859 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001860 * state is automatically disabled if the time is elapsed. If <must_update> is
1861 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001862 *
1863 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001864 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001865void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001866{
1867 struct proxy *px = sv->proxy;
1868 unsigned w;
1869
Willy Tarreau69530f52023-04-28 09:16:15 +02001870 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 +01001871 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001872 if (sv->next_state == SRV_ST_STARTING)
1873 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001874 }
1875
1876 /* We must take care of not pushing the server to full throttle during slow starts.
1877 * It must also start immediately, at least at the minimal step when leaving maintenance.
1878 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001879 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau69530f52023-04-28 09:16:15 +02001880 w = (px->lbprm.wdiv * (ns_to_sec(now_ns) - sv->last_change) + sv->slowstart) / sv->slowstart;
Willy Tarreau004e0452013-11-21 11:22:01 +01001881 else
1882 w = px->lbprm.wdiv;
1883
Emeric Brun52a91d32017-08-31 14:41:55 +02001884 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001885
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001886 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001887 if (must_update)
Aurelien DARRAGON76e25552023-04-18 11:00:17 +02001888 srv_update_status(sv, 0, SRV_OP_STCHGC_NONE);
Willy Tarreau004e0452013-11-21 11:22:01 +01001889}
1890
Willy Tarreaubaaee002006-06-26 02:48:02 +02001891/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001892 * Parses weight_str and configures sv accordingly.
1893 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001894 *
1895 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001896 */
1897const char *server_parse_weight_change_request(struct server *sv,
1898 const char *weight_str)
1899{
1900 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001901 long int w;
1902 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001903
1904 px = sv->proxy;
1905
1906 /* if the weight is terminated with '%', it is set relative to
1907 * the initial weight, otherwise it is absolute.
1908 */
1909 if (!*weight_str)
1910 return "Require <weight> or <weight%>.\n";
1911
Simon Hormanb796afa2013-02-12 10:45:53 +09001912 w = strtol(weight_str, &end, 10);
1913 if (end == weight_str)
1914 return "Empty weight string empty or preceded by garbage";
1915 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001916 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001917 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001918 /* Avoid integer overflow */
1919 if (w > 25600)
1920 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001921 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001922 if (w > 256)
1923 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001924 }
1925 else if (w < 0 || w > 256)
1926 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001927 else if (end[0] != '\0')
1928 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001929
1930 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1931 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1932
1933 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001934 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001935
1936 return NULL;
1937}
1938
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001939/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001940 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1941 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001942 * Returns:
1943 * - error string on error
1944 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001945 *
1946 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001947 */
1948const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001949 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001950{
1951 unsigned char ip[INET6_ADDRSTRLEN];
1952
1953 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001954 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001955 return NULL;
1956 }
1957 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001958 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001959 return NULL;
1960 }
1961
1962 return "Could not understand IP address format.\n";
1963}
1964
Willy Tarreau46b7f532018-08-21 11:54:26 +02001965/*
1966 * Must be called with the server lock held.
1967 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001968const char *server_parse_maxconn_change_request(struct server *sv,
1969 const char *maxconn_str)
1970{
1971 long int v;
1972 char *end;
1973
1974 if (!*maxconn_str)
1975 return "Require <maxconn>.\n";
1976
1977 v = strtol(maxconn_str, &end, 10);
1978 if (end == maxconn_str)
1979 return "maxconn string empty or preceded by garbage";
1980 else if (end[0] != '\0')
1981 return "Trailing garbage in maxconn string";
1982
1983 if (sv->maxconn == sv->minconn) { // static maxconn
1984 sv->maxconn = sv->minconn = v;
1985 } else { // dynamic maxconn
1986 sv->maxconn = v;
1987 }
1988
1989 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02001990 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001991
1992 return NULL;
1993}
1994
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001995static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1996 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001997{
1998 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001999 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002000 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002001 NULL,
2002 };
2003
2004 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002005 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002006
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002007 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002008}
2009
William Lallemand0d058672022-03-16 15:44:42 +01002010int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002011{
2012 struct sample_expr *expr;
2013
2014 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 +01002015 if (!expr) {
2016 memprintf(err, "error detected while parsing sni expression : %s", *err);
2017 return ERR_ALERT | ERR_FATAL;
2018 }
2019
2020 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2021 memprintf(err, "error detected while parsing sni expression : "
2022 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002023 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002024 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002025 return ERR_ALERT | ERR_FATAL;
2026 }
2027
2028 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2029 release_sample_expr(newsrv->ssl_ctx.sni);
2030 newsrv->ssl_ctx.sni = expr;
2031
2032 return 0;
2033}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002034
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002035static 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 +01002036{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002037 char *msg = "error encountered while processing ";
2038 char *quote = "'";
2039 char *token = args[cur_arg];
2040
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002041 if (err && *err) {
2042 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002043 msg = *err;
2044 quote = "";
2045 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002046 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002047
2048 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002049 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002050 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002051 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002052}
2053
Christopher Faulet0b365e32022-08-03 11:21:14 +02002054static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002055{
2056 int range_sz;
2057
2058 range_sz = src->conn_src.sport_range->size;
2059 if (range_sz > 0) {
2060 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2061 if (srv->conn_src.sport_range != NULL) {
2062 int i;
2063
2064 for (i = 0; i < range_sz; i++) {
2065 srv->conn_src.sport_range->ports[i] =
2066 src->conn_src.sport_range->ports[i];
2067 }
2068 }
2069 }
2070}
2071
2072/*
2073 * Copy <src> server connection source settings to <srv> server everything needed.
2074 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002075static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002076{
2077 srv->conn_src.opts = src->conn_src.opts;
2078 srv->conn_src.source_addr = src->conn_src.source_addr;
2079
2080 /* Source port range copy. */
2081 if (src->conn_src.sport_range != NULL)
2082 srv_conn_src_sport_range_cpy(srv, src);
2083
2084#ifdef CONFIG_HAP_TRANSPARENT
2085 if (src->conn_src.bind_hdr_name != NULL) {
2086 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2087 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2088 }
2089 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2090 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2091#endif
2092 if (src->conn_src.iface_name != NULL)
2093 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2094}
2095
2096/*
2097 * Copy <src> server SSL settings to <srv> server allocating
2098 * everything needed.
2099 */
2100#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002101static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002102{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002103 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002104 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2105
Christopher Faulet4ab26792021-12-01 09:50:41 +01002106 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2107 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2108
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002109 if (src->ssl_ctx.ca_file != NULL)
2110 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2111 if (src->ssl_ctx.crl_file != NULL)
2112 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002113 if (src->ssl_ctx.client_crt != NULL)
2114 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002115
2116 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2117
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002118
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002119 if (src->ssl_ctx.verify_host != NULL)
2120 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2121 if (src->ssl_ctx.ciphers != NULL)
2122 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002123 if (src->ssl_ctx.options)
2124 srv->ssl_ctx.options = src->ssl_ctx.options;
2125 if (src->ssl_ctx.methods.flags)
2126 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2127 if (src->ssl_ctx.methods.min)
2128 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2129 if (src->ssl_ctx.methods.max)
2130 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2131
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002132 if (src->ssl_ctx.ciphersuites != NULL)
2133 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002134 if (src->sni_expr != NULL)
2135 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002136
Olivier Houchardc7566002018-11-20 23:33:50 +01002137 if (src->ssl_ctx.alpn_str) {
2138 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2139 if (srv->ssl_ctx.alpn_str) {
2140 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2141 src->ssl_ctx.alpn_len);
2142 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2143 }
2144 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002145
Olivier Houchardc7566002018-11-20 23:33:50 +01002146 if (src->ssl_ctx.npn_str) {
2147 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2148 if (srv->ssl_ctx.npn_str) {
2149 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2150 src->ssl_ctx.npn_len);
2151 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2152 }
2153 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002154}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002155
2156/* Activate ssl on server <s>.
2157 * do nothing if there is no change to apply
2158 *
2159 * Must be called with the server lock held.
2160 */
2161void srv_set_ssl(struct server *s, int use_ssl)
2162{
2163 if (s->use_ssl == use_ssl)
2164 return;
2165
2166 s->use_ssl = use_ssl;
2167 if (s->use_ssl)
2168 s->xprt = xprt_get(XPRT_SSL);
2169 else
William Dauchya087f872022-01-06 16:57:15 +01002170 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002171}
2172
2173#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002174
2175/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002176 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002177 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002178 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002179 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002180int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002181{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002182 char *hostname_dn;
2183 int hostname_len, hostname_dn_len;
2184
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002185 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002186 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002187
Christopher Faulet67957bd2017-09-27 11:00:59 +02002188 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002189 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002190 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002191 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002192 if (hostname_dn_len == -1)
2193 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002194
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002195
Christopher Faulet67957bd2017-09-27 11:00:59 +02002196 free(srv->hostname);
2197 free(srv->hostname_dn);
2198 srv->hostname = strdup(hostname);
2199 srv->hostname_dn = strdup(hostname_dn);
2200 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002201 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002202 goto err;
2203
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002204 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002205
2206 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002207 ha_free(&srv->hostname);
2208 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002209 return -1;
2210}
2211
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002212/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002213 * Copy <src> server settings to <srv> server allocating
2214 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002215 * This function is not supposed to be called at any time, but only
2216 * during server settings parsing or during server allocations from
2217 * a server template, and just after having calloc()'ed a new server.
2218 * So, <src> may only be a default server (when parsing server settings)
2219 * or a server template (during server allocations from a server template).
2220 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2221 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002222 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002223void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002224{
2225 /* Connection source settings copy */
2226 srv_conn_src_cpy(srv, src);
2227
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002228 if (srv_tmpl) {
2229 srv->addr = src->addr;
2230 srv->svc_port = src->svc_port;
2231 }
2232
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002233 srv->pp_opts = src->pp_opts;
2234 if (src->rdr_pfx != NULL) {
2235 srv->rdr_pfx = strdup(src->rdr_pfx);
2236 srv->rdr_len = src->rdr_len;
2237 }
2238 if (src->cookie != NULL) {
2239 srv->cookie = strdup(src->cookie);
2240 srv->cklen = src->cklen;
2241 }
2242 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002243 srv->check.addr = src->check.addr;
2244 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002245 srv->check.use_ssl = src->check.use_ssl;
2246 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002247 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002248 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002249 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002250 /* Note: 'flags' field has potentially been already initialized. */
2251 srv->flags |= src->flags;
2252 srv->do_check = src->do_check;
2253 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002254 srv->check.inter = src->check.inter;
2255 srv->check.fastinter = src->check.fastinter;
2256 srv->check.downinter = src->check.downinter;
2257 srv->agent.use_ssl = src->agent.use_ssl;
2258 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002259
2260 if (src->agent.tcpcheck_rules) {
2261 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2262 if (srv->agent.tcpcheck_rules) {
2263 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2264 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2265 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2266 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2267 &src->agent.tcpcheck_rules->preset_vars);
2268 }
2269 }
2270
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002271 srv->agent.inter = src->agent.inter;
2272 srv->agent.fastinter = src->agent.fastinter;
2273 srv->agent.downinter = src->agent.downinter;
2274 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002275 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002276 srv->minconn = src->minconn;
2277 srv->maxconn = src->maxconn;
2278 srv->slowstart = src->slowstart;
2279 srv->observe = src->observe;
2280 srv->onerror = src->onerror;
2281 srv->onmarkeddown = src->onmarkeddown;
2282 srv->onmarkedup = src->onmarkedup;
2283 if (src->trackit != NULL)
2284 srv->trackit = strdup(src->trackit);
2285 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2286 srv->uweight = srv->iweight = src->iweight;
2287
2288 srv->check.send_proxy = src->check.send_proxy;
2289 /* health: up, but will fall down at first failure */
2290 srv->check.rise = srv->check.health = src->check.rise;
2291 srv->check.fall = src->check.fall;
2292
2293 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002294 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2295 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2296 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002297 srv->check.state |= CHK_ST_PAUSED;
2298 srv->check.health = 0;
2299 }
2300
2301 /* health: up but will fall down at first failure */
2302 srv->agent.rise = srv->agent.health = src->agent.rise;
2303 srv->agent.fall = src->agent.fall;
2304
2305 if (src->resolvers_id != NULL)
2306 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002307 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2308 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2309 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2310 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2311 srv->resolv_opts.family_prio = AF_INET6;
2312 memcpy(srv->resolv_opts.pref_net,
2313 src->resolv_opts.pref_net,
2314 sizeof srv->resolv_opts.pref_net);
2315 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002316
2317 srv->init_addr_methods = src->init_addr_methods;
2318 srv->init_addr = src->init_addr;
2319#if defined(USE_OPENSSL)
2320 srv_ssl_settings_cpy(srv, src);
2321#endif
2322#ifdef TCP_USER_TIMEOUT
2323 srv->tcp_ut = src->tcp_ut;
2324#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002325 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002326 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002327 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002328 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002329 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002330
Olivier Houchard8da5f982017-08-04 18:35:36 +02002331 if (srv_tmpl)
2332 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002333
2334 srv->check.via_socks4 = src->check.via_socks4;
2335 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002336}
2337
Willy Tarreau198e92a2021-03-05 10:23:32 +01002338/* allocate a server and attach it to the global servers_list. Returns
2339 * the server on success, otherwise NULL.
2340 */
William Lallemand313bfd12018-10-26 14:47:32 +02002341struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002342{
2343 struct server *srv;
2344
2345 srv = calloc(1, sizeof *srv);
2346 if (!srv)
2347 return NULL;
2348
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002349 srv_take(srv);
2350
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002351 srv->obj_type = OBJ_TYPE_SERVER;
2352 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002353 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002354 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002355 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002356 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002357 MT_LIST_INIT(&srv->prev_deleted);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002358 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002359
Emeric Brun52a91d32017-08-31 14:41:55 +02002360 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Willy Tarreau69530f52023-04-28 09:16:15 +02002361 srv->last_change = ns_to_sec(now_ns);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002362
Christopher Faulet38290462020-04-21 11:46:40 +02002363 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002364 srv->check.status = HCHK_STATUS_INI;
2365 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002366 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002367 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002368
Christopher Faulet38290462020-04-21 11:46:40 +02002369 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002370 srv->agent.status = HCHK_STATUS_INI;
2371 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002372 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002373 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002374#if defined(USE_QUIC)
2375 srv->cids = EB_ROOT_UNIQUE;
2376#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002377
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002378 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002379#ifdef USE_OPENSSL
2380 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2381#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002382
Willy Tarreau975b1552019-06-06 16:25:55 +02002383 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002384 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002385 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002386 return srv;
2387}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002388
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002389/* Increment the server refcount. */
2390void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002391{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002392 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002393}
2394
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002395/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2396 * dynamic servers, its refcount is decremented first. The free operations are
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002397 * conducted only if the refcount is nul.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002398 *
2399 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002400 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002401 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002402struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002403{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002404 struct server *next = NULL;
2405
William Lallemand4c395fc2021-08-20 10:10:15 +02002406 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002407 goto end;
2408
2409 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002410
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002411 /* For dynamic servers, decrement the reference counter. Only free the
2412 * server when reaching zero.
2413 */
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002414 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2415 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002416
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002417 /* make sure we are removed from our 'next->prev_deleted' list
2418 * This doesn't require full thread isolation as we're using mt lists
2419 * However this could easily be turned into regular list if required
2420 * (with the proper use of thread isolation)
2421 */
2422 MT_LIST_DELETE(&srv->prev_deleted);
2423
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002424 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002425 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002426
2427 free(srv->id);
2428 free(srv->cookie);
2429 free(srv->hostname);
2430 free(srv->hostname_dn);
2431 free((char*)srv->conf.file);
2432 free(srv->per_thr);
Willy Tarreauc21a1872022-11-21 14:14:06 +01002433 free(srv->per_tgrp);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002434 free(srv->curr_idle_thr);
2435 free(srv->resolvers_id);
2436 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002437 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002438
Aurelien DARRAGONb5ee8be2023-03-09 14:28:00 +01002439 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2440 xprt_get(XPRT_SSL)->destroy_srv(srv);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002441 HA_SPIN_DESTROY(&srv->lock);
2442
Willy Tarreau2b718102021-04-21 07:32:39 +02002443 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002444 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002445
2446 EXTRA_COUNTERS_FREE(srv->extra_counters);
2447
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002448 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002449
2450 end:
2451 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002452}
2453
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002454/* Remove a server <srv> from a tracking list if <srv> is tracking another
2455 * server. No special care is taken if <srv> is tracked itself by another one :
2456 * this situation should be avoided by the caller.
2457 *
2458 * Not thread-safe.
2459 */
2460static void release_server_track(struct server *srv)
2461{
2462 struct server *strack = srv->track;
2463 struct server **base;
2464
2465 if (!strack)
2466 return;
2467
2468 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2469 if (*base == srv) {
2470 *base = srv->tracknext;
2471 return;
2472 }
2473 }
2474
2475 /* srv not found on the tracking list, this should never happen */
2476 BUG_ON(!*base);
2477}
2478
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002479/*
2480 * Parse as much as possible such a range string argument: low[-high]
2481 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2482 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2483 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2484 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002485 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002486static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2487 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002488{
2489 char *nb_high_arg;
2490
2491 *nb_high = 0;
2492 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002493 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002494
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002495 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002496 *nb_high_arg++ = '\0';
2497 *nb_high = atoi(nb_high_arg);
2498 }
2499 else {
2500 *nb_high += *nb_low;
2501 *nb_low = 1;
2502 }
2503
2504 if (*nb_low < 0 || *nb_high < *nb_low)
2505 return -1;
2506
2507 return 0;
2508}
2509
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002510/* Parse as much as possible such a range string argument: low[-high]
2511 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2512 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2513 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002514 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002515 * initialize a new server on startup.
2516 *
2517 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2518 * Returns 0 if succeeded, -1 if not.
2519 */
2520static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2521 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002522{
2523 chunk_printf(&trash, "%s%d", prefix, nb);
2524 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002525 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002526}
2527
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002528/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002529 * Note that a server template is a special server with
2530 * a few different parameters than a server which has
2531 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002532 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002533 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002534 * initialize a new server on startup.
2535 *
Joseph Herlant44466822018-11-15 08:57:51 -08002536 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002537 * 'srv' template included.
2538 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002539static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002540{
2541 int i;
2542 struct server *newsrv;
2543
2544 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 +02002545 newsrv = new_server(px);
2546 if (!newsrv)
2547 goto err;
2548
Christopher Faulet75bef002020-11-02 22:04:55 +01002549 newsrv->conf.file = strdup(srv->conf.file);
2550 newsrv->conf.line = srv->conf.line;
2551
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002552 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002553 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002554
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002555 if (newsrv->sni_expr) {
2556 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2557 if (!newsrv->ssl_ctx.sni)
2558 goto err;
2559 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002560
Emeric Brun34067662021-06-11 10:48:45 +02002561 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002562 if (newsrv->srvrq)
2563 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002564
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002565 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002566 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002567
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002568 /* Linked backwards first. This will be restablished after parsing. */
2569 newsrv->next = px->srv;
2570 px->srv = newsrv;
2571 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002572 _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 +02002573
2574 return i - srv->tmpl_info.nb_low;
2575
2576 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002577 _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 +02002578 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002579 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002580 free_check(&newsrv->agent);
2581 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002582 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002583 }
2584 free(newsrv);
2585 return i - srv->tmpl_info.nb_low;
2586}
2587
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002588/* Allocate a new server pointed by <srv> and try to parse the first arguments
2589 * in <args> as an address for a server or an address-range for a template or
2590 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2591 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002592 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002593 * initialize a new server on startup.
2594 *
2595 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2596 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2597 * <srv> will be set to NULL.
2598 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002599static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2600 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002601 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002602{
2603 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002604 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002605 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002606 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002607 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002608 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002609
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002610 *srv = NULL;
2611
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002612 /* There is no mandatory first arguments for default server. */
2613 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2614 if (parse_flags & SRV_PARSE_TEMPLATE) {
2615 if (!*args[3]) {
2616 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002617 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2618 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002619 err_code |= ERR_ALERT | ERR_FATAL;
2620 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002621 }
2622
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002623 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002624 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002625 else {
2626 if (!*args[2]) {
2627 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002628 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2629 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002630 err_code |= ERR_ALERT | ERR_FATAL;
2631 goto out;
2632 }
2633
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002634 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002635 }
2636
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002637 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002638 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2639 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002640 err_code |= ERR_ALERT | ERR_FATAL;
2641 goto out;
2642 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002643 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002644
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002645 *cur_arg = 2;
2646 if (parse_flags & SRV_PARSE_TEMPLATE) {
2647 /* Parse server-template <nb | range> arg. */
2648 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002649 ha_alert("Wrong %s number or range arg '%s'.\n",
2650 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002651 err_code |= ERR_ALERT | ERR_FATAL;
2652 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002653 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002654 (*cur_arg)++;
2655 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002656
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002657 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2658 struct sockaddr_storage *sk;
2659 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002660
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002661 *srv = newsrv = new_server(curproxy);
2662 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002663 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002664 err_code |= ERR_ALERT | ERR_ABORT;
2665 goto out;
2666 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002667 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002668
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002669 if (parse_flags & SRV_PARSE_TEMPLATE) {
2670 newsrv->tmpl_info.nb_low = tmpl_range_low;
2671 newsrv->tmpl_info.nb_high = tmpl_range_high;
2672 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002673
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002674 if (parse_flags & SRV_PARSE_DYNAMIC)
2675 newsrv->flags |= SRV_F_DYNAMIC;
2676
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002677 /* Note: for a server template, its id is its prefix.
2678 * This is a temporary id which will be used for server allocations to come
2679 * after parsing.
2680 */
2681 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2682 newsrv->id = strdup(args[1]);
2683 else
2684 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002685
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002686 /* revision defaults to 0 */
2687 newsrv->rid = 0;
2688
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002689 /* several ways to check the port component :
2690 * - IP => port=+0, relative (IPv4 only)
2691 * - IP: => port=+0, relative
2692 * - IP:N => port=N, absolute
2693 * - IP:+N => port=+N, relative
2694 * - IP:-N => port=-N, relative
2695 */
2696 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2697 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002698
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002699 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002700 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002701 (parse_flags & SRV_PARSE_INITIAL_RESOLVE ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
2702 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002703 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002704 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002705 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002706 goto out;
2707 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002708
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002709 if (!port1 || !port2) {
2710 /* no port specified, +offset, -offset */
2711 newsrv->flags |= SRV_F_MAPPORTS;
2712 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002713
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002714 /* save hostname and create associated name resolution */
2715 if (fqdn) {
2716 if (fqdn[0] == '_') { /* SRV record */
2717 /* Check if a SRV request already exists, and if not, create it */
2718 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2719 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2720 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002721 err_code |= ERR_ALERT | ERR_FATAL;
2722 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002723 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002724 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002725 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002726 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002727 ha_alert("Can't create DNS resolution for server '%s'\n",
2728 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002729 err_code |= ERR_ALERT | ERR_FATAL;
2730 goto out;
2731 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002732 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002733
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002734 newsrv->addr = *sk;
2735 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002736 /*
2737 * we don't need to lock the server here, because
2738 * we are in the process of initializing.
2739 *
2740 * Note that the server is not attached into the proxy tree if
2741 * this is a dynamic server.
2742 */
2743 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002744
Willy Tarreau14e7f292021-10-27 17:41:07 +02002745 if (!newsrv->srvrq && !newsrv->hostname &&
2746 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002747 ha_alert("Unknown protocol family %d '%s'\n",
2748 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002749 err_code |= ERR_ALERT | ERR_FATAL;
2750 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002751 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002752
2753 (*cur_arg)++;
2754 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002755 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2756 /* Copy default server settings to new server */
2757 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2758 } else {
2759 /* Initialize dynamic server weight to 1 */
2760 newsrv->uweight = newsrv->iweight = 1;
2761
2762 /* A dynamic server is disabled on startup */
2763 newsrv->next_admin = SRV_ADMF_FMAINT;
2764 newsrv->next_state = SRV_ST_STOPPED;
2765 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002766
2767 /* Set default values for checks */
2768 newsrv->check.inter = DEF_CHKINTR;
2769 newsrv->check.rise = DEF_RISETIME;
2770 newsrv->check.fall = DEF_FALLTIME;
2771
2772 newsrv->agent.inter = DEF_CHKINTR;
2773 newsrv->agent.rise = DEF_AGENT_RISETIME;
2774 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002775 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002776 HA_SPIN_INIT(&newsrv->lock);
2777 }
2778 else {
2779 *srv = newsrv = &curproxy->defsrv;
2780 *cur_arg = 1;
2781 newsrv->resolv_opts.family_prio = AF_INET6;
2782 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002783 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002784
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002785 free(fqdn);
2786 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002787
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002788out:
2789 free(fqdn);
2790 return err_code;
2791}
Willy Tarreau272adea2014-03-31 10:39:59 +02002792
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002793/* Parse the server keyword in <args>.
2794 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2795 * might not be the case if an error is reported.
2796 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002797 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002798 * initialize a new server on startup.
2799 *
2800 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2801 * interrupted.
2802 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002803static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2804 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002805 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002806{
2807 int err_code = 0;
2808 struct srv_kw *kw;
2809 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002810 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002811
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002812 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002813 if (!kw) {
2814 best = srv_find_best_kw(args[*cur_arg]);
2815 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002816 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2817 args[*cur_arg], best,
2818 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2819 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002820 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002821 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2822 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2823 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002824
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002825 return ERR_ALERT | ERR_FATAL;
2826 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002827
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002828 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002829 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2830 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002831 err_code = ERR_ALERT | ERR_FATAL;
2832 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002833 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002834
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002835 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002836 ha_alert("'%s' option is not accepted in default-server sections\n",
2837 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002838 err_code = ERR_ALERT;
2839 goto out;
2840 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002841 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002842 ha_alert("'%s' option is not accepted for dynamic server\n",
2843 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002844 err_code |= ERR_ALERT;
2845 goto out;
2846 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002847
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002848 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2849 if (err_code) {
2850 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2851 free(errmsg);
2852 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002853
2854out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002855 if (kw->skip != -1)
2856 *cur_arg += 1 + kw->skip;
2857
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002858 return err_code;
2859}
2860
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002861/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002862 * initialize a new server on startup.
2863 */
2864static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2865 struct server *srv, struct proxy *proxy,
2866 char **errmsg)
2867{
2868 int ret;
2869
2870 if (!srv->sni_expr)
2871 return 0;
2872
2873 ret = server_parse_sni_expr(srv, proxy, errmsg);
2874 if (!ret)
2875 return 0;
2876
2877 return ret;
2878}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002879
2880/* Server initializations finalization.
2881 * Initialize health check, agent check and SNI expression if enabled.
2882 * Must not be called for a default server instance.
2883 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002884 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002885 * initialize a new server on startup.
2886 */
2887static int _srv_parse_finalize(char **args, int cur_arg,
2888 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002889 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002890{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002891 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002892 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002893
2894 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002895 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002896 return ERR_ALERT | ERR_FATAL;
2897 }
2898
2899 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002900 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2901 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002902 return ERR_ALERT | ERR_FATAL;
2903 }
2904
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002905 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2906 if (errmsg) {
2907 ha_alert("%s\n", errmsg);
2908 free(errmsg);
2909 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002910 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002911 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002912
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002913 /* A dynamic server is disabled on startup. It must not be counted as
2914 * an active backend entry.
2915 */
2916 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2917 if (srv->flags & SRV_F_BACKUP)
2918 px->srv_bck++;
2919 else
2920 px->srv_act++;
2921 }
2922
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002923 srv_lb_commit_status(srv);
2924
2925 return 0;
2926}
2927
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002928int parse_server(const char *file, int linenum, char **args,
2929 struct proxy *curproxy, const struct proxy *defproxy,
2930 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002931{
2932 struct server *newsrv = NULL;
2933 int err_code = 0;
2934
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002935 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002936
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002937 set_usermsgs_ctx(file, linenum, NULL);
2938
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002939 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002940 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002941 err_code |= ERR_ALERT | ERR_FATAL;
2942 goto out;
2943 }
2944 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2945 err_code |= ERR_ALERT | ERR_FATAL;
2946 goto out;
2947 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002948
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002949 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2950 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2951 if (!*args[2])
2952 return 0;
2953 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002954
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002955 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002956 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002957
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002958 /* the servers are linked backwards first */
2959 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2960 newsrv->next = curproxy->srv;
2961 curproxy->srv = newsrv;
2962 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002963
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002964 if (err_code & ERR_CODE)
2965 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002966
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002967 newsrv->conf.file = strdup(file);
2968 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002969
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002970 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002971 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002972 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002973 if (err_code & ERR_FATAL)
2974 goto out;
2975 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002976
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002977 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002978 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002979 if (err_code & ERR_FATAL)
2980 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002981 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002982
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002983 if (parse_flags & SRV_PARSE_TEMPLATE)
2984 _srv_parse_tmpl_init(newsrv, curproxy);
2985
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02002986 /* If the server id is fixed, insert it in the proxy used_id tree.
2987 * This is needed to detect a later duplicate id via srv_parse_id.
2988 *
2989 * If no is specified, a dynamic one is generated in
2990 * check_config_validity.
2991 */
2992 if (newsrv->flags & SRV_F_FORCED_ID)
2993 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
2994
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02002995 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002996 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02002997
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002998 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002999 return 0;
3000
3001 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003002 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003003 return err_code;
3004}
3005
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003006/* Returns a pointer to the first server matching either id <id>.
3007 * NULL is returned if no match is found.
3008 * the lookup is performed in the backend <bk>
3009 */
3010struct server *server_find_by_id(struct proxy *bk, int id)
3011{
3012 struct eb32_node *eb32;
3013 struct server *curserver;
3014
3015 if (!bk || (id ==0))
3016 return NULL;
3017
3018 /* <bk> has no backend capabilities, so it can't have a server */
3019 if (!(bk->cap & PR_CAP_BE))
3020 return NULL;
3021
3022 curserver = NULL;
3023
3024 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3025 if (eb32)
3026 curserver = container_of(eb32, struct server, conf.id);
3027
3028 return curserver;
3029}
3030
3031/* Returns a pointer to the first server matching either name <name>, or id
3032 * if <name> starts with a '#'. NULL is returned if no match is found.
3033 * the lookup is performed in the backend <bk>
3034 */
3035struct server *server_find_by_name(struct proxy *bk, const char *name)
3036{
3037 struct server *curserver;
3038
3039 if (!bk || !name)
3040 return NULL;
3041
3042 /* <bk> has no backend capabilities, so it can't have a server */
3043 if (!(bk->cap & PR_CAP_BE))
3044 return NULL;
3045
3046 curserver = NULL;
3047 if (*name == '#') {
3048 curserver = server_find_by_id(bk, atoi(name + 1));
3049 if (curserver)
3050 return curserver;
3051 }
3052 else {
3053 curserver = bk->srv;
3054
3055 while (curserver && (strcmp(curserver->id, name) != 0))
3056 curserver = curserver->next;
3057
3058 if (curserver)
3059 return curserver;
3060 }
3061
3062 return NULL;
3063}
3064
3065struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3066{
3067 struct server *byname;
3068 struct server *byid;
3069
3070 if (!name && !id)
3071 return NULL;
3072
3073 if (diff)
3074 *diff = 0;
3075
3076 byname = byid = NULL;
3077
3078 if (name) {
3079 byname = server_find_by_name(bk, name);
3080 if (byname && (!id || byname->puid == id))
3081 return byname;
3082 }
3083
3084 /* remaining possibilities :
3085 * - name not set
3086 * - name set but not found
3087 * - name found but ID doesn't match
3088 */
3089 if (id) {
3090 byid = server_find_by_id(bk, id);
3091 if (byid) {
3092 if (byname) {
3093 /* use id only if forced by configuration */
3094 if (byid->flags & SRV_F_FORCED_ID) {
3095 if (diff)
3096 *diff |= 2;
3097 return byid;
3098 }
3099 else {
3100 if (diff)
3101 *diff |= 1;
3102 return byname;
3103 }
3104 }
3105
3106 /* remaining possibilities:
3107 * - name not set
3108 * - name set but not found
3109 */
3110 if (name && diff)
3111 *diff |= 2;
3112 return byid;
3113 }
3114
3115 /* id bot found */
3116 if (byname) {
3117 if (diff)
3118 *diff |= 1;
3119 return byname;
3120 }
3121 }
3122
3123 return NULL;
3124}
3125
Simon Horman7d09b9a2013-02-12 10:45:51 +09003126/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003127 * update a server's current IP address.
3128 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3129 * ip is in network format.
3130 * updater is a string which contains an information about the requester of the update.
3131 * updater is used if not NULL.
3132 *
3133 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003134 *
3135 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003136 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003137int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003138{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003139 /* save the new IP family & address if necessary */
3140 switch (ip_sin_family) {
3141 case AF_INET:
3142 if (s->addr.ss_family == ip_sin_family &&
3143 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3144 return 0;
3145 break;
3146 case AF_INET6:
3147 if (s->addr.ss_family == ip_sin_family &&
3148 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3149 return 0;
3150 break;
3151 };
3152
Baptiste Assmann14e40142015-04-14 01:13:07 +02003153 /* generates a log line and a warning on stderr */
3154 if (1) {
3155 /* book enough space for both IPv4 and IPv6 */
3156 char oldip[INET6_ADDRSTRLEN];
3157 char newip[INET6_ADDRSTRLEN];
3158
3159 memset(oldip, '\0', INET6_ADDRSTRLEN);
3160 memset(newip, '\0', INET6_ADDRSTRLEN);
3161
3162 /* copy old IP address in a string */
3163 switch (s->addr.ss_family) {
3164 case AF_INET:
3165 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3166 break;
3167 case AF_INET6:
3168 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3169 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003170 default:
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003171 strlcpy2(oldip, "(none)", sizeof(oldip));
Christopher Fauletb0b76072020-09-08 10:38:40 +02003172 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003173 };
3174
3175 /* copy new IP address in a string */
3176 switch (ip_sin_family) {
3177 case AF_INET:
3178 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3179 break;
3180 case AF_INET6:
3181 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3182 break;
3183 };
3184
3185 /* save log line into a buffer */
3186 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3187 s->proxy->id, s->id, oldip, newip, updater);
3188
3189 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003190 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003191
3192 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003193 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003194 }
3195
3196 /* save the new IP family */
3197 s->addr.ss_family = ip_sin_family;
3198 /* save the new IP address */
3199 switch (ip_sin_family) {
3200 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003201 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003202 break;
3203 case AF_INET6:
3204 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3205 break;
3206 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003207 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003208 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003209
3210 return 0;
3211}
3212
William Dauchy7cabc062021-02-11 22:51:24 +01003213/* update agent health check address and port
3214 * addr can be ip4/ip6 or a hostname
3215 * if one error occurs, don't apply anything
3216 * must be called with the server lock held.
3217 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003218const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003219{
3220 struct sockaddr_storage sk;
3221 struct buffer *msg;
3222 int new_port;
3223
3224 msg = get_trash_chunk();
3225 chunk_reset(msg);
3226
3227 if (!(s->agent.state & CHK_ST_ENABLED)) {
3228 chunk_strcat(msg, "agent checks are not enabled on this server");
3229 goto out;
3230 }
3231 if (addr) {
3232 memset(&sk, 0, sizeof(struct sockaddr_storage));
3233 if (str2ip(addr, &sk) == NULL) {
3234 chunk_appendf(msg, "invalid addr '%s'", addr);
3235 goto out;
3236 }
3237 }
3238 if (port) {
3239 if (strl2irc(port, strlen(port), &new_port) != 0) {
3240 chunk_appendf(msg, "provided port is not an integer");
3241 goto out;
3242 }
3243 if (new_port < 0 || new_port > 65535) {
3244 chunk_appendf(msg, "provided port is invalid");
3245 goto out;
3246 }
3247 }
3248out:
3249 if (msg->data)
3250 return msg->area;
3251 else {
3252 if (addr)
3253 set_srv_agent_addr(s, &sk);
3254 if (port)
3255 set_srv_agent_port(s, new_port);
3256 }
3257 return NULL;
3258}
3259
William Dauchyb456e1f2021-02-11 22:51:23 +01003260/* update server health check address and port
3261 * addr must be ip4 or ip6, it won't be resolved
3262 * if one error occurs, don't apply anything
3263 * must be called with the server lock held.
3264 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003265const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003266{
3267 struct sockaddr_storage sk;
3268 struct buffer *msg;
3269 int new_port;
3270
3271 msg = get_trash_chunk();
3272 chunk_reset(msg);
3273
3274 if (!(s->check.state & CHK_ST_ENABLED)) {
3275 chunk_strcat(msg, "health checks are not enabled on this server");
3276 goto out;
3277 }
3278 if (addr) {
3279 memset(&sk, 0, sizeof(struct sockaddr_storage));
3280 if (str2ip2(addr, &sk, 0) == NULL) {
3281 chunk_appendf(msg, "invalid addr '%s'", addr);
3282 goto out;
3283 }
3284 }
3285 if (port) {
3286 if (strl2irc(port, strlen(port), &new_port) != 0) {
3287 chunk_appendf(msg, "provided port is not an integer");
3288 goto out;
3289 }
3290 if (new_port < 0 || new_port > 65535) {
3291 chunk_appendf(msg, "provided port is invalid");
3292 goto out;
3293 }
3294 /* prevent the update of port to 0 if MAPPORTS are in use */
3295 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3296 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3297 goto out;
3298 }
3299 }
3300out:
3301 if (msg->data)
3302 return msg->area;
3303 else {
3304 if (addr)
3305 s->check.addr = sk;
3306 if (port)
3307 s->check.port = new_port;
3308 }
3309 return NULL;
3310}
3311
Baptiste Assmann14e40142015-04-14 01:13:07 +02003312/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003313 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3314 *
3315 * Caller can pass its name through <updater> to get it integrated in the response message
3316 * returned by the function.
3317 *
3318 * The function first does the following, in that order:
3319 * - validates the new addr and/or port
3320 * - checks if an update is required (new IP or port is different than current ones)
3321 * - checks the update is allowed:
3322 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3323 * - allow all changes if no CHECKS are configured
3324 * - if CHECK is configured:
3325 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3326 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3327 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003328 *
3329 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003330 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003331const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003332{
3333 struct sockaddr_storage sa;
3334 int ret, port_change_required;
3335 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003336 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003337 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003338 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003339
3340 msg = get_trash_chunk();
3341 chunk_reset(msg);
3342
3343 if (addr) {
3344 memset(&sa, 0, sizeof(struct sockaddr_storage));
3345 if (str2ip2(addr, &sa, 0) == NULL) {
3346 chunk_printf(msg, "Invalid addr '%s'", addr);
3347 goto out;
3348 }
3349
3350 /* changes are allowed on AF_INET* families only */
3351 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3352 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3353 goto out;
3354 }
3355
3356 /* collecting data currently setup */
3357 memset(current_addr, '\0', sizeof(current_addr));
3358 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3359 /* changes are allowed on AF_INET* families only */
3360 if ((ret != AF_INET) && (ret != AF_INET6)) {
3361 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3362 goto out;
3363 }
3364
3365 /* applying ADDR changes if required and allowed
3366 * ipcmp returns 0 when both ADDR are the same
3367 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003368 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003369 chunk_appendf(msg, "no need to change the addr");
3370 goto port;
3371 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003372 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003373 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003374
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003375 /* update report for caller */
3376 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3377 }
3378
3379 port:
3380 if (port) {
3381 char sign = '\0';
3382 char *endptr;
3383
3384 if (addr)
3385 chunk_appendf(msg, ", ");
3386
3387 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003388 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003389
3390 /* check if PORT change is required */
3391 port_change_required = 0;
3392
3393 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003394 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003395 new_port = strtol(port, &endptr, 10);
3396 if ((errno != 0) || (port == endptr)) {
3397 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3398 goto out;
3399 }
3400
3401 /* check if caller triggers a port mapped or offset */
3402 if (sign == '-' || (sign == '+')) {
3403 /* check if server currently uses port map */
3404 if (!(s->flags & SRV_F_MAPPORTS)) {
3405 /* switch from fixed port to port map mandatorily triggers
3406 * a port change */
3407 port_change_required = 1;
3408 /* check is configured
3409 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3410 * prevent PORT change if check doesn't have it's dedicated port while switching
3411 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003412 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003413 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.");
3414 goto out;
3415 }
3416 }
3417 /* we're already using port maps */
3418 else {
3419 port_change_required = current_port != new_port;
3420 }
3421 }
3422 /* fixed port */
3423 else {
3424 port_change_required = current_port != new_port;
3425 }
3426
3427 /* applying PORT changes if required and update response message */
3428 if (port_change_required) {
3429 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003430 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003431 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003432
3433 /* prepare message */
3434 chunk_appendf(msg, "port changed from '");
3435 if (s->flags & SRV_F_MAPPORTS)
3436 chunk_appendf(msg, "+");
3437 chunk_appendf(msg, "%d' to '", current_port);
3438
3439 if (sign == '-') {
3440 s->flags |= SRV_F_MAPPORTS;
3441 chunk_appendf(msg, "%c", sign);
3442 /* just use for result output */
3443 new_port = -new_port;
3444 }
3445 else if (sign == '+') {
3446 s->flags |= SRV_F_MAPPORTS;
3447 chunk_appendf(msg, "%c", sign);
3448 }
3449 else {
3450 s->flags &= ~SRV_F_MAPPORTS;
3451 }
3452
3453 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003454 }
3455 else {
3456 chunk_appendf(msg, "no need to change the port");
3457 }
3458 }
3459
3460out:
William Dauchy6318d332020-05-02 21:52:36 +02003461 if (changed) {
3462 /* force connection cleanup on the given server */
3463 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003464 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003465 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003466 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003467 if (updater)
3468 chunk_appendf(msg, " by '%s'", updater);
3469 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003470 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003471}
3472
Christopher Faulet5efdef22021-03-11 18:03:21 +01003473/*
3474 * update server status based on result of SRV resolution
3475 * returns:
3476 * 0 if server status is updated
3477 * 1 if server status has not changed
3478 *
3479 * Must be called with the server lock held.
3480 */
3481int srvrq_update_srv_status(struct server *s, int has_no_ip)
3482{
3483 if (!s->srvrq)
3484 return 1;
3485
3486 /* since this server has an IP, it can go back in production */
3487 if (has_no_ip == 0) {
3488 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3489 return 1;
3490 }
3491
3492 if (s->next_admin & SRV_ADMF_RMAINT)
3493 return 1;
3494
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003495 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOENT);
Christopher Faulet5efdef22021-03-11 18:03:21 +01003496 return 0;
3497}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003498
3499/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003500 * update server status based on result of name resolution
3501 * returns:
3502 * 0 if server status is updated
3503 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003504 *
3505 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003506 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003507int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003508{
Emeric Brun750fe792020-12-23 16:51:12 +01003509 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003510 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003511 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003512
Jerome Magnin012261a2020-07-28 13:38:22 +02003513 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003514 if (resolution == NULL)
3515 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003516
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003517 switch (resolution->status) {
3518 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003519 /* status when HAProxy has just (re)started.
3520 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003521 break;
3522
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003523 case RSLV_STATUS_VALID:
3524 /*
3525 * resume health checks
3526 * server will be turned back on if health check is safe
3527 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003528 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003529 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003530 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003531 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOIP);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003532 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003533 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003534
Emeric Brun52a91d32017-08-31 14:41:55 +02003535 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003536 return 1;
3537 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3538 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3539 s->proxy->id, s->id);
3540
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003541 ha_warning("%s.\n", trash.area);
3542 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003543 return 0;
3544
3545 case RSLV_STATUS_NX:
3546 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003547 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3548 if (!tick_is_expired(exp, now_ms))
3549 break;
3550
3551 if (s->next_admin & SRV_ADMF_RMAINT)
3552 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003553 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NX);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003554 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003555
3556 case RSLV_STATUS_TIMEOUT:
3557 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003558 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3559 if (!tick_is_expired(exp, now_ms))
3560 break;
3561
3562 if (s->next_admin & SRV_ADMF_RMAINT)
3563 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003564 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_TIMEOUT);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003565 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003566
3567 case RSLV_STATUS_REFUSED:
3568 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003569 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3570 if (!tick_is_expired(exp, now_ms))
3571 break;
3572
3573 if (s->next_admin & SRV_ADMF_RMAINT)
3574 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003575 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_REFUSED);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003576 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003577
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003578 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003579 /* stop server if resolution failed for a long enough period */
3580 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3581 if (!tick_is_expired(exp, now_ms))
3582 break;
3583
3584 if (s->next_admin & SRV_ADMF_RMAINT)
3585 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003586 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_UNSPEC);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003587 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003588 }
3589
3590 return 1;
3591}
3592
3593/*
3594 * Server Name Resolution valid response callback
3595 * It expects:
3596 * - <nameserver>: the name server which answered the valid response
3597 * - <response>: buffer containing a valid DNS response
3598 * - <response_len>: size of <response>
3599 * It performs the following actions:
3600 * - ignore response if current ip found and server family not met
3601 * - update with first new ip found if family is met and current IP is not found
3602 * returns:
3603 * 0 on error
3604 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003605 *
3606 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003607 */
Emeric Brun08622d32020-12-23 17:41:43 +01003608int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003609{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003610 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003611 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003612 void *serverip, *firstip;
3613 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003614 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003615 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003616 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003617
Christopher Faulet67957bd2017-09-27 11:00:59 +02003618 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003619 if (!s)
3620 return 1;
3621
Christopher Faulet49531e82021-03-10 15:07:27 +01003622 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003623 /* If DNS resolution is disabled ignore it.
3624 * This is the case if the server was associated to
3625 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003626 */
Emeric Brun34067662021-06-11 10:48:45 +02003627 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003628 return 1;
3629 }
3630
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003631 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003632 if (!resolution)
3633 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003634
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003635 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003636 firstip = NULL; /* pointer to the first valid response found */
3637 /* it will be used as the new IP if a change is required */
3638 firstip_sin_family = AF_UNSPEC;
3639 serverip = NULL; /* current server IP address */
3640
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003641 /* initializing server IP pointer */
3642 server_sin_family = s->addr.ss_family;
3643 switch (server_sin_family) {
3644 case AF_INET:
3645 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3646 break;
3647
3648 case AF_INET6:
3649 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3650 break;
3651
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003652 case AF_UNSPEC:
3653 break;
3654
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003655 default:
3656 goto invalid;
3657 }
3658
Emeric Brund30e9a12020-12-23 18:49:16 +01003659 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3660 serverip, server_sin_family, &firstip,
3661 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003662
3663 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003664 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003665 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003666
Emeric Brun456de772020-12-23 18:17:31 +01003667 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003668 goto save_ip;
3669
Emeric Brun456de772020-12-23 18:17:31 +01003670 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003671 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003672 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003673
Emeric Brun456de772020-12-23 18:17:31 +01003674 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003675 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003676 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003677 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003678 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003679
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003680 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003681 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003682 goto invalid;
3683
3684 }
3685
3686 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003687 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003688 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003689 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003690 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003691 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003692 else
3693 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003694 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003695
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003696 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003697 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3698 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003699 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003700
3701 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003702 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003703 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003704 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003705 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003706 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3707 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708 return 0;
3709}
3710
3711/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003712 * SRV record error management callback
3713 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003714 * 0 if we can trash answser items.
3715 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003716 *
3717 * Grabs the server's lock.
3718 */
3719int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3720{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003721 struct resolv_srvrq *srvrq;
3722 struct resolv_resolution *res;
3723 struct resolvers *resolvers;
3724 int exp;
3725
3726 /* SRV records */
3727 srvrq = objt_resolv_srvrq(requester->owner);
3728 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003729 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003730
3731 resolvers = srvrq->resolvers;
3732 res = requester->resolution;
3733
3734 switch (res->status) {
3735
3736 case RSLV_STATUS_NX:
3737 /* stop server if resolution is NX for a long enough period */
3738 exp = tick_add(res->last_valid, resolvers->hold.nx);
3739 if (!tick_is_expired(exp, now_ms))
3740 return 1;
3741 break;
3742
3743 case RSLV_STATUS_TIMEOUT:
3744 /* stop server if resolution is TIMEOUT for a long enough period */
3745 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3746 if (!tick_is_expired(exp, now_ms))
3747 return 1;
3748 break;
3749
3750 case RSLV_STATUS_REFUSED:
3751 /* stop server if resolution is REFUSED for a long enough period */
3752 exp = tick_add(res->last_valid, resolvers->hold.refused);
3753 if (!tick_is_expired(exp, now_ms))
3754 return 1;
3755 break;
3756
3757 default:
3758 /* stop server if resolution failed for a long enough period */
3759 exp = tick_add(res->last_valid, resolvers->hold.other);
3760 if (!tick_is_expired(exp, now_ms))
3761 return 1;
3762 }
3763
Emeric Brun34067662021-06-11 10:48:45 +02003764 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003765 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003766
Emeric Brun12ca6582021-06-10 15:25:25 +02003767 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003768}
3769
3770/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003771 * Server Name Resolution error management callback
3772 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003773 * 0 if we can trash answser items.
3774 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003775 *
3776 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003777 */
Emeric Brun08622d32020-12-23 17:41:43 +01003778int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003779{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003780 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003781
Christopher Faulet67957bd2017-09-27 11:00:59 +02003782 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003783 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003784 return 0;
3785
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003786 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003787 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003788 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003789 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003790 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003791 return 0;
3792 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003793 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003794
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003795 return 1;
3796}
3797
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003798/*
3799 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003800 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003801 * It returns a pointer to the first server found or NULL if <ip> is not yet
3802 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003803 *
3804 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003805 */
3806struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3807{
3808 struct server *tmpsrv;
3809 struct proxy *be;
3810
3811 if (!srv)
3812 return NULL;
3813
3814 be = srv->proxy;
3815 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003816 /* we found the current server is the same, ignore it */
3817 if (srv == tmpsrv)
3818 continue;
3819
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003820 /* We want to compare the IP in the record with the IP of the servers in the
3821 * same backend, only if:
3822 * * DNS resolution is enabled on the server
3823 * * the hostname used for the resolution by our server is the same than the
3824 * one used for the server found in the backend
3825 * * the server found in the backend is not our current server
3826 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003827 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003828 if ((tmpsrv->hostname_dn == NULL) ||
3829 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003830 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003831 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003832 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003833 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003834 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003835
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003836 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003837 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003838 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003839 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003840 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003841
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003842 /* At this point, we have 2 different servers using the same DNS hostname
3843 * for their respective resolution.
3844 */
3845 if (*ip_family == tmpsrv->addr.ss_family &&
3846 ((tmpsrv->addr.ss_family == AF_INET &&
3847 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3848 (tmpsrv->addr.ss_family == AF_INET6 &&
3849 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003850 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003851 return tmpsrv;
3852 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003853 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003854 }
3855
Emeric Brune9fd6b52017-11-02 17:20:39 +01003856
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003857 return NULL;
3858}
3859
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003860/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3861 * resolver. This is suited for initial address configuration. Returns 0 on
3862 * success otherwise a non-zero error code. In case of error, *err_code, if
3863 * not NULL, is filled up.
3864 */
3865int srv_set_addr_via_libc(struct server *srv, int *err_code)
3866{
3867 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003868 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003869 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003870 return 1;
3871 }
3872 return 0;
3873}
3874
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003875/* Set the server's FDQN (->hostname) from <hostname>.
3876 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003877 *
3878 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003879 */
Emeric Brun08622d32020-12-23 17:41:43 +01003880int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003881{
Emeric Brun08622d32020-12-23 17:41:43 +01003882 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003883 char *hostname_dn;
3884 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003886 /* Note that the server lock is already held. */
3887 if (!srv->resolvers)
3888 return -1;
3889
Emeric Brun08622d32020-12-23 17:41:43 +01003890 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003891 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003892 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003893 * and we can't enable it at run time for now.
3894 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003895 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003896 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003897
3898 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003899 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003900 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003901 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003902 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003903 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003904 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003906 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003907 if (resolution &&
3908 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003909 resolution->hostname_dn_len == hostname_dn_len &&
3910 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003911 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003912
Willy Tarreau6878f802021-10-20 14:07:31 +02003913 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003914
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003915 free(srv->hostname);
3916 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003917 srv->hostname = strdup(hostname);
3918 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003919 srv->hostname_dn_len = hostname_dn_len;
3920 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003921 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003922
Baptiste Assmann13a92322019-06-07 09:40:55 +02003923 if (srv->flags & SRV_F_NO_RESOLUTION)
3924 goto end;
3925
Emeric Brund30e9a12020-12-23 18:49:16 +01003926 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003927 goto err;
3928
3929 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003930 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003931 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003932 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003933
3934 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003935 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003936 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003937 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003938}
3939
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003940/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3941 * from the state file. This is suited for initial address configuration.
3942 * Returns 0 on success otherwise a non-zero error code. In case of error,
3943 * *err_code, if not NULL, is filled up.
3944 */
3945static int srv_apply_lastaddr(struct server *srv, int *err_code)
3946{
3947 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3948 if (err_code)
3949 *err_code |= ERR_WARN;
3950 return 1;
3951 }
3952 return 0;
3953}
3954
Willy Tarreau25e51522016-11-04 15:10:17 +01003955/* returns 0 if no error, otherwise a combination of ERR_* flags */
3956static int srv_iterate_initaddr(struct server *srv)
3957{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003958 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003959 int return_code = 0;
3960 int err_code;
3961 unsigned int methods;
3962
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003963 /* If no addr and no hostname set, get the name from the DNS SRV request */
3964 if (!name && srv->srvrq)
3965 name = srv->srvrq->name;
3966
Willy Tarreau25e51522016-11-04 15:10:17 +01003967 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003968 if (!methods) {
3969 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003970 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3971 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003972 if (srv->resolvers_id) {
3973 /* dns resolution is configured, add "none" to not fail on startup */
3974 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3975 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003976 }
3977
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003978 /* "-dr" : always append "none" so that server addresses resolution
3979 * failures are silently ignored, this is convenient to validate some
3980 * configs out of their environment.
3981 */
3982 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3983 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3984
Willy Tarreau25e51522016-11-04 15:10:17 +01003985 while (methods) {
3986 err_code = 0;
3987 switch (srv_get_next_initaddr(&methods)) {
3988 case SRV_IADDR_LAST:
3989 if (!srv->lastaddr)
3990 continue;
3991 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003992 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003993 return_code |= err_code;
3994 break;
3995
3996 case SRV_IADDR_LIBC:
3997 if (!srv->hostname)
3998 continue;
3999 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004000 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004001 return_code |= err_code;
4002 break;
4003
Willy Tarreau37ebe122016-11-04 15:17:58 +01004004 case SRV_IADDR_NONE:
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004005 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004006 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004007 ha_warning("could not resolve address '%s', disabling server.\n",
4008 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004009 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004010 return return_code;
4011
Willy Tarreau4310d362016-11-02 15:05:56 +01004012 case SRV_IADDR_IP:
4013 ipcpy(&srv->init_addr, &srv->addr);
4014 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004015 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4016 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004017 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004018 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004019
Willy Tarreau25e51522016-11-04 15:10:17 +01004020 default: /* unhandled method */
4021 break;
4022 }
4023 }
4024
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004025 if (!return_code)
4026 ha_alert("no method found to resolve address '%s'.\n", name);
4027 else
4028 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004029
4030 return_code |= ERR_ALERT | ERR_FATAL;
4031 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004032out:
4033 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004034 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004035 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004036}
4037
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004038/*
4039 * This function parses all backends and all servers within each backend
4040 * and performs servers' addr resolution based on information provided by:
4041 * - configuration file
4042 * - server-state file (states provided by an 'old' haproxy process)
4043 *
4044 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4045 */
4046int srv_init_addr(void)
4047{
4048 struct proxy *curproxy;
4049 int return_code = 0;
4050
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004051 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004052 while (curproxy) {
4053 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004054
4055 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004056 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004057 goto srv_init_addr_next;
4058
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004059 for (srv = curproxy->srv; srv; srv = srv->next) {
4060 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004061 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004062 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004063 reset_usermsgs_ctx();
4064 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004065
4066 srv_init_addr_next:
4067 curproxy = curproxy->next;
4068 }
4069
4070 return return_code;
4071}
4072
Willy Tarreau46b7f532018-08-21 11:54:26 +02004073/*
4074 * Must be called with the server lock held.
4075 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004076const 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 +02004077{
4078
Willy Tarreau83061a82018-07-13 11:56:34 +02004079 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004080
4081 msg = get_trash_chunk();
4082 chunk_reset(msg);
4083
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004084 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004085 chunk_appendf(msg, "no need to change the FDQN");
4086 goto out;
4087 }
4088
4089 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4090 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4091 goto out;
4092 }
4093
4094 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4095 server->proxy->id, server->id, server->hostname, fqdn);
4096
Emeric Brun08622d32020-12-23 17:41:43 +01004097 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004098 chunk_reset(msg);
4099 chunk_appendf(msg, "could not update %s/%s FQDN",
4100 server->proxy->id, server->id);
4101 goto out;
4102 }
4103
4104 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004105 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004106
4107 out:
4108 if (updater)
4109 chunk_appendf(msg, " by '%s'", updater);
4110 chunk_appendf(msg, "\n");
4111
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004112 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004113}
4114
4115
Willy Tarreau21b069d2016-11-23 17:15:08 +01004116/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4117 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004118 * 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 +01004119 * used for CLI commands requiring a server name.
4120 * Important: the <arg> is modified to remove the '/'.
4121 */
4122struct server *cli_find_server(struct appctx *appctx, char *arg)
4123{
4124 struct proxy *px;
4125 struct server *sv;
4126 char *line;
4127
4128 /* split "backend/server" and make <line> point to server */
4129 for (line = arg; *line; line++)
4130 if (*line == '/') {
4131 *line++ = '\0';
4132 break;
4133 }
4134
4135 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004136 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004137 return NULL;
4138 }
4139
4140 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004141 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004142 return NULL;
4143 }
4144
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004145 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004146 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004147 return NULL;
4148 }
4149
4150 return sv;
4151}
4152
William Lallemand222baf22016-11-19 02:00:33 +01004153
Willy Tarreau46b7f532018-08-21 11:54:26 +02004154/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004155static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004156{
4157 struct server *sv;
4158 const char *warning;
4159
4160 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4161 return 1;
4162
4163 sv = cli_find_server(appctx, args[2]);
4164 if (!sv)
4165 return 1;
4166
4167 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004168 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004169 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004170 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004171 if (warning)
4172 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004173 }
4174 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004175 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004176 if (strcmp(args[4], "ready") == 0)
4177 srv_adm_set_ready(sv);
4178 else if (strcmp(args[4], "drain") == 0)
4179 srv_adm_set_drain(sv);
4180 else if (strcmp(args[4], "maint") == 0)
4181 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004182 else
4183 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004184 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004185 }
4186 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004187 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004188 if (sv->track)
4189 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004190 else if (strcmp(args[4], "up") == 0) {
4191 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004192 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004193 }
4194 else if (strcmp(args[4], "stopping") == 0) {
4195 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004196 srv_set_stopping(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004197 }
4198 else if (strcmp(args[4], "down") == 0) {
4199 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004200 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004201 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004202 else
4203 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004204 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004205 }
4206 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004207 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004208 if (!(sv->agent.state & CHK_ST_ENABLED))
4209 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004210 else if (strcmp(args[4], "up") == 0) {
4211 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004212 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004213 }
4214 else if (strcmp(args[4], "down") == 0) {
4215 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004216 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004217 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004218 else
4219 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004220 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004221 }
Misiek2da082d2017-01-09 09:40:42 +01004222 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004223 char *addr = NULL;
4224 char *port = NULL;
4225 if (strlen(args[4]) == 0) {
4226 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4227 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004228 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004229 }
4230 addr = args[4];
4231 if (strcmp(args[5], "port") == 0)
4232 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004233 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004234 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004235 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004236 if (warning)
4237 cli_msg(appctx, LOG_WARNING, warning);
4238 }
4239 else if (strcmp(args[3], "agent-port") == 0) {
4240 char *port = NULL;
4241 if (strlen(args[4]) == 0) {
4242 cli_err(appctx, "set server <b>/<s> agent-port requires"
4243 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004244 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004245 }
4246 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004247 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004248 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004249 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004250 if (warning)
4251 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004252 }
4253 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004254 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004255 if (!(sv->agent.state & CHK_ST_ENABLED))
4256 cli_err(appctx, "agent checks are not enabled on this server.\n");
4257 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004258 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004259 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004260 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004261 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004262 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004263 else if (strcmp(args[3], "check-addr") == 0) {
4264 char *addr = NULL;
4265 char *port = NULL;
4266 if (strlen(args[4]) == 0) {
4267 cli_err(appctx, "set server <b>/<s> check-addr requires"
4268 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004269 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004270 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004271 addr = args[4];
4272 if (strcmp(args[5], "port") == 0)
4273 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004274 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004275 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004276 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004277 if (warning)
4278 cli_msg(appctx, LOG_WARNING, warning);
4279 }
4280 else if (strcmp(args[3], "check-port") == 0) {
4281 char *port = NULL;
4282 if (strlen(args[4]) == 0) {
4283 cli_err(appctx, "set server <b>/<s> check-port requires"
4284 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004285 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004286 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004287 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004288 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004289 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004290 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004291 if (warning)
4292 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004293 }
4294 else if (strcmp(args[3], "addr") == 0) {
4295 char *addr = NULL;
4296 char *port = NULL;
4297 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004298 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004299 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004300 }
4301 else {
4302 addr = args[4];
4303 }
4304 if (strcmp(args[5], "port") == 0) {
4305 port = args[6];
4306 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004307 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004308 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004309 if (warning)
4310 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004311 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004312 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004313 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004314 else if (strcmp(args[3], "fqdn") == 0) {
4315 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004316 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004317 goto out;
4318 }
4319 if (!sv->resolvers) {
4320 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4321 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004322 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004323 if (sv->srvrq) {
4324 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004325 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004326 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004327 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4328 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004329 /* ensure runtime resolver will process this new fqdn */
4330 if (sv->flags & SRV_F_NO_RESOLUTION) {
4331 sv->flags &= ~SRV_F_NO_RESOLUTION;
4332 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004333 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004334 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004335 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004336 if (warning)
4337 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004338 }
William Dauchyf6370442020-11-14 19:25:33 +01004339 else if (strcmp(args[3], "ssl") == 0) {
4340#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004341 if (sv->flags & SRV_F_DYNAMIC) {
4342 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4343 goto out;
4344 }
4345
William Dauchyf6370442020-11-14 19:25:33 +01004346 if (sv->ssl_ctx.ctx == NULL) {
4347 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4348 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004349 goto out;
4350 }
4351
4352 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4353 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004354 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004355 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004356 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004357 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004358 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004359 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004360 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004361 }
4362 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004363 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004364 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4365#else
4366 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4367#endif
4368 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004369 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004370 "usage: set server <backend>/<server> "
4371 "addr | agent | agent-addr | agent-port | agent-send | "
4372 "check-addr | check-port | fqdn | health | ssl | "
4373 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004374 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004375 out:
William Lallemand222baf22016-11-19 02:00:33 +01004376 return 1;
4377}
4378
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004379static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004380{
William Lallemand6b160942016-11-22 12:34:35 +01004381 struct proxy *px;
4382 struct server *sv;
4383 char *line;
4384
4385
4386 /* split "backend/server" and make <line> point to server */
4387 for (line = args[2]; *line; line++)
4388 if (*line == '/') {
4389 *line++ = '\0';
4390 break;
4391 }
4392
Willy Tarreau9d008692019-08-09 11:21:01 +02004393 if (!*line)
4394 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004395
Willy Tarreau9d008692019-08-09 11:21:01 +02004396 if (!get_backend_server(args[2], line, &px, &sv))
4397 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004398
4399 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004400 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4401 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004402 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004403 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004404 return 1;
4405}
4406
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004407/* Parse a "set weight" command.
4408 *
4409 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004410 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004411static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004412{
4413 struct server *sv;
4414 const char *warning;
4415
4416 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4417 return 1;
4418
4419 sv = cli_find_server(appctx, args[2]);
4420 if (!sv)
4421 return 1;
4422
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004423 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4424
William Lallemand6b160942016-11-22 12:34:35 +01004425 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004426 if (warning)
4427 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004428
4429 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4430
William Lallemand6b160942016-11-22 12:34:35 +01004431 return 1;
4432}
4433
Willy Tarreau46b7f532018-08-21 11:54:26 +02004434/* parse a "set maxconn server" command. It always returns 1.
4435 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004436 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004437 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004438static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004439{
4440 struct server *sv;
4441 const char *warning;
4442
4443 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4444 return 1;
4445
4446 sv = cli_find_server(appctx, args[3]);
4447 if (!sv)
4448 return 1;
4449
Amaury Denoyelle02742862021-06-18 11:11:36 +02004450 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4451
Willy Tarreaub8026272016-11-23 11:26:56 +01004452 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004453 if (warning)
4454 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004455
Amaury Denoyelle02742862021-06-18 11:11:36 +02004456 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4457
Willy Tarreaub8026272016-11-23 11:26:56 +01004458 return 1;
4459}
William Lallemand6b160942016-11-22 12:34:35 +01004460
Willy Tarreau46b7f532018-08-21 11:54:26 +02004461/* parse a "disable agent" command. It always returns 1.
4462 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004463 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004464 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004465static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004466{
4467 struct server *sv;
4468
4469 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4470 return 1;
4471
4472 sv = cli_find_server(appctx, args[2]);
4473 if (!sv)
4474 return 1;
4475
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004476 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004477 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004478 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004479 return 1;
4480}
4481
Willy Tarreau46b7f532018-08-21 11:54:26 +02004482/* parse a "disable health" command. It always returns 1.
4483 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004484 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004485 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004486static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004487{
4488 struct server *sv;
4489
4490 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4491 return 1;
4492
4493 sv = cli_find_server(appctx, args[2]);
4494 if (!sv)
4495 return 1;
4496
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004497 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004498 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004499 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004500 return 1;
4501}
4502
Willy Tarreau46b7f532018-08-21 11:54:26 +02004503/* parse a "disable server" command. It always returns 1.
4504 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004505 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004506 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004507static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004508{
4509 struct server *sv;
4510
4511 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4512 return 1;
4513
4514 sv = cli_find_server(appctx, args[2]);
4515 if (!sv)
4516 return 1;
4517
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004518 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004519 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004520 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004521 return 1;
4522}
4523
Willy Tarreau46b7f532018-08-21 11:54:26 +02004524/* parse a "enable agent" command. It always returns 1.
4525 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004526 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004527 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004528static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004529{
4530 struct server *sv;
4531
4532 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4533 return 1;
4534
4535 sv = cli_find_server(appctx, args[2]);
4536 if (!sv)
4537 return 1;
4538
Willy Tarreau9d008692019-08-09 11:21:01 +02004539 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4540 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004541
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004542 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004543 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004544 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004545 return 1;
4546}
4547
Willy Tarreau46b7f532018-08-21 11:54:26 +02004548/* parse a "enable health" command. It always returns 1.
4549 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004550 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004551 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004552static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004553{
4554 struct server *sv;
4555
4556 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4557 return 1;
4558
4559 sv = cli_find_server(appctx, args[2]);
4560 if (!sv)
4561 return 1;
4562
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004563 if (!(sv->check.state & CHK_ST_CONFIGURED))
4564 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4565
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004566 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004567 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004568 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004569 return 1;
4570}
4571
Willy Tarreau46b7f532018-08-21 11:54:26 +02004572/* parse a "enable server" command. It always returns 1.
4573 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004574 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004575 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004576static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004577{
4578 struct server *sv;
4579
4580 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4581 return 1;
4582
4583 sv = cli_find_server(appctx, args[2]);
4584 if (!sv)
4585 return 1;
4586
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004587 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004588 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004589 if (!(sv->flags & SRV_F_COOKIESET)
4590 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4591 sv->cookie)
4592 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004593 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004594 return 1;
4595}
4596
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004597/* Allocates data structure related to load balancing for the server <sv>. It
4598 * is only required for dynamic servers.
4599 *
4600 * At the moment, the server lock is not used as this function is only called
4601 * for a dynamic server not yet registered.
4602 *
4603 * Returns 1 on success, 0 on allocation failure.
4604 */
4605static int srv_alloc_lb(struct server *sv, struct proxy *be)
4606{
4607 int node;
4608
4609 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4610 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4611 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4612 sv->lb_nodes_now = 0;
4613
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004614 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4615 ((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 +01004616 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4617
4618 if (!sv->lb_nodes)
4619 return 0;
4620
4621 for (node = 0; node < sv->lb_nodes_tot; node++) {
4622 sv->lb_nodes[node].server = sv;
4623 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4624 }
4625 }
4626
4627 return 1;
4628}
4629
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004630/* updates the server's weight during a warmup stage. Once the final weight is
4631 * reached, the task automatically stops. Note that any server status change
4632 * must have updated s->last_change accordingly.
4633 */
4634static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4635{
4636 struct server *s = context;
4637
4638 /* by default, plan on stopping the task */
4639 t->expire = TICK_ETERNITY;
4640 if ((s->next_admin & SRV_ADMF_MAINT) ||
4641 (s->next_state != SRV_ST_STARTING))
4642 return t;
4643
4644 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4645
4646 /* recalculate the weights and update the state */
4647 server_recalc_eweight(s, 1);
4648
4649 /* probably that we can refill this server with a bit more connections */
4650 pendconn_grab_from_px(s);
4651
4652 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4653
4654 /* get back there in 1 second or 1/20th of the slowstart interval,
4655 * whichever is greater, resulting in small 5% steps.
4656 */
4657 if (s->next_state == SRV_ST_STARTING)
4658 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4659 return t;
4660}
4661
4662/* Allocate the slowstart task if the server is configured with a slowstart
4663 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4664 *
4665 * Returns 0 on success else non-zero.
4666 */
4667static int init_srv_slowstart(struct server *srv)
4668{
4669 struct task *t;
4670
4671 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004672 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004673 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4674 return ERR_ALERT | ERR_FATAL;
4675 }
4676
4677 /* We need a warmup task that will be called when the server
4678 * state switches from down to up.
4679 */
4680 srv->warmup = t;
4681 t->process = server_warmup;
4682 t->context = srv;
4683
4684 /* server can be in this state only because of */
4685 if (srv->next_state == SRV_ST_STARTING) {
4686 task_schedule(srv->warmup,
4687 tick_add(now_ms,
Willy Tarreau69530f52023-04-28 09:16:15 +02004688 MS_TO_TICKS(MAX(1000, (ns_to_sec(now_ns) - srv->last_change)) / 20)));
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004689 }
4690 }
4691
4692 return ERR_NONE;
4693}
4694REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4695
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004696/* Memory allocation and initialization of the per_thr field.
4697 * Returns 0 if the field has been successfully initialized, -1 on failure.
4698 */
4699int srv_init_per_thr(struct server *srv)
4700{
4701 int i;
4702
4703 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004704 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4705 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004706 return -1;
4707
4708 for (i = 0; i < global.nbthread; i++) {
4709 srv->per_thr[i].idle_conns = EB_ROOT;
4710 srv->per_thr[i].safe_conns = EB_ROOT;
4711 srv->per_thr[i].avail_conns = EB_ROOT;
4712 MT_LIST_INIT(&srv->per_thr[i].streams);
4713 }
4714
4715 return 0;
4716}
4717
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004718/* Parse a "add server" command
4719 * Returns 0 if the server has been successfully initialized, 1 on failure.
4720 */
4721static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4722{
4723 struct proxy *be;
4724 struct server *srv;
4725 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004726 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004727 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004728 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4729
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004730 usermsgs_clr("CLI");
4731
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004732 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4733 return 1;
4734
4735 ++args;
4736
4737 sv_name = be_name = args[1];
4738 /* split backend/server arg */
4739 while (*sv_name && *(++sv_name)) {
4740 if (*sv_name == '/') {
4741 *sv_name = '\0';
4742 ++sv_name;
4743 break;
4744 }
4745 }
4746
4747 if (!*sv_name)
4748 return cli_err(appctx, "Require 'backend/server'.");
4749
Amaury Denoyellecece9182021-04-20 17:09:08 +02004750 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004751 if (!be)
4752 return cli_err(appctx, "No such backend.");
4753
4754 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004755 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004756 return 1;
4757 }
4758
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004759 /* At this point, some operations might not be thread-safe anymore. This
4760 * might be the case for parsing handlers which were designed to run
4761 * only at the starting stage on single-thread mode.
4762 *
4763 * Activate thread isolation to ensure thread-safety.
4764 */
4765 thread_isolate();
4766
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004767 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004768 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4769 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004770 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004771
4772 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004773 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004774
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004775 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004776 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004777 }
4778
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004779 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4780 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004781 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004782
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004783 /* A dynamic server does not currently support resolution.
4784 *
4785 * Initialize it explicitly to the "none" method to ensure no
4786 * resolution will ever be executed.
4787 */
4788 srv->init_addr_methods = SRV_IADDR_NONE;
4789
Amaury Denoyelle30467232021-03-12 18:03:27 +01004790 if (srv->mux_proto) {
4791 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004792 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004793 goto out;
4794 }
4795 }
4796
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004797 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004798 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004799 goto out;
4800 }
4801
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004802 if (srv->max_idle_conns != 0) {
4803 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4804 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004805 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004806 goto out;
4807 }
4808 }
4809
4810 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004811 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004812 goto out;
4813 }
4814
4815 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4816 COUNTERS_SV,
4817 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004818 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004819 goto out;
4820 }
4821
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004822 /* ensure minconn/maxconn consistency */
4823 srv_minmax_conn_apply(srv);
4824
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004825 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4826 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004827 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4828 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4829 goto out;
4830 }
4831 }
4832
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004833 if (srv->trackit) {
4834 if (srv_apply_track(srv, be))
4835 goto out;
4836 }
4837
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004838 /* Init check/agent if configured. The check is manually disabled
4839 * because a dynamic server is started in a disable state. It must be
4840 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004841 */
4842 if (srv->do_check) {
4843 if (init_srv_check(srv))
4844 goto out;
4845
4846 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004847 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004848
4849 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004850 if (init_srv_agent_check(srv))
4851 goto out;
4852
4853 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004854 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004855
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004856 /* Init slowstart if needed. */
4857 if (init_srv_slowstart(srv))
4858 goto out;
4859
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004860 /* Attach the server to the end of the proxy linked list. Note that this
4861 * operation is not thread-safe so this is executed under thread
4862 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004863 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004864 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004865 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004866
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004867 /* TODO use a double-linked list for px->srv */
4868 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004869 struct server *next = be->srv;
4870
4871 while (1) {
4872 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004873 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004874 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004875 goto out;
4876 }
4877
4878 if (!next->next)
4879 break;
4880
4881 next = next->next;
4882 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004883
4884 next->next = srv;
4885 }
4886 else {
4887 srv->next = be->srv;
4888 be->srv = srv;
4889 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004890
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004891 /* generate the server id if not manually specified */
4892 if (!srv->puid) {
4893 next_id = get_next_id(&be->conf.used_server_id, 1);
4894 if (!next_id) {
4895 ha_alert("Cannot attach server : no id left in proxy\n");
4896 goto out;
4897 }
4898
4899 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004900 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004901 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004902
4903 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004904 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4905 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004906 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4907 if (srv->addr_node.key)
4908 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004909
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004910 /* check if LSB bit (odd bit) is set for reuse_cnt */
4911 if (srv_id_reuse_cnt & 1) {
4912 /* cnt must be increased */
4913 srv_id_reuse_cnt++;
4914 }
4915 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4916 * save some space
4917 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
4918 */
4919 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
4920
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01004921 /* adding server cannot fail when we reach this:
4922 * publishing EVENT_HDL_SUB_SERVER_ADD
4923 */
4924 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
4925
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004926 thread_release();
4927
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004928 /* Start the check task. The server must be fully initialized.
4929 *
4930 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4931 * need to randomly spread the task interval for dynamic servers.
4932 */
4933 if (srv->check.state & CHK_ST_CONFIGURED) {
4934 if (!start_check_task(&srv->check, 0, 1, 1))
4935 ha_alert("System might be unstable, consider to execute a reload");
4936 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004937 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004938 if (!start_check_task(&srv->agent, 0, 1, 1))
4939 ha_alert("System might be unstable, consider to execute a reload");
4940 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004941
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004942 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004943 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004944
4945 return 0;
4946
4947out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004948 if (srv) {
4949 if (srv->track)
4950 release_server_track(srv);
4951
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004952 if (srv->check.state & CHK_ST_CONFIGURED)
4953 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004954 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004955 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004956
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004957 /* remove the server from the proxy linked list */
4958 if (be->srv == srv) {
4959 be->srv = srv->next;
4960 }
4961 else {
4962 struct server *prev;
4963 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4964 ;
4965 if (prev)
4966 prev->next = srv->next;
4967 }
4968
4969 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004970
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004971 thread_release();
4972
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004973 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004974 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004975
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004976 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004977 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004978
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004979 return 1;
4980}
4981
Amaury Denoyellee5580432021-04-15 14:41:20 +02004982/* Parse a "del server" command
4983 * Returns 0 if the server has been successfully initialized, 1 on failure.
4984 */
4985static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4986{
4987 struct proxy *be;
4988 struct server *srv;
4989 char *be_name, *sv_name;
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01004990 struct server *prev_del;
Amaury Denoyellee5580432021-04-15 14:41:20 +02004991
4992 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4993 return 1;
4994
4995 ++args;
4996
4997 sv_name = be_name = args[1];
4998 /* split backend/server arg */
4999 while (*sv_name && *(++sv_name)) {
5000 if (*sv_name == '/') {
5001 *sv_name = '\0';
5002 ++sv_name;
5003 break;
5004 }
5005 }
5006
5007 if (!*sv_name)
5008 return cli_err(appctx, "Require 'backend/server'.");
5009
5010 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005011 * requires thread isolation. In addition, any place referencing the
5012 * server about to be deleted would be unsafe after our operation, so
5013 * we must be certain to be alone so that no other thread has even
5014 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005015 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005016 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005017
5018 get_backend_server(be_name, sv_name, &be, &srv);
5019 if (!be) {
5020 cli_err(appctx, "No such backend.");
5021 goto out;
5022 }
5023
5024 if (!srv) {
5025 cli_err(appctx, "No such server.");
5026 goto out;
5027 }
5028
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005029 if (srv->flags & SRV_F_NON_PURGEABLE) {
5030 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 +02005031 goto out;
5032 }
5033
5034 /* Only servers in maintenance can be deleted. This ensures that the
5035 * server is not present anymore in the lb structures (through
5036 * lbprm.set_server_status_down).
5037 */
5038 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5039 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5040 goto out;
5041 }
5042
5043 /* Ensure that there is no active/idle/pending connection on the server.
5044 *
5045 * TODO idle connections should not prevent server deletion. A proper
5046 * cleanup function should be implemented to be used here.
5047 */
5048 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005049 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005050 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5051 goto out;
5052 }
5053
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005054 /* removing cannot fail anymore when we reach this:
5055 * publishing EVENT_HDL_SUB_SERVER_DEL
5056 */
5057 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5058
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005059 /* remove srv from tracking list */
5060 if (srv->track)
5061 release_server_track(srv);
5062
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005063 /* stop the check task if running */
5064 if (srv->check.state & CHK_ST_CONFIGURED)
5065 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005066 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005067 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005068
5069 /* detach the server from the proxy linked list
5070 * The proxy servers list is currently not protected by a lock, so this
5071 * requires thread_isolate/release.
5072 */
5073
5074 /* be->srv cannot be empty since we have already found the server with
5075 * get_backend_server */
5076 BUG_ON(!be->srv);
5077 if (be->srv == srv) {
5078 be->srv = srv->next;
5079 }
5080 else {
5081 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005082 for (next = be->srv; srv != next->next; next = next->next) {
5083 /* srv cannot be not found since we have already found
5084 * it with get_backend_server */
5085 BUG_ON(!next);
5086 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005087
Amaury Denoyellee5580432021-04-15 14:41:20 +02005088 next->next = srv->next;
5089 }
5090
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005091 /* Some deleted servers could still point to us using their 'next',
5092 * update them as needed
5093 * Please note the small race between the POP and APPEND, although in
5094 * this situation this is not an issue as we are under full thread
5095 * isolation
5096 */
5097 while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) {
5098 /* update its 'next' ptr */
5099 prev_del->next = srv->next;
5100 if (srv->next) {
5101 /* now it is our 'next' responsibility */
5102 MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted);
5103 }
5104 }
5105
5106 /* we ourselves need to inform our 'next' that we will still point it */
5107 if (srv->next)
5108 MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted);
5109
Amaury Denoyellee5580432021-04-15 14:41:20 +02005110 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005111 eb32_delete(&srv->conf.id);
5112 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005113 if (srv->addr_node.key)
5114 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005115
5116 /* remove srv from idle_node tree for idle conn cleanup */
5117 eb32_delete(&srv->idle_node);
5118
Aurelien DARRAGON75b9d1c2023-01-24 14:40:01 +01005119 /* flag the server as deleted
5120 * (despite the server being removed from primary server list,
5121 * one could still access the server data from a valid ptr)
5122 * Deleted flag helps detecting when a server is in transient removal
5123 * state.
5124 * ie: removed from the list but not yet freed/purged from memory.
5125 */
5126 srv->flags |= SRV_F_DELETED;
5127
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005128 /* set LSB bit (odd bit) for reuse_cnt */
5129 srv_id_reuse_cnt |= 1;
5130
Amaury Denoyellee5580432021-04-15 14:41:20 +02005131 thread_release();
5132
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005133 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005134 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005135
5136 cli_msg(appctx, LOG_INFO, "Server deleted.");
5137
5138 return 0;
5139
5140out:
5141 thread_release();
5142
5143 return 1;
5144}
5145
William Lallemand222baf22016-11-19 02:00:33 +01005146/* register cli keywords */
5147static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005148 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5149 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005150 { { "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 +02005151 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5152 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005153 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5154 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5155 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5156 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5157 { { "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 +01005158 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5159 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005160 {{},}
5161}};
5162
Willy Tarreau0108d902018-11-25 19:14:37 +01005163INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005164
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005165/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5166 * field is used to retrieve the identifier of the tracked server, either with
5167 * the format "proxy/server" or just "server". <curproxy> must point to the
5168 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5169 * to find the tracked server.
5170 *
5171 * Returns 0 if the server track has been activated else non-zero.
5172 *
5173 * Not thread-safe.
5174 */
5175int srv_apply_track(struct server *srv, struct proxy *curproxy)
5176{
5177 struct proxy *px;
5178 struct server *strack, *loop;
5179 char *pname, *sname;
5180
5181 if (!srv->trackit)
5182 return 1;
5183
5184 pname = srv->trackit;
5185 sname = strrchr(pname, '/');
5186
5187 if (sname) {
5188 *sname++ = '\0';
5189 }
5190 else {
5191 sname = pname;
5192 pname = NULL;
5193 }
5194
5195 if (pname) {
5196 px = proxy_be_by_name(pname);
5197 if (!px) {
5198 ha_alert("unable to find required proxy '%s' for tracking.\n",
5199 pname);
5200 return 1;
5201 }
5202 }
5203 else {
5204 px = curproxy;
5205 }
5206
5207 strack = findserver(px, sname);
5208 if (!strack) {
5209 ha_alert("unable to find required server '%s' for tracking.\n",
5210 sname);
5211 return 1;
5212 }
5213
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005214 if (strack->flags & SRV_F_DYNAMIC) {
5215 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5216 px->id, strack->id);
5217 return 1;
5218 }
5219
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005220 if (!strack->do_check && !strack->do_agent && !strack->track &&
5221 !strack->trackit) {
5222 ha_alert("unable to use %s/%s for "
5223 "tracking as it does not have any check nor agent enabled.\n",
5224 px->id, strack->id);
5225 return 1;
5226 }
5227
5228 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5229 ;
5230
5231 if (srv == strack || loop) {
5232 ha_alert("unable to track %s/%s as it "
5233 "belongs to a tracking chain looping back to %s/%s.\n",
5234 px->id, strack->id, px->id,
5235 srv == strack ? strack->id : loop->id);
5236 return 1;
5237 }
5238
5239 if (curproxy != px &&
5240 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5241 ha_alert("unable to use %s/%s for"
5242 "tracking: disable-on-404 option inconsistency.\n",
5243 px->id, strack->id);
5244 return 1;
5245 }
5246
5247 srv->track = strack;
5248 srv->tracknext = strack->trackers;
5249 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005250 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005251
5252 ha_free(&srv->trackit);
5253
5254 return 0;
5255}
5256
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005257/* This function propagates srv state change to lb algorithms */
5258static void srv_lb_propagate(struct server *s)
5259{
5260 struct proxy *px = s->proxy;
5261
5262 if (px->lbprm.update_server_eweight)
5263 px->lbprm.update_server_eweight(s);
5264 else if (srv_willbe_usable(s)) {
5265 if (px->lbprm.set_server_status_up)
5266 px->lbprm.set_server_status_up(s);
5267 }
5268 else {
5269 if (px->lbprm.set_server_status_down)
5270 px->lbprm.set_server_status_down(s);
5271 }
5272}
5273
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005274/* directly update server state based on an operational change
5275 * (compare current and next state to know which transition to apply)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005276 *
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005277 * The function returns the number of requeued sessions (either taken by
5278 * the server or redispatched to others servers) due to the server state
5279 * change.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005280 */
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005281static int _srv_update_status_op(struct server *s, enum srv_op_st_chg_cause cause)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005282{
Willy Tarreau83061a82018-07-13 11:56:34 +02005283 struct buffer *tmptrash = NULL;
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005284 int log_level;
5285 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5286 int xferred = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005287
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005288 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5289 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005290
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005291 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5292 srv_shutdown_streams(s, SF_ERR_DOWN);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005293
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005294 /* we might have streams queued on this server and waiting for
5295 * a connection. Those which are redispatchable will be queued
5296 * to another server or to the proxy itself.
5297 */
5298 xferred = pendconn_redistribute(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005299
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005300 /* no maintenance + server DOWN: publish event SERVER DOWN */
5301 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005302
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005303 tmptrash = alloc_trash_chunk();
5304 if (tmptrash) {
5305 chunk_printf(tmptrash,
5306 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5307 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005308
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005309 srv_append_op_chg_cause(tmptrash, s, cause);
5310 srv_append_more(tmptrash, s, xferred, 0);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005311
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005312 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005313
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005314 /* we don't send an alert if the server was previously paused */
5315 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
5316 send_log(s->proxy, log_level, "%s.\n",
5317 tmptrash->area);
5318 send_email_alert(s, log_level, "%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005319 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005320 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005321 }
5322 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5323 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005324
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005325 /* we might have streams queued on this server and waiting for
5326 * a connection. Those which are redispatchable will be queued
5327 * to another server or to the proxy itself.
5328 */
5329 xferred = pendconn_redistribute(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005330
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005331 tmptrash = alloc_trash_chunk();
5332 if (tmptrash) {
5333 chunk_printf(tmptrash,
5334 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5335 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005336
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005337 srv_append_op_chg_cause(tmptrash, s, cause);
5338 srv_append_more(tmptrash, s, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005339
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005340 ha_warning("%s.\n", tmptrash->area);
5341 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5342 tmptrash->area);
5343 free_trash_chunk(tmptrash);
5344 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005345 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005346 }
5347 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5348 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005349
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005350 if (s->next_state == SRV_ST_STARTING && s->warmup)
5351 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005352
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005353 server_recalc_eweight(s, 0);
5354 /* now propagate the status change to any LB algorithms */
5355 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005356
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005357 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5358 * and it's not a backup server and its effective weight is > 0,
5359 * then it can accept new connections, so we shut down all streams
5360 * on all backup servers.
5361 */
5362 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5363 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5364 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005365
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005366 /* check if we can handle some connections queued at the proxy. We
5367 * will take as many as we can handle.
5368 */
5369 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005370
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005371 /* no maintenance + server going UP: publish event SERVER UP */
5372 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005373
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005374 tmptrash = alloc_trash_chunk();
5375 if (tmptrash) {
5376 chunk_printf(tmptrash,
5377 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5378 s->proxy->id, s->id);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005379
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005380 srv_append_op_chg_cause(tmptrash, s, cause);
5381 srv_append_more(tmptrash, s, xferred, 0);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005382
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005383 ha_warning("%s.\n", tmptrash->area);
5384 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5385 tmptrash->area);
5386 send_email_alert(s, LOG_NOTICE, "%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005387 tmptrash->area);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005388 free_trash_chunk(tmptrash);
5389 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005390 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005391 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005392 else if (s->cur_eweight != s->next_eweight) {
5393 /* now propagate the status change to any LB algorithms */
5394 srv_lb_propagate(s);
5395 }
5396 return xferred;
5397}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005398
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005399/* deduct and update server state from an administrative change
5400 * (use current and next admin to deduct the administrative transition that
5401 * may result in server state update)
5402 *
5403 * The function returns the number of requeued sessions (either taken by
5404 * the server or redispatched to others servers) due to the server state
5405 * change.
5406 */
5407static int _srv_update_status_adm(struct server *s, enum srv_adm_st_chg_cause cause)
5408{
5409 struct buffer *tmptrash = NULL;
5410 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5411 int xferred = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005412
5413 /* Maintenance must also disable health checks */
5414 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5415 if (s->check.state & CHK_ST_ENABLED) {
5416 s->check.state |= CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005417 s->check.health = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005418 }
5419
5420 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005421 tmptrash = alloc_trash_chunk();
5422 if (tmptrash) {
5423 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005424 "%sServer %s/%s was DOWN and now enters maintenance",
5425 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005426 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005427 srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005428
Olivier Houchard796a2b32017-10-24 17:42:47 +02005429 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005430 ha_warning("%s.\n", tmptrash->area);
5431 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5432 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005433 }
5434 free_trash_chunk(tmptrash);
5435 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005436 }
5437 }
5438 else { /* server was still running */
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005439 s->check.health = 0; /* failure */
Emeric Brune3114802017-12-21 14:42:26 +01005440
5441 s->next_state = SRV_ST_STOPPED;
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005442 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005443
Emeric Brun64cc49c2017-10-03 14:46:45 +02005444 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5445 srv_shutdown_streams(s, SF_ERR_DOWN);
5446
William Dauchy6318d332020-05-02 21:52:36 +02005447 /* force connection cleanup on the given server */
5448 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005449 /* we might have streams queued on this server and waiting for
5450 * a connection. Those which are redispatchable will be queued
5451 * to another server or to the proxy itself.
5452 */
5453 xferred = pendconn_redistribute(s);
5454
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005455 /* maintenance on previously running server: publish event SERVER DOWN */
5456 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5457
Emeric Brun64cc49c2017-10-03 14:46:45 +02005458 tmptrash = alloc_trash_chunk();
5459 if (tmptrash) {
5460 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005461 "%sServer %s/%s is going DOWN for maintenance",
Emeric Brun64cc49c2017-10-03 14:46:45 +02005462 s->flags & SRV_F_BACKUP ? "Backup " : "",
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005463 s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005464 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005465 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005466
5467 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005468 ha_warning("%s.\n", tmptrash->area);
5469 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5470 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005471 }
5472 free_trash_chunk(tmptrash);
5473 tmptrash = NULL;
5474 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005475 }
5476 }
5477 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5478 /* OK here we're leaving maintenance, we have many things to check,
5479 * because the server might possibly be coming back up depending on
5480 * its state. In practice, leaving maintenance means that we should
5481 * immediately turn to UP (more or less the slowstart) under the
5482 * following conditions :
5483 * - server is neither checked nor tracked
5484 * - server tracks another server which is not checked
5485 * - server tracks another server which is already up
5486 * Which sums up as something simpler :
5487 * "either the tracking server is up or the server's checks are disabled
5488 * or up". Otherwise we only re-enable health checks. There's a special
5489 * case associated to the stopping state which can be inherited. Note
5490 * that the server might still be in drain mode, which is naturally dealt
5491 * with by the lower level functions.
5492 */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005493 if (s->check.state & CHK_ST_ENABLED) {
5494 s->check.state &= ~CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005495 s->check.health = s->check.rise; /* start OK but check immediately */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005496 }
5497
5498 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5499 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5500 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5501 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5502 s->next_state = SRV_ST_STOPPING;
5503 }
5504 else {
5505 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005506 if (s->slowstart > 0) {
5507 if (s->warmup)
5508 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5509 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005510 else
5511 s->next_state = SRV_ST_RUNNING;
5512 }
5513
5514 }
5515
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005516 /* ignore if server stays down when leaving maintenance mode */
5517 if (s->next_state != SRV_ST_STOPPED) {
5518 /* leaving maintenance + server UP: publish event SERVER UP */
5519 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5520 }
5521
Emeric Brun64cc49c2017-10-03 14:46:45 +02005522 tmptrash = alloc_trash_chunk();
5523 if (tmptrash) {
5524 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5525 chunk_printf(tmptrash,
5526 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5527 s->flags & SRV_F_BACKUP ? "Backup " : "",
5528 s->proxy->id, s->id,
5529 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5530 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5531 }
5532 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5533 chunk_printf(tmptrash,
5534 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5535 s->flags & SRV_F_BACKUP ? "Backup " : "",
5536 s->proxy->id, s->id, s->hostname,
5537 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5538 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5539 }
5540 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5541 chunk_printf(tmptrash,
5542 "%sServer %s/%s is %s/%s (leaving maintenance)",
5543 s->flags & SRV_F_BACKUP ? "Backup " : "",
5544 s->proxy->id, s->id,
5545 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5546 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5547 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005548 ha_warning("%s.\n", tmptrash->area);
5549 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5550 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005551 free_trash_chunk(tmptrash);
5552 tmptrash = NULL;
5553 }
5554
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005555 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005556 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005557 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005558
Willy Tarreau6a78e612018-08-07 10:14:53 +02005559 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5560 * and it's not a backup server and its effective weight is > 0,
5561 * then it can accept new connections, so we shut down all streams
5562 * on all backup servers.
5563 */
5564 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5565 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5566 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5567
5568 /* check if we can handle some connections queued at the proxy. We
5569 * will take as many as we can handle.
5570 */
5571 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005572 }
5573 else if (s->next_admin & SRV_ADMF_MAINT) {
5574 /* remaining in maintenance mode, let's inform precisely about the
5575 * situation.
5576 */
5577 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5578 tmptrash = alloc_trash_chunk();
5579 if (tmptrash) {
5580 chunk_printf(tmptrash,
5581 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5582 s->flags & SRV_F_BACKUP ? "Backup " : "",
5583 s->proxy->id, s->id);
5584
5585 if (s->track) /* normally it's mandatory here */
5586 chunk_appendf(tmptrash, " via %s/%s",
5587 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005588 ha_warning("%s.\n", tmptrash->area);
5589 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5590 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005591 free_trash_chunk(tmptrash);
5592 tmptrash = NULL;
5593 }
5594 }
5595 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5596 tmptrash = alloc_trash_chunk();
5597 if (tmptrash) {
5598 chunk_printf(tmptrash,
5599 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5600 s->flags & SRV_F_BACKUP ? "Backup " : "",
5601 s->proxy->id, s->id, s->hostname);
5602
5603 if (s->track) /* normally it's mandatory here */
5604 chunk_appendf(tmptrash, " via %s/%s",
5605 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005606 ha_warning("%s.\n", tmptrash->area);
5607 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5608 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005609 free_trash_chunk(tmptrash);
5610 tmptrash = NULL;
5611 }
5612 }
5613 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5614 tmptrash = alloc_trash_chunk();
5615 if (tmptrash) {
5616 chunk_printf(tmptrash,
5617 "%sServer %s/%s remains in forced maintenance",
5618 s->flags & SRV_F_BACKUP ? "Backup " : "",
5619 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005620 ha_warning("%s.\n", tmptrash->area);
5621 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5622 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005623 free_trash_chunk(tmptrash);
5624 tmptrash = NULL;
5625 }
5626 }
5627 /* don't report anything when leaving drain mode and remaining in maintenance */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005628 }
5629
5630 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5631 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5632 /* drain state is applied only if not yet in maint */
5633
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005634 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005635
5636 /* we might have streams queued on this server and waiting for
5637 * a connection. Those which are redispatchable will be queued
5638 * to another server or to the proxy itself.
5639 */
5640 xferred = pendconn_redistribute(s);
5641
5642 tmptrash = alloc_trash_chunk();
5643 if (tmptrash) {
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005644 chunk_printf(tmptrash, "%sServer %s/%s enters drain state",
5645 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005646 srv_append_adm_chg_cause(tmptrash, s, cause);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005647 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005648
5649 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005650 ha_warning("%s.\n", tmptrash->area);
5651 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5652 tmptrash->area);
5653 send_email_alert(s, LOG_NOTICE, "%s",
5654 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005655 }
5656 free_trash_chunk(tmptrash);
5657 tmptrash = NULL;
5658 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005659 }
5660 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5661 /* OK completely leaving drain mode */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005662 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005663
5664 tmptrash = alloc_trash_chunk();
5665 if (tmptrash) {
Aurelien DARRAGON9a1df022023-03-27 10:17:31 +02005666 if (s->cur_admin & SRV_ADMF_FDRAIN) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005667 chunk_printf(tmptrash,
5668 "%sServer %s/%s is %s (leaving forced drain)",
5669 s->flags & SRV_F_BACKUP ? "Backup " : "",
5670 s->proxy->id, s->id,
5671 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5672 }
5673 else {
5674 chunk_printf(tmptrash,
5675 "%sServer %s/%s is %s (leaving drain)",
5676 s->flags & SRV_F_BACKUP ? "Backup " : "",
5677 s->proxy->id, s->id,
5678 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5679 if (s->track) /* normally it's mandatory here */
5680 chunk_appendf(tmptrash, " via %s/%s",
5681 s->track->proxy->id, s->track->id);
5682 }
5683
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005684 ha_warning("%s.\n", tmptrash->area);
5685 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5686 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005687 free_trash_chunk(tmptrash);
5688 tmptrash = NULL;
5689 }
5690
5691 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005692 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005693 }
5694 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5695 /* remaining in drain mode after removing one of its flags */
5696
5697 tmptrash = alloc_trash_chunk();
5698 if (tmptrash) {
5699 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5700 chunk_printf(tmptrash,
5701 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5702 s->flags & SRV_F_BACKUP ? "Backup " : "",
5703 s->proxy->id, s->id);
5704
5705 if (s->track) /* normally it's mandatory here */
5706 chunk_appendf(tmptrash, " via %s/%s",
5707 s->track->proxy->id, s->track->id);
5708 }
5709 else {
5710 chunk_printf(tmptrash,
5711 "%sServer %s/%s remains in forced drain mode",
5712 s->flags & SRV_F_BACKUP ? "Backup " : "",
5713 s->proxy->id, s->id);
5714 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005715 ha_warning("%s.\n", tmptrash->area);
5716 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5717 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005718 free_trash_chunk(tmptrash);
5719 tmptrash = NULL;
5720 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005721 }
5722 }
Aurelien DARRAGONf71e0642023-04-19 16:19:58 +02005723 return xferred;
5724}
5725
5726/*
5727 * This function applies server's status changes.
5728 *
5729 * Must be called with the server lock held. This may also be called at init
5730 * time as the result of parsing the state file, in which case no lock will be
5731 * held, and the server's warmup task can be null.
5732 * <type> should be 0 for operational and 1 for administrative
5733 * <cause> must be srv_op_st_chg_cause enum for operational and
5734 * srv_adm_st_chg_cause enum for administrative
5735 */
5736static void srv_update_status(struct server *s, int type, int cause)
5737{
5738 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5739 enum srv_state srv_prev_state = s->cur_state;
5740
5741 if (type)
5742 _srv_update_status_adm(s, cause);
5743 else
5744 _srv_update_status_op(s, cause);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005745
Aurelien DARRAGON22151c72023-04-17 17:45:58 +02005746 /* explicitly commit state changes (even if it was already applied implicitly
5747 * by some lb state change function), so we don't miss anything
5748 */
5749 srv_lb_commit_status(s);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005750
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005751 /* check if server stats must be updated due the the server state change */
5752 if (srv_prev_state != s->cur_state) {
5753 if (srv_prev_state == SRV_ST_STOPPED) {
5754 /* server was down and no longer is */
Willy Tarreau69530f52023-04-28 09:16:15 +02005755 if (s->last_change < ns_to_sec(now_ns)) // ignore negative times
5756 s->down_time += ns_to_sec(now_ns) - s->last_change;
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005757 }
Aurelien DARRAGON5f80f8b2023-04-19 18:22:21 +02005758 else if (s->cur_state == SRV_ST_STOPPED) {
5759 /* server was up and is currently down */
5760 s->counters.down_trans++;
5761 }
Willy Tarreau69530f52023-04-28 09:16:15 +02005762 s->last_change = ns_to_sec(now_ns);
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005763 }
5764
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005765 /* check if backend stats must be updated due to the server state change */
5766 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5767 set_backend_down(s->proxy); /* backend going down */
5768 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) {
5769 /* backend was down and is back up again:
5770 * no helper function, updating last_change and backend downtime stats
5771 */
Willy Tarreau69530f52023-04-28 09:16:15 +02005772 if (s->proxy->last_change < ns_to_sec(now_ns)) // ignore negative times
5773 s->proxy->down_time += ns_to_sec(now_ns) - s->proxy->last_change;
5774 s->proxy->last_change = ns_to_sec(now_ns);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005775 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005776}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005777
Willy Tarreau144f84a2021-03-02 16:09:26 +01005778struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005779{
5780 struct connection *conn;
5781
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005782 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005783 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005784 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005785 }
5786
5787 return task;
5788}
5789
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005790/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005791 * moving them all.
5792 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005793 *
5794 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005795 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005796static 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 +02005797{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005798 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005799 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005800 int i = 0;
5801
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005802 node = eb_first(idle_tree);
5803 while (node) {
5804 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005805 if (toremove_nb != -1 && i >= toremove_nb)
5806 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005807
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005808 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005809 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005810 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005811 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005812
5813 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005814 }
5815 return i;
5816}
William Dauchy6318d332020-05-02 21:52:36 +02005817/* cleanup connections for a given server
5818 * might be useful when going on forced maintenance or live changing ip/port
5819 */
William Dauchy707ad322020-05-04 13:52:40 +02005820static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005821{
William Dauchy6318d332020-05-02 21:52:36 +02005822 int did_remove;
5823 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005824
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005825 /* nothing to do if pool-max-conn is null */
5826 if (!srv->max_idle_conns)
5827 return;
5828
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005829 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005830 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005831 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005832 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005833 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 +02005834 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005835 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 +02005836 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005837 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005838 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005839 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005840
5841 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5842 break;
William Dauchy6318d332020-05-02 21:52:36 +02005843 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005844}
5845
5846/* removes an idle conn after updating the server idle conns counters */
5847void srv_release_conn(struct server *srv, struct connection *conn)
5848{
5849 if (conn->flags & CO_FL_LIST_MASK) {
5850 /* The connection is currently in the server's idle list, so tell it
5851 * there's one less connection available in that list.
5852 */
5853 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5854 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5855 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5856 }
5857 else {
5858 /* The connection is not private and not in any server's idle
5859 * list, so decrement the current number of used connections
5860 */
5861 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5862 }
5863
5864 /* Remove the connection from any tree (safe, idle or available) */
5865 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5866 conn_delete_from_tree(&conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01005867 conn->flags &= ~CO_FL_LIST_MASK;
Willy Tarreau260f3242021-10-06 18:30:04 +02005868 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5869}
5870
5871/* retrieve a connection from its <hash> in <tree>
5872 * returns NULL if no connection found
5873 */
5874struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5875{
Willy Tarreau85223482022-09-29 20:32:43 +02005876 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005877 struct connection *conn = NULL;
5878 struct conn_hash_node *hash_node = NULL;
5879
Willy Tarreau85223482022-09-29 20:32:43 +02005880 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005881 if (node) {
5882 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5883 conn = hash_node->conn;
5884 }
5885
5886 return conn;
5887}
5888
5889/* retrieve the next connection sharing the same hash as <conn>
5890 * returns NULL if no connection found
5891 */
5892struct connection *srv_lookup_conn_next(struct connection *conn)
5893{
Willy Tarreau85223482022-09-29 20:32:43 +02005894 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005895 struct connection *next_conn = NULL;
5896 struct conn_hash_node *hash_node = NULL;
5897
Willy Tarreau85223482022-09-29 20:32:43 +02005898 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005899 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005900 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005901 next_conn = hash_node->conn;
5902 }
5903
5904 return next_conn;
5905}
5906
5907/* This adds an idle connection to the server's list if the connection is
5908 * reusable, not held by any owner anymore, but still has available streams.
5909 */
5910int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5911{
5912 /* we try to keep the connection in the server's idle list
5913 * if we don't have too many FD in use, and if the number of
5914 * idle+current conns is lower than what was observed before
5915 * last purge, or if we already don't have idle conns for the
5916 * current thread and we don't exceed last count by global.nbthread.
5917 */
5918 if (!(conn->flags & CO_FL_PRIVATE) &&
5919 srv && srv->pool_purge_delay > 0 &&
5920 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
5921 ha_used_fds < global.tune.pool_high_count &&
5922 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
5923 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
5924 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
5925 (ha_used_fds < global.tune.pool_low_count &&
5926 (srv->curr_used_conns + srv->curr_idle_conns <=
5927 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
5928 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
5929 int retadd;
5930
5931 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
5932 if (retadd > srv->max_idle_conns) {
5933 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5934 return 0;
5935 }
5936 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5937
5938 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5939 conn_delete_from_tree(&conn->hash_node->node);
5940
5941 if (is_safe) {
5942 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005943 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005944 _HA_ATOMIC_INC(&srv->curr_safe_nb);
5945 } else {
5946 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005947 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005948 _HA_ATOMIC_INC(&srv->curr_idle_nb);
5949 }
5950 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5951 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
5952
5953 __ha_barrier_full();
5954 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5955 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5956 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5957 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5958 now_ms);
5959 eb32_insert(&idle_conn_srv, &srv->idle_node);
5960 if (!task_in_wq(idle_conn_task) && !
5961 task_in_rq(idle_conn_task)) {
5962 task_schedule(idle_conn_task,
5963 srv->idle_node.key);
5964 }
5965
5966 }
5967 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5968 }
5969 return 1;
5970 }
5971 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02005972}
5973
Willy Tarreau144f84a2021-03-02 16:09:26 +01005974struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005975{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005976 struct server *srv;
5977 struct eb32_node *eb;
5978 int i;
5979 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005980
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005981 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005982 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5983 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005984 int exceed_conns;
5985 int to_kill;
5986 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005987
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005988 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5989 if (!eb) {
5990 /* we might have reached the end of the tree, typically because
5991 * <now_ms> is in the first half and we're first scanning the last
5992 * half. Let's loop back to the beginning of the tree now.
5993 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005994
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005995 eb = eb32_first(&idle_conn_srv);
5996 if (likely(!eb))
5997 break;
5998 }
5999 if (tick_is_lt(now_ms, eb->key)) {
6000 /* timer not expired yet, revisit it later */
6001 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006002 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006003 }
6004 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006005
6006 /* Calculate how many idle connections we want to kill :
6007 * we want to remove half the difference between the total
6008 * of established connections (used or idle) and the max
6009 * number of used connections.
6010 */
6011 curr_idle = srv->curr_idle_conns;
6012 if (curr_idle == 0)
6013 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006014 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006015 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006016
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006017 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006018 if (srv->est_need_conns < srv->max_used_conns)
6019 srv->est_need_conns = srv->max_used_conns;
6020
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006021 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006022
Willy Tarreau18ed7892020-07-02 19:05:30 +02006023 if (exceed_conns <= 0)
6024 goto remove;
6025
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006026 /* check all threads starting with ours */
6027 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006028 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006029 int j;
6030 int did_remove = 0;
6031
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006032 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6033 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006034
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006035 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006036 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 +02006037 if (j > 0)
6038 did_remove = 1;
6039 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006040 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 +01006041 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006042 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006043
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006044 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006045 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006046
6047 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6048 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006049 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006050remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006051 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006052
6053 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006054 /* There are still more idle connections, add the
6055 * server back in the tree.
6056 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006057 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006058 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006059 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006060 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006061 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006062 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6063
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006064 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006065 return task;
6066}
Olivier Houchard88698d92019-04-16 19:07:22 +02006067
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006068/* Close remaining idle connections. This functions is designed to be run on
6069 * process shutdown. This guarantees a proper socket shutdown to avoid
6070 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6071 * bypassed.
6072 *
6073 * This function is not thread-safe so it must only be called via a global
6074 * deinit function.
6075 */
6076static void srv_close_idle_conns(struct server *srv)
6077{
6078 struct eb_root **cleaned_tree;
6079 int i;
6080
6081 for (i = 0; i < global.nbthread; ++i) {
6082 struct eb_root *conn_trees[] = {
6083 &srv->per_thr[i].idle_conns,
6084 &srv->per_thr[i].safe_conns,
6085 &srv->per_thr[i].avail_conns,
6086 NULL
6087 };
6088
6089 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6090 while (!eb_is_empty(*cleaned_tree)) {
6091 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6092 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6093 struct connection *conn = conn_hash_node->conn;
6094
6095 if (conn->ctrl->ctrl_close)
6096 conn->ctrl->ctrl_close(conn);
6097 ebmb_delete(node);
6098 }
6099 }
6100 }
6101}
6102
6103REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6104
Willy Tarreau76cc6992020-07-01 18:49:24 +02006105/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6106static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006107 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006108 char **err)
6109{
6110 if (too_many_args(1, args, err, NULL))
6111 return -1;
6112
6113 if (strcmp(args[1], "on") == 0)
6114 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6115 else if (strcmp(args[1], "off") == 0)
6116 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6117 else {
6118 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6119 return -1;
6120 }
6121 return 0;
6122}
6123
Olivier Houchard88698d92019-04-16 19:07:22 +02006124/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6125static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006126 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006127 char **err)
6128{
6129 int arg = -1;
6130
6131 if (too_many_args(1, args, err, NULL))
6132 return -1;
6133
6134 if (*(args[1]) != 0)
6135 arg = atoi(args[1]);
6136
6137 if (arg < 0 || arg > 100) {
6138 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6139 return -1;
6140 }
6141
6142 if (args[0][10] == 'h')
6143 global.tune.pool_high_ratio = arg;
6144 else
6145 global.tune.pool_low_ratio = arg;
6146 return 0;
6147}
6148
6149/* config keyword parsers */
6150static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006151 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006152 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6153 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6154 { 0, NULL, NULL }
6155}};
6156
6157INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6158
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006159/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006160 * Local variables:
6161 * c-indent-level: 8
6162 * c-basic-offset: 8
6163 * End:
6164 */