blob: ab7350942249439a627306f6758cc69a4b408ffc [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
Willy Tarreau3ff577e2018-08-02 11:48:52 +020053static void srv_update_status(struct server *s);
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{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +0100141 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200142 return s->down_time;
143
144 return now.tv_sec - s->last_change + s->down_time;
145}
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)
150 return now.tv_sec - s->counters.last_sess;
151
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
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100169/*
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100170 * Use this to publish EVENT_HDL_SUB_SERVER family type event
171 * from srv facility
172 * Event will be published in both global subscription list and
173 * server dedicated subscription list
174 * server ptr must be valid
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100175 * must be called with srv lock or under thread_isolate
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100176 */
177static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate)
178{
179 struct event_hdl_cb_data_server cb_data;
180
181 /* safe data assignments */
182 cb_data.safe.puid = srv->puid;
183 cb_data.safe.rid = srv->rid;
184 cb_data.safe.flags = srv->flags;
185 snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id);
Aurelien DARRAGONd7142132023-03-22 17:35:47 +0100186 cb_data.safe.proxy_name[0] = '\0';
187 cb_data.safe.proxy_uuid = -1; /* default value */
188 if (srv->proxy) {
189 cb_data.safe.proxy_uuid = srv->proxy->uuid;
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100190 snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id);
Aurelien DARRAGONd7142132023-03-22 17:35:47 +0100191 }
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100192 /* unsafe data assignments */
193 cb_data.unsafe.ptr = srv;
194 cb_data.unsafe.thread_isolate = thread_isolate;
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100195 cb_data.unsafe.srv_lock = !thread_isolate;
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100196 /* publish in server dedicated sub list */
197 event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data));
198 /* publish in global subscription list */
199 event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data));
200}
201
202/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100203 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200204 * Unlikely, but it can happen. The server's proxy must be at least
205 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100206 */
207static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100208{
209 struct proxy *p = s->proxy;
210 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100211
212 for (tmpserv = p->srv; tmpserv != NULL;
213 tmpserv = tmpserv->next) {
214 if (tmpserv == s)
215 continue;
216 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
217 continue;
218 if (tmpserv->cookie &&
219 strcmp(tmpserv->cookie, s->cookie) == 0) {
220 ha_warning("We generated two equal cookies for two different servers.\n"
221 "Please change the secret key for '%s'.\n",
222 s->proxy->id);
223 }
224 }
225
226}
227
Willy Tarreau46b7f532018-08-21 11:54:26 +0200228/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200229 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200230 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100231void srv_set_dyncookie(struct server *s)
232{
233 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100234 char *tmpbuf;
235 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100236 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100237 size_t buffer_len;
238 int addr_len;
239 int port;
240
Willy Tarreau595e7672020-10-20 17:30:08 +0200241 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200242
Olivier Houchard4e694042017-03-14 20:01:29 +0100243 if ((s->flags & SRV_F_COOKIESET) ||
244 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
245 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200246 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100247 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100248
249 if (s->addr.ss_family != AF_INET &&
250 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200251 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100252 /*
253 * Buffer to calculate the cookie value.
254 * The buffer contains the secret key + the server IP address
255 * + the TCP port.
256 */
257 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
258 /*
259 * The TCP port should use only 2 bytes, but is stored in
260 * an unsigned int in struct server, so let's use 4, to be
261 * on the safe side.
262 */
263 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200264 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100265 memcpy(tmpbuf, p->dyncookie_key, key_len);
266 memcpy(&(tmpbuf[key_len]),
267 s->addr.ss_family == AF_INET ?
268 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
269 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
270 addr_len);
271 /*
272 * Make sure it's the same across all the load balancers,
273 * no matter their endianness.
274 */
275 port = htonl(s->svc_port);
276 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
277 hash_value = XXH64(tmpbuf, buffer_len, 0);
278 memprintf(&s->cookie, "%016llx", hash_value);
279 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200280 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100281 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100282
283 /* Don't bother checking if the dyncookie is duplicated if
284 * the server is marked as "disabled", maybe it doesn't have
285 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100286 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100287 if (!(s->next_admin & SRV_ADMF_FMAINT))
288 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200289 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200290 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100291}
292
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200293/* Returns true if it's possible to reuse an idle connection from server <srv>
294 * for a websocket stream. This is the case if server is configured to use the
295 * same protocol for both HTTP and websocket streams. This depends on the value
296 * of "proto", "alpn" and "ws" keywords.
297 */
298int srv_check_reuse_ws(struct server *srv)
299{
300 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
301 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
302 * for mux selection.
303 */
304 const struct ist srv_mux = srv->mux_proto ?
305 srv->mux_proto->token : IST_NULL;
306
307 switch (srv->ws) {
308 /* "auto" means use the same protocol : reuse is possible. */
309 case SRV_WS_AUTO:
310 return 1;
311
312 /* "h2" means use h2 for websocket : reuse is possible if
313 * server mux is h2.
314 */
315 case SRV_WS_H2:
316 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
317 return 1;
318 break;
319
320 /* "h1" means use h1 for websocket : reuse is possible if
321 * server mux is h1.
322 */
323 case SRV_WS_H1:
324 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
325 return 1;
326 break;
327 }
328 }
329 else {
330 /* ALPN selection.
331 * Based on the assumption that only "h2" and "http/1.1" token
332 * are used on server ALPN.
333 */
334 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
335 srv->ssl_ctx.alpn_len);
336
337 switch (srv->ws) {
338 case SRV_WS_AUTO:
339 /* for auto mode, consider reuse as possible if the
340 * server uses a single protocol ALPN
341 */
342 if (!istchr(alpn, ','))
343 return 1;
344 break;
345
346 case SRV_WS_H2:
347 return isteq(alpn, ist("\x02h2"));
348
349 case SRV_WS_H1:
350 return isteq(alpn, ist("\x08http/1.1"));
351 }
352 }
353
354 return 0;
355}
356
357/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
358 * is a valid value indicating to use the fallback mux.
359 */
360const struct mux_ops *srv_get_ws_proto(struct server *srv)
361{
362 const struct mux_proto_list *mux = NULL;
363
364 switch (srv->ws) {
365 case SRV_WS_AUTO:
366 mux = srv->mux_proto;
367 break;
368
369 case SRV_WS_H1:
370 mux = get_mux_proto(ist("h1"));
371 break;
372
373 case SRV_WS_H2:
374 mux = get_mux_proto(ist("h2"));
375 break;
376 }
377
378 return mux ? mux->mux : NULL;
379}
380
Willy Tarreau21faa912012-10-10 08:27:36 +0200381/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200382 * Must be called with the server lock held. The server is first removed from
383 * the proxy tree if it was already attached. If <reattach> is true, the server
384 * will then be attached in the proxy tree. The proxy lock is held to
385 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700386 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200387static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700388{
389 struct proxy *p = s->proxy;
390 char *key;
391
392 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
393
394 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700395 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700396 free(key);
397 return;
398 }
399
400 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
401 ebpt_delete(&s->addr_node);
402 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
403
404 free(s->addr_node.key);
405 }
406
407 s->addr_node.key = key;
408
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200409 if (reattach) {
410 if (s->addr_node.key) {
411 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
412 ebis_insert(&p->used_server_addr, &s->addr_node);
413 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
414 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700415 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700416}
417
418/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200419 * Registers the server keyword list <kwl> as a list of valid keywords for next
420 * parsing sessions.
421 */
422void srv_register_keywords(struct srv_kw_list *kwl)
423{
Willy Tarreau2b718102021-04-21 07:32:39 +0200424 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200425}
426
427/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
428 * keyword is found with a NULL ->parse() function, then an attempt is made to
429 * find one with a valid ->parse() function. This way it is possible to declare
430 * platform-dependant, known keywords as NULL, then only declare them as valid
431 * if some options are met. Note that if the requested keyword contains an
432 * opening parenthesis, everything from this point is ignored.
433 */
434struct srv_kw *srv_find_kw(const char *kw)
435{
436 int index;
437 const char *kwend;
438 struct srv_kw_list *kwl;
439 struct srv_kw *ret = NULL;
440
441 kwend = strchr(kw, '(');
442 if (!kwend)
443 kwend = kw + strlen(kw);
444
445 list_for_each_entry(kwl, &srv_keywords.list, list) {
446 for (index = 0; kwl->kw[index].kw != NULL; index++) {
447 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
448 kwl->kw[index].kw[kwend-kw] == 0) {
449 if (kwl->kw[index].parse)
450 return &kwl->kw[index]; /* found it !*/
451 else
452 ret = &kwl->kw[index]; /* may be OK */
453 }
454 }
455 }
456 return ret;
457}
458
459/* Dumps all registered "server" keywords to the <out> string pointer. The
460 * unsupported keywords are only dumped if their supported form was not
461 * found.
462 */
463void srv_dump_kws(char **out)
464{
465 struct srv_kw_list *kwl;
466 int index;
467
Christopher Faulet784063e2020-05-18 12:14:18 +0200468 if (!out)
469 return;
470
Willy Tarreau21faa912012-10-10 08:27:36 +0200471 *out = NULL;
472 list_for_each_entry(kwl, &srv_keywords.list, list) {
473 for (index = 0; kwl->kw[index].kw != NULL; index++) {
474 if (kwl->kw[index].parse ||
475 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
476 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
477 kwl->scope,
478 kwl->kw[index].kw,
479 kwl->kw[index].skip ? " <arg>" : "",
480 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
481 kwl->kw[index].parse ? "" : " (not supported)");
482 }
483 }
484 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100485}
486
487/* Try to find in srv_keyword the word that looks closest to <word> by counting
488 * transitions between letters, digits and other characters. Will return the
489 * best matching word if found, otherwise NULL. An optional array of extra
490 * words to compare may be passed in <extra>, but it must then be terminated
491 * by a NULL entry. If unused it may be NULL.
492 */
493static const char *srv_find_best_kw(const char *word)
494{
495 uint8_t word_sig[1024];
496 uint8_t list_sig[1024];
497 const struct srv_kw_list *kwl;
498 const char *best_ptr = NULL;
499 int dist, best_dist = INT_MAX;
500 const char **extra;
501 int index;
502
503 make_word_fingerprint(word_sig, word);
504 list_for_each_entry(kwl, &srv_keywords.list, list) {
505 for (index = 0; kwl->kw[index].kw != NULL; index++) {
506 make_word_fingerprint(list_sig, kwl->kw[index].kw);
507 dist = word_fingerprint_distance(word_sig, list_sig);
508 if (dist < best_dist) {
509 best_dist = dist;
510 best_ptr = kwl->kw[index].kw;
511 }
512 }
513 }
514
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100515 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100516 make_word_fingerprint(list_sig, *extra);
517 dist = word_fingerprint_distance(word_sig, list_sig);
518 if (dist < best_dist) {
519 best_dist = dist;
520 best_ptr = *extra;
521 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100522 }
523
524 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
525 best_ptr = NULL;
526
527 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200528}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100529
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100530/* Parse the "backup" server keyword */
531static int srv_parse_backup(char **args, int *cur_arg,
532 struct proxy *curproxy, struct server *newsrv, char **err)
533{
534 newsrv->flags |= SRV_F_BACKUP;
535 return 0;
536}
537
Alexander Liu2a54bb72019-05-22 19:44:48 +0800538
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100539/* Parse the "cookie" server keyword */
540static int srv_parse_cookie(char **args, int *cur_arg,
541 struct proxy *curproxy, struct server *newsrv, char **err)
542{
543 char *arg;
544
545 arg = args[*cur_arg + 1];
546 if (!*arg) {
547 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
548 return ERR_ALERT | ERR_FATAL;
549 }
550
551 free(newsrv->cookie);
552 newsrv->cookie = strdup(arg);
553 newsrv->cklen = strlen(arg);
554 newsrv->flags |= SRV_F_COOKIESET;
555 return 0;
556}
557
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100558/* Parse the "disabled" server keyword */
559static int srv_parse_disabled(char **args, int *cur_arg,
560 struct proxy *curproxy, struct server *newsrv, char **err)
561{
Emeric Brun52a91d32017-08-31 14:41:55 +0200562 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
563 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100564 newsrv->check.state |= CHK_ST_PAUSED;
565 newsrv->check.health = 0;
566 return 0;
567}
568
569/* Parse the "enabled" server keyword */
570static int srv_parse_enabled(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_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100575 newsrv->check.state &= ~CHK_ST_PAUSED;
576 newsrv->check.health = newsrv->check.rise;
577 return 0;
578}
579
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100580/* Parse the "error-limit" server keyword */
581static int srv_parse_error_limit(char **args, int *cur_arg,
582 struct proxy *curproxy, struct server *newsrv, char **err)
583{
584 if (!*args[*cur_arg + 1]) {
585 memprintf(err, "'%s' expects an integer argument.",
586 args[*cur_arg]);
587 return ERR_ALERT | ERR_FATAL;
588 }
589
590 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
591
592 if (newsrv->consecutive_errors_limit <= 0) {
593 memprintf(err, "%s has to be > 0.",
594 args[*cur_arg]);
595 return ERR_ALERT | ERR_FATAL;
596 }
597
598 return 0;
599}
600
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200601/* Parse the "ws" keyword */
602static int srv_parse_ws(char **args, int *cur_arg,
603 struct proxy *curproxy, struct server *newsrv, char **err)
604{
605 if (!args[*cur_arg + 1]) {
606 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
607 return ERR_ALERT | ERR_FATAL;
608 }
609
610 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
611 newsrv->ws = SRV_WS_H1;
612 }
613 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
614 newsrv->ws = SRV_WS_H2;
615 }
616 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
617 newsrv->ws = SRV_WS_AUTO;
618 }
619 else {
620 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
621 return ERR_ALERT | ERR_FATAL;
622 }
623
624
625 return 0;
626}
627
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100628/* Parse the "init-addr" server keyword */
629static int srv_parse_init_addr(char **args, int *cur_arg,
630 struct proxy *curproxy, struct server *newsrv, char **err)
631{
632 char *p, *end;
633 int done;
634 struct sockaddr_storage sa;
635
636 newsrv->init_addr_methods = 0;
637 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
638
639 for (p = args[*cur_arg + 1]; *p; p = end) {
640 /* cut on next comma */
641 for (end = p; *end && *end != ','; end++);
642 if (*end)
643 *(end++) = 0;
644
645 memset(&sa, 0, sizeof(sa));
646 if (strcmp(p, "libc") == 0) {
647 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
648 }
649 else if (strcmp(p, "last") == 0) {
650 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
651 }
652 else if (strcmp(p, "none") == 0) {
653 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
654 }
655 else if (str2ip2(p, &sa, 0)) {
656 if (is_addr(&newsrv->init_addr)) {
657 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
658 args[*cur_arg], p);
659 return ERR_ALERT | ERR_FATAL;
660 }
661 newsrv->init_addr = sa;
662 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
663 }
664 else {
665 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
666 args[*cur_arg], p);
667 return ERR_ALERT | ERR_FATAL;
668 }
669 if (!done) {
670 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
671 args[*cur_arg], p);
672 return ERR_ALERT | ERR_FATAL;
673 }
674 }
675
676 return 0;
677}
678
679/* Parse the "log-proto" server keyword */
680static int srv_parse_log_proto(char **args, int *cur_arg,
681 struct proxy *curproxy, struct server *newsrv, char **err)
682{
683 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
684 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
685 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
686 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
687 else {
688 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
689 args[*cur_arg], args[*cur_arg + 1]);
690 return ERR_ALERT | ERR_FATAL;
691 }
692
693 return 0;
694}
695
696/* Parse the "maxconn" server keyword */
697static int srv_parse_maxconn(char **args, int *cur_arg,
698 struct proxy *curproxy, struct server *newsrv, char **err)
699{
700 newsrv->maxconn = atol(args[*cur_arg + 1]);
701 return 0;
702}
703
704/* Parse the "maxqueue" server keyword */
705static int srv_parse_maxqueue(char **args, int *cur_arg,
706 struct proxy *curproxy, struct server *newsrv, char **err)
707{
708 newsrv->maxqueue = atol(args[*cur_arg + 1]);
709 return 0;
710}
711
712/* Parse the "minconn" server keyword */
713static int srv_parse_minconn(char **args, int *cur_arg,
714 struct proxy *curproxy, struct server *newsrv, char **err)
715{
716 newsrv->minconn = atol(args[*cur_arg + 1]);
717 return 0;
718}
719
Willy Tarreau9c538e02019-01-23 10:21:49 +0100720static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
721{
722 char *arg;
723
724 arg = args[*cur_arg + 1];
725 if (!*arg) {
726 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
727 return ERR_ALERT | ERR_FATAL;
728 }
729 newsrv->max_reuse = atoi(arg);
730
731 return 0;
732}
733
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100734static 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 +0100735{
736 const char *res;
737 char *arg;
738 unsigned int time;
739
740 arg = args[*cur_arg + 1];
741 if (!*arg) {
742 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
743 return ERR_ALERT | ERR_FATAL;
744 }
745 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200746 if (res == PARSE_TIME_OVER) {
747 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
748 args[*cur_arg+1], args[*cur_arg]);
749 return ERR_ALERT | ERR_FATAL;
750 }
751 else if (res == PARSE_TIME_UNDER) {
752 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
753 args[*cur_arg+1], args[*cur_arg]);
754 return ERR_ALERT | ERR_FATAL;
755 }
756 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100757 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
758 *res, args[*cur_arg]);
759 return ERR_ALERT | ERR_FATAL;
760 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100761 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100762
763 return 0;
764}
765
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200766static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
767{
768 char *arg;
769
770 arg = args[*cur_arg + 1];
771 if (!*arg) {
772 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
773 return ERR_ALERT | ERR_FATAL;
774 }
775
776 newsrv->low_idle_conns = atoi(arg);
777 return 0;
778}
779
Olivier Houchard006e3102018-12-10 18:30:32 +0100780static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
781{
782 char *arg;
783
784 arg = args[*cur_arg + 1];
785 if (!*arg) {
786 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
787 return ERR_ALERT | ERR_FATAL;
788 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100789
Olivier Houchard006e3102018-12-10 18:30:32 +0100790 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100791 if ((int)newsrv->max_idle_conns < -1) {
792 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
793 return ERR_ALERT | ERR_FATAL;
794 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100795
796 return 0;
797}
798
Willy Tarreaudff55432012-10-10 17:51:05 +0200799/* parse the "id" server keyword */
800static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
801{
802 struct eb32_node *node;
803
804 if (!*args[*cur_arg + 1]) {
805 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
806 return ERR_ALERT | ERR_FATAL;
807 }
808
809 newsrv->puid = atol(args[*cur_arg + 1]);
810 newsrv->conf.id.key = newsrv->puid;
811
812 if (newsrv->puid <= 0) {
813 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
814 return ERR_ALERT | ERR_FATAL;
815 }
816
817 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
818 if (node) {
819 struct server *target = container_of(node, struct server, conf.id);
820 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
821 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
822 target->id);
823 return ERR_ALERT | ERR_FATAL;
824 }
825
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200826 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200827 return 0;
828}
829
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100830/* Parse the "namespace" server keyword */
831static int srv_parse_namespace(char **args, int *cur_arg,
832 struct proxy *curproxy, struct server *newsrv, char **err)
833{
Willy Tarreaue5733232019-05-22 19:24:06 +0200834#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100835 char *arg;
836
837 arg = args[*cur_arg + 1];
838 if (!*arg) {
839 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
840 return ERR_ALERT | ERR_FATAL;
841 }
842
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100843 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100844 /* Use the namespace associated with the connection (if present). */
845 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
846 return 0;
847 }
848
849 /*
850 * As this parser may be called several times for the same 'default-server'
851 * object, or for a new 'server' instance deriving from a 'default-server'
852 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
853 */
854 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
855
856 newsrv->netns = netns_store_lookup(arg, strlen(arg));
857 if (!newsrv->netns)
858 newsrv->netns = netns_store_insert(arg);
859
860 if (!newsrv->netns) {
861 memprintf(err, "Cannot open namespace '%s'", arg);
862 return ERR_ALERT | ERR_FATAL;
863 }
864
865 return 0;
866#else
867 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
868 return ERR_ALERT | ERR_FATAL;
869#endif
870}
871
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100872/* Parse the "no-backup" server keyword */
873static int srv_parse_no_backup(char **args, int *cur_arg,
874 struct proxy *curproxy, struct server *newsrv, char **err)
875{
876 newsrv->flags &= ~SRV_F_BACKUP;
877 return 0;
878}
879
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100880
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100881/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200882static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100883{
884 srv->pp_opts &= ~flags;
885 return 0;
886}
887
888/* Parse the "no-send-proxy" server keyword */
889static int srv_parse_no_send_proxy(char **args, int *cur_arg,
890 struct proxy *curproxy, struct server *newsrv, char **err)
891{
892 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
893}
894
895/* Parse the "no-send-proxy-v2" server keyword */
896static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
897 struct proxy *curproxy, struct server *newsrv, char **err)
898{
899 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
900}
901
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200902/* Parse the "shard" server keyword */
903static int srv_parse_shard(char **args, int *cur_arg,
904 struct proxy *curproxy, struct server *newsrv, char **err)
905{
906 newsrv->shard = atol(args[*cur_arg + 1]);
907 return 0;
908}
909
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200910/* Parse the "no-tfo" server keyword */
911static int srv_parse_no_tfo(char **args, int *cur_arg,
912 struct proxy *curproxy, struct server *newsrv, char **err)
913{
914 newsrv->flags &= ~SRV_F_FASTOPEN;
915 return 0;
916}
917
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100918/* Parse the "non-stick" server keyword */
919static int srv_parse_non_stick(char **args, int *cur_arg,
920 struct proxy *curproxy, struct server *newsrv, char **err)
921{
922 newsrv->flags |= SRV_F_NON_STICK;
923 return 0;
924}
925
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100926/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200927static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100928{
929 srv->pp_opts |= flags;
930 return 0;
931}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200932/* parse the "proto" server keyword */
933static int srv_parse_proto(char **args, int *cur_arg,
934 struct proxy *px, struct server *newsrv, char **err)
935{
936 struct ist proto;
937
938 if (!*args[*cur_arg + 1]) {
939 memprintf(err, "'%s' : missing value", args[*cur_arg]);
940 return ERR_ALERT | ERR_FATAL;
941 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100942 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200943 newsrv->mux_proto = get_mux_proto(proto);
944 if (!newsrv->mux_proto) {
945 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
946 return ERR_ALERT | ERR_FATAL;
947 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200948 return 0;
949}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100950
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100951/* parse the "proxy-v2-options" */
952static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
953 struct proxy *px, struct server *newsrv, char **err)
954{
955 char *p, *n;
956 for (p = args[*cur_arg+1]; p; p = n) {
957 n = strchr(p, ',');
958 if (n)
959 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100960 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100961 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100962 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100963 newsrv->pp_opts |= SRV_PP_V2_SSL;
964 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100965 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100966 newsrv->pp_opts |= SRV_PP_V2_SSL;
967 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100968 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100969 newsrv->pp_opts |= SRV_PP_V2_SSL;
970 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100971 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100972 newsrv->pp_opts |= SRV_PP_V2_SSL;
973 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100974 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100975 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100976 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100977 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100978 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100979 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100980 } else
981 goto fail;
982 }
983 return 0;
984 fail:
985 if (err)
986 memprintf(err, "'%s' : proxy v2 option not implemented", p);
987 return ERR_ALERT | ERR_FATAL;
988}
989
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100990/* Parse the "observe" server keyword */
991static int srv_parse_observe(char **args, int *cur_arg,
992 struct proxy *curproxy, struct server *newsrv, char **err)
993{
994 char *arg;
995
996 arg = args[*cur_arg + 1];
997 if (!*arg) {
998 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
999 return ERR_ALERT | ERR_FATAL;
1000 }
1001
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001002 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001003 newsrv->observe = HANA_OBS_NONE;
1004 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001005 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001006 newsrv->observe = HANA_OBS_LAYER4;
1007 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001008 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001009 if (curproxy->mode != PR_MODE_HTTP) {
1010 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
1011 return ERR_ALERT;
1012 }
1013 newsrv->observe = HANA_OBS_LAYER7;
1014 }
1015 else {
1016 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
1017 "but got '%s'\n", args[*cur_arg], arg);
1018 return ERR_ALERT | ERR_FATAL;
1019 }
1020
1021 return 0;
1022}
1023
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001024/* Parse the "on-error" server keyword */
1025static int srv_parse_on_error(char **args, int *cur_arg,
1026 struct proxy *curproxy, struct server *newsrv, char **err)
1027{
1028 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
1029 newsrv->onerror = HANA_ONERR_FASTINTER;
1030 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
1031 newsrv->onerror = HANA_ONERR_FAILCHK;
1032 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
1033 newsrv->onerror = HANA_ONERR_SUDDTH;
1034 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
1035 newsrv->onerror = HANA_ONERR_MARKDWN;
1036 else {
1037 memprintf(err, "'%s' expects one of 'fastinter', "
1038 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1039 args[*cur_arg], args[*cur_arg + 1]);
1040 return ERR_ALERT | ERR_FATAL;
1041 }
1042
1043 return 0;
1044}
1045
1046/* Parse the "on-marked-down" server keyword */
1047static int srv_parse_on_marked_down(char **args, int *cur_arg,
1048 struct proxy *curproxy, struct server *newsrv, char **err)
1049{
1050 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1051 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1052 else {
1053 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1054 args[*cur_arg], args[*cur_arg + 1]);
1055 return ERR_ALERT | ERR_FATAL;
1056 }
1057
1058 return 0;
1059}
1060
1061/* Parse the "on-marked-up" server keyword */
1062static int srv_parse_on_marked_up(char **args, int *cur_arg,
1063 struct proxy *curproxy, struct server *newsrv, char **err)
1064{
1065 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1066 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1067 else {
1068 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1069 args[*cur_arg], args[*cur_arg + 1]);
1070 return ERR_ALERT | ERR_FATAL;
1071 }
1072
1073 return 0;
1074}
1075
Frédéric Lécaille16186232017-03-14 16:42:49 +01001076/* Parse the "redir" server keyword */
1077static int srv_parse_redir(char **args, int *cur_arg,
1078 struct proxy *curproxy, struct server *newsrv, char **err)
1079{
1080 char *arg;
1081
1082 arg = args[*cur_arg + 1];
1083 if (!*arg) {
1084 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1085 return ERR_ALERT | ERR_FATAL;
1086 }
1087
1088 free(newsrv->rdr_pfx);
1089 newsrv->rdr_pfx = strdup(arg);
1090 newsrv->rdr_len = strlen(arg);
1091
1092 return 0;
1093}
1094
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001095/* Parse the "resolvers" server keyword */
1096static int srv_parse_resolvers(char **args, int *cur_arg,
1097 struct proxy *curproxy, struct server *newsrv, char **err)
1098{
1099 free(newsrv->resolvers_id);
1100 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1101 return 0;
1102}
1103
1104/* Parse the "resolve-net" server keyword */
1105static int srv_parse_resolve_net(char **args, int *cur_arg,
1106 struct proxy *curproxy, struct server *newsrv, char **err)
1107{
1108 char *p, *e;
1109 unsigned char mask;
1110 struct resolv_options *opt;
1111
1112 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1113 memprintf(err, "'%s' expects a list of networks.",
1114 args[*cur_arg]);
1115 return ERR_ALERT | ERR_FATAL;
1116 }
1117
1118 opt = &newsrv->resolv_opts;
1119
1120 /* Split arguments by comma, and convert it from ipv4 or ipv6
1121 * string network in in_addr or in6_addr.
1122 */
1123 p = args[*cur_arg + 1];
1124 e = p;
1125 while (*p != '\0') {
1126 /* If no room available, return error. */
1127 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1128 memprintf(err, "'%s' exceed %d networks.",
1129 args[*cur_arg], SRV_MAX_PREF_NET);
1130 return ERR_ALERT | ERR_FATAL;
1131 }
1132 /* look for end or comma. */
1133 while (*e != ',' && *e != '\0')
1134 e++;
1135 if (*e == ',') {
1136 *e = '\0';
1137 e++;
1138 }
1139 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1140 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1141 /* Try to convert input string from ipv4 or ipv6 network. */
1142 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1143 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1144 &mask)) {
1145 /* Try to convert input string from ipv6 network. */
1146 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1147 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1148 } else {
1149 /* All network conversions fail, return error. */
1150 memprintf(err, "'%s' invalid network '%s'.",
1151 args[*cur_arg], p);
1152 return ERR_ALERT | ERR_FATAL;
1153 }
1154 opt->pref_net_nb++;
1155 p = e;
1156 }
1157
1158 return 0;
1159}
1160
1161/* Parse the "resolve-opts" server keyword */
1162static int srv_parse_resolve_opts(char **args, int *cur_arg,
1163 struct proxy *curproxy, struct server *newsrv, char **err)
1164{
1165 char *p, *end;
1166
1167 for (p = args[*cur_arg + 1]; *p; p = end) {
1168 /* cut on next comma */
1169 for (end = p; *end && *end != ','; end++);
1170 if (*end)
1171 *(end++) = 0;
1172
1173 if (strcmp(p, "allow-dup-ip") == 0) {
1174 newsrv->resolv_opts.accept_duplicate_ip = 1;
1175 }
1176 else if (strcmp(p, "ignore-weight") == 0) {
1177 newsrv->resolv_opts.ignore_weight = 1;
1178 }
1179 else if (strcmp(p, "prevent-dup-ip") == 0) {
1180 newsrv->resolv_opts.accept_duplicate_ip = 0;
1181 }
1182 else {
1183 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1184 args[*cur_arg], p);
1185 return ERR_ALERT | ERR_FATAL;
1186 }
1187 }
1188
1189 return 0;
1190}
1191
1192/* Parse the "resolve-prefer" server keyword */
1193static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1194 struct proxy *curproxy, struct server *newsrv, char **err)
1195{
1196 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1197 newsrv->resolv_opts.family_prio = AF_INET;
1198 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1199 newsrv->resolv_opts.family_prio = AF_INET6;
1200 else {
1201 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1202 args[*cur_arg]);
1203 return ERR_ALERT | ERR_FATAL;
1204 }
1205
1206 return 0;
1207}
1208
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001209/* Parse the "send-proxy" server keyword */
1210static int srv_parse_send_proxy(char **args, int *cur_arg,
1211 struct proxy *curproxy, struct server *newsrv, char **err)
1212{
1213 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1214}
1215
1216/* Parse the "send-proxy-v2" server keyword */
1217static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1218 struct proxy *curproxy, struct server *newsrv, char **err)
1219{
1220 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1221}
1222
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001223/* Parse the "slowstart" server keyword */
1224static int srv_parse_slowstart(char **args, int *cur_arg,
1225 struct proxy *curproxy, struct server *newsrv, char **err)
1226{
1227 /* slowstart is stored in seconds */
1228 unsigned int val;
1229 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1230
1231 if (time_err == PARSE_TIME_OVER) {
1232 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1233 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1234 return ERR_ALERT | ERR_FATAL;
1235 }
1236 else if (time_err == PARSE_TIME_UNDER) {
1237 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1238 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1239 return ERR_ALERT | ERR_FATAL;
1240 }
1241 else if (time_err) {
1242 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1243 *time_err, newsrv->id);
1244 return ERR_ALERT | ERR_FATAL;
1245 }
1246 newsrv->slowstart = (val + 999) / 1000;
1247
1248 return 0;
1249}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001250
1251/* Parse the "source" server keyword */
1252static int srv_parse_source(char **args, int *cur_arg,
1253 struct proxy *curproxy, struct server *newsrv, char **err)
1254{
1255 char *errmsg;
1256 int port_low, port_high;
1257 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001258
1259 errmsg = NULL;
1260
1261 if (!*args[*cur_arg + 1]) {
1262 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1263 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1264 goto err;
1265 }
1266
1267 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001268 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1269 &errmsg, NULL, NULL,
1270 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 +01001271 if (!sk) {
1272 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1273 goto err;
1274 }
1275
Frédéric Lécailledba97072017-03-17 15:33:50 +01001276 newsrv->conn_src.opts |= CO_SRC_BIND;
1277 newsrv->conn_src.source_addr = *sk;
1278
1279 if (port_low != port_high) {
1280 int i;
1281
Frédéric Lécailledba97072017-03-17 15:33:50 +01001282 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001283 if (!newsrv->conn_src.sport_range) {
1284 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1285 goto err;
1286 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001287 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1288 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1289 }
1290
1291 *cur_arg += 2;
1292 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001293 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001294#if defined(CONFIG_HAP_TRANSPARENT)
1295 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001296 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1297 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001298 goto err;
1299 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001300 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001301 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1302 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1303 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001304 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001305 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1306 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1307 }
1308 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1309 char *name, *end;
1310
1311 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001312 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001313 name++;
1314
1315 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001316 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001317 end++;
1318
1319 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1320 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1321 free(newsrv->conn_src.bind_hdr_name);
1322 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001323 if (!newsrv->conn_src.bind_hdr_name) {
1324 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1325 goto err;
1326 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001327 newsrv->conn_src.bind_hdr_len = end - name;
1328 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1329 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1330 newsrv->conn_src.bind_hdr_occ = -1;
1331
1332 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001333 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001334 end++;
1335 if (*end == ',') {
1336 end++;
1337 name = end;
1338 if (*end == '-')
1339 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001340 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001341 end++;
1342 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1343 }
1344
1345 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001346 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1347 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001348 goto err;
1349 }
1350 }
1351 else {
1352 struct sockaddr_storage *sk;
1353 int port1, port2;
1354
1355 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001356 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1357 &errmsg, NULL, NULL,
1358 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001359 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001360 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001361 goto err;
1362 }
1363
Frédéric Lécailledba97072017-03-17 15:33:50 +01001364 newsrv->conn_src.tproxy_addr = *sk;
1365 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1366 }
1367 global.last_checks |= LSTCHK_NETADM;
1368 *cur_arg += 2;
1369 continue;
1370#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001371 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 +01001372 goto err;
1373#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1374 } /* "usesrc" */
1375
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001376 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001377#ifdef SO_BINDTODEVICE
1378 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001379 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001380 goto err;
1381 }
1382 free(newsrv->conn_src.iface_name);
1383 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1384 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1385 global.last_checks |= LSTCHK_NETADM;
1386#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001387 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001388 goto err;
1389#endif
1390 *cur_arg += 2;
1391 continue;
1392 }
1393 /* this keyword in not an option of "source" */
1394 break;
1395 } /* while */
1396
1397 return 0;
1398
1399 err:
1400 free(errmsg);
1401 return ERR_ALERT | ERR_FATAL;
1402}
1403
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001404/* Parse the "stick" server keyword */
1405static int srv_parse_stick(char **args, int *cur_arg,
1406 struct proxy *curproxy, struct server *newsrv, char **err)
1407{
1408 newsrv->flags &= ~SRV_F_NON_STICK;
1409 return 0;
1410}
1411
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001412/* Parse the "track" server keyword */
1413static int srv_parse_track(char **args, int *cur_arg,
1414 struct proxy *curproxy, struct server *newsrv, char **err)
1415{
1416 char *arg;
1417
1418 arg = args[*cur_arg + 1];
1419 if (!*arg) {
1420 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1421 return ERR_ALERT | ERR_FATAL;
1422 }
1423
1424 free(newsrv->trackit);
1425 newsrv->trackit = strdup(arg);
1426
1427 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001428}
1429
1430/* Parse the "socks4" server keyword */
1431static int srv_parse_socks4(char **args, int *cur_arg,
1432 struct proxy *curproxy, struct server *newsrv, char **err)
1433{
1434 char *errmsg;
1435 int port_low, port_high;
1436 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001437
1438 errmsg = NULL;
1439
1440 if (!*args[*cur_arg + 1]) {
1441 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1442 goto err;
1443 }
1444
1445 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001446 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1447 &errmsg, NULL, NULL,
1448 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001449 if (!sk) {
1450 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1451 goto err;
1452 }
1453
Alexander Liu2a54bb72019-05-22 19:44:48 +08001454 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1455 newsrv->socks4_addr = *sk;
1456
Alexander Liu2a54bb72019-05-22 19:44:48 +08001457 return 0;
1458
1459 err:
1460 free(errmsg);
1461 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001462}
1463
Frédéric Lécailledba97072017-03-17 15:33:50 +01001464
Willy Tarreau034c88c2017-01-23 23:36:45 +01001465/* parse the "tfo" server keyword */
1466static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1467{
1468 newsrv->flags |= SRV_F_FASTOPEN;
1469 return 0;
1470}
1471
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001472/* parse the "usesrc" server keyword */
1473static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1474{
1475 memprintf(err, "'%s' only allowed after a '%s' statement.",
1476 "usesrc", "source");
1477 return ERR_ALERT | ERR_FATAL;
1478}
1479
1480/* parse the "weight" server keyword */
1481static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1482{
1483 int w;
1484
1485 w = atol(args[*cur_arg + 1]);
1486 if (w < 0 || w > SRV_UWGHT_MAX) {
1487 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1488 newsrv->id, SRV_UWGHT_MAX, w);
1489 return ERR_ALERT | ERR_FATAL;
1490 }
1491 newsrv->uweight = newsrv->iweight = w;
1492
1493 return 0;
1494}
1495
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001496/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001497 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001498 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001499 *
1500 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001501 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001502void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001503{
Willy Tarreau751153e2021-02-17 13:33:24 +01001504 struct stream *stream;
1505 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001506 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001507
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001508 for (thr = 0; thr < global.nbthread; thr++)
1509 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1510 if (stream->srv_conn == srv)
1511 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001512}
1513
1514/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001515 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001516 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001517 *
1518 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001519 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001520void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001521{
1522 struct server *srv;
1523
1524 for (srv = px->srv; srv != NULL; srv = srv->next)
1525 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001526 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001527}
1528
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001529static void srv_append_op_chg_cause(struct buffer *msg, struct server *s)
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001530{
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001531 switch (s->op_st_chg_cause) {
1532 case SRV_OP_STCHGC_NONE:
1533 break; /* do nothing */
1534 case SRV_OP_STCHGC_HEALTH:
1535 check_append_info(msg, &s->check);
1536 break;
1537 case SRV_OP_STCHGC_AGENT:
1538 check_append_info(msg, &s->agent);
1539 break;
1540 default:
1541 chunk_appendf(msg, ", %s", srv_op_st_chg_cause(s->op_st_chg_cause));
1542 break;
1543 }
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001544}
1545
1546static void srv_append_adm_chg_cause(struct buffer *msg, struct server *s)
1547{
1548 if (s->adm_st_chg_cause)
1549 chunk_appendf(msg, " (%s)", srv_adm_st_chg_cause(s->adm_st_chg_cause));
1550}
1551
1552/* Appends some information to a message string related to a server tracking
1553 * or requeued connections info.
1554 *
1555 * If <forced> is null and the server tracks another one, a "via"
Emeric Brun5a133512017-10-19 14:42:30 +02001556 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001557 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001558 *
1559 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001560 */
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001561static void srv_append_more(struct buffer *msg, struct server *s,
1562 int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001563{
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02001564 if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001565 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001566 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001567
1568 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001569 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001570 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1571 " %d sessions active, %d requeued, %d remaining in queue",
1572 s->proxy->srv_act, s->proxy->srv_bck,
1573 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001574 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001575 else
1576 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1577 " %d sessions requeued, %d total in queue",
1578 s->proxy->srv_act, s->proxy->srv_bck,
1579 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001580 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001581 }
1582}
1583
Emeric Brun5a133512017-10-19 14:42:30 +02001584/* Marks server <s> down, regardless of its checks' statuses. The server is
1585 * registered in a list to postpone the counting of the remaining servers on
1586 * the proxy and transfers queued streams whenever possible to other servers at
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001587 * a sync point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001588 *
1589 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001590 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001591void srv_set_stopped(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001592{
1593 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001594
Emeric Brun64cc49c2017-10-03 14:46:45 +02001595 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001596 return;
1597
Emeric Brun52a91d32017-08-31 14:41:55 +02001598 s->next_state = SRV_ST_STOPPED;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001599 s->op_st_chg_cause = cause;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001600
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001601 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001602 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001603
Emeric Brun9f0b4582017-10-23 14:39:51 +02001604 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001605 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001606 srv_set_stopped(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001607 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001608 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001609}
1610
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001611/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001612 * in maintenance. The server is registered in a list to postpone the counting
1613 * of the remaining servers on the proxy and tries to grab requests from the
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001614 * proxy at a sync point. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001615 *
1616 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001617 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001618void srv_set_running(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001619{
1620 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001621
Emeric Brun64cc49c2017-10-03 14:46:45 +02001622 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001623 return;
1624
Emeric Brun52a91d32017-08-31 14:41:55 +02001625 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001626 return;
1627
Emeric Brun52a91d32017-08-31 14:41:55 +02001628 s->next_state = SRV_ST_STARTING;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001629 s->op_st_chg_cause = cause;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001630
Emeric Brun64cc49c2017-10-03 14:46:45 +02001631 if (s->slowstart <= 0)
1632 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001633
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001634 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001635 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001636
Emeric Brun9f0b4582017-10-23 14:39:51 +02001637 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001638 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001639 srv_set_running(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001640 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001641 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001642}
1643
Willy Tarreau8eb77842014-05-21 13:54:57 +02001644/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001645 * isn't in maintenance. The server is registered in a list to postpone the
1646 * counting of the remaining servers on the proxy and tries to grab requests
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001647 * from the proxy. Maintenance servers are ignored.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001648 *
1649 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001650 */
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001651void srv_set_stopping(struct server *s, enum srv_op_st_chg_cause cause)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001652{
1653 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001654
Emeric Brun64cc49c2017-10-03 14:46:45 +02001655 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001656 return;
1657
Emeric Brun52a91d32017-08-31 14:41:55 +02001658 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001659 return;
1660
Emeric Brun52a91d32017-08-31 14:41:55 +02001661 s->next_state = SRV_ST_STOPPING;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001662 s->op_st_chg_cause = cause;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001663
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001664 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001665 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001666
Emeric Brun9f0b4582017-10-23 14:39:51 +02001667 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001668 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02001669 srv_set_stopping(srv, SRV_OP_STCHGC_NONE);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001670 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001671 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001672}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001673
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001674/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1675 * enforce either maint mode or drain mode. It is not allowed to set more than
1676 * one flag at once. The equivalent "inherited" flag is propagated to all
1677 * tracking servers. Maintenance mode disables health checks (but not agent
1678 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001679 * is done. If <cause> is non-null, it will be displayed at the end of the log
1680 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001681 *
1682 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001683 */
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001684void 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 +02001685{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001686 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001687
1688 if (!mode)
1689 return;
1690
1691 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001692 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001693 return;
1694
Emeric Brun52a91d32017-08-31 14:41:55 +02001695 s->next_admin |= mode;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001696 s->adm_st_chg_cause = cause;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001697
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001698 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001699 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001700
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001701 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001702 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1703 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001704 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001705
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001706 /* compute the inherited flag to propagate */
1707 if (mode & SRV_ADMF_MAINT)
1708 mode = SRV_ADMF_IMAINT;
1709 else if (mode & SRV_ADMF_DRAIN)
1710 mode = SRV_ADMF_IDRAIN;
1711
Emeric Brun9f0b4582017-10-23 14:39:51 +02001712 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001713 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001714 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001715 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001716 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001717}
1718
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001719/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1720 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1721 * than one flag at once. The equivalent "inherited" flag is propagated to all
1722 * tracking servers. Leaving maintenance mode re-enables health checks. When
1723 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001724 *
1725 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001726 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001727void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001728{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001729 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001730
1731 if (!mode)
1732 return;
1733
1734 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001735 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001736 return;
1737
Emeric Brun52a91d32017-08-31 14:41:55 +02001738 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001739
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001740 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001741 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001742
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001743 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001744 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1745 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001746 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001747
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001748 if (mode & SRV_ADMF_MAINT)
1749 mode = SRV_ADMF_IMAINT;
1750 else if (mode & SRV_ADMF_DRAIN)
1751 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001752
Emeric Brun9f0b4582017-10-23 14:39:51 +02001753 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001754 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001755 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001756 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001757 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001758}
1759
Willy Tarreau757478e2016-11-03 19:22:19 +01001760/* principle: propagate maint and drain to tracking servers. This is useful
1761 * upon startup so that inherited states are correct.
1762 */
1763static void srv_propagate_admin_state(struct server *srv)
1764{
1765 struct server *srv2;
1766
1767 if (!srv->trackers)
1768 return;
1769
1770 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001771 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001772 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001773 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau757478e2016-11-03 19:22:19 +01001774
Emeric Brun52a91d32017-08-31 14:41:55 +02001775 if (srv->next_admin & SRV_ADMF_DRAIN)
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02001776 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, SRV_ADM_STCHGC_NONE);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001777 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001778 }
1779}
1780
1781/* Compute and propagate the admin states for all servers in proxy <px>.
1782 * Only servers *not* tracking another one are considered, because other
1783 * ones will be handled when the server they track is visited.
1784 */
1785void srv_compute_all_admin_states(struct proxy *px)
1786{
1787 struct server *srv;
1788
1789 for (srv = px->srv; srv; srv = srv->next) {
1790 if (srv->track)
1791 continue;
1792 srv_propagate_admin_state(srv);
1793 }
1794}
1795
Willy Tarreaudff55432012-10-10 17:51:05 +02001796/* Note: must not be declared <const> as its list will be overwritten.
1797 * Please take care of keeping this list alphabetically sorted, doing so helps
1798 * all code contributors.
1799 * Optional keywords are also declared with a NULL ->parse() function so that
1800 * the config parser can report an appropriate error when a known keyword was
1801 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001802 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001803 */
1804static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001805 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001806 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001807 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1808 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001809 { "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 +02001810 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001811 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001812 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1813 { "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 +01001814 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1815 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001816 { "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 +01001817 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001818 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001819 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1820 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1821 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1822 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001823 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001824 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1825 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1826 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1827 { "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 +01001828 { "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 */
1829 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1830 { "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 +01001831 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001832 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001833 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001834 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001835 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001836 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001837 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001838 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1839 { "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 +02001840 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001841 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001842 { "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 +01001843 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001844 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001845 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001846 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001847 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1848 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001849 { NULL, NULL, 0 },
1850}};
1851
Willy Tarreau0108d902018-11-25 19:14:37 +01001852INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001853
Willy Tarreau004e0452013-11-21 11:22:01 +01001854/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001855 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001856 * state is automatically disabled if the time is elapsed. If <must_update> is
1857 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001858 *
1859 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001860 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001861void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001862{
1863 struct proxy *px = sv->proxy;
1864 unsigned w;
1865
1866 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1867 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001868 if (sv->next_state == SRV_ST_STARTING)
1869 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001870 }
1871
1872 /* We must take care of not pushing the server to full throttle during slow starts.
1873 * It must also start immediately, at least at the minimal step when leaving maintenance.
1874 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001875 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001876 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1877 else
1878 w = px->lbprm.wdiv;
1879
Emeric Brun52a91d32017-08-31 14:41:55 +02001880 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001881
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001882 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001883 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001884 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001885}
1886
Willy Tarreaubaaee002006-06-26 02:48:02 +02001887/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001888 * Parses weight_str and configures sv accordingly.
1889 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001890 *
1891 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001892 */
1893const char *server_parse_weight_change_request(struct server *sv,
1894 const char *weight_str)
1895{
1896 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001897 long int w;
1898 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001899
1900 px = sv->proxy;
1901
1902 /* if the weight is terminated with '%', it is set relative to
1903 * the initial weight, otherwise it is absolute.
1904 */
1905 if (!*weight_str)
1906 return "Require <weight> or <weight%>.\n";
1907
Simon Hormanb796afa2013-02-12 10:45:53 +09001908 w = strtol(weight_str, &end, 10);
1909 if (end == weight_str)
1910 return "Empty weight string empty or preceded by garbage";
1911 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001912 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001913 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001914 /* Avoid integer overflow */
1915 if (w > 25600)
1916 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001917 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001918 if (w > 256)
1919 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001920 }
1921 else if (w < 0 || w > 256)
1922 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001923 else if (end[0] != '\0')
1924 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001925
1926 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1927 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1928
1929 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001930 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001931
1932 return NULL;
1933}
1934
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001935/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001936 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1937 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001938 * Returns:
1939 * - error string on error
1940 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001941 *
1942 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001943 */
1944const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001945 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001946{
1947 unsigned char ip[INET6_ADDRSTRLEN];
1948
1949 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001950 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001951 return NULL;
1952 }
1953 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001954 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001955 return NULL;
1956 }
1957
1958 return "Could not understand IP address format.\n";
1959}
1960
Willy Tarreau46b7f532018-08-21 11:54:26 +02001961/*
1962 * Must be called with the server lock held.
1963 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001964const char *server_parse_maxconn_change_request(struct server *sv,
1965 const char *maxconn_str)
1966{
1967 long int v;
1968 char *end;
1969
1970 if (!*maxconn_str)
1971 return "Require <maxconn>.\n";
1972
1973 v = strtol(maxconn_str, &end, 10);
1974 if (end == maxconn_str)
1975 return "maxconn string empty or preceded by garbage";
1976 else if (end[0] != '\0')
1977 return "Trailing garbage in maxconn string";
1978
1979 if (sv->maxconn == sv->minconn) { // static maxconn
1980 sv->maxconn = sv->minconn = v;
1981 } else { // dynamic maxconn
1982 sv->maxconn = v;
1983 }
1984
1985 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02001986 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001987
1988 return NULL;
1989}
1990
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001991static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1992 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001993{
1994 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001995 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001996 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001997 NULL,
1998 };
1999
2000 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002001 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002002
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002003 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002004}
2005
William Lallemand0d058672022-03-16 15:44:42 +01002006int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002007{
2008 struct sample_expr *expr;
2009
2010 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 +01002011 if (!expr) {
2012 memprintf(err, "error detected while parsing sni expression : %s", *err);
2013 return ERR_ALERT | ERR_FATAL;
2014 }
2015
2016 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2017 memprintf(err, "error detected while parsing sni expression : "
2018 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002019 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002020 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002021 return ERR_ALERT | ERR_FATAL;
2022 }
2023
2024 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2025 release_sample_expr(newsrv->ssl_ctx.sni);
2026 newsrv->ssl_ctx.sni = expr;
2027
2028 return 0;
2029}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002030
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002031static 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 +01002032{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002033 char *msg = "error encountered while processing ";
2034 char *quote = "'";
2035 char *token = args[cur_arg];
2036
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002037 if (err && *err) {
2038 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002039 msg = *err;
2040 quote = "";
2041 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002042 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002043
2044 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002045 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002046 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002047 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002048}
2049
Christopher Faulet0b365e32022-08-03 11:21:14 +02002050static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002051{
2052 int range_sz;
2053
2054 range_sz = src->conn_src.sport_range->size;
2055 if (range_sz > 0) {
2056 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2057 if (srv->conn_src.sport_range != NULL) {
2058 int i;
2059
2060 for (i = 0; i < range_sz; i++) {
2061 srv->conn_src.sport_range->ports[i] =
2062 src->conn_src.sport_range->ports[i];
2063 }
2064 }
2065 }
2066}
2067
2068/*
2069 * Copy <src> server connection source settings to <srv> server everything needed.
2070 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002071static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002072{
2073 srv->conn_src.opts = src->conn_src.opts;
2074 srv->conn_src.source_addr = src->conn_src.source_addr;
2075
2076 /* Source port range copy. */
2077 if (src->conn_src.sport_range != NULL)
2078 srv_conn_src_sport_range_cpy(srv, src);
2079
2080#ifdef CONFIG_HAP_TRANSPARENT
2081 if (src->conn_src.bind_hdr_name != NULL) {
2082 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2083 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2084 }
2085 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2086 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2087#endif
2088 if (src->conn_src.iface_name != NULL)
2089 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2090}
2091
2092/*
2093 * Copy <src> server SSL settings to <srv> server allocating
2094 * everything needed.
2095 */
2096#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002097static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002098{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002099 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002100 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2101
Christopher Faulet4ab26792021-12-01 09:50:41 +01002102 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2103 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2104
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002105 if (src->ssl_ctx.ca_file != NULL)
2106 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2107 if (src->ssl_ctx.crl_file != NULL)
2108 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002109 if (src->ssl_ctx.client_crt != NULL)
2110 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002111
2112 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2113
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002114
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002115 if (src->ssl_ctx.verify_host != NULL)
2116 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2117 if (src->ssl_ctx.ciphers != NULL)
2118 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002119 if (src->ssl_ctx.options)
2120 srv->ssl_ctx.options = src->ssl_ctx.options;
2121 if (src->ssl_ctx.methods.flags)
2122 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2123 if (src->ssl_ctx.methods.min)
2124 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2125 if (src->ssl_ctx.methods.max)
2126 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2127
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002128 if (src->ssl_ctx.ciphersuites != NULL)
2129 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002130 if (src->sni_expr != NULL)
2131 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002132
Olivier Houchardc7566002018-11-20 23:33:50 +01002133 if (src->ssl_ctx.alpn_str) {
2134 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2135 if (srv->ssl_ctx.alpn_str) {
2136 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2137 src->ssl_ctx.alpn_len);
2138 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2139 }
2140 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002141
Olivier Houchardc7566002018-11-20 23:33:50 +01002142 if (src->ssl_ctx.npn_str) {
2143 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2144 if (srv->ssl_ctx.npn_str) {
2145 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2146 src->ssl_ctx.npn_len);
2147 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2148 }
2149 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002150}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002151
2152/* Activate ssl on server <s>.
2153 * do nothing if there is no change to apply
2154 *
2155 * Must be called with the server lock held.
2156 */
2157void srv_set_ssl(struct server *s, int use_ssl)
2158{
2159 if (s->use_ssl == use_ssl)
2160 return;
2161
2162 s->use_ssl = use_ssl;
2163 if (s->use_ssl)
2164 s->xprt = xprt_get(XPRT_SSL);
2165 else
William Dauchya087f872022-01-06 16:57:15 +01002166 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002167}
2168
2169#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002170
2171/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002172 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002173 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002174 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002175 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002176int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002177{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002178 char *hostname_dn;
2179 int hostname_len, hostname_dn_len;
2180
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002181 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002182 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002183
Christopher Faulet67957bd2017-09-27 11:00:59 +02002184 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002185 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002186 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002187 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002188 if (hostname_dn_len == -1)
2189 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002190
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002191
Christopher Faulet67957bd2017-09-27 11:00:59 +02002192 free(srv->hostname);
2193 free(srv->hostname_dn);
2194 srv->hostname = strdup(hostname);
2195 srv->hostname_dn = strdup(hostname_dn);
2196 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002197 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002198 goto err;
2199
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002200 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002201
2202 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002203 ha_free(&srv->hostname);
2204 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002205 return -1;
2206}
2207
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002208/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002209 * Copy <src> server settings to <srv> server allocating
2210 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002211 * This function is not supposed to be called at any time, but only
2212 * during server settings parsing or during server allocations from
2213 * a server template, and just after having calloc()'ed a new server.
2214 * So, <src> may only be a default server (when parsing server settings)
2215 * or a server template (during server allocations from a server template).
2216 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2217 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002218 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002219void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002220{
2221 /* Connection source settings copy */
2222 srv_conn_src_cpy(srv, src);
2223
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002224 if (srv_tmpl) {
2225 srv->addr = src->addr;
2226 srv->svc_port = src->svc_port;
2227 }
2228
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002229 srv->pp_opts = src->pp_opts;
2230 if (src->rdr_pfx != NULL) {
2231 srv->rdr_pfx = strdup(src->rdr_pfx);
2232 srv->rdr_len = src->rdr_len;
2233 }
2234 if (src->cookie != NULL) {
2235 srv->cookie = strdup(src->cookie);
2236 srv->cklen = src->cklen;
2237 }
2238 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002239 srv->check.addr = src->check.addr;
2240 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002241 srv->check.use_ssl = src->check.use_ssl;
2242 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002243 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002244 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002245 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002246 /* Note: 'flags' field has potentially been already initialized. */
2247 srv->flags |= src->flags;
2248 srv->do_check = src->do_check;
2249 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002250 srv->check.inter = src->check.inter;
2251 srv->check.fastinter = src->check.fastinter;
2252 srv->check.downinter = src->check.downinter;
2253 srv->agent.use_ssl = src->agent.use_ssl;
2254 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002255
2256 if (src->agent.tcpcheck_rules) {
2257 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2258 if (srv->agent.tcpcheck_rules) {
2259 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2260 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2261 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2262 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2263 &src->agent.tcpcheck_rules->preset_vars);
2264 }
2265 }
2266
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002267 srv->agent.inter = src->agent.inter;
2268 srv->agent.fastinter = src->agent.fastinter;
2269 srv->agent.downinter = src->agent.downinter;
2270 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002271 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002272 srv->minconn = src->minconn;
2273 srv->maxconn = src->maxconn;
2274 srv->slowstart = src->slowstart;
2275 srv->observe = src->observe;
2276 srv->onerror = src->onerror;
2277 srv->onmarkeddown = src->onmarkeddown;
2278 srv->onmarkedup = src->onmarkedup;
2279 if (src->trackit != NULL)
2280 srv->trackit = strdup(src->trackit);
2281 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2282 srv->uweight = srv->iweight = src->iweight;
2283
2284 srv->check.send_proxy = src->check.send_proxy;
2285 /* health: up, but will fall down at first failure */
2286 srv->check.rise = srv->check.health = src->check.rise;
2287 srv->check.fall = src->check.fall;
2288
2289 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002290 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2291 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2292 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002293 srv->check.state |= CHK_ST_PAUSED;
2294 srv->check.health = 0;
2295 }
2296
2297 /* health: up but will fall down at first failure */
2298 srv->agent.rise = srv->agent.health = src->agent.rise;
2299 srv->agent.fall = src->agent.fall;
2300
2301 if (src->resolvers_id != NULL)
2302 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002303 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2304 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2305 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2306 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2307 srv->resolv_opts.family_prio = AF_INET6;
2308 memcpy(srv->resolv_opts.pref_net,
2309 src->resolv_opts.pref_net,
2310 sizeof srv->resolv_opts.pref_net);
2311 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002312
2313 srv->init_addr_methods = src->init_addr_methods;
2314 srv->init_addr = src->init_addr;
2315#if defined(USE_OPENSSL)
2316 srv_ssl_settings_cpy(srv, src);
2317#endif
2318#ifdef TCP_USER_TIMEOUT
2319 srv->tcp_ut = src->tcp_ut;
2320#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002321 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002322 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002323 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002324 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002325 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002326
Olivier Houchard8da5f982017-08-04 18:35:36 +02002327 if (srv_tmpl)
2328 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002329
2330 srv->check.via_socks4 = src->check.via_socks4;
2331 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002332}
2333
Willy Tarreau198e92a2021-03-05 10:23:32 +01002334/* allocate a server and attach it to the global servers_list. Returns
2335 * the server on success, otherwise NULL.
2336 */
William Lallemand313bfd12018-10-26 14:47:32 +02002337struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002338{
2339 struct server *srv;
2340
2341 srv = calloc(1, sizeof *srv);
2342 if (!srv)
2343 return NULL;
2344
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002345 srv_take(srv);
2346
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002347 srv->obj_type = OBJ_TYPE_SERVER;
2348 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002349 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002350 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002351 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002352 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002353 MT_LIST_INIT(&srv->prev_deleted);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002354 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002355
Emeric Brun52a91d32017-08-31 14:41:55 +02002356 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002357 srv->last_change = now.tv_sec;
2358
Christopher Faulet38290462020-04-21 11:46:40 +02002359 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002360 srv->check.status = HCHK_STATUS_INI;
2361 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002362 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002363 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002364
Christopher Faulet38290462020-04-21 11:46:40 +02002365 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002366 srv->agent.status = HCHK_STATUS_INI;
2367 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002368 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002369 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002370#if defined(USE_QUIC)
2371 srv->cids = EB_ROOT_UNIQUE;
2372#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002373
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002374 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002375#ifdef USE_OPENSSL
2376 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2377#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002378
Willy Tarreau975b1552019-06-06 16:25:55 +02002379 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002380 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002381 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002382 return srv;
2383}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002384
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002385/* Increment the server refcount. */
2386void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002387{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002388 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002389}
2390
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002391/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2392 * dynamic servers, its refcount is decremented first. The free operations are
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002393 * conducted only if the refcount is nul.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002394 *
2395 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002396 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002397 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002398struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002399{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002400 struct server *next = NULL;
2401
William Lallemand4c395fc2021-08-20 10:10:15 +02002402 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002403 goto end;
2404
2405 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002406
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002407 /* For dynamic servers, decrement the reference counter. Only free the
2408 * server when reaching zero.
2409 */
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002410 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2411 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002412
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002413 /* make sure we are removed from our 'next->prev_deleted' list
2414 * This doesn't require full thread isolation as we're using mt lists
2415 * However this could easily be turned into regular list if required
2416 * (with the proper use of thread isolation)
2417 */
2418 MT_LIST_DELETE(&srv->prev_deleted);
2419
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002420 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002421 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002422
2423 free(srv->id);
2424 free(srv->cookie);
2425 free(srv->hostname);
2426 free(srv->hostname_dn);
2427 free((char*)srv->conf.file);
2428 free(srv->per_thr);
Willy Tarreauc21a1872022-11-21 14:14:06 +01002429 free(srv->per_tgrp);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002430 free(srv->curr_idle_thr);
2431 free(srv->resolvers_id);
2432 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002433 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002434
Aurelien DARRAGONb5ee8be2023-03-09 14:28:00 +01002435 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2436 xprt_get(XPRT_SSL)->destroy_srv(srv);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002437 HA_SPIN_DESTROY(&srv->lock);
2438
Willy Tarreau2b718102021-04-21 07:32:39 +02002439 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002440 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002441
2442 EXTRA_COUNTERS_FREE(srv->extra_counters);
2443
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002444 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002445
2446 end:
2447 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002448}
2449
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002450/* Remove a server <srv> from a tracking list if <srv> is tracking another
2451 * server. No special care is taken if <srv> is tracked itself by another one :
2452 * this situation should be avoided by the caller.
2453 *
2454 * Not thread-safe.
2455 */
2456static void release_server_track(struct server *srv)
2457{
2458 struct server *strack = srv->track;
2459 struct server **base;
2460
2461 if (!strack)
2462 return;
2463
2464 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2465 if (*base == srv) {
2466 *base = srv->tracknext;
2467 return;
2468 }
2469 }
2470
2471 /* srv not found on the tracking list, this should never happen */
2472 BUG_ON(!*base);
2473}
2474
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002475/*
2476 * Parse as much as possible such a range string argument: low[-high]
2477 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2478 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2479 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2480 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002481 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002482static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2483 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002484{
2485 char *nb_high_arg;
2486
2487 *nb_high = 0;
2488 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002489 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002490
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002491 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002492 *nb_high_arg++ = '\0';
2493 *nb_high = atoi(nb_high_arg);
2494 }
2495 else {
2496 *nb_high += *nb_low;
2497 *nb_low = 1;
2498 }
2499
2500 if (*nb_low < 0 || *nb_high < *nb_low)
2501 return -1;
2502
2503 return 0;
2504}
2505
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002506/* Parse as much as possible such a range string argument: low[-high]
2507 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2508 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2509 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002510 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002511 * initialize a new server on startup.
2512 *
2513 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2514 * Returns 0 if succeeded, -1 if not.
2515 */
2516static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2517 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002518{
2519 chunk_printf(&trash, "%s%d", prefix, nb);
2520 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002521 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002522}
2523
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002524/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002525 * Note that a server template is a special server with
2526 * a few different parameters than a server which has
2527 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002528 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002529 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002530 * initialize a new server on startup.
2531 *
Joseph Herlant44466822018-11-15 08:57:51 -08002532 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002533 * 'srv' template included.
2534 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002535static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002536{
2537 int i;
2538 struct server *newsrv;
2539
2540 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 +02002541 newsrv = new_server(px);
2542 if (!newsrv)
2543 goto err;
2544
Christopher Faulet75bef002020-11-02 22:04:55 +01002545 newsrv->conf.file = strdup(srv->conf.file);
2546 newsrv->conf.line = srv->conf.line;
2547
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002548 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002549 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002550
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002551 if (newsrv->sni_expr) {
2552 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2553 if (!newsrv->ssl_ctx.sni)
2554 goto err;
2555 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002556
Emeric Brun34067662021-06-11 10:48:45 +02002557 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002558 if (newsrv->srvrq)
2559 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002560
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002561 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002562 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002563
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002564 /* Linked backwards first. This will be restablished after parsing. */
2565 newsrv->next = px->srv;
2566 px->srv = newsrv;
2567 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002568 _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 +02002569
2570 return i - srv->tmpl_info.nb_low;
2571
2572 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002573 _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 +02002574 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002575 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002576 free_check(&newsrv->agent);
2577 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002578 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002579 }
2580 free(newsrv);
2581 return i - srv->tmpl_info.nb_low;
2582}
2583
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002584/* Allocate a new server pointed by <srv> and try to parse the first arguments
2585 * in <args> as an address for a server or an address-range for a template or
2586 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2587 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002588 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002589 * initialize a new server on startup.
2590 *
2591 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2592 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2593 * <srv> will be set to NULL.
2594 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002595static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2596 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002597 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002598{
2599 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002600 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002601 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002602 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002603 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002604 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002605
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002606 *srv = NULL;
2607
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002608 /* There is no mandatory first arguments for default server. */
2609 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2610 if (parse_flags & SRV_PARSE_TEMPLATE) {
2611 if (!*args[3]) {
2612 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002613 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2614 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002615 err_code |= ERR_ALERT | ERR_FATAL;
2616 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002617 }
2618
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002619 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002620 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002621 else {
2622 if (!*args[2]) {
2623 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002624 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2625 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002626 err_code |= ERR_ALERT | ERR_FATAL;
2627 goto out;
2628 }
2629
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002630 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002631 }
2632
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002633 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002634 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2635 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002636 err_code |= ERR_ALERT | ERR_FATAL;
2637 goto out;
2638 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002639 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002640
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002641 *cur_arg = 2;
2642 if (parse_flags & SRV_PARSE_TEMPLATE) {
2643 /* Parse server-template <nb | range> arg. */
2644 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002645 ha_alert("Wrong %s number or range arg '%s'.\n",
2646 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002647 err_code |= ERR_ALERT | ERR_FATAL;
2648 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002649 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002650 (*cur_arg)++;
2651 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002652
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002653 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2654 struct sockaddr_storage *sk;
2655 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002656
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002657 *srv = newsrv = new_server(curproxy);
2658 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002659 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002660 err_code |= ERR_ALERT | ERR_ABORT;
2661 goto out;
2662 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002663 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002664
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002665 if (parse_flags & SRV_PARSE_TEMPLATE) {
2666 newsrv->tmpl_info.nb_low = tmpl_range_low;
2667 newsrv->tmpl_info.nb_high = tmpl_range_high;
2668 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002669
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002670 if (parse_flags & SRV_PARSE_DYNAMIC)
2671 newsrv->flags |= SRV_F_DYNAMIC;
2672
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002673 /* Note: for a server template, its id is its prefix.
2674 * This is a temporary id which will be used for server allocations to come
2675 * after parsing.
2676 */
2677 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2678 newsrv->id = strdup(args[1]);
2679 else
2680 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002681
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002682 /* revision defaults to 0 */
2683 newsrv->rid = 0;
2684
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002685 /* several ways to check the port component :
2686 * - IP => port=+0, relative (IPv4 only)
2687 * - IP: => port=+0, relative
2688 * - IP:N => port=N, absolute
2689 * - IP:+N => port=+N, relative
2690 * - IP:-N => port=-N, relative
2691 */
2692 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2693 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002694
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002695 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002696 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002697 (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);
2698 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002699 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002700 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002701 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002702 goto out;
2703 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002704
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002705 if (!port1 || !port2) {
2706 /* no port specified, +offset, -offset */
2707 newsrv->flags |= SRV_F_MAPPORTS;
2708 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002709
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002710 /* save hostname and create associated name resolution */
2711 if (fqdn) {
2712 if (fqdn[0] == '_') { /* SRV record */
2713 /* Check if a SRV request already exists, and if not, create it */
2714 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2715 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2716 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002717 err_code |= ERR_ALERT | ERR_FATAL;
2718 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002719 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002720 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002721 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002722 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002723 ha_alert("Can't create DNS resolution for server '%s'\n",
2724 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002725 err_code |= ERR_ALERT | ERR_FATAL;
2726 goto out;
2727 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002728 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002729
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002730 newsrv->addr = *sk;
2731 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002732 /*
2733 * we don't need to lock the server here, because
2734 * we are in the process of initializing.
2735 *
2736 * Note that the server is not attached into the proxy tree if
2737 * this is a dynamic server.
2738 */
2739 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002740
Willy Tarreau14e7f292021-10-27 17:41:07 +02002741 if (!newsrv->srvrq && !newsrv->hostname &&
2742 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002743 ha_alert("Unknown protocol family %d '%s'\n",
2744 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002745 err_code |= ERR_ALERT | ERR_FATAL;
2746 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002747 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002748
2749 (*cur_arg)++;
2750 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002751 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2752 /* Copy default server settings to new server */
2753 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2754 } else {
2755 /* Initialize dynamic server weight to 1 */
2756 newsrv->uweight = newsrv->iweight = 1;
2757
2758 /* A dynamic server is disabled on startup */
2759 newsrv->next_admin = SRV_ADMF_FMAINT;
2760 newsrv->next_state = SRV_ST_STOPPED;
2761 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002762
2763 /* Set default values for checks */
2764 newsrv->check.inter = DEF_CHKINTR;
2765 newsrv->check.rise = DEF_RISETIME;
2766 newsrv->check.fall = DEF_FALLTIME;
2767
2768 newsrv->agent.inter = DEF_CHKINTR;
2769 newsrv->agent.rise = DEF_AGENT_RISETIME;
2770 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002771 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002772 HA_SPIN_INIT(&newsrv->lock);
2773 }
2774 else {
2775 *srv = newsrv = &curproxy->defsrv;
2776 *cur_arg = 1;
2777 newsrv->resolv_opts.family_prio = AF_INET6;
2778 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002779 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002780
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002781 free(fqdn);
2782 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002783
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002784out:
2785 free(fqdn);
2786 return err_code;
2787}
Willy Tarreau272adea2014-03-31 10:39:59 +02002788
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002789/* Parse the server keyword in <args>.
2790 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2791 * might not be the case if an error is reported.
2792 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002793 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002794 * initialize a new server on startup.
2795 *
2796 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2797 * interrupted.
2798 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002799static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2800 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002801 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002802{
2803 int err_code = 0;
2804 struct srv_kw *kw;
2805 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002806 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002807
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002808 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002809 if (!kw) {
2810 best = srv_find_best_kw(args[*cur_arg]);
2811 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002812 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2813 args[*cur_arg], best,
2814 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2815 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002816 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002817 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2818 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2819 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002820
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002821 return ERR_ALERT | ERR_FATAL;
2822 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002823
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002824 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002825 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2826 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002827 err_code = ERR_ALERT | ERR_FATAL;
2828 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002829 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002830
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002831 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002832 ha_alert("'%s' option is not accepted in default-server sections\n",
2833 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002834 err_code = ERR_ALERT;
2835 goto out;
2836 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002837 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002838 ha_alert("'%s' option is not accepted for dynamic server\n",
2839 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002840 err_code |= ERR_ALERT;
2841 goto out;
2842 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002843
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002844 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2845 if (err_code) {
2846 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2847 free(errmsg);
2848 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002849
2850out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002851 if (kw->skip != -1)
2852 *cur_arg += 1 + kw->skip;
2853
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002854 return err_code;
2855}
2856
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002857/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002858 * initialize a new server on startup.
2859 */
2860static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2861 struct server *srv, struct proxy *proxy,
2862 char **errmsg)
2863{
2864 int ret;
2865
2866 if (!srv->sni_expr)
2867 return 0;
2868
2869 ret = server_parse_sni_expr(srv, proxy, errmsg);
2870 if (!ret)
2871 return 0;
2872
2873 return ret;
2874}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002875
2876/* Server initializations finalization.
2877 * Initialize health check, agent check and SNI expression if enabled.
2878 * Must not be called for a default server instance.
2879 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002880 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002881 * initialize a new server on startup.
2882 */
2883static int _srv_parse_finalize(char **args, int cur_arg,
2884 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002885 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002886{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002887 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002888 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002889
2890 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002891 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002892 return ERR_ALERT | ERR_FATAL;
2893 }
2894
2895 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002896 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2897 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002898 return ERR_ALERT | ERR_FATAL;
2899 }
2900
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002901 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2902 if (errmsg) {
2903 ha_alert("%s\n", errmsg);
2904 free(errmsg);
2905 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002906 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002907 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002908
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002909 /* A dynamic server is disabled on startup. It must not be counted as
2910 * an active backend entry.
2911 */
2912 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2913 if (srv->flags & SRV_F_BACKUP)
2914 px->srv_bck++;
2915 else
2916 px->srv_act++;
2917 }
2918
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002919 srv_lb_commit_status(srv);
2920
2921 return 0;
2922}
2923
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002924int parse_server(const char *file, int linenum, char **args,
2925 struct proxy *curproxy, const struct proxy *defproxy,
2926 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002927{
2928 struct server *newsrv = NULL;
2929 int err_code = 0;
2930
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002931 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002932
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002933 set_usermsgs_ctx(file, linenum, NULL);
2934
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002935 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002936 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002937 err_code |= ERR_ALERT | ERR_FATAL;
2938 goto out;
2939 }
2940 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2941 err_code |= ERR_ALERT | ERR_FATAL;
2942 goto out;
2943 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002944
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002945 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2946 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2947 if (!*args[2])
2948 return 0;
2949 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002950
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002951 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002952 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002953
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002954 /* the servers are linked backwards first */
2955 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2956 newsrv->next = curproxy->srv;
2957 curproxy->srv = newsrv;
2958 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002959
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002960 if (err_code & ERR_CODE)
2961 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002962
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002963 newsrv->conf.file = strdup(file);
2964 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002965
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002966 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002967 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002968 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002969 if (err_code & ERR_FATAL)
2970 goto out;
2971 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002972
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002973 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002974 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002975 if (err_code & ERR_FATAL)
2976 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002977 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002978
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002979 if (parse_flags & SRV_PARSE_TEMPLATE)
2980 _srv_parse_tmpl_init(newsrv, curproxy);
2981
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02002982 /* If the server id is fixed, insert it in the proxy used_id tree.
2983 * This is needed to detect a later duplicate id via srv_parse_id.
2984 *
2985 * If no is specified, a dynamic one is generated in
2986 * check_config_validity.
2987 */
2988 if (newsrv->flags & SRV_F_FORCED_ID)
2989 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
2990
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02002991 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002992 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02002993
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002994 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002995 return 0;
2996
2997 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002998 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002999 return err_code;
3000}
3001
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003002/* Returns a pointer to the first server matching either id <id>.
3003 * NULL is returned if no match is found.
3004 * the lookup is performed in the backend <bk>
3005 */
3006struct server *server_find_by_id(struct proxy *bk, int id)
3007{
3008 struct eb32_node *eb32;
3009 struct server *curserver;
3010
3011 if (!bk || (id ==0))
3012 return NULL;
3013
3014 /* <bk> has no backend capabilities, so it can't have a server */
3015 if (!(bk->cap & PR_CAP_BE))
3016 return NULL;
3017
3018 curserver = NULL;
3019
3020 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3021 if (eb32)
3022 curserver = container_of(eb32, struct server, conf.id);
3023
3024 return curserver;
3025}
3026
3027/* Returns a pointer to the first server matching either name <name>, or id
3028 * if <name> starts with a '#'. NULL is returned if no match is found.
3029 * the lookup is performed in the backend <bk>
3030 */
3031struct server *server_find_by_name(struct proxy *bk, const char *name)
3032{
3033 struct server *curserver;
3034
3035 if (!bk || !name)
3036 return NULL;
3037
3038 /* <bk> has no backend capabilities, so it can't have a server */
3039 if (!(bk->cap & PR_CAP_BE))
3040 return NULL;
3041
3042 curserver = NULL;
3043 if (*name == '#') {
3044 curserver = server_find_by_id(bk, atoi(name + 1));
3045 if (curserver)
3046 return curserver;
3047 }
3048 else {
3049 curserver = bk->srv;
3050
3051 while (curserver && (strcmp(curserver->id, name) != 0))
3052 curserver = curserver->next;
3053
3054 if (curserver)
3055 return curserver;
3056 }
3057
3058 return NULL;
3059}
3060
3061struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3062{
3063 struct server *byname;
3064 struct server *byid;
3065
3066 if (!name && !id)
3067 return NULL;
3068
3069 if (diff)
3070 *diff = 0;
3071
3072 byname = byid = NULL;
3073
3074 if (name) {
3075 byname = server_find_by_name(bk, name);
3076 if (byname && (!id || byname->puid == id))
3077 return byname;
3078 }
3079
3080 /* remaining possibilities :
3081 * - name not set
3082 * - name set but not found
3083 * - name found but ID doesn't match
3084 */
3085 if (id) {
3086 byid = server_find_by_id(bk, id);
3087 if (byid) {
3088 if (byname) {
3089 /* use id only if forced by configuration */
3090 if (byid->flags & SRV_F_FORCED_ID) {
3091 if (diff)
3092 *diff |= 2;
3093 return byid;
3094 }
3095 else {
3096 if (diff)
3097 *diff |= 1;
3098 return byname;
3099 }
3100 }
3101
3102 /* remaining possibilities:
3103 * - name not set
3104 * - name set but not found
3105 */
3106 if (name && diff)
3107 *diff |= 2;
3108 return byid;
3109 }
3110
3111 /* id bot found */
3112 if (byname) {
3113 if (diff)
3114 *diff |= 1;
3115 return byname;
3116 }
3117 }
3118
3119 return NULL;
3120}
3121
Simon Horman7d09b9a2013-02-12 10:45:51 +09003122/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003123 * update a server's current IP address.
3124 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3125 * ip is in network format.
3126 * updater is a string which contains an information about the requester of the update.
3127 * updater is used if not NULL.
3128 *
3129 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003130 *
3131 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003132 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003133int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003134{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003135 /* save the new IP family & address if necessary */
3136 switch (ip_sin_family) {
3137 case AF_INET:
3138 if (s->addr.ss_family == ip_sin_family &&
3139 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3140 return 0;
3141 break;
3142 case AF_INET6:
3143 if (s->addr.ss_family == ip_sin_family &&
3144 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3145 return 0;
3146 break;
3147 };
3148
Baptiste Assmann14e40142015-04-14 01:13:07 +02003149 /* generates a log line and a warning on stderr */
3150 if (1) {
3151 /* book enough space for both IPv4 and IPv6 */
3152 char oldip[INET6_ADDRSTRLEN];
3153 char newip[INET6_ADDRSTRLEN];
3154
3155 memset(oldip, '\0', INET6_ADDRSTRLEN);
3156 memset(newip, '\0', INET6_ADDRSTRLEN);
3157
3158 /* copy old IP address in a string */
3159 switch (s->addr.ss_family) {
3160 case AF_INET:
3161 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3162 break;
3163 case AF_INET6:
3164 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3165 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003166 default:
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003167 strlcpy2(oldip, "(none)", sizeof(oldip));
Christopher Fauletb0b76072020-09-08 10:38:40 +02003168 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003169 };
3170
3171 /* copy new IP address in a string */
3172 switch (ip_sin_family) {
3173 case AF_INET:
3174 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3175 break;
3176 case AF_INET6:
3177 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3178 break;
3179 };
3180
3181 /* save log line into a buffer */
3182 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3183 s->proxy->id, s->id, oldip, newip, updater);
3184
3185 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003186 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003187
3188 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003189 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003190 }
3191
3192 /* save the new IP family */
3193 s->addr.ss_family = ip_sin_family;
3194 /* save the new IP address */
3195 switch (ip_sin_family) {
3196 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003197 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003198 break;
3199 case AF_INET6:
3200 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3201 break;
3202 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003203 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003204 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003205
3206 return 0;
3207}
3208
William Dauchy7cabc062021-02-11 22:51:24 +01003209/* update agent health check address and port
3210 * addr can be ip4/ip6 or a hostname
3211 * if one error occurs, don't apply anything
3212 * must be called with the server lock held.
3213 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003214const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003215{
3216 struct sockaddr_storage sk;
3217 struct buffer *msg;
3218 int new_port;
3219
3220 msg = get_trash_chunk();
3221 chunk_reset(msg);
3222
3223 if (!(s->agent.state & CHK_ST_ENABLED)) {
3224 chunk_strcat(msg, "agent checks are not enabled on this server");
3225 goto out;
3226 }
3227 if (addr) {
3228 memset(&sk, 0, sizeof(struct sockaddr_storage));
3229 if (str2ip(addr, &sk) == NULL) {
3230 chunk_appendf(msg, "invalid addr '%s'", addr);
3231 goto out;
3232 }
3233 }
3234 if (port) {
3235 if (strl2irc(port, strlen(port), &new_port) != 0) {
3236 chunk_appendf(msg, "provided port is not an integer");
3237 goto out;
3238 }
3239 if (new_port < 0 || new_port > 65535) {
3240 chunk_appendf(msg, "provided port is invalid");
3241 goto out;
3242 }
3243 }
3244out:
3245 if (msg->data)
3246 return msg->area;
3247 else {
3248 if (addr)
3249 set_srv_agent_addr(s, &sk);
3250 if (port)
3251 set_srv_agent_port(s, new_port);
3252 }
3253 return NULL;
3254}
3255
William Dauchyb456e1f2021-02-11 22:51:23 +01003256/* update server health check address and port
3257 * addr must be ip4 or ip6, it won't be resolved
3258 * if one error occurs, don't apply anything
3259 * must be called with the server lock held.
3260 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003261const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003262{
3263 struct sockaddr_storage sk;
3264 struct buffer *msg;
3265 int new_port;
3266
3267 msg = get_trash_chunk();
3268 chunk_reset(msg);
3269
3270 if (!(s->check.state & CHK_ST_ENABLED)) {
3271 chunk_strcat(msg, "health checks are not enabled on this server");
3272 goto out;
3273 }
3274 if (addr) {
3275 memset(&sk, 0, sizeof(struct sockaddr_storage));
3276 if (str2ip2(addr, &sk, 0) == NULL) {
3277 chunk_appendf(msg, "invalid addr '%s'", addr);
3278 goto out;
3279 }
3280 }
3281 if (port) {
3282 if (strl2irc(port, strlen(port), &new_port) != 0) {
3283 chunk_appendf(msg, "provided port is not an integer");
3284 goto out;
3285 }
3286 if (new_port < 0 || new_port > 65535) {
3287 chunk_appendf(msg, "provided port is invalid");
3288 goto out;
3289 }
3290 /* prevent the update of port to 0 if MAPPORTS are in use */
3291 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3292 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3293 goto out;
3294 }
3295 }
3296out:
3297 if (msg->data)
3298 return msg->area;
3299 else {
3300 if (addr)
3301 s->check.addr = sk;
3302 if (port)
3303 s->check.port = new_port;
3304 }
3305 return NULL;
3306}
3307
Baptiste Assmann14e40142015-04-14 01:13:07 +02003308/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003309 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3310 *
3311 * Caller can pass its name through <updater> to get it integrated in the response message
3312 * returned by the function.
3313 *
3314 * The function first does the following, in that order:
3315 * - validates the new addr and/or port
3316 * - checks if an update is required (new IP or port is different than current ones)
3317 * - checks the update is allowed:
3318 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3319 * - allow all changes if no CHECKS are configured
3320 * - if CHECK is configured:
3321 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3322 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3323 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003324 *
3325 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003326 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003327const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003328{
3329 struct sockaddr_storage sa;
3330 int ret, port_change_required;
3331 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003332 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003333 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003334 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003335
3336 msg = get_trash_chunk();
3337 chunk_reset(msg);
3338
3339 if (addr) {
3340 memset(&sa, 0, sizeof(struct sockaddr_storage));
3341 if (str2ip2(addr, &sa, 0) == NULL) {
3342 chunk_printf(msg, "Invalid addr '%s'", addr);
3343 goto out;
3344 }
3345
3346 /* changes are allowed on AF_INET* families only */
3347 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3348 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3349 goto out;
3350 }
3351
3352 /* collecting data currently setup */
3353 memset(current_addr, '\0', sizeof(current_addr));
3354 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3355 /* changes are allowed on AF_INET* families only */
3356 if ((ret != AF_INET) && (ret != AF_INET6)) {
3357 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3358 goto out;
3359 }
3360
3361 /* applying ADDR changes if required and allowed
3362 * ipcmp returns 0 when both ADDR are the same
3363 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003364 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003365 chunk_appendf(msg, "no need to change the addr");
3366 goto port;
3367 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003368 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003369 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003370
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003371 /* update report for caller */
3372 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3373 }
3374
3375 port:
3376 if (port) {
3377 char sign = '\0';
3378 char *endptr;
3379
3380 if (addr)
3381 chunk_appendf(msg, ", ");
3382
3383 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003384 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003385
3386 /* check if PORT change is required */
3387 port_change_required = 0;
3388
3389 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003390 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003391 new_port = strtol(port, &endptr, 10);
3392 if ((errno != 0) || (port == endptr)) {
3393 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3394 goto out;
3395 }
3396
3397 /* check if caller triggers a port mapped or offset */
3398 if (sign == '-' || (sign == '+')) {
3399 /* check if server currently uses port map */
3400 if (!(s->flags & SRV_F_MAPPORTS)) {
3401 /* switch from fixed port to port map mandatorily triggers
3402 * a port change */
3403 port_change_required = 1;
3404 /* check is configured
3405 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3406 * prevent PORT change if check doesn't have it's dedicated port while switching
3407 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003408 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003409 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.");
3410 goto out;
3411 }
3412 }
3413 /* we're already using port maps */
3414 else {
3415 port_change_required = current_port != new_port;
3416 }
3417 }
3418 /* fixed port */
3419 else {
3420 port_change_required = current_port != new_port;
3421 }
3422
3423 /* applying PORT changes if required and update response message */
3424 if (port_change_required) {
3425 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003426 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003427 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003428
3429 /* prepare message */
3430 chunk_appendf(msg, "port changed from '");
3431 if (s->flags & SRV_F_MAPPORTS)
3432 chunk_appendf(msg, "+");
3433 chunk_appendf(msg, "%d' to '", current_port);
3434
3435 if (sign == '-') {
3436 s->flags |= SRV_F_MAPPORTS;
3437 chunk_appendf(msg, "%c", sign);
3438 /* just use for result output */
3439 new_port = -new_port;
3440 }
3441 else if (sign == '+') {
3442 s->flags |= SRV_F_MAPPORTS;
3443 chunk_appendf(msg, "%c", sign);
3444 }
3445 else {
3446 s->flags &= ~SRV_F_MAPPORTS;
3447 }
3448
3449 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003450 }
3451 else {
3452 chunk_appendf(msg, "no need to change the port");
3453 }
3454 }
3455
3456out:
William Dauchy6318d332020-05-02 21:52:36 +02003457 if (changed) {
3458 /* force connection cleanup on the given server */
3459 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003460 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003461 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003462 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003463 if (updater)
3464 chunk_appendf(msg, " by '%s'", updater);
3465 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003466 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003467}
3468
Christopher Faulet5efdef22021-03-11 18:03:21 +01003469/*
3470 * update server status based on result of SRV resolution
3471 * returns:
3472 * 0 if server status is updated
3473 * 1 if server status has not changed
3474 *
3475 * Must be called with the server lock held.
3476 */
3477int srvrq_update_srv_status(struct server *s, int has_no_ip)
3478{
3479 if (!s->srvrq)
3480 return 1;
3481
3482 /* since this server has an IP, it can go back in production */
3483 if (has_no_ip == 0) {
3484 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3485 return 1;
3486 }
3487
3488 if (s->next_admin & SRV_ADMF_RMAINT)
3489 return 1;
3490
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003491 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOENT);
Christopher Faulet5efdef22021-03-11 18:03:21 +01003492 return 0;
3493}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003494
3495/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003496 * update server status based on result of name resolution
3497 * returns:
3498 * 0 if server status is updated
3499 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003500 *
3501 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003502 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003503int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003504{
Emeric Brun750fe792020-12-23 16:51:12 +01003505 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003506 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003507 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003508
Jerome Magnin012261a2020-07-28 13:38:22 +02003509 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003510 if (resolution == NULL)
3511 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003512
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003513 switch (resolution->status) {
3514 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003515 /* status when HAProxy has just (re)started.
3516 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003517 break;
3518
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003519 case RSLV_STATUS_VALID:
3520 /*
3521 * resume health checks
3522 * server will be turned back on if health check is safe
3523 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003524 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003525 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003526 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003527 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NOIP);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003528 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003529 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003530
Emeric Brun52a91d32017-08-31 14:41:55 +02003531 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003532 return 1;
3533 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3534 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3535 s->proxy->id, s->id);
3536
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003537 ha_warning("%s.\n", trash.area);
3538 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003539 return 0;
3540
3541 case RSLV_STATUS_NX:
3542 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003543 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3544 if (!tick_is_expired(exp, now_ms))
3545 break;
3546
3547 if (s->next_admin & SRV_ADMF_RMAINT)
3548 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003549 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_NX);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003550 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003551
3552 case RSLV_STATUS_TIMEOUT:
3553 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003554 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3555 if (!tick_is_expired(exp, now_ms))
3556 break;
3557
3558 if (s->next_admin & SRV_ADMF_RMAINT)
3559 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003560 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_TIMEOUT);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003561 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003562
3563 case RSLV_STATUS_REFUSED:
3564 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003565 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3566 if (!tick_is_expired(exp, now_ms))
3567 break;
3568
3569 if (s->next_admin & SRV_ADMF_RMAINT)
3570 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003571 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_REFUSED);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003572 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003573
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003574 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003575 /* stop server if resolution failed for a long enough period */
3576 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3577 if (!tick_is_expired(exp, now_ms))
3578 break;
3579
3580 if (s->next_admin & SRV_ADMF_RMAINT)
3581 return 1;
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02003582 srv_set_admin_flag(s, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_DNS_UNSPEC);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003583 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003584 }
3585
3586 return 1;
3587}
3588
3589/*
3590 * Server Name Resolution valid response callback
3591 * It expects:
3592 * - <nameserver>: the name server which answered the valid response
3593 * - <response>: buffer containing a valid DNS response
3594 * - <response_len>: size of <response>
3595 * It performs the following actions:
3596 * - ignore response if current ip found and server family not met
3597 * - update with first new ip found if family is met and current IP is not found
3598 * returns:
3599 * 0 on error
3600 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003601 *
3602 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003603 */
Emeric Brun08622d32020-12-23 17:41:43 +01003604int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003605{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003606 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003607 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003608 void *serverip, *firstip;
3609 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003610 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003611 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003612 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003613
Christopher Faulet67957bd2017-09-27 11:00:59 +02003614 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003615 if (!s)
3616 return 1;
3617
Christopher Faulet49531e82021-03-10 15:07:27 +01003618 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003619 /* If DNS resolution is disabled ignore it.
3620 * This is the case if the server was associated to
3621 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003622 */
Emeric Brun34067662021-06-11 10:48:45 +02003623 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003624 return 1;
3625 }
3626
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003627 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003628 if (!resolution)
3629 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003630
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003631 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003632 firstip = NULL; /* pointer to the first valid response found */
3633 /* it will be used as the new IP if a change is required */
3634 firstip_sin_family = AF_UNSPEC;
3635 serverip = NULL; /* current server IP address */
3636
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003637 /* initializing server IP pointer */
3638 server_sin_family = s->addr.ss_family;
3639 switch (server_sin_family) {
3640 case AF_INET:
3641 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3642 break;
3643
3644 case AF_INET6:
3645 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3646 break;
3647
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003648 case AF_UNSPEC:
3649 break;
3650
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003651 default:
3652 goto invalid;
3653 }
3654
Emeric Brund30e9a12020-12-23 18:49:16 +01003655 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3656 serverip, server_sin_family, &firstip,
3657 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003658
3659 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003660 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003661 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003662
Emeric Brun456de772020-12-23 18:17:31 +01003663 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003664 goto save_ip;
3665
Emeric Brun456de772020-12-23 18:17:31 +01003666 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003667 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003668 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003669
Emeric Brun456de772020-12-23 18:17:31 +01003670 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003671 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003672 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003673 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003674 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003675
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003676 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003677 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003678 goto invalid;
3679
3680 }
3681
3682 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003683 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003684 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003685 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003686 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003687 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003688 else
3689 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003690 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003691
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003692 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003693 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3694 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003695 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003696
3697 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003698 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003699 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003700 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003701 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003702 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3703 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003704 return 0;
3705}
3706
3707/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003708 * SRV record error management callback
3709 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003710 * 0 if we can trash answser items.
3711 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003712 *
3713 * Grabs the server's lock.
3714 */
3715int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3716{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003717 struct resolv_srvrq *srvrq;
3718 struct resolv_resolution *res;
3719 struct resolvers *resolvers;
3720 int exp;
3721
3722 /* SRV records */
3723 srvrq = objt_resolv_srvrq(requester->owner);
3724 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003725 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003726
3727 resolvers = srvrq->resolvers;
3728 res = requester->resolution;
3729
3730 switch (res->status) {
3731
3732 case RSLV_STATUS_NX:
3733 /* stop server if resolution is NX for a long enough period */
3734 exp = tick_add(res->last_valid, resolvers->hold.nx);
3735 if (!tick_is_expired(exp, now_ms))
3736 return 1;
3737 break;
3738
3739 case RSLV_STATUS_TIMEOUT:
3740 /* stop server if resolution is TIMEOUT for a long enough period */
3741 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3742 if (!tick_is_expired(exp, now_ms))
3743 return 1;
3744 break;
3745
3746 case RSLV_STATUS_REFUSED:
3747 /* stop server if resolution is REFUSED for a long enough period */
3748 exp = tick_add(res->last_valid, resolvers->hold.refused);
3749 if (!tick_is_expired(exp, now_ms))
3750 return 1;
3751 break;
3752
3753 default:
3754 /* stop server if resolution failed for a long enough period */
3755 exp = tick_add(res->last_valid, resolvers->hold.other);
3756 if (!tick_is_expired(exp, now_ms))
3757 return 1;
3758 }
3759
Emeric Brun34067662021-06-11 10:48:45 +02003760 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003761 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003762
Emeric Brun12ca6582021-06-10 15:25:25 +02003763 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003764}
3765
3766/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003767 * Server Name Resolution error management callback
3768 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003769 * 0 if we can trash answser items.
3770 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003771 *
3772 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003773 */
Emeric Brun08622d32020-12-23 17:41:43 +01003774int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003775{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003776 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003777
Christopher Faulet67957bd2017-09-27 11:00:59 +02003778 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003779 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003780 return 0;
3781
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003782 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003783 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003784 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003785 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003786 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003787 return 0;
3788 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003789 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003790
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003791 return 1;
3792}
3793
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003794/*
3795 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003796 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003797 * It returns a pointer to the first server found or NULL if <ip> is not yet
3798 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003799 *
3800 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003801 */
3802struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3803{
3804 struct server *tmpsrv;
3805 struct proxy *be;
3806
3807 if (!srv)
3808 return NULL;
3809
3810 be = srv->proxy;
3811 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003812 /* we found the current server is the same, ignore it */
3813 if (srv == tmpsrv)
3814 continue;
3815
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003816 /* We want to compare the IP in the record with the IP of the servers in the
3817 * same backend, only if:
3818 * * DNS resolution is enabled on the server
3819 * * the hostname used for the resolution by our server is the same than the
3820 * one used for the server found in the backend
3821 * * the server found in the backend is not our current server
3822 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003823 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003824 if ((tmpsrv->hostname_dn == NULL) ||
3825 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003826 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003827 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003828 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003829 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003830 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003831
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003832 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003833 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003834 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003835 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003836 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003837
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003838 /* At this point, we have 2 different servers using the same DNS hostname
3839 * for their respective resolution.
3840 */
3841 if (*ip_family == tmpsrv->addr.ss_family &&
3842 ((tmpsrv->addr.ss_family == AF_INET &&
3843 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3844 (tmpsrv->addr.ss_family == AF_INET6 &&
3845 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003846 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003847 return tmpsrv;
3848 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003849 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003850 }
3851
Emeric Brune9fd6b52017-11-02 17:20:39 +01003852
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003853 return NULL;
3854}
3855
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003856/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3857 * resolver. This is suited for initial address configuration. Returns 0 on
3858 * success otherwise a non-zero error code. In case of error, *err_code, if
3859 * not NULL, is filled up.
3860 */
3861int srv_set_addr_via_libc(struct server *srv, int *err_code)
3862{
3863 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003864 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003865 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003866 return 1;
3867 }
3868 return 0;
3869}
3870
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003871/* Set the server's FDQN (->hostname) from <hostname>.
3872 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003873 *
3874 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003875 */
Emeric Brun08622d32020-12-23 17:41:43 +01003876int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003877{
Emeric Brun08622d32020-12-23 17:41:43 +01003878 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003879 char *hostname_dn;
3880 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003881
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003882 /* Note that the server lock is already held. */
3883 if (!srv->resolvers)
3884 return -1;
3885
Emeric Brun08622d32020-12-23 17:41:43 +01003886 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003887 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003888 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003889 * and we can't enable it at run time for now.
3890 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003891 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003892 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003893
3894 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003895 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003896 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003897 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003898 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003899 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003900 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003901
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003902 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003903 if (resolution &&
3904 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003905 resolution->hostname_dn_len == hostname_dn_len &&
3906 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003907 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003908
Willy Tarreau6878f802021-10-20 14:07:31 +02003909 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003910
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003911 free(srv->hostname);
3912 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003913 srv->hostname = strdup(hostname);
3914 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003915 srv->hostname_dn_len = hostname_dn_len;
3916 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003917 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003918
Baptiste Assmann13a92322019-06-07 09:40:55 +02003919 if (srv->flags & SRV_F_NO_RESOLUTION)
3920 goto end;
3921
Emeric Brund30e9a12020-12-23 18:49:16 +01003922 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003923 goto err;
3924
3925 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003926 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003927 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003928 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003929
3930 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003931 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003932 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003933 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003934}
3935
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003936/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3937 * from the state file. This is suited for initial address configuration.
3938 * Returns 0 on success otherwise a non-zero error code. In case of error,
3939 * *err_code, if not NULL, is filled up.
3940 */
3941static int srv_apply_lastaddr(struct server *srv, int *err_code)
3942{
3943 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3944 if (err_code)
3945 *err_code |= ERR_WARN;
3946 return 1;
3947 }
3948 return 0;
3949}
3950
Willy Tarreau25e51522016-11-04 15:10:17 +01003951/* returns 0 if no error, otherwise a combination of ERR_* flags */
3952static int srv_iterate_initaddr(struct server *srv)
3953{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003954 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003955 int return_code = 0;
3956 int err_code;
3957 unsigned int methods;
3958
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003959 /* If no addr and no hostname set, get the name from the DNS SRV request */
3960 if (!name && srv->srvrq)
3961 name = srv->srvrq->name;
3962
Willy Tarreau25e51522016-11-04 15:10:17 +01003963 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003964 if (!methods) {
3965 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003966 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3967 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003968 if (srv->resolvers_id) {
3969 /* dns resolution is configured, add "none" to not fail on startup */
3970 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3971 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003972 }
3973
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003974 /* "-dr" : always append "none" so that server addresses resolution
3975 * failures are silently ignored, this is convenient to validate some
3976 * configs out of their environment.
3977 */
3978 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3979 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3980
Willy Tarreau25e51522016-11-04 15:10:17 +01003981 while (methods) {
3982 err_code = 0;
3983 switch (srv_get_next_initaddr(&methods)) {
3984 case SRV_IADDR_LAST:
3985 if (!srv->lastaddr)
3986 continue;
3987 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003988 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003989 return_code |= err_code;
3990 break;
3991
3992 case SRV_IADDR_LIBC:
3993 if (!srv->hostname)
3994 continue;
3995 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003996 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003997 return_code |= err_code;
3998 break;
3999
Willy Tarreau37ebe122016-11-04 15:17:58 +01004000 case SRV_IADDR_NONE:
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02004001 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, SRV_ADM_STCHGC_NONE);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004002 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004003 ha_warning("could not resolve address '%s', disabling server.\n",
4004 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004005 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004006 return return_code;
4007
Willy Tarreau4310d362016-11-02 15:05:56 +01004008 case SRV_IADDR_IP:
4009 ipcpy(&srv->init_addr, &srv->addr);
4010 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004011 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4012 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004013 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004014 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004015
Willy Tarreau25e51522016-11-04 15:10:17 +01004016 default: /* unhandled method */
4017 break;
4018 }
4019 }
4020
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004021 if (!return_code)
4022 ha_alert("no method found to resolve address '%s'.\n", name);
4023 else
4024 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004025
4026 return_code |= ERR_ALERT | ERR_FATAL;
4027 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004028out:
4029 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004030 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004031 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004032}
4033
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004034/*
4035 * This function parses all backends and all servers within each backend
4036 * and performs servers' addr resolution based on information provided by:
4037 * - configuration file
4038 * - server-state file (states provided by an 'old' haproxy process)
4039 *
4040 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4041 */
4042int srv_init_addr(void)
4043{
4044 struct proxy *curproxy;
4045 int return_code = 0;
4046
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004047 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004048 while (curproxy) {
4049 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004050
4051 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004052 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004053 goto srv_init_addr_next;
4054
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004055 for (srv = curproxy->srv; srv; srv = srv->next) {
4056 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004057 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004058 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004059 reset_usermsgs_ctx();
4060 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004061
4062 srv_init_addr_next:
4063 curproxy = curproxy->next;
4064 }
4065
4066 return return_code;
4067}
4068
Willy Tarreau46b7f532018-08-21 11:54:26 +02004069/*
4070 * Must be called with the server lock held.
4071 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004072const 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 +02004073{
4074
Willy Tarreau83061a82018-07-13 11:56:34 +02004075 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004076
4077 msg = get_trash_chunk();
4078 chunk_reset(msg);
4079
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004080 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004081 chunk_appendf(msg, "no need to change the FDQN");
4082 goto out;
4083 }
4084
4085 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4086 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4087 goto out;
4088 }
4089
4090 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4091 server->proxy->id, server->id, server->hostname, fqdn);
4092
Emeric Brun08622d32020-12-23 17:41:43 +01004093 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004094 chunk_reset(msg);
4095 chunk_appendf(msg, "could not update %s/%s FQDN",
4096 server->proxy->id, server->id);
4097 goto out;
4098 }
4099
4100 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004101 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004102
4103 out:
4104 if (updater)
4105 chunk_appendf(msg, " by '%s'", updater);
4106 chunk_appendf(msg, "\n");
4107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004108 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004109}
4110
4111
Willy Tarreau21b069d2016-11-23 17:15:08 +01004112/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4113 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004114 * 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 +01004115 * used for CLI commands requiring a server name.
4116 * Important: the <arg> is modified to remove the '/'.
4117 */
4118struct server *cli_find_server(struct appctx *appctx, char *arg)
4119{
4120 struct proxy *px;
4121 struct server *sv;
4122 char *line;
4123
4124 /* split "backend/server" and make <line> point to server */
4125 for (line = arg; *line; line++)
4126 if (*line == '/') {
4127 *line++ = '\0';
4128 break;
4129 }
4130
4131 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004132 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004133 return NULL;
4134 }
4135
4136 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004137 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004138 return NULL;
4139 }
4140
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004141 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004142 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004143 return NULL;
4144 }
4145
4146 return sv;
4147}
4148
William Lallemand222baf22016-11-19 02:00:33 +01004149
Willy Tarreau46b7f532018-08-21 11:54:26 +02004150/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004151static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004152{
4153 struct server *sv;
4154 const char *warning;
4155
4156 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4157 return 1;
4158
4159 sv = cli_find_server(appctx, args[2]);
4160 if (!sv)
4161 return 1;
4162
4163 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004164 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004165 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004166 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004167 if (warning)
4168 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004169 }
4170 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004171 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004172 if (strcmp(args[4], "ready") == 0)
4173 srv_adm_set_ready(sv);
4174 else if (strcmp(args[4], "drain") == 0)
4175 srv_adm_set_drain(sv);
4176 else if (strcmp(args[4], "maint") == 0)
4177 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004178 else
4179 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004180 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004181 }
4182 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004183 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004184 if (sv->track)
4185 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004186 else if (strcmp(args[4], "up") == 0) {
4187 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004188 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004189 }
4190 else if (strcmp(args[4], "stopping") == 0) {
4191 sv->check.health = sv->check.rise + sv->check.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004192 srv_set_stopping(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004193 }
4194 else if (strcmp(args[4], "down") == 0) {
4195 sv->check.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004196 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004197 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004198 else
4199 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004200 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004201 }
4202 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004203 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004204 if (!(sv->agent.state & CHK_ST_ENABLED))
4205 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004206 else if (strcmp(args[4], "up") == 0) {
4207 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004208 srv_set_running(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004209 }
4210 else if (strcmp(args[4], "down") == 0) {
4211 sv->agent.health = 0;
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02004212 srv_set_stopped(sv, SRV_OP_STCHGC_CLI);
William Lallemand222baf22016-11-19 02:00:33 +01004213 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004214 else
4215 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004216 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004217 }
Misiek2da082d2017-01-09 09:40:42 +01004218 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004219 char *addr = NULL;
4220 char *port = NULL;
4221 if (strlen(args[4]) == 0) {
4222 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4223 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004224 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004225 }
4226 addr = args[4];
4227 if (strcmp(args[5], "port") == 0)
4228 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004229 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004230 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004231 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004232 if (warning)
4233 cli_msg(appctx, LOG_WARNING, warning);
4234 }
4235 else if (strcmp(args[3], "agent-port") == 0) {
4236 char *port = NULL;
4237 if (strlen(args[4]) == 0) {
4238 cli_err(appctx, "set server <b>/<s> agent-port requires"
4239 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004240 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004241 }
4242 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004243 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004244 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004245 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004246 if (warning)
4247 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004248 }
4249 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004250 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004251 if (!(sv->agent.state & CHK_ST_ENABLED))
4252 cli_err(appctx, "agent checks are not enabled on this server.\n");
4253 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004254 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004255 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004256 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004257 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004258 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004259 else if (strcmp(args[3], "check-addr") == 0) {
4260 char *addr = NULL;
4261 char *port = NULL;
4262 if (strlen(args[4]) == 0) {
4263 cli_err(appctx, "set server <b>/<s> check-addr requires"
4264 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004265 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004266 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004267 addr = args[4];
4268 if (strcmp(args[5], "port") == 0)
4269 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004270 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004271 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004272 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004273 if (warning)
4274 cli_msg(appctx, LOG_WARNING, warning);
4275 }
4276 else if (strcmp(args[3], "check-port") == 0) {
4277 char *port = NULL;
4278 if (strlen(args[4]) == 0) {
4279 cli_err(appctx, "set server <b>/<s> check-port requires"
4280 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004281 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004282 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004283 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004284 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004285 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004286 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004287 if (warning)
4288 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004289 }
4290 else if (strcmp(args[3], "addr") == 0) {
4291 char *addr = NULL;
4292 char *port = NULL;
4293 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004294 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004295 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004296 }
4297 else {
4298 addr = args[4];
4299 }
4300 if (strcmp(args[5], "port") == 0) {
4301 port = args[6];
4302 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004303 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004304 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004305 if (warning)
4306 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004307 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004308 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004309 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004310 else if (strcmp(args[3], "fqdn") == 0) {
4311 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004312 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004313 goto out;
4314 }
4315 if (!sv->resolvers) {
4316 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4317 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004318 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004319 if (sv->srvrq) {
4320 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004321 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004322 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004323 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4324 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004325 /* ensure runtime resolver will process this new fqdn */
4326 if (sv->flags & SRV_F_NO_RESOLUTION) {
4327 sv->flags &= ~SRV_F_NO_RESOLUTION;
4328 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004329 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004330 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004331 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004332 if (warning)
4333 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004334 }
William Dauchyf6370442020-11-14 19:25:33 +01004335 else if (strcmp(args[3], "ssl") == 0) {
4336#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004337 if (sv->flags & SRV_F_DYNAMIC) {
4338 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4339 goto out;
4340 }
4341
William Dauchyf6370442020-11-14 19:25:33 +01004342 if (sv->ssl_ctx.ctx == NULL) {
4343 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4344 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004345 goto out;
4346 }
4347
4348 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4349 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004350 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004351 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004352 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004353 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004354 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004355 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004356 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004357 }
4358 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004359 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004360 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4361#else
4362 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4363#endif
4364 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004365 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004366 "usage: set server <backend>/<server> "
4367 "addr | agent | agent-addr | agent-port | agent-send | "
4368 "check-addr | check-port | fqdn | health | ssl | "
4369 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004370 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004371 out:
William Lallemand222baf22016-11-19 02:00:33 +01004372 return 1;
4373}
4374
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004375static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004376{
William Lallemand6b160942016-11-22 12:34:35 +01004377 struct proxy *px;
4378 struct server *sv;
4379 char *line;
4380
4381
4382 /* split "backend/server" and make <line> point to server */
4383 for (line = args[2]; *line; line++)
4384 if (*line == '/') {
4385 *line++ = '\0';
4386 break;
4387 }
4388
Willy Tarreau9d008692019-08-09 11:21:01 +02004389 if (!*line)
4390 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004391
Willy Tarreau9d008692019-08-09 11:21:01 +02004392 if (!get_backend_server(args[2], line, &px, &sv))
4393 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004394
4395 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004396 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4397 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004398 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004399 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004400 return 1;
4401}
4402
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004403/* Parse a "set weight" command.
4404 *
4405 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004406 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004407static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004408{
4409 struct server *sv;
4410 const char *warning;
4411
4412 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4413 return 1;
4414
4415 sv = cli_find_server(appctx, args[2]);
4416 if (!sv)
4417 return 1;
4418
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004419 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4420
William Lallemand6b160942016-11-22 12:34:35 +01004421 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004422 if (warning)
4423 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004424
4425 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4426
William Lallemand6b160942016-11-22 12:34:35 +01004427 return 1;
4428}
4429
Willy Tarreau46b7f532018-08-21 11:54:26 +02004430/* parse a "set maxconn server" command. It always returns 1.
4431 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004432 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004433 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004434static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004435{
4436 struct server *sv;
4437 const char *warning;
4438
4439 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4440 return 1;
4441
4442 sv = cli_find_server(appctx, args[3]);
4443 if (!sv)
4444 return 1;
4445
Amaury Denoyelle02742862021-06-18 11:11:36 +02004446 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4447
Willy Tarreaub8026272016-11-23 11:26:56 +01004448 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004449 if (warning)
4450 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004451
Amaury Denoyelle02742862021-06-18 11:11:36 +02004452 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4453
Willy Tarreaub8026272016-11-23 11:26:56 +01004454 return 1;
4455}
William Lallemand6b160942016-11-22 12:34:35 +01004456
Willy Tarreau46b7f532018-08-21 11:54:26 +02004457/* parse a "disable agent" command. It always returns 1.
4458 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004459 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004460 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004461static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004462{
4463 struct server *sv;
4464
4465 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4466 return 1;
4467
4468 sv = cli_find_server(appctx, args[2]);
4469 if (!sv)
4470 return 1;
4471
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004472 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004473 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004474 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004475 return 1;
4476}
4477
Willy Tarreau46b7f532018-08-21 11:54:26 +02004478/* parse a "disable health" command. It always returns 1.
4479 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004480 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004481 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004482static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004483{
4484 struct server *sv;
4485
4486 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4487 return 1;
4488
4489 sv = cli_find_server(appctx, args[2]);
4490 if (!sv)
4491 return 1;
4492
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004493 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004494 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004495 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004496 return 1;
4497}
4498
Willy Tarreau46b7f532018-08-21 11:54:26 +02004499/* parse a "disable server" command. It always returns 1.
4500 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004501 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004502 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004503static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004504{
4505 struct server *sv;
4506
4507 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4508 return 1;
4509
4510 sv = cli_find_server(appctx, args[2]);
4511 if (!sv)
4512 return 1;
4513
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004514 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004515 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004516 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004517 return 1;
4518}
4519
Willy Tarreau46b7f532018-08-21 11:54:26 +02004520/* parse a "enable agent" command. It always returns 1.
4521 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004522 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004523 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004524static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004525{
4526 struct server *sv;
4527
4528 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4529 return 1;
4530
4531 sv = cli_find_server(appctx, args[2]);
4532 if (!sv)
4533 return 1;
4534
Willy Tarreau9d008692019-08-09 11:21:01 +02004535 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4536 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004537
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004538 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004539 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004540 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004541 return 1;
4542}
4543
Willy Tarreau46b7f532018-08-21 11:54:26 +02004544/* parse a "enable health" command. It always returns 1.
4545 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004546 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004547 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004548static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004549{
4550 struct server *sv;
4551
4552 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4553 return 1;
4554
4555 sv = cli_find_server(appctx, args[2]);
4556 if (!sv)
4557 return 1;
4558
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004559 if (!(sv->check.state & CHK_ST_CONFIGURED))
4560 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4561
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004562 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004563 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004564 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004565 return 1;
4566}
4567
Willy Tarreau46b7f532018-08-21 11:54:26 +02004568/* parse a "enable server" command. It always returns 1.
4569 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004570 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004572static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004573{
4574 struct server *sv;
4575
4576 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4577 return 1;
4578
4579 sv = cli_find_server(appctx, args[2]);
4580 if (!sv)
4581 return 1;
4582
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004583 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004584 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004585 if (!(sv->flags & SRV_F_COOKIESET)
4586 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4587 sv->cookie)
4588 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004589 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004590 return 1;
4591}
4592
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004593/* Allocates data structure related to load balancing for the server <sv>. It
4594 * is only required for dynamic servers.
4595 *
4596 * At the moment, the server lock is not used as this function is only called
4597 * for a dynamic server not yet registered.
4598 *
4599 * Returns 1 on success, 0 on allocation failure.
4600 */
4601static int srv_alloc_lb(struct server *sv, struct proxy *be)
4602{
4603 int node;
4604
4605 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4606 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4607 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4608 sv->lb_nodes_now = 0;
4609
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004610 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4611 ((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 +01004612 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4613
4614 if (!sv->lb_nodes)
4615 return 0;
4616
4617 for (node = 0; node < sv->lb_nodes_tot; node++) {
4618 sv->lb_nodes[node].server = sv;
4619 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4620 }
4621 }
4622
4623 return 1;
4624}
4625
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004626/* updates the server's weight during a warmup stage. Once the final weight is
4627 * reached, the task automatically stops. Note that any server status change
4628 * must have updated s->last_change accordingly.
4629 */
4630static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4631{
4632 struct server *s = context;
4633
4634 /* by default, plan on stopping the task */
4635 t->expire = TICK_ETERNITY;
4636 if ((s->next_admin & SRV_ADMF_MAINT) ||
4637 (s->next_state != SRV_ST_STARTING))
4638 return t;
4639
4640 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4641
4642 /* recalculate the weights and update the state */
4643 server_recalc_eweight(s, 1);
4644
4645 /* probably that we can refill this server with a bit more connections */
4646 pendconn_grab_from_px(s);
4647
4648 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4649
4650 /* get back there in 1 second or 1/20th of the slowstart interval,
4651 * whichever is greater, resulting in small 5% steps.
4652 */
4653 if (s->next_state == SRV_ST_STARTING)
4654 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4655 return t;
4656}
4657
4658/* Allocate the slowstart task if the server is configured with a slowstart
4659 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4660 *
4661 * Returns 0 on success else non-zero.
4662 */
4663static int init_srv_slowstart(struct server *srv)
4664{
4665 struct task *t;
4666
4667 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004668 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004669 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4670 return ERR_ALERT | ERR_FATAL;
4671 }
4672
4673 /* We need a warmup task that will be called when the server
4674 * state switches from down to up.
4675 */
4676 srv->warmup = t;
4677 t->process = server_warmup;
4678 t->context = srv;
4679
4680 /* server can be in this state only because of */
4681 if (srv->next_state == SRV_ST_STARTING) {
4682 task_schedule(srv->warmup,
4683 tick_add(now_ms,
4684 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4685 }
4686 }
4687
4688 return ERR_NONE;
4689}
4690REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4691
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004692/* Memory allocation and initialization of the per_thr field.
4693 * Returns 0 if the field has been successfully initialized, -1 on failure.
4694 */
4695int srv_init_per_thr(struct server *srv)
4696{
4697 int i;
4698
4699 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004700 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4701 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004702 return -1;
4703
4704 for (i = 0; i < global.nbthread; i++) {
4705 srv->per_thr[i].idle_conns = EB_ROOT;
4706 srv->per_thr[i].safe_conns = EB_ROOT;
4707 srv->per_thr[i].avail_conns = EB_ROOT;
4708 MT_LIST_INIT(&srv->per_thr[i].streams);
4709 }
4710
4711 return 0;
4712}
4713
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004714/* Parse a "add server" command
4715 * Returns 0 if the server has been successfully initialized, 1 on failure.
4716 */
4717static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4718{
4719 struct proxy *be;
4720 struct server *srv;
4721 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004722 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004723 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004724 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4725
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004726 usermsgs_clr("CLI");
4727
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004728 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4729 return 1;
4730
4731 ++args;
4732
4733 sv_name = be_name = args[1];
4734 /* split backend/server arg */
4735 while (*sv_name && *(++sv_name)) {
4736 if (*sv_name == '/') {
4737 *sv_name = '\0';
4738 ++sv_name;
4739 break;
4740 }
4741 }
4742
4743 if (!*sv_name)
4744 return cli_err(appctx, "Require 'backend/server'.");
4745
Amaury Denoyellecece9182021-04-20 17:09:08 +02004746 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004747 if (!be)
4748 return cli_err(appctx, "No such backend.");
4749
4750 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004751 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004752 return 1;
4753 }
4754
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004755 /* At this point, some operations might not be thread-safe anymore. This
4756 * might be the case for parsing handlers which were designed to run
4757 * only at the starting stage on single-thread mode.
4758 *
4759 * Activate thread isolation to ensure thread-safety.
4760 */
4761 thread_isolate();
4762
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004763 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004764 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4765 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004766 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004767
4768 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004769 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004770
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004771 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004772 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004773 }
4774
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004775 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4776 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004777 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004778
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004779 /* A dynamic server does not currently support resolution.
4780 *
4781 * Initialize it explicitly to the "none" method to ensure no
4782 * resolution will ever be executed.
4783 */
4784 srv->init_addr_methods = SRV_IADDR_NONE;
4785
Amaury Denoyelle30467232021-03-12 18:03:27 +01004786 if (srv->mux_proto) {
4787 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004788 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004789 goto out;
4790 }
4791 }
4792
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004793 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004794 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004795 goto out;
4796 }
4797
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004798 if (srv->max_idle_conns != 0) {
4799 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4800 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004801 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004802 goto out;
4803 }
4804 }
4805
4806 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004807 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004808 goto out;
4809 }
4810
4811 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4812 COUNTERS_SV,
4813 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004814 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004815 goto out;
4816 }
4817
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004818 /* ensure minconn/maxconn consistency */
4819 srv_minmax_conn_apply(srv);
4820
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004821 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4822 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004823 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4824 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4825 goto out;
4826 }
4827 }
4828
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004829 if (srv->trackit) {
4830 if (srv_apply_track(srv, be))
4831 goto out;
4832 }
4833
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004834 /* Init check/agent if configured. The check is manually disabled
4835 * because a dynamic server is started in a disable state. It must be
4836 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004837 */
4838 if (srv->do_check) {
4839 if (init_srv_check(srv))
4840 goto out;
4841
4842 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004843 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004844
4845 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004846 if (init_srv_agent_check(srv))
4847 goto out;
4848
4849 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004850 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004851
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004852 /* Init slowstart if needed. */
4853 if (init_srv_slowstart(srv))
4854 goto out;
4855
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004856 /* Attach the server to the end of the proxy linked list. Note that this
4857 * operation is not thread-safe so this is executed under thread
4858 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004859 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004860 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004861 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004862
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004863 /* TODO use a double-linked list for px->srv */
4864 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004865 struct server *next = be->srv;
4866
4867 while (1) {
4868 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004869 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004870 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004871 goto out;
4872 }
4873
4874 if (!next->next)
4875 break;
4876
4877 next = next->next;
4878 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004879
4880 next->next = srv;
4881 }
4882 else {
4883 srv->next = be->srv;
4884 be->srv = srv;
4885 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004886
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004887 /* generate the server id if not manually specified */
4888 if (!srv->puid) {
4889 next_id = get_next_id(&be->conf.used_server_id, 1);
4890 if (!next_id) {
4891 ha_alert("Cannot attach server : no id left in proxy\n");
4892 goto out;
4893 }
4894
4895 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004896 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004897 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004898
4899 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004900 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4901 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004902 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4903 if (srv->addr_node.key)
4904 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004905
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004906 /* check if LSB bit (odd bit) is set for reuse_cnt */
4907 if (srv_id_reuse_cnt & 1) {
4908 /* cnt must be increased */
4909 srv_id_reuse_cnt++;
4910 }
4911 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4912 * save some space
4913 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
4914 */
4915 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
4916
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01004917 /* adding server cannot fail when we reach this:
4918 * publishing EVENT_HDL_SUB_SERVER_ADD
4919 */
4920 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
4921
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004922 thread_release();
4923
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004924 /* Start the check task. The server must be fully initialized.
4925 *
4926 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4927 * need to randomly spread the task interval for dynamic servers.
4928 */
4929 if (srv->check.state & CHK_ST_CONFIGURED) {
4930 if (!start_check_task(&srv->check, 0, 1, 1))
4931 ha_alert("System might be unstable, consider to execute a reload");
4932 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004933 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004934 if (!start_check_task(&srv->agent, 0, 1, 1))
4935 ha_alert("System might be unstable, consider to execute a reload");
4936 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004937
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004938 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004939 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004940
4941 return 0;
4942
4943out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004944 if (srv) {
4945 if (srv->track)
4946 release_server_track(srv);
4947
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004948 if (srv->check.state & CHK_ST_CONFIGURED)
4949 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004950 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004951 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004952
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004953 /* remove the server from the proxy linked list */
4954 if (be->srv == srv) {
4955 be->srv = srv->next;
4956 }
4957 else {
4958 struct server *prev;
4959 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4960 ;
4961 if (prev)
4962 prev->next = srv->next;
4963 }
4964
4965 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004966
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004967 thread_release();
4968
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004969 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004970 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004971
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004972 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004973 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004974
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004975 return 1;
4976}
4977
Amaury Denoyellee5580432021-04-15 14:41:20 +02004978/* Parse a "del server" command
4979 * Returns 0 if the server has been successfully initialized, 1 on failure.
4980 */
4981static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4982{
4983 struct proxy *be;
4984 struct server *srv;
4985 char *be_name, *sv_name;
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01004986 struct server *prev_del;
Amaury Denoyellee5580432021-04-15 14:41:20 +02004987
4988 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4989 return 1;
4990
4991 ++args;
4992
4993 sv_name = be_name = args[1];
4994 /* split backend/server arg */
4995 while (*sv_name && *(++sv_name)) {
4996 if (*sv_name == '/') {
4997 *sv_name = '\0';
4998 ++sv_name;
4999 break;
5000 }
5001 }
5002
5003 if (!*sv_name)
5004 return cli_err(appctx, "Require 'backend/server'.");
5005
5006 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005007 * requires thread isolation. In addition, any place referencing the
5008 * server about to be deleted would be unsafe after our operation, so
5009 * we must be certain to be alone so that no other thread has even
5010 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005011 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005012 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005013
5014 get_backend_server(be_name, sv_name, &be, &srv);
5015 if (!be) {
5016 cli_err(appctx, "No such backend.");
5017 goto out;
5018 }
5019
5020 if (!srv) {
5021 cli_err(appctx, "No such server.");
5022 goto out;
5023 }
5024
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005025 if (srv->flags & SRV_F_NON_PURGEABLE) {
5026 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 +02005027 goto out;
5028 }
5029
5030 /* Only servers in maintenance can be deleted. This ensures that the
5031 * server is not present anymore in the lb structures (through
5032 * lbprm.set_server_status_down).
5033 */
5034 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5035 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5036 goto out;
5037 }
5038
5039 /* Ensure that there is no active/idle/pending connection on the server.
5040 *
5041 * TODO idle connections should not prevent server deletion. A proper
5042 * cleanup function should be implemented to be used here.
5043 */
5044 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005045 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005046 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5047 goto out;
5048 }
5049
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005050 /* removing cannot fail anymore when we reach this:
5051 * publishing EVENT_HDL_SUB_SERVER_DEL
5052 */
5053 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5054
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005055 /* remove srv from tracking list */
5056 if (srv->track)
5057 release_server_track(srv);
5058
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005059 /* stop the check task if running */
5060 if (srv->check.state & CHK_ST_CONFIGURED)
5061 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005062 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005063 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005064
5065 /* detach the server from the proxy linked list
5066 * The proxy servers list is currently not protected by a lock, so this
5067 * requires thread_isolate/release.
5068 */
5069
5070 /* be->srv cannot be empty since we have already found the server with
5071 * get_backend_server */
5072 BUG_ON(!be->srv);
5073 if (be->srv == srv) {
5074 be->srv = srv->next;
5075 }
5076 else {
5077 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005078 for (next = be->srv; srv != next->next; next = next->next) {
5079 /* srv cannot be not found since we have already found
5080 * it with get_backend_server */
5081 BUG_ON(!next);
5082 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005083
Amaury Denoyellee5580432021-04-15 14:41:20 +02005084 next->next = srv->next;
5085 }
5086
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005087 /* Some deleted servers could still point to us using their 'next',
5088 * update them as needed
5089 * Please note the small race between the POP and APPEND, although in
5090 * this situation this is not an issue as we are under full thread
5091 * isolation
5092 */
5093 while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) {
5094 /* update its 'next' ptr */
5095 prev_del->next = srv->next;
5096 if (srv->next) {
5097 /* now it is our 'next' responsibility */
5098 MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted);
5099 }
5100 }
5101
5102 /* we ourselves need to inform our 'next' that we will still point it */
5103 if (srv->next)
5104 MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted);
5105
Amaury Denoyellee5580432021-04-15 14:41:20 +02005106 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005107 eb32_delete(&srv->conf.id);
5108 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005109 if (srv->addr_node.key)
5110 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005111
5112 /* remove srv from idle_node tree for idle conn cleanup */
5113 eb32_delete(&srv->idle_node);
5114
Aurelien DARRAGON75b9d1c2023-01-24 14:40:01 +01005115 /* flag the server as deleted
5116 * (despite the server being removed from primary server list,
5117 * one could still access the server data from a valid ptr)
5118 * Deleted flag helps detecting when a server is in transient removal
5119 * state.
5120 * ie: removed from the list but not yet freed/purged from memory.
5121 */
5122 srv->flags |= SRV_F_DELETED;
5123
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005124 /* set LSB bit (odd bit) for reuse_cnt */
5125 srv_id_reuse_cnt |= 1;
5126
Amaury Denoyellee5580432021-04-15 14:41:20 +02005127 thread_release();
5128
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005129 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005130 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005131
5132 cli_msg(appctx, LOG_INFO, "Server deleted.");
5133
5134 return 0;
5135
5136out:
5137 thread_release();
5138
5139 return 1;
5140}
5141
William Lallemand222baf22016-11-19 02:00:33 +01005142/* register cli keywords */
5143static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005144 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5145 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005146 { { "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 +02005147 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5148 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005149 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5150 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5151 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5152 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5153 { { "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 +01005154 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5155 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005156 {{},}
5157}};
5158
Willy Tarreau0108d902018-11-25 19:14:37 +01005159INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005160
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005161/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5162 * field is used to retrieve the identifier of the tracked server, either with
5163 * the format "proxy/server" or just "server". <curproxy> must point to the
5164 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5165 * to find the tracked server.
5166 *
5167 * Returns 0 if the server track has been activated else non-zero.
5168 *
5169 * Not thread-safe.
5170 */
5171int srv_apply_track(struct server *srv, struct proxy *curproxy)
5172{
5173 struct proxy *px;
5174 struct server *strack, *loop;
5175 char *pname, *sname;
5176
5177 if (!srv->trackit)
5178 return 1;
5179
5180 pname = srv->trackit;
5181 sname = strrchr(pname, '/');
5182
5183 if (sname) {
5184 *sname++ = '\0';
5185 }
5186 else {
5187 sname = pname;
5188 pname = NULL;
5189 }
5190
5191 if (pname) {
5192 px = proxy_be_by_name(pname);
5193 if (!px) {
5194 ha_alert("unable to find required proxy '%s' for tracking.\n",
5195 pname);
5196 return 1;
5197 }
5198 }
5199 else {
5200 px = curproxy;
5201 }
5202
5203 strack = findserver(px, sname);
5204 if (!strack) {
5205 ha_alert("unable to find required server '%s' for tracking.\n",
5206 sname);
5207 return 1;
5208 }
5209
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005210 if (strack->flags & SRV_F_DYNAMIC) {
5211 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5212 px->id, strack->id);
5213 return 1;
5214 }
5215
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005216 if (!strack->do_check && !strack->do_agent && !strack->track &&
5217 !strack->trackit) {
5218 ha_alert("unable to use %s/%s for "
5219 "tracking as it does not have any check nor agent enabled.\n",
5220 px->id, strack->id);
5221 return 1;
5222 }
5223
5224 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5225 ;
5226
5227 if (srv == strack || loop) {
5228 ha_alert("unable to track %s/%s as it "
5229 "belongs to a tracking chain looping back to %s/%s.\n",
5230 px->id, strack->id, px->id,
5231 srv == strack ? strack->id : loop->id);
5232 return 1;
5233 }
5234
5235 if (curproxy != px &&
5236 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5237 ha_alert("unable to use %s/%s for"
5238 "tracking: disable-on-404 option inconsistency.\n",
5239 px->id, strack->id);
5240 return 1;
5241 }
5242
5243 srv->track = strack;
5244 srv->tracknext = strack->trackers;
5245 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005246 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005247
5248 ha_free(&srv->trackit);
5249
5250 return 0;
5251}
5252
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005253/* This function propagates srv state change to lb algorithms */
5254static void srv_lb_propagate(struct server *s)
5255{
5256 struct proxy *px = s->proxy;
5257
5258 if (px->lbprm.update_server_eweight)
5259 px->lbprm.update_server_eweight(s);
5260 else if (srv_willbe_usable(s)) {
5261 if (px->lbprm.set_server_status_up)
5262 px->lbprm.set_server_status_up(s);
5263 }
5264 else {
5265 if (px->lbprm.set_server_status_down)
5266 px->lbprm.set_server_status_down(s);
5267 }
5268}
5269
Emeric Brun64cc49c2017-10-03 14:46:45 +02005270/*
Aurelien DARRAGON0ddf0522023-03-27 11:57:28 +02005271 * This function applies server's status changes.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005272 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005273 * Must be called with the server lock held. This may also be called at init
5274 * time as the result of parsing the state file, in which case no lock will be
5275 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005276 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005277static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005278{
Emeric Brun64cc49c2017-10-03 14:46:45 +02005279 int xferred;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005280 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5281 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005282 enum srv_state srv_prev_state = s->cur_state;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005283 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005284 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005285
Emeric Brun64cc49c2017-10-03 14:46:45 +02005286 /* If currently main is not set we try to apply pending state changes */
5287 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5288 int next_admin;
5289
5290 /* Backup next admin */
5291 next_admin = s->next_admin;
5292
5293 /* restore current admin state */
5294 s->next_admin = s->cur_admin;
5295
5296 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005297 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005298
5299 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5300 srv_shutdown_streams(s, SF_ERR_DOWN);
5301
5302 /* we might have streams queued on this server and waiting for
5303 * a connection. Those which are redispatchable will be queued
5304 * to another server or to the proxy itself.
5305 */
5306 xferred = pendconn_redistribute(s);
5307
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005308 /* no maintenance + server DOWN: publish event SERVER DOWN */
5309 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5310
Emeric Brun64cc49c2017-10-03 14:46:45 +02005311 tmptrash = alloc_trash_chunk();
5312 if (tmptrash) {
5313 chunk_printf(tmptrash,
5314 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5315 s->proxy->id, s->id);
5316
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02005317 srv_append_op_chg_cause(tmptrash, s);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005318 srv_append_more(tmptrash, s, xferred, 0);
5319
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005320 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005321
5322 /* we don't send an alert if the server was previously paused */
5323 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005324 send_log(s->proxy, log_level, "%s.\n",
5325 tmptrash->area);
5326 send_email_alert(s, log_level, "%s",
5327 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005328 free_trash_chunk(tmptrash);
5329 tmptrash = NULL;
5330 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005331 }
5332 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005333 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005334
5335 /* we might have streams queued on this server and waiting for
5336 * a connection. Those which are redispatchable will be queued
5337 * to another server or to the proxy itself.
5338 */
5339 xferred = pendconn_redistribute(s);
5340
5341 tmptrash = alloc_trash_chunk();
5342 if (tmptrash) {
5343 chunk_printf(tmptrash,
5344 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5345 s->proxy->id, s->id);
5346
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02005347 srv_append_op_chg_cause(tmptrash, s);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005348 srv_append_more(tmptrash, s, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005349
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005350 ha_warning("%s.\n", tmptrash->area);
5351 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5352 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005353 free_trash_chunk(tmptrash);
5354 tmptrash = NULL;
5355 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005356 }
5357 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5358 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005359
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005360 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005361 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5362
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005363 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005364 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005365 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005366
5367 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5368 * and it's not a backup server and its effective weight is > 0,
5369 * then it can accept new connections, so we shut down all streams
5370 * on all backup servers.
5371 */
5372 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5373 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5374 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5375
5376 /* check if we can handle some connections queued at the proxy. We
5377 * will take as many as we can handle.
5378 */
5379 xferred = pendconn_grab_from_px(s);
5380
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005381 /* no maintenance + server going UP: publish event SERVER UP */
5382 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5383
Emeric Brun64cc49c2017-10-03 14:46:45 +02005384 tmptrash = alloc_trash_chunk();
5385 if (tmptrash) {
5386 chunk_printf(tmptrash,
5387 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5388 s->proxy->id, s->id);
5389
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02005390 srv_append_op_chg_cause(tmptrash, s);
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005391 srv_append_more(tmptrash, s, xferred, 0);
5392
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005393 ha_warning("%s.\n", tmptrash->area);
5394 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5395 tmptrash->area);
5396 send_email_alert(s, LOG_NOTICE, "%s",
5397 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005398 free_trash_chunk(tmptrash);
5399 tmptrash = NULL;
5400 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005401 }
5402 else if (s->cur_eweight != s->next_eweight) {
5403 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005404 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005405 }
5406
5407 s->next_admin = next_admin;
5408 }
5409
Aurelien DARRAGON1746b562023-04-04 10:17:40 +02005410 /* reset operational change cause */
5411 s->op_st_chg_cause = SRV_OP_STCHGC_NONE;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005412
5413 /* Now we try to apply pending admin changes */
5414
5415 /* Maintenance must also disable health checks */
5416 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5417 if (s->check.state & CHK_ST_ENABLED) {
5418 s->check.state |= CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005419 s->check.health = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005420 }
5421
5422 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005423 tmptrash = alloc_trash_chunk();
5424 if (tmptrash) {
5425 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005426 "%sServer %s/%s was DOWN and now enters maintenance",
5427 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
5428 srv_append_adm_chg_cause(tmptrash, s);
5429 srv_append_more(tmptrash, s, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005430
Olivier Houchard796a2b32017-10-24 17:42:47 +02005431 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005432 ha_warning("%s.\n", tmptrash->area);
5433 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5434 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005435 }
5436 free_trash_chunk(tmptrash);
5437 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005438 }
5439 }
5440 else { /* server was still running */
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005441 s->check.health = 0; /* failure */
Emeric Brune3114802017-12-21 14:42:26 +01005442
5443 s->next_state = SRV_ST_STOPPED;
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005444 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005445
Emeric Brun64cc49c2017-10-03 14:46:45 +02005446 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5447 srv_shutdown_streams(s, SF_ERR_DOWN);
5448
William Dauchy6318d332020-05-02 21:52:36 +02005449 /* force connection cleanup on the given server */
5450 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005451 /* we might have streams queued on this server and waiting for
5452 * a connection. Those which are redispatchable will be queued
5453 * to another server or to the proxy itself.
5454 */
5455 xferred = pendconn_redistribute(s);
5456
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005457 /* maintenance on previously running server: publish event SERVER DOWN */
5458 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5459
Emeric Brun64cc49c2017-10-03 14:46:45 +02005460 tmptrash = alloc_trash_chunk();
5461 if (tmptrash) {
5462 chunk_printf(tmptrash,
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005463 "%sServer %s/%s is going DOWN for maintenance",
Emeric Brun64cc49c2017-10-03 14:46:45 +02005464 s->flags & SRV_F_BACKUP ? "Backup " : "",
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005465 s->proxy->id, s->id);
5466 srv_append_adm_chg_cause(tmptrash, s);
5467 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005468
5469 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005470 ha_warning("%s.\n", tmptrash->area);
5471 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5472 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005473 }
5474 free_trash_chunk(tmptrash);
5475 tmptrash = NULL;
5476 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005477 }
5478 }
5479 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5480 /* OK here we're leaving maintenance, we have many things to check,
5481 * because the server might possibly be coming back up depending on
5482 * its state. In practice, leaving maintenance means that we should
5483 * immediately turn to UP (more or less the slowstart) under the
5484 * following conditions :
5485 * - server is neither checked nor tracked
5486 * - server tracks another server which is not checked
5487 * - server tracks another server which is already up
5488 * Which sums up as something simpler :
5489 * "either the tracking server is up or the server's checks are disabled
5490 * or up". Otherwise we only re-enable health checks. There's a special
5491 * case associated to the stopping state which can be inherited. Note
5492 * that the server might still be in drain mode, which is naturally dealt
5493 * with by the lower level functions.
5494 */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005495 if (s->check.state & CHK_ST_ENABLED) {
5496 s->check.state &= ~CHK_ST_PAUSED;
Aurelien DARRAGONc54b98a2023-04-18 12:08:18 +02005497 s->check.health = s->check.rise; /* start OK but check immediately */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005498 }
5499
5500 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5501 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5502 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5503 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5504 s->next_state = SRV_ST_STOPPING;
5505 }
5506 else {
5507 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005508 if (s->slowstart > 0) {
5509 if (s->warmup)
5510 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5511 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005512 else
5513 s->next_state = SRV_ST_RUNNING;
5514 }
5515
5516 }
5517
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005518 /* ignore if server stays down when leaving maintenance mode */
5519 if (s->next_state != SRV_ST_STOPPED) {
5520 /* leaving maintenance + server UP: publish event SERVER UP */
5521 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5522 }
5523
Emeric Brun64cc49c2017-10-03 14:46:45 +02005524 tmptrash = alloc_trash_chunk();
5525 if (tmptrash) {
5526 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5527 chunk_printf(tmptrash,
5528 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5529 s->flags & SRV_F_BACKUP ? "Backup " : "",
5530 s->proxy->id, s->id,
5531 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5532 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5533 }
5534 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5535 chunk_printf(tmptrash,
5536 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5537 s->flags & SRV_F_BACKUP ? "Backup " : "",
5538 s->proxy->id, s->id, s->hostname,
5539 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5540 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5541 }
5542 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5543 chunk_printf(tmptrash,
5544 "%sServer %s/%s is %s/%s (leaving maintenance)",
5545 s->flags & SRV_F_BACKUP ? "Backup " : "",
5546 s->proxy->id, s->id,
5547 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5548 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5549 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005550 ha_warning("%s.\n", tmptrash->area);
5551 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5552 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005553 free_trash_chunk(tmptrash);
5554 tmptrash = NULL;
5555 }
5556
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005557 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005558 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005559 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005560
Willy Tarreau6a78e612018-08-07 10:14:53 +02005561 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5562 * and it's not a backup server and its effective weight is > 0,
5563 * then it can accept new connections, so we shut down all streams
5564 * on all backup servers.
5565 */
5566 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5567 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5568 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5569
5570 /* check if we can handle some connections queued at the proxy. We
5571 * will take as many as we can handle.
5572 */
5573 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005574 }
5575 else if (s->next_admin & SRV_ADMF_MAINT) {
5576 /* remaining in maintenance mode, let's inform precisely about the
5577 * situation.
5578 */
5579 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5580 tmptrash = alloc_trash_chunk();
5581 if (tmptrash) {
5582 chunk_printf(tmptrash,
5583 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5584 s->flags & SRV_F_BACKUP ? "Backup " : "",
5585 s->proxy->id, s->id);
5586
5587 if (s->track) /* normally it's mandatory here */
5588 chunk_appendf(tmptrash, " via %s/%s",
5589 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005590 ha_warning("%s.\n", tmptrash->area);
5591 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5592 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005593 free_trash_chunk(tmptrash);
5594 tmptrash = NULL;
5595 }
5596 }
5597 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5598 tmptrash = alloc_trash_chunk();
5599 if (tmptrash) {
5600 chunk_printf(tmptrash,
5601 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5602 s->flags & SRV_F_BACKUP ? "Backup " : "",
5603 s->proxy->id, s->id, s->hostname);
5604
5605 if (s->track) /* normally it's mandatory here */
5606 chunk_appendf(tmptrash, " via %s/%s",
5607 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005608 ha_warning("%s.\n", tmptrash->area);
5609 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5610 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005611 free_trash_chunk(tmptrash);
5612 tmptrash = NULL;
5613 }
5614 }
5615 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5616 tmptrash = alloc_trash_chunk();
5617 if (tmptrash) {
5618 chunk_printf(tmptrash,
5619 "%sServer %s/%s remains in forced maintenance",
5620 s->flags & SRV_F_BACKUP ? "Backup " : "",
5621 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005622 ha_warning("%s.\n", tmptrash->area);
5623 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5624 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005625 free_trash_chunk(tmptrash);
5626 tmptrash = NULL;
5627 }
5628 }
5629 /* don't report anything when leaving drain mode and remaining in maintenance */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005630 }
5631
5632 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5633 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5634 /* drain state is applied only if not yet in maint */
5635
Aurelien DARRAGON85b91372023-04-18 12:02:48 +02005636 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005637
5638 /* we might have streams queued on this server and waiting for
5639 * a connection. Those which are redispatchable will be queued
5640 * to another server or to the proxy itself.
5641 */
5642 xferred = pendconn_redistribute(s);
5643
5644 tmptrash = alloc_trash_chunk();
5645 if (tmptrash) {
Aurelien DARRAGONf3b48a82023-04-14 18:07:09 +02005646 chunk_printf(tmptrash, "%sServer %s/%s enters drain state",
5647 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id);
5648 srv_append_adm_chg_cause(tmptrash, s);
5649 srv_append_more(tmptrash, s, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005650
5651 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005652 ha_warning("%s.\n", tmptrash->area);
5653 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5654 tmptrash->area);
5655 send_email_alert(s, LOG_NOTICE, "%s",
5656 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005657 }
5658 free_trash_chunk(tmptrash);
5659 tmptrash = NULL;
5660 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005661 }
5662 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5663 /* OK completely leaving drain mode */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005664 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005665
5666 tmptrash = alloc_trash_chunk();
5667 if (tmptrash) {
Aurelien DARRAGON9a1df022023-03-27 10:17:31 +02005668 if (s->cur_admin & SRV_ADMF_FDRAIN) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005669 chunk_printf(tmptrash,
5670 "%sServer %s/%s is %s (leaving forced drain)",
5671 s->flags & SRV_F_BACKUP ? "Backup " : "",
5672 s->proxy->id, s->id,
5673 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5674 }
5675 else {
5676 chunk_printf(tmptrash,
5677 "%sServer %s/%s is %s (leaving drain)",
5678 s->flags & SRV_F_BACKUP ? "Backup " : "",
5679 s->proxy->id, s->id,
5680 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5681 if (s->track) /* normally it's mandatory here */
5682 chunk_appendf(tmptrash, " via %s/%s",
5683 s->track->proxy->id, s->track->id);
5684 }
5685
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005686 ha_warning("%s.\n", tmptrash->area);
5687 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5688 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005689 free_trash_chunk(tmptrash);
5690 tmptrash = NULL;
5691 }
5692
5693 /* now propagate the status change to any LB algorithms */
Aurelien DARRAGON8bbe6432023-04-17 13:53:56 +02005694 srv_lb_propagate(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005695 }
5696 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5697 /* remaining in drain mode after removing one of its flags */
5698
5699 tmptrash = alloc_trash_chunk();
5700 if (tmptrash) {
5701 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5702 chunk_printf(tmptrash,
5703 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5704 s->flags & SRV_F_BACKUP ? "Backup " : "",
5705 s->proxy->id, s->id);
5706
5707 if (s->track) /* normally it's mandatory here */
5708 chunk_appendf(tmptrash, " via %s/%s",
5709 s->track->proxy->id, s->track->id);
5710 }
5711 else {
5712 chunk_printf(tmptrash,
5713 "%sServer %s/%s remains in forced drain mode",
5714 s->flags & SRV_F_BACKUP ? "Backup " : "",
5715 s->proxy->id, s->id);
5716 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005717 ha_warning("%s.\n", tmptrash->area);
5718 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5719 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005720 free_trash_chunk(tmptrash);
5721 tmptrash = NULL;
5722 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005723 }
5724 }
5725
Aurelien DARRAGON9b1ccd72023-04-03 17:40:28 +02005726 /* Re-set adm st change to none */
5727 s->adm_st_chg_cause = SRV_ADM_STCHGC_NONE;
Aurelien DARRAGON22151c72023-04-17 17:45:58 +02005728
5729 /* explicitly commit state changes (even if it was already applied implicitly
5730 * by some lb state change function), so we don't miss anything
5731 */
5732 srv_lb_commit_status(s);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005733
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005734 /* check if server stats must be updated due the the server state change */
5735 if (srv_prev_state != s->cur_state) {
5736 if (srv_prev_state == SRV_ST_STOPPED) {
5737 /* server was down and no longer is */
5738 if (s->last_change < now.tv_sec) // ignore negative times
5739 s->down_time += now.tv_sec - s->last_change;
5740 }
Aurelien DARRAGON5f80f8b2023-04-19 18:22:21 +02005741 else if (s->cur_state == SRV_ST_STOPPED) {
5742 /* server was up and is currently down */
5743 s->counters.down_trans++;
5744 }
Aurelien DARRAGON9f5853f2023-04-18 13:52:27 +02005745 s->last_change = now.tv_sec;
5746 }
5747
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005748 /* check if backend stats must be updated due to the server state change */
5749 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5750 set_backend_down(s->proxy); /* backend going down */
5751 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) {
5752 /* backend was down and is back up again:
5753 * no helper function, updating last_change and backend downtime stats
5754 */
5755 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5756 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5757 s->proxy->last_change = now.tv_sec;
5758 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005759}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005760
Willy Tarreau144f84a2021-03-02 16:09:26 +01005761struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005762{
5763 struct connection *conn;
5764
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005765 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005766 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005767 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005768 }
5769
5770 return task;
5771}
5772
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005773/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005774 * moving them all.
5775 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005776 *
5777 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005778 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005779static 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 +02005780{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005781 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005782 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005783 int i = 0;
5784
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005785 node = eb_first(idle_tree);
5786 while (node) {
5787 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005788 if (toremove_nb != -1 && i >= toremove_nb)
5789 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005790
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005791 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005792 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005793 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005794 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005795
5796 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005797 }
5798 return i;
5799}
William Dauchy6318d332020-05-02 21:52:36 +02005800/* cleanup connections for a given server
5801 * might be useful when going on forced maintenance or live changing ip/port
5802 */
William Dauchy707ad322020-05-04 13:52:40 +02005803static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005804{
William Dauchy6318d332020-05-02 21:52:36 +02005805 int did_remove;
5806 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005807
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005808 /* nothing to do if pool-max-conn is null */
5809 if (!srv->max_idle_conns)
5810 return;
5811
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005812 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005813 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005814 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005815 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005816 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 +02005817 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005818 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 +02005819 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005820 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005821 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005822 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005823
5824 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5825 break;
William Dauchy6318d332020-05-02 21:52:36 +02005826 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005827}
5828
5829/* removes an idle conn after updating the server idle conns counters */
5830void srv_release_conn(struct server *srv, struct connection *conn)
5831{
5832 if (conn->flags & CO_FL_LIST_MASK) {
5833 /* The connection is currently in the server's idle list, so tell it
5834 * there's one less connection available in that list.
5835 */
5836 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5837 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5838 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5839 }
5840 else {
5841 /* The connection is not private and not in any server's idle
5842 * list, so decrement the current number of used connections
5843 */
5844 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5845 }
5846
5847 /* Remove the connection from any tree (safe, idle or available) */
5848 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5849 conn_delete_from_tree(&conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01005850 conn->flags &= ~CO_FL_LIST_MASK;
Willy Tarreau260f3242021-10-06 18:30:04 +02005851 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5852}
5853
5854/* retrieve a connection from its <hash> in <tree>
5855 * returns NULL if no connection found
5856 */
5857struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5858{
Willy Tarreau85223482022-09-29 20:32:43 +02005859 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005860 struct connection *conn = NULL;
5861 struct conn_hash_node *hash_node = NULL;
5862
Willy Tarreau85223482022-09-29 20:32:43 +02005863 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005864 if (node) {
5865 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5866 conn = hash_node->conn;
5867 }
5868
5869 return conn;
5870}
5871
5872/* retrieve the next connection sharing the same hash as <conn>
5873 * returns NULL if no connection found
5874 */
5875struct connection *srv_lookup_conn_next(struct connection *conn)
5876{
Willy Tarreau85223482022-09-29 20:32:43 +02005877 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005878 struct connection *next_conn = NULL;
5879 struct conn_hash_node *hash_node = NULL;
5880
Willy Tarreau85223482022-09-29 20:32:43 +02005881 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005882 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005883 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005884 next_conn = hash_node->conn;
5885 }
5886
5887 return next_conn;
5888}
5889
5890/* This adds an idle connection to the server's list if the connection is
5891 * reusable, not held by any owner anymore, but still has available streams.
5892 */
5893int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5894{
5895 /* we try to keep the connection in the server's idle list
5896 * if we don't have too many FD in use, and if the number of
5897 * idle+current conns is lower than what was observed before
5898 * last purge, or if we already don't have idle conns for the
5899 * current thread and we don't exceed last count by global.nbthread.
5900 */
5901 if (!(conn->flags & CO_FL_PRIVATE) &&
5902 srv && srv->pool_purge_delay > 0 &&
5903 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
5904 ha_used_fds < global.tune.pool_high_count &&
5905 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
5906 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
5907 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
5908 (ha_used_fds < global.tune.pool_low_count &&
5909 (srv->curr_used_conns + srv->curr_idle_conns <=
5910 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
5911 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
5912 int retadd;
5913
5914 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
5915 if (retadd > srv->max_idle_conns) {
5916 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5917 return 0;
5918 }
5919 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5920
5921 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5922 conn_delete_from_tree(&conn->hash_node->node);
5923
5924 if (is_safe) {
5925 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005926 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005927 _HA_ATOMIC_INC(&srv->curr_safe_nb);
5928 } else {
5929 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005930 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005931 _HA_ATOMIC_INC(&srv->curr_idle_nb);
5932 }
5933 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5934 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
5935
5936 __ha_barrier_full();
5937 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5938 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5939 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5940 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5941 now_ms);
5942 eb32_insert(&idle_conn_srv, &srv->idle_node);
5943 if (!task_in_wq(idle_conn_task) && !
5944 task_in_rq(idle_conn_task)) {
5945 task_schedule(idle_conn_task,
5946 srv->idle_node.key);
5947 }
5948
5949 }
5950 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5951 }
5952 return 1;
5953 }
5954 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02005955}
5956
Willy Tarreau144f84a2021-03-02 16:09:26 +01005957struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005958{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005959 struct server *srv;
5960 struct eb32_node *eb;
5961 int i;
5962 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005963
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005964 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005965 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5966 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005967 int exceed_conns;
5968 int to_kill;
5969 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005970
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005971 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5972 if (!eb) {
5973 /* we might have reached the end of the tree, typically because
5974 * <now_ms> is in the first half and we're first scanning the last
5975 * half. Let's loop back to the beginning of the tree now.
5976 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005977
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005978 eb = eb32_first(&idle_conn_srv);
5979 if (likely(!eb))
5980 break;
5981 }
5982 if (tick_is_lt(now_ms, eb->key)) {
5983 /* timer not expired yet, revisit it later */
5984 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005985 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005986 }
5987 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005988
5989 /* Calculate how many idle connections we want to kill :
5990 * we want to remove half the difference between the total
5991 * of established connections (used or idle) and the max
5992 * number of used connections.
5993 */
5994 curr_idle = srv->curr_idle_conns;
5995 if (curr_idle == 0)
5996 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005997 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005998 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005999
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006000 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006001 if (srv->est_need_conns < srv->max_used_conns)
6002 srv->est_need_conns = srv->max_used_conns;
6003
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006004 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006005
Willy Tarreau18ed7892020-07-02 19:05:30 +02006006 if (exceed_conns <= 0)
6007 goto remove;
6008
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006009 /* check all threads starting with ours */
6010 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006011 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006012 int j;
6013 int did_remove = 0;
6014
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006015 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6016 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006017
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006018 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006019 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 +02006020 if (j > 0)
6021 did_remove = 1;
6022 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006023 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 +01006024 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006025 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006026
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006027 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006028 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006029
6030 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6031 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006032 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006033remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006034 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006035
6036 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006037 /* There are still more idle connections, add the
6038 * server back in the tree.
6039 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006040 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006041 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006042 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006043 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006044 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006045 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6046
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006047 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006048 return task;
6049}
Olivier Houchard88698d92019-04-16 19:07:22 +02006050
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006051/* Close remaining idle connections. This functions is designed to be run on
6052 * process shutdown. This guarantees a proper socket shutdown to avoid
6053 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6054 * bypassed.
6055 *
6056 * This function is not thread-safe so it must only be called via a global
6057 * deinit function.
6058 */
6059static void srv_close_idle_conns(struct server *srv)
6060{
6061 struct eb_root **cleaned_tree;
6062 int i;
6063
6064 for (i = 0; i < global.nbthread; ++i) {
6065 struct eb_root *conn_trees[] = {
6066 &srv->per_thr[i].idle_conns,
6067 &srv->per_thr[i].safe_conns,
6068 &srv->per_thr[i].avail_conns,
6069 NULL
6070 };
6071
6072 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6073 while (!eb_is_empty(*cleaned_tree)) {
6074 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6075 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6076 struct connection *conn = conn_hash_node->conn;
6077
6078 if (conn->ctrl->ctrl_close)
6079 conn->ctrl->ctrl_close(conn);
6080 ebmb_delete(node);
6081 }
6082 }
6083 }
6084}
6085
6086REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6087
Willy Tarreau76cc6992020-07-01 18:49:24 +02006088/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6089static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006090 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006091 char **err)
6092{
6093 if (too_many_args(1, args, err, NULL))
6094 return -1;
6095
6096 if (strcmp(args[1], "on") == 0)
6097 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6098 else if (strcmp(args[1], "off") == 0)
6099 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6100 else {
6101 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6102 return -1;
6103 }
6104 return 0;
6105}
6106
Olivier Houchard88698d92019-04-16 19:07:22 +02006107/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6108static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006109 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006110 char **err)
6111{
6112 int arg = -1;
6113
6114 if (too_many_args(1, args, err, NULL))
6115 return -1;
6116
6117 if (*(args[1]) != 0)
6118 arg = atoi(args[1]);
6119
6120 if (arg < 0 || arg > 100) {
6121 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6122 return -1;
6123 }
6124
6125 if (args[0][10] == 'h')
6126 global.tune.pool_high_ratio = arg;
6127 else
6128 global.tune.pool_low_ratio = arg;
6129 return 0;
6130}
6131
6132/* config keyword parsers */
6133static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006134 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006135 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6136 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6137 { 0, NULL, NULL }
6138}};
6139
6140INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6141
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006142/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006143 * Local variables:
6144 * c-indent-level: 8
6145 * c-basic-offset: 8
6146 * End:
6147 */