blob: 12475375c1204e5953ecdae3e4b5cd9968477cb3 [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
Simon Hormana3608442013-11-01 16:46:15 +0900107int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +0200108{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +0100109 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200110 return s->down_time;
111
112 return now.tv_sec - s->last_change + s->down_time;
113}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500115int srv_lastsession(const struct server *s)
116{
117 if (s->counters.last_sess)
118 return now.tv_sec - s->counters.last_sess;
119
120 return -1;
121}
122
Simon Horman4a741432013-02-23 15:35:38 +0900123int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +0200124{
Simon Horman4a741432013-02-23 15:35:38 +0900125 const struct server *s = check->server;
126
Aurelien DARRAGON7d541a92022-12-07 12:17:24 +0100127 if ((check->state & (CHK_ST_CONFIGURED|CHK_ST_FASTINTER)) == CHK_ST_CONFIGURED &&
128 (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900129 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100130
Emeric Brun52a91d32017-08-31 14:41:55 +0200131 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900132 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100133
Simon Horman4a741432013-02-23 15:35:38 +0900134 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100135}
136
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137/*
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100138 * Use this to publish EVENT_HDL_SUB_SERVER family type event
139 * from srv facility
140 * Event will be published in both global subscription list and
141 * server dedicated subscription list
142 * server ptr must be valid
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100143 * must be called with srv lock or under thread_isolate
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100144 */
145static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate)
146{
147 struct event_hdl_cb_data_server cb_data;
148
149 /* safe data assignments */
150 cb_data.safe.puid = srv->puid;
151 cb_data.safe.rid = srv->rid;
152 cb_data.safe.flags = srv->flags;
153 snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id);
154 if (srv->proxy)
155 snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id);
156 /* unsafe data assignments */
157 cb_data.unsafe.ptr = srv;
158 cb_data.unsafe.thread_isolate = thread_isolate;
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100159 cb_data.unsafe.srv_lock = !thread_isolate;
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100160 /* publish in server dedicated sub list */
161 event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data));
162 /* publish in global subscription list */
163 event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data));
164}
165
166/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100167 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200168 * Unlikely, but it can happen. The server's proxy must be at least
169 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100170 */
171static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100172{
173 struct proxy *p = s->proxy;
174 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100175
176 for (tmpserv = p->srv; tmpserv != NULL;
177 tmpserv = tmpserv->next) {
178 if (tmpserv == s)
179 continue;
180 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
181 continue;
182 if (tmpserv->cookie &&
183 strcmp(tmpserv->cookie, s->cookie) == 0) {
184 ha_warning("We generated two equal cookies for two different servers.\n"
185 "Please change the secret key for '%s'.\n",
186 s->proxy->id);
187 }
188 }
189
190}
191
Willy Tarreau46b7f532018-08-21 11:54:26 +0200192/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200193 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200194 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100195void srv_set_dyncookie(struct server *s)
196{
197 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100198 char *tmpbuf;
199 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100200 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100201 size_t buffer_len;
202 int addr_len;
203 int port;
204
Willy Tarreau595e7672020-10-20 17:30:08 +0200205 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200206
Olivier Houchard4e694042017-03-14 20:01:29 +0100207 if ((s->flags & SRV_F_COOKIESET) ||
208 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
209 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200210 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100211 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100212
213 if (s->addr.ss_family != AF_INET &&
214 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200215 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100216 /*
217 * Buffer to calculate the cookie value.
218 * The buffer contains the secret key + the server IP address
219 * + the TCP port.
220 */
221 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
222 /*
223 * The TCP port should use only 2 bytes, but is stored in
224 * an unsigned int in struct server, so let's use 4, to be
225 * on the safe side.
226 */
227 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200228 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100229 memcpy(tmpbuf, p->dyncookie_key, key_len);
230 memcpy(&(tmpbuf[key_len]),
231 s->addr.ss_family == AF_INET ?
232 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
233 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
234 addr_len);
235 /*
236 * Make sure it's the same across all the load balancers,
237 * no matter their endianness.
238 */
239 port = htonl(s->svc_port);
240 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
241 hash_value = XXH64(tmpbuf, buffer_len, 0);
242 memprintf(&s->cookie, "%016llx", hash_value);
243 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200244 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100245 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100246
247 /* Don't bother checking if the dyncookie is duplicated if
248 * the server is marked as "disabled", maybe it doesn't have
249 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100250 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100251 if (!(s->next_admin & SRV_ADMF_FMAINT))
252 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200253 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200254 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100255}
256
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200257/* Returns true if it's possible to reuse an idle connection from server <srv>
258 * for a websocket stream. This is the case if server is configured to use the
259 * same protocol for both HTTP and websocket streams. This depends on the value
260 * of "proto", "alpn" and "ws" keywords.
261 */
262int srv_check_reuse_ws(struct server *srv)
263{
264 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
265 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
266 * for mux selection.
267 */
268 const struct ist srv_mux = srv->mux_proto ?
269 srv->mux_proto->token : IST_NULL;
270
271 switch (srv->ws) {
272 /* "auto" means use the same protocol : reuse is possible. */
273 case SRV_WS_AUTO:
274 return 1;
275
276 /* "h2" means use h2 for websocket : reuse is possible if
277 * server mux is h2.
278 */
279 case SRV_WS_H2:
280 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
281 return 1;
282 break;
283
284 /* "h1" means use h1 for websocket : reuse is possible if
285 * server mux is h1.
286 */
287 case SRV_WS_H1:
288 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
289 return 1;
290 break;
291 }
292 }
293 else {
294 /* ALPN selection.
295 * Based on the assumption that only "h2" and "http/1.1" token
296 * are used on server ALPN.
297 */
298 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
299 srv->ssl_ctx.alpn_len);
300
301 switch (srv->ws) {
302 case SRV_WS_AUTO:
303 /* for auto mode, consider reuse as possible if the
304 * server uses a single protocol ALPN
305 */
306 if (!istchr(alpn, ','))
307 return 1;
308 break;
309
310 case SRV_WS_H2:
311 return isteq(alpn, ist("\x02h2"));
312
313 case SRV_WS_H1:
314 return isteq(alpn, ist("\x08http/1.1"));
315 }
316 }
317
318 return 0;
319}
320
321/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
322 * is a valid value indicating to use the fallback mux.
323 */
324const struct mux_ops *srv_get_ws_proto(struct server *srv)
325{
326 const struct mux_proto_list *mux = NULL;
327
328 switch (srv->ws) {
329 case SRV_WS_AUTO:
330 mux = srv->mux_proto;
331 break;
332
333 case SRV_WS_H1:
334 mux = get_mux_proto(ist("h1"));
335 break;
336
337 case SRV_WS_H2:
338 mux = get_mux_proto(ist("h2"));
339 break;
340 }
341
342 return mux ? mux->mux : NULL;
343}
344
Willy Tarreau21faa912012-10-10 08:27:36 +0200345/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200346 * Must be called with the server lock held. The server is first removed from
347 * the proxy tree if it was already attached. If <reattach> is true, the server
348 * will then be attached in the proxy tree. The proxy lock is held to
349 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700350 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200351static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700352{
353 struct proxy *p = s->proxy;
354 char *key;
355
356 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
357
358 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700359 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700360 free(key);
361 return;
362 }
363
364 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
365 ebpt_delete(&s->addr_node);
366 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
367
368 free(s->addr_node.key);
369 }
370
371 s->addr_node.key = key;
372
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200373 if (reattach) {
374 if (s->addr_node.key) {
375 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
376 ebis_insert(&p->used_server_addr, &s->addr_node);
377 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
378 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700379 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700380}
381
382/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200383 * Registers the server keyword list <kwl> as a list of valid keywords for next
384 * parsing sessions.
385 */
386void srv_register_keywords(struct srv_kw_list *kwl)
387{
Willy Tarreau2b718102021-04-21 07:32:39 +0200388 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200389}
390
391/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
392 * keyword is found with a NULL ->parse() function, then an attempt is made to
393 * find one with a valid ->parse() function. This way it is possible to declare
394 * platform-dependant, known keywords as NULL, then only declare them as valid
395 * if some options are met. Note that if the requested keyword contains an
396 * opening parenthesis, everything from this point is ignored.
397 */
398struct srv_kw *srv_find_kw(const char *kw)
399{
400 int index;
401 const char *kwend;
402 struct srv_kw_list *kwl;
403 struct srv_kw *ret = NULL;
404
405 kwend = strchr(kw, '(');
406 if (!kwend)
407 kwend = kw + strlen(kw);
408
409 list_for_each_entry(kwl, &srv_keywords.list, list) {
410 for (index = 0; kwl->kw[index].kw != NULL; index++) {
411 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
412 kwl->kw[index].kw[kwend-kw] == 0) {
413 if (kwl->kw[index].parse)
414 return &kwl->kw[index]; /* found it !*/
415 else
416 ret = &kwl->kw[index]; /* may be OK */
417 }
418 }
419 }
420 return ret;
421}
422
423/* Dumps all registered "server" keywords to the <out> string pointer. The
424 * unsupported keywords are only dumped if their supported form was not
425 * found.
426 */
427void srv_dump_kws(char **out)
428{
429 struct srv_kw_list *kwl;
430 int index;
431
Christopher Faulet784063e2020-05-18 12:14:18 +0200432 if (!out)
433 return;
434
Willy Tarreau21faa912012-10-10 08:27:36 +0200435 *out = NULL;
436 list_for_each_entry(kwl, &srv_keywords.list, list) {
437 for (index = 0; kwl->kw[index].kw != NULL; index++) {
438 if (kwl->kw[index].parse ||
439 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
440 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
441 kwl->scope,
442 kwl->kw[index].kw,
443 kwl->kw[index].skip ? " <arg>" : "",
444 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
445 kwl->kw[index].parse ? "" : " (not supported)");
446 }
447 }
448 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100449}
450
451/* Try to find in srv_keyword the word that looks closest to <word> by counting
452 * transitions between letters, digits and other characters. Will return the
453 * best matching word if found, otherwise NULL. An optional array of extra
454 * words to compare may be passed in <extra>, but it must then be terminated
455 * by a NULL entry. If unused it may be NULL.
456 */
457static const char *srv_find_best_kw(const char *word)
458{
459 uint8_t word_sig[1024];
460 uint8_t list_sig[1024];
461 const struct srv_kw_list *kwl;
462 const char *best_ptr = NULL;
463 int dist, best_dist = INT_MAX;
464 const char **extra;
465 int index;
466
467 make_word_fingerprint(word_sig, word);
468 list_for_each_entry(kwl, &srv_keywords.list, list) {
469 for (index = 0; kwl->kw[index].kw != NULL; index++) {
470 make_word_fingerprint(list_sig, kwl->kw[index].kw);
471 dist = word_fingerprint_distance(word_sig, list_sig);
472 if (dist < best_dist) {
473 best_dist = dist;
474 best_ptr = kwl->kw[index].kw;
475 }
476 }
477 }
478
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100479 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100480 make_word_fingerprint(list_sig, *extra);
481 dist = word_fingerprint_distance(word_sig, list_sig);
482 if (dist < best_dist) {
483 best_dist = dist;
484 best_ptr = *extra;
485 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100486 }
487
488 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
489 best_ptr = NULL;
490
491 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200492}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100493
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100494/* Parse the "backup" server keyword */
495static int srv_parse_backup(char **args, int *cur_arg,
496 struct proxy *curproxy, struct server *newsrv, char **err)
497{
498 newsrv->flags |= SRV_F_BACKUP;
499 return 0;
500}
501
Alexander Liu2a54bb72019-05-22 19:44:48 +0800502
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100503/* Parse the "cookie" server keyword */
504static int srv_parse_cookie(char **args, int *cur_arg,
505 struct proxy *curproxy, struct server *newsrv, char **err)
506{
507 char *arg;
508
509 arg = args[*cur_arg + 1];
510 if (!*arg) {
511 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
512 return ERR_ALERT | ERR_FATAL;
513 }
514
515 free(newsrv->cookie);
516 newsrv->cookie = strdup(arg);
517 newsrv->cklen = strlen(arg);
518 newsrv->flags |= SRV_F_COOKIESET;
519 return 0;
520}
521
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100522/* Parse the "disabled" server keyword */
523static int srv_parse_disabled(char **args, int *cur_arg,
524 struct proxy *curproxy, struct server *newsrv, char **err)
525{
Emeric Brun52a91d32017-08-31 14:41:55 +0200526 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
527 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100528 newsrv->check.state |= CHK_ST_PAUSED;
529 newsrv->check.health = 0;
530 return 0;
531}
532
533/* Parse the "enabled" server keyword */
534static int srv_parse_enabled(char **args, int *cur_arg,
535 struct proxy *curproxy, struct server *newsrv, char **err)
536{
Emeric Brun52a91d32017-08-31 14:41:55 +0200537 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
538 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100539 newsrv->check.state &= ~CHK_ST_PAUSED;
540 newsrv->check.health = newsrv->check.rise;
541 return 0;
542}
543
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100544/* Parse the "error-limit" server keyword */
545static int srv_parse_error_limit(char **args, int *cur_arg,
546 struct proxy *curproxy, struct server *newsrv, char **err)
547{
548 if (!*args[*cur_arg + 1]) {
549 memprintf(err, "'%s' expects an integer argument.",
550 args[*cur_arg]);
551 return ERR_ALERT | ERR_FATAL;
552 }
553
554 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
555
556 if (newsrv->consecutive_errors_limit <= 0) {
557 memprintf(err, "%s has to be > 0.",
558 args[*cur_arg]);
559 return ERR_ALERT | ERR_FATAL;
560 }
561
562 return 0;
563}
564
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200565/* Parse the "ws" keyword */
566static int srv_parse_ws(char **args, int *cur_arg,
567 struct proxy *curproxy, struct server *newsrv, char **err)
568{
569 if (!args[*cur_arg + 1]) {
570 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
571 return ERR_ALERT | ERR_FATAL;
572 }
573
574 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
575 newsrv->ws = SRV_WS_H1;
576 }
577 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
578 newsrv->ws = SRV_WS_H2;
579 }
580 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
581 newsrv->ws = SRV_WS_AUTO;
582 }
583 else {
584 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
585 return ERR_ALERT | ERR_FATAL;
586 }
587
588
589 return 0;
590}
591
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100592/* Parse the "init-addr" server keyword */
593static int srv_parse_init_addr(char **args, int *cur_arg,
594 struct proxy *curproxy, struct server *newsrv, char **err)
595{
596 char *p, *end;
597 int done;
598 struct sockaddr_storage sa;
599
600 newsrv->init_addr_methods = 0;
601 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
602
603 for (p = args[*cur_arg + 1]; *p; p = end) {
604 /* cut on next comma */
605 for (end = p; *end && *end != ','; end++);
606 if (*end)
607 *(end++) = 0;
608
609 memset(&sa, 0, sizeof(sa));
610 if (strcmp(p, "libc") == 0) {
611 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
612 }
613 else if (strcmp(p, "last") == 0) {
614 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
615 }
616 else if (strcmp(p, "none") == 0) {
617 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
618 }
619 else if (str2ip2(p, &sa, 0)) {
620 if (is_addr(&newsrv->init_addr)) {
621 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
622 args[*cur_arg], p);
623 return ERR_ALERT | ERR_FATAL;
624 }
625 newsrv->init_addr = sa;
626 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
627 }
628 else {
629 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
630 args[*cur_arg], p);
631 return ERR_ALERT | ERR_FATAL;
632 }
633 if (!done) {
634 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
635 args[*cur_arg], p);
636 return ERR_ALERT | ERR_FATAL;
637 }
638 }
639
640 return 0;
641}
642
643/* Parse the "log-proto" server keyword */
644static int srv_parse_log_proto(char **args, int *cur_arg,
645 struct proxy *curproxy, struct server *newsrv, char **err)
646{
647 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
648 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
649 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
650 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
651 else {
652 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
653 args[*cur_arg], args[*cur_arg + 1]);
654 return ERR_ALERT | ERR_FATAL;
655 }
656
657 return 0;
658}
659
660/* Parse the "maxconn" server keyword */
661static int srv_parse_maxconn(char **args, int *cur_arg,
662 struct proxy *curproxy, struct server *newsrv, char **err)
663{
664 newsrv->maxconn = atol(args[*cur_arg + 1]);
665 return 0;
666}
667
668/* Parse the "maxqueue" server keyword */
669static int srv_parse_maxqueue(char **args, int *cur_arg,
670 struct proxy *curproxy, struct server *newsrv, char **err)
671{
672 newsrv->maxqueue = atol(args[*cur_arg + 1]);
673 return 0;
674}
675
676/* Parse the "minconn" server keyword */
677static int srv_parse_minconn(char **args, int *cur_arg,
678 struct proxy *curproxy, struct server *newsrv, char **err)
679{
680 newsrv->minconn = atol(args[*cur_arg + 1]);
681 return 0;
682}
683
Willy Tarreau9c538e02019-01-23 10:21:49 +0100684static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
685{
686 char *arg;
687
688 arg = args[*cur_arg + 1];
689 if (!*arg) {
690 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
691 return ERR_ALERT | ERR_FATAL;
692 }
693 newsrv->max_reuse = atoi(arg);
694
695 return 0;
696}
697
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100698static 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 +0100699{
700 const char *res;
701 char *arg;
702 unsigned int time;
703
704 arg = args[*cur_arg + 1];
705 if (!*arg) {
706 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
707 return ERR_ALERT | ERR_FATAL;
708 }
709 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200710 if (res == PARSE_TIME_OVER) {
711 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
712 args[*cur_arg+1], args[*cur_arg]);
713 return ERR_ALERT | ERR_FATAL;
714 }
715 else if (res == PARSE_TIME_UNDER) {
716 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
717 args[*cur_arg+1], args[*cur_arg]);
718 return ERR_ALERT | ERR_FATAL;
719 }
720 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100721 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
722 *res, args[*cur_arg]);
723 return ERR_ALERT | ERR_FATAL;
724 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100725 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100726
727 return 0;
728}
729
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200730static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
731{
732 char *arg;
733
734 arg = args[*cur_arg + 1];
735 if (!*arg) {
736 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
737 return ERR_ALERT | ERR_FATAL;
738 }
739
740 newsrv->low_idle_conns = atoi(arg);
741 return 0;
742}
743
Olivier Houchard006e3102018-12-10 18:30:32 +0100744static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
745{
746 char *arg;
747
748 arg = args[*cur_arg + 1];
749 if (!*arg) {
750 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
751 return ERR_ALERT | ERR_FATAL;
752 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100753
Olivier Houchard006e3102018-12-10 18:30:32 +0100754 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100755 if ((int)newsrv->max_idle_conns < -1) {
756 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
757 return ERR_ALERT | ERR_FATAL;
758 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100759
760 return 0;
761}
762
Willy Tarreaudff55432012-10-10 17:51:05 +0200763/* parse the "id" server keyword */
764static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
765{
766 struct eb32_node *node;
767
768 if (!*args[*cur_arg + 1]) {
769 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
770 return ERR_ALERT | ERR_FATAL;
771 }
772
773 newsrv->puid = atol(args[*cur_arg + 1]);
774 newsrv->conf.id.key = newsrv->puid;
775
776 if (newsrv->puid <= 0) {
777 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
778 return ERR_ALERT | ERR_FATAL;
779 }
780
781 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
782 if (node) {
783 struct server *target = container_of(node, struct server, conf.id);
784 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
785 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
786 target->id);
787 return ERR_ALERT | ERR_FATAL;
788 }
789
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200790 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200791 return 0;
792}
793
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100794/* Parse the "namespace" server keyword */
795static int srv_parse_namespace(char **args, int *cur_arg,
796 struct proxy *curproxy, struct server *newsrv, char **err)
797{
Willy Tarreaue5733232019-05-22 19:24:06 +0200798#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100799 char *arg;
800
801 arg = args[*cur_arg + 1];
802 if (!*arg) {
803 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
804 return ERR_ALERT | ERR_FATAL;
805 }
806
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100807 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100808 /* Use the namespace associated with the connection (if present). */
809 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
810 return 0;
811 }
812
813 /*
814 * As this parser may be called several times for the same 'default-server'
815 * object, or for a new 'server' instance deriving from a 'default-server'
816 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
817 */
818 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
819
820 newsrv->netns = netns_store_lookup(arg, strlen(arg));
821 if (!newsrv->netns)
822 newsrv->netns = netns_store_insert(arg);
823
824 if (!newsrv->netns) {
825 memprintf(err, "Cannot open namespace '%s'", arg);
826 return ERR_ALERT | ERR_FATAL;
827 }
828
829 return 0;
830#else
831 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
832 return ERR_ALERT | ERR_FATAL;
833#endif
834}
835
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100836/* Parse the "no-backup" server keyword */
837static int srv_parse_no_backup(char **args, int *cur_arg,
838 struct proxy *curproxy, struct server *newsrv, char **err)
839{
840 newsrv->flags &= ~SRV_F_BACKUP;
841 return 0;
842}
843
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100844
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100845/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200846static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100847{
848 srv->pp_opts &= ~flags;
849 return 0;
850}
851
852/* Parse the "no-send-proxy" server keyword */
853static int srv_parse_no_send_proxy(char **args, int *cur_arg,
854 struct proxy *curproxy, struct server *newsrv, char **err)
855{
856 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
857}
858
859/* Parse the "no-send-proxy-v2" server keyword */
860static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
861 struct proxy *curproxy, struct server *newsrv, char **err)
862{
863 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
864}
865
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200866/* Parse the "shard" server keyword */
867static int srv_parse_shard(char **args, int *cur_arg,
868 struct proxy *curproxy, struct server *newsrv, char **err)
869{
870 newsrv->shard = atol(args[*cur_arg + 1]);
871 return 0;
872}
873
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200874/* Parse the "no-tfo" server keyword */
875static int srv_parse_no_tfo(char **args, int *cur_arg,
876 struct proxy *curproxy, struct server *newsrv, char **err)
877{
878 newsrv->flags &= ~SRV_F_FASTOPEN;
879 return 0;
880}
881
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100882/* Parse the "non-stick" server keyword */
883static int srv_parse_non_stick(char **args, int *cur_arg,
884 struct proxy *curproxy, struct server *newsrv, char **err)
885{
886 newsrv->flags |= SRV_F_NON_STICK;
887 return 0;
888}
889
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100890/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200891static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100892{
893 srv->pp_opts |= flags;
894 return 0;
895}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200896/* parse the "proto" server keyword */
897static int srv_parse_proto(char **args, int *cur_arg,
898 struct proxy *px, struct server *newsrv, char **err)
899{
900 struct ist proto;
901
902 if (!*args[*cur_arg + 1]) {
903 memprintf(err, "'%s' : missing value", args[*cur_arg]);
904 return ERR_ALERT | ERR_FATAL;
905 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100906 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200907 newsrv->mux_proto = get_mux_proto(proto);
908 if (!newsrv->mux_proto) {
909 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
910 return ERR_ALERT | ERR_FATAL;
911 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200912 return 0;
913}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100914
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100915/* parse the "proxy-v2-options" */
916static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
917 struct proxy *px, struct server *newsrv, char **err)
918{
919 char *p, *n;
920 for (p = args[*cur_arg+1]; p; p = n) {
921 n = strchr(p, ',');
922 if (n)
923 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100924 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100925 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100926 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100927 newsrv->pp_opts |= SRV_PP_V2_SSL;
928 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100929 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100930 newsrv->pp_opts |= SRV_PP_V2_SSL;
931 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100932 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100933 newsrv->pp_opts |= SRV_PP_V2_SSL;
934 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100935 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100936 newsrv->pp_opts |= SRV_PP_V2_SSL;
937 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100938 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100939 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100940 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100941 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100942 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100943 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100944 } else
945 goto fail;
946 }
947 return 0;
948 fail:
949 if (err)
950 memprintf(err, "'%s' : proxy v2 option not implemented", p);
951 return ERR_ALERT | ERR_FATAL;
952}
953
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100954/* Parse the "observe" server keyword */
955static int srv_parse_observe(char **args, int *cur_arg,
956 struct proxy *curproxy, struct server *newsrv, char **err)
957{
958 char *arg;
959
960 arg = args[*cur_arg + 1];
961 if (!*arg) {
962 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
963 return ERR_ALERT | ERR_FATAL;
964 }
965
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100966 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100967 newsrv->observe = HANA_OBS_NONE;
968 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100969 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100970 newsrv->observe = HANA_OBS_LAYER4;
971 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100972 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100973 if (curproxy->mode != PR_MODE_HTTP) {
974 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
975 return ERR_ALERT;
976 }
977 newsrv->observe = HANA_OBS_LAYER7;
978 }
979 else {
980 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
981 "but got '%s'\n", args[*cur_arg], arg);
982 return ERR_ALERT | ERR_FATAL;
983 }
984
985 return 0;
986}
987
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100988/* Parse the "on-error" server keyword */
989static int srv_parse_on_error(char **args, int *cur_arg,
990 struct proxy *curproxy, struct server *newsrv, char **err)
991{
992 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
993 newsrv->onerror = HANA_ONERR_FASTINTER;
994 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
995 newsrv->onerror = HANA_ONERR_FAILCHK;
996 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
997 newsrv->onerror = HANA_ONERR_SUDDTH;
998 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
999 newsrv->onerror = HANA_ONERR_MARKDWN;
1000 else {
1001 memprintf(err, "'%s' expects one of 'fastinter', "
1002 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1003 args[*cur_arg], args[*cur_arg + 1]);
1004 return ERR_ALERT | ERR_FATAL;
1005 }
1006
1007 return 0;
1008}
1009
1010/* Parse the "on-marked-down" server keyword */
1011static int srv_parse_on_marked_down(char **args, int *cur_arg,
1012 struct proxy *curproxy, struct server *newsrv, char **err)
1013{
1014 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1015 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1016 else {
1017 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1018 args[*cur_arg], args[*cur_arg + 1]);
1019 return ERR_ALERT | ERR_FATAL;
1020 }
1021
1022 return 0;
1023}
1024
1025/* Parse the "on-marked-up" server keyword */
1026static int srv_parse_on_marked_up(char **args, int *cur_arg,
1027 struct proxy *curproxy, struct server *newsrv, char **err)
1028{
1029 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1030 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1031 else {
1032 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1033 args[*cur_arg], args[*cur_arg + 1]);
1034 return ERR_ALERT | ERR_FATAL;
1035 }
1036
1037 return 0;
1038}
1039
Frédéric Lécaille16186232017-03-14 16:42:49 +01001040/* Parse the "redir" server keyword */
1041static int srv_parse_redir(char **args, int *cur_arg,
1042 struct proxy *curproxy, struct server *newsrv, char **err)
1043{
1044 char *arg;
1045
1046 arg = args[*cur_arg + 1];
1047 if (!*arg) {
1048 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1049 return ERR_ALERT | ERR_FATAL;
1050 }
1051
1052 free(newsrv->rdr_pfx);
1053 newsrv->rdr_pfx = strdup(arg);
1054 newsrv->rdr_len = strlen(arg);
1055
1056 return 0;
1057}
1058
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001059/* Parse the "resolvers" server keyword */
1060static int srv_parse_resolvers(char **args, int *cur_arg,
1061 struct proxy *curproxy, struct server *newsrv, char **err)
1062{
1063 free(newsrv->resolvers_id);
1064 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1065 return 0;
1066}
1067
1068/* Parse the "resolve-net" server keyword */
1069static int srv_parse_resolve_net(char **args, int *cur_arg,
1070 struct proxy *curproxy, struct server *newsrv, char **err)
1071{
1072 char *p, *e;
1073 unsigned char mask;
1074 struct resolv_options *opt;
1075
1076 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1077 memprintf(err, "'%s' expects a list of networks.",
1078 args[*cur_arg]);
1079 return ERR_ALERT | ERR_FATAL;
1080 }
1081
1082 opt = &newsrv->resolv_opts;
1083
1084 /* Split arguments by comma, and convert it from ipv4 or ipv6
1085 * string network in in_addr or in6_addr.
1086 */
1087 p = args[*cur_arg + 1];
1088 e = p;
1089 while (*p != '\0') {
1090 /* If no room available, return error. */
1091 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1092 memprintf(err, "'%s' exceed %d networks.",
1093 args[*cur_arg], SRV_MAX_PREF_NET);
1094 return ERR_ALERT | ERR_FATAL;
1095 }
1096 /* look for end or comma. */
1097 while (*e != ',' && *e != '\0')
1098 e++;
1099 if (*e == ',') {
1100 *e = '\0';
1101 e++;
1102 }
1103 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1104 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1105 /* Try to convert input string from ipv4 or ipv6 network. */
1106 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1107 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1108 &mask)) {
1109 /* Try to convert input string from ipv6 network. */
1110 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1111 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1112 } else {
1113 /* All network conversions fail, return error. */
1114 memprintf(err, "'%s' invalid network '%s'.",
1115 args[*cur_arg], p);
1116 return ERR_ALERT | ERR_FATAL;
1117 }
1118 opt->pref_net_nb++;
1119 p = e;
1120 }
1121
1122 return 0;
1123}
1124
1125/* Parse the "resolve-opts" server keyword */
1126static int srv_parse_resolve_opts(char **args, int *cur_arg,
1127 struct proxy *curproxy, struct server *newsrv, char **err)
1128{
1129 char *p, *end;
1130
1131 for (p = args[*cur_arg + 1]; *p; p = end) {
1132 /* cut on next comma */
1133 for (end = p; *end && *end != ','; end++);
1134 if (*end)
1135 *(end++) = 0;
1136
1137 if (strcmp(p, "allow-dup-ip") == 0) {
1138 newsrv->resolv_opts.accept_duplicate_ip = 1;
1139 }
1140 else if (strcmp(p, "ignore-weight") == 0) {
1141 newsrv->resolv_opts.ignore_weight = 1;
1142 }
1143 else if (strcmp(p, "prevent-dup-ip") == 0) {
1144 newsrv->resolv_opts.accept_duplicate_ip = 0;
1145 }
1146 else {
1147 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1148 args[*cur_arg], p);
1149 return ERR_ALERT | ERR_FATAL;
1150 }
1151 }
1152
1153 return 0;
1154}
1155
1156/* Parse the "resolve-prefer" server keyword */
1157static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1158 struct proxy *curproxy, struct server *newsrv, char **err)
1159{
1160 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1161 newsrv->resolv_opts.family_prio = AF_INET;
1162 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1163 newsrv->resolv_opts.family_prio = AF_INET6;
1164 else {
1165 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1166 args[*cur_arg]);
1167 return ERR_ALERT | ERR_FATAL;
1168 }
1169
1170 return 0;
1171}
1172
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001173/* Parse the "send-proxy" server keyword */
1174static int srv_parse_send_proxy(char **args, int *cur_arg,
1175 struct proxy *curproxy, struct server *newsrv, char **err)
1176{
1177 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1178}
1179
1180/* Parse the "send-proxy-v2" server keyword */
1181static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1182 struct proxy *curproxy, struct server *newsrv, char **err)
1183{
1184 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1185}
1186
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001187/* Parse the "slowstart" server keyword */
1188static int srv_parse_slowstart(char **args, int *cur_arg,
1189 struct proxy *curproxy, struct server *newsrv, char **err)
1190{
1191 /* slowstart is stored in seconds */
1192 unsigned int val;
1193 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1194
1195 if (time_err == PARSE_TIME_OVER) {
1196 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1197 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1198 return ERR_ALERT | ERR_FATAL;
1199 }
1200 else if (time_err == PARSE_TIME_UNDER) {
1201 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1202 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1203 return ERR_ALERT | ERR_FATAL;
1204 }
1205 else if (time_err) {
1206 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1207 *time_err, newsrv->id);
1208 return ERR_ALERT | ERR_FATAL;
1209 }
1210 newsrv->slowstart = (val + 999) / 1000;
1211
1212 return 0;
1213}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001214
1215/* Parse the "source" server keyword */
1216static int srv_parse_source(char **args, int *cur_arg,
1217 struct proxy *curproxy, struct server *newsrv, char **err)
1218{
1219 char *errmsg;
1220 int port_low, port_high;
1221 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001222
1223 errmsg = NULL;
1224
1225 if (!*args[*cur_arg + 1]) {
1226 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1227 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1228 goto err;
1229 }
1230
1231 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001232 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1233 &errmsg, NULL, NULL,
1234 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 +01001235 if (!sk) {
1236 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1237 goto err;
1238 }
1239
Frédéric Lécailledba97072017-03-17 15:33:50 +01001240 newsrv->conn_src.opts |= CO_SRC_BIND;
1241 newsrv->conn_src.source_addr = *sk;
1242
1243 if (port_low != port_high) {
1244 int i;
1245
Frédéric Lécailledba97072017-03-17 15:33:50 +01001246 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001247 if (!newsrv->conn_src.sport_range) {
1248 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1249 goto err;
1250 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001251 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1252 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1253 }
1254
1255 *cur_arg += 2;
1256 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001257 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001258#if defined(CONFIG_HAP_TRANSPARENT)
1259 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1261 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001262 goto err;
1263 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001264 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001265 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1266 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1267 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001268 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001269 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1270 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1271 }
1272 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1273 char *name, *end;
1274
1275 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001276 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001277 name++;
1278
1279 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001280 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001281 end++;
1282
1283 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1284 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1285 free(newsrv->conn_src.bind_hdr_name);
1286 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001287 if (!newsrv->conn_src.bind_hdr_name) {
1288 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1289 goto err;
1290 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001291 newsrv->conn_src.bind_hdr_len = end - name;
1292 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1293 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1294 newsrv->conn_src.bind_hdr_occ = -1;
1295
1296 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001297 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001298 end++;
1299 if (*end == ',') {
1300 end++;
1301 name = end;
1302 if (*end == '-')
1303 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001304 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001305 end++;
1306 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1307 }
1308
1309 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1311 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001312 goto err;
1313 }
1314 }
1315 else {
1316 struct sockaddr_storage *sk;
1317 int port1, port2;
1318
1319 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001320 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1321 &errmsg, NULL, NULL,
1322 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001323 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001324 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001325 goto err;
1326 }
1327
Frédéric Lécailledba97072017-03-17 15:33:50 +01001328 newsrv->conn_src.tproxy_addr = *sk;
1329 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1330 }
1331 global.last_checks |= LSTCHK_NETADM;
1332 *cur_arg += 2;
1333 continue;
1334#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 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 +01001336 goto err;
1337#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1338 } /* "usesrc" */
1339
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001340 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001341#ifdef SO_BINDTODEVICE
1342 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001343 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001344 goto err;
1345 }
1346 free(newsrv->conn_src.iface_name);
1347 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1348 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1349 global.last_checks |= LSTCHK_NETADM;
1350#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001351 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001352 goto err;
1353#endif
1354 *cur_arg += 2;
1355 continue;
1356 }
1357 /* this keyword in not an option of "source" */
1358 break;
1359 } /* while */
1360
1361 return 0;
1362
1363 err:
1364 free(errmsg);
1365 return ERR_ALERT | ERR_FATAL;
1366}
1367
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001368/* Parse the "stick" server keyword */
1369static int srv_parse_stick(char **args, int *cur_arg,
1370 struct proxy *curproxy, struct server *newsrv, char **err)
1371{
1372 newsrv->flags &= ~SRV_F_NON_STICK;
1373 return 0;
1374}
1375
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001376/* Parse the "track" server keyword */
1377static int srv_parse_track(char **args, int *cur_arg,
1378 struct proxy *curproxy, struct server *newsrv, char **err)
1379{
1380 char *arg;
1381
1382 arg = args[*cur_arg + 1];
1383 if (!*arg) {
1384 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1385 return ERR_ALERT | ERR_FATAL;
1386 }
1387
1388 free(newsrv->trackit);
1389 newsrv->trackit = strdup(arg);
1390
1391 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001392}
1393
1394/* Parse the "socks4" server keyword */
1395static int srv_parse_socks4(char **args, int *cur_arg,
1396 struct proxy *curproxy, struct server *newsrv, char **err)
1397{
1398 char *errmsg;
1399 int port_low, port_high;
1400 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001401
1402 errmsg = NULL;
1403
1404 if (!*args[*cur_arg + 1]) {
1405 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1406 goto err;
1407 }
1408
1409 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001410 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1411 &errmsg, NULL, NULL,
1412 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001413 if (!sk) {
1414 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1415 goto err;
1416 }
1417
Alexander Liu2a54bb72019-05-22 19:44:48 +08001418 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1419 newsrv->socks4_addr = *sk;
1420
Alexander Liu2a54bb72019-05-22 19:44:48 +08001421 return 0;
1422
1423 err:
1424 free(errmsg);
1425 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001426}
1427
Frédéric Lécailledba97072017-03-17 15:33:50 +01001428
Willy Tarreau034c88c2017-01-23 23:36:45 +01001429/* parse the "tfo" server keyword */
1430static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1431{
1432 newsrv->flags |= SRV_F_FASTOPEN;
1433 return 0;
1434}
1435
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001436/* parse the "usesrc" server keyword */
1437static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1438{
1439 memprintf(err, "'%s' only allowed after a '%s' statement.",
1440 "usesrc", "source");
1441 return ERR_ALERT | ERR_FATAL;
1442}
1443
1444/* parse the "weight" server keyword */
1445static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1446{
1447 int w;
1448
1449 w = atol(args[*cur_arg + 1]);
1450 if (w < 0 || w > SRV_UWGHT_MAX) {
1451 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1452 newsrv->id, SRV_UWGHT_MAX, w);
1453 return ERR_ALERT | ERR_FATAL;
1454 }
1455 newsrv->uweight = newsrv->iweight = w;
1456
1457 return 0;
1458}
1459
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001460/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001461 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001462 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001463 *
1464 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001466void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001467{
Willy Tarreau751153e2021-02-17 13:33:24 +01001468 struct stream *stream;
1469 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001470 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001471
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001472 for (thr = 0; thr < global.nbthread; thr++)
1473 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1474 if (stream->srv_conn == srv)
1475 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001476}
1477
1478/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001479 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001480 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001481 *
1482 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001483 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001484void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001485{
1486 struct server *srv;
1487
1488 for (srv = px->srv; srv != NULL; srv = srv->next)
1489 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001490 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001491}
1492
Willy Tarreaubda92272014-05-20 21:55:30 +02001493/* Appends some information to a message string related to a server going UP or
1494 * DOWN. If both <forced> and <reason> are null and the server tracks another
1495 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001496 * If <check> is non-null, an entire string describing the check result will be
1497 * appended after a comma and a space (eg: to report some information from the
1498 * check that changed the state). In the other case, the string will be built
1499 * using the check results stored into the struct server if present.
1500 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001501 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001502 *
1503 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001504 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001505void srv_append_status(struct buffer *msg, struct server *s,
1506 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001507{
Emeric Brun5a133512017-10-19 14:42:30 +02001508 short status = s->op_st_chg.status;
1509 short code = s->op_st_chg.code;
1510 long duration = s->op_st_chg.duration;
1511 char *desc = s->op_st_chg.reason;
1512
1513 if (check) {
1514 status = check->status;
1515 code = check->code;
1516 duration = check->duration;
1517 desc = check->desc;
1518 }
1519
1520 if (status != -1) {
1521 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1522
1523 if (status >= HCHK_STATUS_L57DATA)
1524 chunk_appendf(msg, ", code: %d", code);
1525
1526 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001527 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001528
1529 chunk_appendf(msg, ", info: \"");
1530
1531 chunk_initlen(&src, desc, 0, strlen(desc));
1532 chunk_asciiencode(msg, &src, '"');
1533
1534 chunk_appendf(msg, "\"");
1535 }
1536
1537 if (duration >= 0)
1538 chunk_appendf(msg, ", check duration: %ldms", duration);
1539 }
1540 else if (desc && *desc) {
1541 chunk_appendf(msg, ", %s", desc);
1542 }
1543 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001544 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001545 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001546
1547 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001548 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001549 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1550 " %d sessions active, %d requeued, %d remaining in queue",
1551 s->proxy->srv_act, s->proxy->srv_bck,
1552 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001553 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001554 else
1555 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1556 " %d sessions requeued, %d total in queue",
1557 s->proxy->srv_act, s->proxy->srv_bck,
1558 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001559 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001560 }
1561}
1562
Emeric Brun5a133512017-10-19 14:42:30 +02001563/* Marks server <s> down, regardless of its checks' statuses. The server is
1564 * registered in a list to postpone the counting of the remaining servers on
1565 * the proxy and transfers queued streams whenever possible to other servers at
1566 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1567 * non-null as the reason for going down or the available data from the check
1568 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001569 *
1570 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001571 */
Emeric Brun5a133512017-10-19 14:42:30 +02001572void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001573{
1574 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001575
Emeric Brun64cc49c2017-10-03 14:46:45 +02001576 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001577 return;
1578
Emeric Brun52a91d32017-08-31 14:41:55 +02001579 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001580 *s->op_st_chg.reason = 0;
1581 s->op_st_chg.status = -1;
1582 if (reason) {
1583 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1584 }
1585 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001586 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001587 s->op_st_chg.code = check->code;
1588 s->op_st_chg.status = check->status;
1589 s->op_st_chg.duration = check->duration;
1590 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001591
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001592 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001593 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001594
Emeric Brun9f0b4582017-10-23 14:39:51 +02001595 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001596 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001597 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001598 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001599 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001600}
1601
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001602/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001603 * in maintenance. The server is registered in a list to postpone the counting
1604 * of the remaining servers on the proxy and tries to grab requests from the
1605 * proxy at a sync point. Maintenance servers are ignored. It stores the
1606 * <reason> if non-null as the reason for going down or the available data
1607 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001608 *
1609 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001610 */
Emeric Brun5a133512017-10-19 14:42:30 +02001611void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001612{
1613 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001614
Emeric Brun64cc49c2017-10-03 14:46:45 +02001615 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001616 return;
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001619 return;
1620
Emeric Brun52a91d32017-08-31 14:41:55 +02001621 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001622 *s->op_st_chg.reason = 0;
1623 s->op_st_chg.status = -1;
1624 if (reason) {
1625 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1626 }
1627 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001628 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001629 s->op_st_chg.code = check->code;
1630 s->op_st_chg.status = check->status;
1631 s->op_st_chg.duration = check->duration;
1632 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001633
Emeric Brun64cc49c2017-10-03 14:46:45 +02001634 if (s->slowstart <= 0)
1635 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001636
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001637 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001638 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001639
Emeric Brun9f0b4582017-10-23 14:39:51 +02001640 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001641 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001642 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001643 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001644 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001645}
1646
Willy Tarreau8eb77842014-05-21 13:54:57 +02001647/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001648 * isn't in maintenance. The server is registered in a list to postpone the
1649 * counting of the remaining servers on the proxy and tries to grab requests
1650 * from the proxy. Maintenance servers are ignored. It stores the
1651 * <reason> if non-null as the reason for going down or the available data
1652 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001653 * up. Note that it makes use of the trash to build the log strings, so <reason>
1654 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001655 *
1656 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001657 */
Emeric Brun5a133512017-10-19 14:42:30 +02001658void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001659{
1660 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001661
Emeric Brun64cc49c2017-10-03 14:46:45 +02001662 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001663 return;
1664
Emeric Brun52a91d32017-08-31 14:41:55 +02001665 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001666 return;
1667
Emeric Brun52a91d32017-08-31 14:41:55 +02001668 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001669 *s->op_st_chg.reason = 0;
1670 s->op_st_chg.status = -1;
1671 if (reason) {
1672 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1673 }
1674 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001675 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001676 s->op_st_chg.code = check->code;
1677 s->op_st_chg.status = check->status;
1678 s->op_st_chg.duration = check->duration;
1679 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001680
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001681 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001682 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001683
Emeric Brun9f0b4582017-10-23 14:39:51 +02001684 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001685 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001686 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001687 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001688 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001689}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001690
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001691/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1692 * enforce either maint mode or drain mode. It is not allowed to set more than
1693 * one flag at once. The equivalent "inherited" flag is propagated to all
1694 * tracking servers. Maintenance mode disables health checks (but not agent
1695 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001696 * is done. If <cause> is non-null, it will be displayed at the end of the log
1697 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001698 *
1699 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001700 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001701void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001702{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001703 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001704
1705 if (!mode)
1706 return;
1707
1708 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001710 return;
1711
Emeric Brun52a91d32017-08-31 14:41:55 +02001712 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001713 if (cause)
1714 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1715
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001716 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001717 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001718
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001719 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001720 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1721 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001722 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001723
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001724 /* compute the inherited flag to propagate */
1725 if (mode & SRV_ADMF_MAINT)
1726 mode = SRV_ADMF_IMAINT;
1727 else if (mode & SRV_ADMF_DRAIN)
1728 mode = SRV_ADMF_IDRAIN;
1729
Emeric Brun9f0b4582017-10-23 14:39:51 +02001730 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001731 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001732 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001733 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001734 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001735}
1736
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001737/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1738 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1739 * than one flag at once. The equivalent "inherited" flag is propagated to all
1740 * tracking servers. Leaving maintenance mode re-enables health checks. When
1741 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001742 *
1743 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001744 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001745void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001746{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001747 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001748
1749 if (!mode)
1750 return;
1751
1752 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001753 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001754 return;
1755
Emeric Brun52a91d32017-08-31 14:41:55 +02001756 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001757
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001758 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001759 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001760
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001761 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001762 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1763 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001764 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001765
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001766 if (mode & SRV_ADMF_MAINT)
1767 mode = SRV_ADMF_IMAINT;
1768 else if (mode & SRV_ADMF_DRAIN)
1769 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001770
Emeric Brun9f0b4582017-10-23 14:39:51 +02001771 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001772 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001773 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001774 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001775 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001776}
1777
Willy Tarreau757478e2016-11-03 19:22:19 +01001778/* principle: propagate maint and drain to tracking servers. This is useful
1779 * upon startup so that inherited states are correct.
1780 */
1781static void srv_propagate_admin_state(struct server *srv)
1782{
1783 struct server *srv2;
1784
1785 if (!srv->trackers)
1786 return;
1787
1788 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001789 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001790 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001791 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001792
Emeric Brun52a91d32017-08-31 14:41:55 +02001793 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001794 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001795 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001796 }
1797}
1798
1799/* Compute and propagate the admin states for all servers in proxy <px>.
1800 * Only servers *not* tracking another one are considered, because other
1801 * ones will be handled when the server they track is visited.
1802 */
1803void srv_compute_all_admin_states(struct proxy *px)
1804{
1805 struct server *srv;
1806
1807 for (srv = px->srv; srv; srv = srv->next) {
1808 if (srv->track)
1809 continue;
1810 srv_propagate_admin_state(srv);
1811 }
1812}
1813
Willy Tarreaudff55432012-10-10 17:51:05 +02001814/* Note: must not be declared <const> as its list will be overwritten.
1815 * Please take care of keeping this list alphabetically sorted, doing so helps
1816 * all code contributors.
1817 * Optional keywords are also declared with a NULL ->parse() function so that
1818 * the config parser can report an appropriate error when a known keyword was
1819 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001820 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001821 */
1822static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001823 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001824 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001825 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1826 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001827 { "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 +02001828 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001829 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001830 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1831 { "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 +01001832 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1833 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001834 { "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 +01001835 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001836 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001837 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1838 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1839 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1840 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001841 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001842 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1843 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1844 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1845 { "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 +01001846 { "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 */
1847 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1848 { "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 +01001849 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001850 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001851 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001852 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001853 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001854 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001855 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001856 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1857 { "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 +02001858 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001859 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001860 { "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 +01001861 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001862 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001863 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001864 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001865 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1866 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001867 { NULL, NULL, 0 },
1868}};
1869
Willy Tarreau0108d902018-11-25 19:14:37 +01001870INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001871
Willy Tarreau004e0452013-11-21 11:22:01 +01001872/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001873 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001874 * state is automatically disabled if the time is elapsed. If <must_update> is
1875 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001876 *
1877 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001878 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001879void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001880{
1881 struct proxy *px = sv->proxy;
1882 unsigned w;
1883
1884 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1885 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001886 if (sv->next_state == SRV_ST_STARTING)
1887 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001888 }
1889
1890 /* We must take care of not pushing the server to full throttle during slow starts.
1891 * It must also start immediately, at least at the minimal step when leaving maintenance.
1892 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001893 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001894 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1895 else
1896 w = px->lbprm.wdiv;
1897
Emeric Brun52a91d32017-08-31 14:41:55 +02001898 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001899
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001900 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001901 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001902 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001903}
1904
Willy Tarreaubaaee002006-06-26 02:48:02 +02001905/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001906 * Parses weight_str and configures sv accordingly.
1907 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001908 *
1909 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001910 */
1911const char *server_parse_weight_change_request(struct server *sv,
1912 const char *weight_str)
1913{
1914 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001915 long int w;
1916 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001917
1918 px = sv->proxy;
1919
1920 /* if the weight is terminated with '%', it is set relative to
1921 * the initial weight, otherwise it is absolute.
1922 */
1923 if (!*weight_str)
1924 return "Require <weight> or <weight%>.\n";
1925
Simon Hormanb796afa2013-02-12 10:45:53 +09001926 w = strtol(weight_str, &end, 10);
1927 if (end == weight_str)
1928 return "Empty weight string empty or preceded by garbage";
1929 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001930 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001931 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001932 /* Avoid integer overflow */
1933 if (w > 25600)
1934 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001935 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001936 if (w > 256)
1937 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001938 }
1939 else if (w < 0 || w > 256)
1940 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001941 else if (end[0] != '\0')
1942 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001943
1944 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1945 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1946
1947 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001948 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001949
1950 return NULL;
1951}
1952
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001953/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001954 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1955 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001956 * Returns:
1957 * - error string on error
1958 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001959 *
1960 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001961 */
1962const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001963 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001964{
1965 unsigned char ip[INET6_ADDRSTRLEN];
1966
1967 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001968 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001969 return NULL;
1970 }
1971 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001972 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001973 return NULL;
1974 }
1975
1976 return "Could not understand IP address format.\n";
1977}
1978
Willy Tarreau46b7f532018-08-21 11:54:26 +02001979/*
1980 * Must be called with the server lock held.
1981 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001982const char *server_parse_maxconn_change_request(struct server *sv,
1983 const char *maxconn_str)
1984{
1985 long int v;
1986 char *end;
1987
1988 if (!*maxconn_str)
1989 return "Require <maxconn>.\n";
1990
1991 v = strtol(maxconn_str, &end, 10);
1992 if (end == maxconn_str)
1993 return "maxconn string empty or preceded by garbage";
1994 else if (end[0] != '\0')
1995 return "Trailing garbage in maxconn string";
1996
1997 if (sv->maxconn == sv->minconn) { // static maxconn
1998 sv->maxconn = sv->minconn = v;
1999 } else { // dynamic maxconn
2000 sv->maxconn = v;
2001 }
2002
2003 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002004 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002005
2006 return NULL;
2007}
2008
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002009static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
2010 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002011{
2012 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002013 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002014 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002015 NULL,
2016 };
2017
2018 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002019 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002020
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002021 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002022}
2023
William Lallemand0d058672022-03-16 15:44:42 +01002024int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002025{
2026 struct sample_expr *expr;
2027
2028 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 +01002029 if (!expr) {
2030 memprintf(err, "error detected while parsing sni expression : %s", *err);
2031 return ERR_ALERT | ERR_FATAL;
2032 }
2033
2034 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2035 memprintf(err, "error detected while parsing sni expression : "
2036 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002037 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002038 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002039 return ERR_ALERT | ERR_FATAL;
2040 }
2041
2042 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2043 release_sample_expr(newsrv->ssl_ctx.sni);
2044 newsrv->ssl_ctx.sni = expr;
2045
2046 return 0;
2047}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002048
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002049static 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 +01002050{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002051 char *msg = "error encountered while processing ";
2052 char *quote = "'";
2053 char *token = args[cur_arg];
2054
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002055 if (err && *err) {
2056 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002057 msg = *err;
2058 quote = "";
2059 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002060 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002061
2062 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002063 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002064 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002065 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002066}
2067
Christopher Faulet0b365e32022-08-03 11:21:14 +02002068static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002069{
2070 int range_sz;
2071
2072 range_sz = src->conn_src.sport_range->size;
2073 if (range_sz > 0) {
2074 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2075 if (srv->conn_src.sport_range != NULL) {
2076 int i;
2077
2078 for (i = 0; i < range_sz; i++) {
2079 srv->conn_src.sport_range->ports[i] =
2080 src->conn_src.sport_range->ports[i];
2081 }
2082 }
2083 }
2084}
2085
2086/*
2087 * Copy <src> server connection source settings to <srv> server everything needed.
2088 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002089static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002090{
2091 srv->conn_src.opts = src->conn_src.opts;
2092 srv->conn_src.source_addr = src->conn_src.source_addr;
2093
2094 /* Source port range copy. */
2095 if (src->conn_src.sport_range != NULL)
2096 srv_conn_src_sport_range_cpy(srv, src);
2097
2098#ifdef CONFIG_HAP_TRANSPARENT
2099 if (src->conn_src.bind_hdr_name != NULL) {
2100 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2101 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2102 }
2103 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2104 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2105#endif
2106 if (src->conn_src.iface_name != NULL)
2107 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2108}
2109
2110/*
2111 * Copy <src> server SSL settings to <srv> server allocating
2112 * everything needed.
2113 */
2114#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002115static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002116{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002117 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002118 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2119
Christopher Faulet4ab26792021-12-01 09:50:41 +01002120 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2121 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2122
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002123 if (src->ssl_ctx.ca_file != NULL)
2124 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2125 if (src->ssl_ctx.crl_file != NULL)
2126 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002127 if (src->ssl_ctx.client_crt != NULL)
2128 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002129
2130 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2131
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002132
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002133 if (src->ssl_ctx.verify_host != NULL)
2134 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2135 if (src->ssl_ctx.ciphers != NULL)
2136 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002137 if (src->ssl_ctx.options)
2138 srv->ssl_ctx.options = src->ssl_ctx.options;
2139 if (src->ssl_ctx.methods.flags)
2140 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2141 if (src->ssl_ctx.methods.min)
2142 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2143 if (src->ssl_ctx.methods.max)
2144 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2145
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002146 if (src->ssl_ctx.ciphersuites != NULL)
2147 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002148 if (src->sni_expr != NULL)
2149 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002150
Olivier Houchardc7566002018-11-20 23:33:50 +01002151 if (src->ssl_ctx.alpn_str) {
2152 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2153 if (srv->ssl_ctx.alpn_str) {
2154 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2155 src->ssl_ctx.alpn_len);
2156 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2157 }
2158 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002159
Olivier Houchardc7566002018-11-20 23:33:50 +01002160 if (src->ssl_ctx.npn_str) {
2161 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2162 if (srv->ssl_ctx.npn_str) {
2163 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2164 src->ssl_ctx.npn_len);
2165 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2166 }
2167 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002168}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002169
2170/* Activate ssl on server <s>.
2171 * do nothing if there is no change to apply
2172 *
2173 * Must be called with the server lock held.
2174 */
2175void srv_set_ssl(struct server *s, int use_ssl)
2176{
2177 if (s->use_ssl == use_ssl)
2178 return;
2179
2180 s->use_ssl = use_ssl;
2181 if (s->use_ssl)
2182 s->xprt = xprt_get(XPRT_SSL);
2183 else
William Dauchya087f872022-01-06 16:57:15 +01002184 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002185}
2186
2187#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002188
2189/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002190 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002191 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002192 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002193 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002194int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002195{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002196 char *hostname_dn;
2197 int hostname_len, hostname_dn_len;
2198
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002199 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002200 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002201
Christopher Faulet67957bd2017-09-27 11:00:59 +02002202 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002203 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002204 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002205 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002206 if (hostname_dn_len == -1)
2207 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002208
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002209
Christopher Faulet67957bd2017-09-27 11:00:59 +02002210 free(srv->hostname);
2211 free(srv->hostname_dn);
2212 srv->hostname = strdup(hostname);
2213 srv->hostname_dn = strdup(hostname_dn);
2214 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002215 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002216 goto err;
2217
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002218 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002219
2220 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002221 ha_free(&srv->hostname);
2222 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002223 return -1;
2224}
2225
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002226/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002227 * Copy <src> server settings to <srv> server allocating
2228 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002229 * This function is not supposed to be called at any time, but only
2230 * during server settings parsing or during server allocations from
2231 * a server template, and just after having calloc()'ed a new server.
2232 * So, <src> may only be a default server (when parsing server settings)
2233 * or a server template (during server allocations from a server template).
2234 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2235 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002236 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002237void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002238{
2239 /* Connection source settings copy */
2240 srv_conn_src_cpy(srv, src);
2241
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002242 if (srv_tmpl) {
2243 srv->addr = src->addr;
2244 srv->svc_port = src->svc_port;
2245 }
2246
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002247 srv->pp_opts = src->pp_opts;
2248 if (src->rdr_pfx != NULL) {
2249 srv->rdr_pfx = strdup(src->rdr_pfx);
2250 srv->rdr_len = src->rdr_len;
2251 }
2252 if (src->cookie != NULL) {
2253 srv->cookie = strdup(src->cookie);
2254 srv->cklen = src->cklen;
2255 }
2256 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002257 srv->check.addr = src->check.addr;
2258 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002259 srv->check.use_ssl = src->check.use_ssl;
2260 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002261 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002262 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002263 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002264 /* Note: 'flags' field has potentially been already initialized. */
2265 srv->flags |= src->flags;
2266 srv->do_check = src->do_check;
2267 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002268 srv->check.inter = src->check.inter;
2269 srv->check.fastinter = src->check.fastinter;
2270 srv->check.downinter = src->check.downinter;
2271 srv->agent.use_ssl = src->agent.use_ssl;
2272 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002273
2274 if (src->agent.tcpcheck_rules) {
2275 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2276 if (srv->agent.tcpcheck_rules) {
2277 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2278 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2279 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2280 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2281 &src->agent.tcpcheck_rules->preset_vars);
2282 }
2283 }
2284
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002285 srv->agent.inter = src->agent.inter;
2286 srv->agent.fastinter = src->agent.fastinter;
2287 srv->agent.downinter = src->agent.downinter;
2288 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002289 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002290 srv->minconn = src->minconn;
2291 srv->maxconn = src->maxconn;
2292 srv->slowstart = src->slowstart;
2293 srv->observe = src->observe;
2294 srv->onerror = src->onerror;
2295 srv->onmarkeddown = src->onmarkeddown;
2296 srv->onmarkedup = src->onmarkedup;
2297 if (src->trackit != NULL)
2298 srv->trackit = strdup(src->trackit);
2299 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2300 srv->uweight = srv->iweight = src->iweight;
2301
2302 srv->check.send_proxy = src->check.send_proxy;
2303 /* health: up, but will fall down at first failure */
2304 srv->check.rise = srv->check.health = src->check.rise;
2305 srv->check.fall = src->check.fall;
2306
2307 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002308 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2309 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2310 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002311 srv->check.state |= CHK_ST_PAUSED;
2312 srv->check.health = 0;
2313 }
2314
2315 /* health: up but will fall down at first failure */
2316 srv->agent.rise = srv->agent.health = src->agent.rise;
2317 srv->agent.fall = src->agent.fall;
2318
2319 if (src->resolvers_id != NULL)
2320 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002321 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2322 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2323 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2324 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2325 srv->resolv_opts.family_prio = AF_INET6;
2326 memcpy(srv->resolv_opts.pref_net,
2327 src->resolv_opts.pref_net,
2328 sizeof srv->resolv_opts.pref_net);
2329 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002330
2331 srv->init_addr_methods = src->init_addr_methods;
2332 srv->init_addr = src->init_addr;
2333#if defined(USE_OPENSSL)
2334 srv_ssl_settings_cpy(srv, src);
2335#endif
2336#ifdef TCP_USER_TIMEOUT
2337 srv->tcp_ut = src->tcp_ut;
2338#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002339 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002340 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002341 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002342 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002343 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002344
Olivier Houchard8da5f982017-08-04 18:35:36 +02002345 if (srv_tmpl)
2346 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002347
2348 srv->check.via_socks4 = src->check.via_socks4;
2349 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002350}
2351
Willy Tarreau198e92a2021-03-05 10:23:32 +01002352/* allocate a server and attach it to the global servers_list. Returns
2353 * the server on success, otherwise NULL.
2354 */
William Lallemand313bfd12018-10-26 14:47:32 +02002355struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002356{
2357 struct server *srv;
2358
2359 srv = calloc(1, sizeof *srv);
2360 if (!srv)
2361 return NULL;
2362
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002363 srv_take(srv);
2364
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002365 srv->obj_type = OBJ_TYPE_SERVER;
2366 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002367 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002368 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002369 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002370 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002371 MT_LIST_INIT(&srv->prev_deleted);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002372 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002373
Emeric Brun52a91d32017-08-31 14:41:55 +02002374 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002375 srv->last_change = now.tv_sec;
2376
Christopher Faulet38290462020-04-21 11:46:40 +02002377 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002378 srv->check.status = HCHK_STATUS_INI;
2379 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002380 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002381 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002382
Christopher Faulet38290462020-04-21 11:46:40 +02002383 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002384 srv->agent.status = HCHK_STATUS_INI;
2385 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002386 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002387 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002388#if defined(USE_QUIC)
2389 srv->cids = EB_ROOT_UNIQUE;
2390#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002391
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002392 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002393#ifdef USE_OPENSSL
2394 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2395#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002396
Willy Tarreau975b1552019-06-06 16:25:55 +02002397 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002398 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002399 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002400 return srv;
2401}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002402
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002403/* Increment the server refcount. */
2404void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002405{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002406 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002407}
2408
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002409/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2410 * dynamic servers, its refcount is decremented first. The free operations are
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002411 * conducted only if the refcount is nul.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002412 *
2413 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002414 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002415 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002416struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002417{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002418 struct server *next = NULL;
2419
William Lallemand4c395fc2021-08-20 10:10:15 +02002420 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002421 goto end;
2422
2423 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002424
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002425 /* For dynamic servers, decrement the reference counter. Only free the
2426 * server when reaching zero.
2427 */
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002428 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2429 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002430
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002431 /* make sure we are removed from our 'next->prev_deleted' list
2432 * This doesn't require full thread isolation as we're using mt lists
2433 * However this could easily be turned into regular list if required
2434 * (with the proper use of thread isolation)
2435 */
2436 MT_LIST_DELETE(&srv->prev_deleted);
2437
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002438 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002439 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002440
2441 free(srv->id);
2442 free(srv->cookie);
2443 free(srv->hostname);
2444 free(srv->hostname_dn);
2445 free((char*)srv->conf.file);
2446 free(srv->per_thr);
Willy Tarreauc21a1872022-11-21 14:14:06 +01002447 free(srv->per_tgrp);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002448 free(srv->curr_idle_thr);
2449 free(srv->resolvers_id);
2450 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002451 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002452
Aurelien DARRAGONb5ee8be2023-03-09 14:28:00 +01002453 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2454 xprt_get(XPRT_SSL)->destroy_srv(srv);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002455 HA_SPIN_DESTROY(&srv->lock);
2456
Willy Tarreau2b718102021-04-21 07:32:39 +02002457 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002458 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002459
2460 EXTRA_COUNTERS_FREE(srv->extra_counters);
2461
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002462 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002463
2464 end:
2465 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002466}
2467
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002468/* Remove a server <srv> from a tracking list if <srv> is tracking another
2469 * server. No special care is taken if <srv> is tracked itself by another one :
2470 * this situation should be avoided by the caller.
2471 *
2472 * Not thread-safe.
2473 */
2474static void release_server_track(struct server *srv)
2475{
2476 struct server *strack = srv->track;
2477 struct server **base;
2478
2479 if (!strack)
2480 return;
2481
2482 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2483 if (*base == srv) {
2484 *base = srv->tracknext;
2485 return;
2486 }
2487 }
2488
2489 /* srv not found on the tracking list, this should never happen */
2490 BUG_ON(!*base);
2491}
2492
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002493/*
2494 * Parse as much as possible such a range string argument: low[-high]
2495 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2496 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2497 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2498 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002499 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002500static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2501 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002502{
2503 char *nb_high_arg;
2504
2505 *nb_high = 0;
2506 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002507 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002508
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002509 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002510 *nb_high_arg++ = '\0';
2511 *nb_high = atoi(nb_high_arg);
2512 }
2513 else {
2514 *nb_high += *nb_low;
2515 *nb_low = 1;
2516 }
2517
2518 if (*nb_low < 0 || *nb_high < *nb_low)
2519 return -1;
2520
2521 return 0;
2522}
2523
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002524/* Parse as much as possible such a range string argument: low[-high]
2525 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2526 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2527 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002528 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002529 * initialize a new server on startup.
2530 *
2531 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2532 * Returns 0 if succeeded, -1 if not.
2533 */
2534static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2535 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002536{
2537 chunk_printf(&trash, "%s%d", prefix, nb);
2538 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002539 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002540}
2541
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002542/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002543 * Note that a server template is a special server with
2544 * a few different parameters than a server which has
2545 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002546 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002547 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002548 * initialize a new server on startup.
2549 *
Joseph Herlant44466822018-11-15 08:57:51 -08002550 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002551 * 'srv' template included.
2552 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002553static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002554{
2555 int i;
2556 struct server *newsrv;
2557
2558 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 +02002559 newsrv = new_server(px);
2560 if (!newsrv)
2561 goto err;
2562
Christopher Faulet75bef002020-11-02 22:04:55 +01002563 newsrv->conf.file = strdup(srv->conf.file);
2564 newsrv->conf.line = srv->conf.line;
2565
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002566 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002567 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002568
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002569 if (newsrv->sni_expr) {
2570 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2571 if (!newsrv->ssl_ctx.sni)
2572 goto err;
2573 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002574
Emeric Brun34067662021-06-11 10:48:45 +02002575 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002576 if (newsrv->srvrq)
2577 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002578
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002579 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002580 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002581
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002582 /* Linked backwards first. This will be restablished after parsing. */
2583 newsrv->next = px->srv;
2584 px->srv = newsrv;
2585 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002586 _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 +02002587
2588 return i - srv->tmpl_info.nb_low;
2589
2590 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002591 _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 +02002592 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002593 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002594 free_check(&newsrv->agent);
2595 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002596 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002597 }
2598 free(newsrv);
2599 return i - srv->tmpl_info.nb_low;
2600}
2601
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002602/* Allocate a new server pointed by <srv> and try to parse the first arguments
2603 * in <args> as an address for a server or an address-range for a template or
2604 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2605 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002606 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002607 * initialize a new server on startup.
2608 *
2609 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2610 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2611 * <srv> will be set to NULL.
2612 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002613static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2614 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002615 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002616{
2617 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002618 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002619 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002620 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002621 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002622 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002623
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002624 *srv = NULL;
2625
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002626 /* There is no mandatory first arguments for default server. */
2627 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2628 if (parse_flags & SRV_PARSE_TEMPLATE) {
2629 if (!*args[3]) {
2630 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002631 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2632 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002633 err_code |= ERR_ALERT | ERR_FATAL;
2634 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002635 }
2636
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002637 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002638 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002639 else {
2640 if (!*args[2]) {
2641 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002642 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2643 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002644 err_code |= ERR_ALERT | ERR_FATAL;
2645 goto out;
2646 }
2647
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002648 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002649 }
2650
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002651 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002652 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2653 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002654 err_code |= ERR_ALERT | ERR_FATAL;
2655 goto out;
2656 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002657 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002658
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002659 *cur_arg = 2;
2660 if (parse_flags & SRV_PARSE_TEMPLATE) {
2661 /* Parse server-template <nb | range> arg. */
2662 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002663 ha_alert("Wrong %s number or range arg '%s'.\n",
2664 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002665 err_code |= ERR_ALERT | ERR_FATAL;
2666 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002667 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002668 (*cur_arg)++;
2669 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002670
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002671 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2672 struct sockaddr_storage *sk;
2673 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002674
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002675 *srv = newsrv = new_server(curproxy);
2676 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002677 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002678 err_code |= ERR_ALERT | ERR_ABORT;
2679 goto out;
2680 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002681 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002682
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002683 if (parse_flags & SRV_PARSE_TEMPLATE) {
2684 newsrv->tmpl_info.nb_low = tmpl_range_low;
2685 newsrv->tmpl_info.nb_high = tmpl_range_high;
2686 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002687
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002688 if (parse_flags & SRV_PARSE_DYNAMIC)
2689 newsrv->flags |= SRV_F_DYNAMIC;
2690
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002691 /* Note: for a server template, its id is its prefix.
2692 * This is a temporary id which will be used for server allocations to come
2693 * after parsing.
2694 */
2695 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2696 newsrv->id = strdup(args[1]);
2697 else
2698 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002699
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002700 /* revision defaults to 0 */
2701 newsrv->rid = 0;
2702
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002703 /* several ways to check the port component :
2704 * - IP => port=+0, relative (IPv4 only)
2705 * - IP: => port=+0, relative
2706 * - IP:N => port=N, absolute
2707 * - IP:+N => port=+N, relative
2708 * - IP:-N => port=-N, relative
2709 */
2710 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2711 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002712
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002713 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002714 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002715 (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);
2716 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002717 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002718 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002719 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002720 goto out;
2721 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002722
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002723 if (!port1 || !port2) {
2724 /* no port specified, +offset, -offset */
2725 newsrv->flags |= SRV_F_MAPPORTS;
2726 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002727
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002728 /* save hostname and create associated name resolution */
2729 if (fqdn) {
2730 if (fqdn[0] == '_') { /* SRV record */
2731 /* Check if a SRV request already exists, and if not, create it */
2732 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2733 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2734 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002735 err_code |= ERR_ALERT | ERR_FATAL;
2736 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002737 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002738 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002739 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002740 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002741 ha_alert("Can't create DNS resolution for server '%s'\n",
2742 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002743 err_code |= ERR_ALERT | ERR_FATAL;
2744 goto out;
2745 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002746 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002747
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002748 newsrv->addr = *sk;
2749 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002750 /*
2751 * we don't need to lock the server here, because
2752 * we are in the process of initializing.
2753 *
2754 * Note that the server is not attached into the proxy tree if
2755 * this is a dynamic server.
2756 */
2757 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002758
Willy Tarreau14e7f292021-10-27 17:41:07 +02002759 if (!newsrv->srvrq && !newsrv->hostname &&
2760 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002761 ha_alert("Unknown protocol family %d '%s'\n",
2762 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002763 err_code |= ERR_ALERT | ERR_FATAL;
2764 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002765 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002766
2767 (*cur_arg)++;
2768 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002769 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2770 /* Copy default server settings to new server */
2771 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2772 } else {
2773 /* Initialize dynamic server weight to 1 */
2774 newsrv->uweight = newsrv->iweight = 1;
2775
2776 /* A dynamic server is disabled on startup */
2777 newsrv->next_admin = SRV_ADMF_FMAINT;
2778 newsrv->next_state = SRV_ST_STOPPED;
2779 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002780
2781 /* Set default values for checks */
2782 newsrv->check.inter = DEF_CHKINTR;
2783 newsrv->check.rise = DEF_RISETIME;
2784 newsrv->check.fall = DEF_FALLTIME;
2785
2786 newsrv->agent.inter = DEF_CHKINTR;
2787 newsrv->agent.rise = DEF_AGENT_RISETIME;
2788 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002789 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002790 HA_SPIN_INIT(&newsrv->lock);
2791 }
2792 else {
2793 *srv = newsrv = &curproxy->defsrv;
2794 *cur_arg = 1;
2795 newsrv->resolv_opts.family_prio = AF_INET6;
2796 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002797 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002798
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002799 free(fqdn);
2800 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002801
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002802out:
2803 free(fqdn);
2804 return err_code;
2805}
Willy Tarreau272adea2014-03-31 10:39:59 +02002806
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002807/* Parse the server keyword in <args>.
2808 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2809 * might not be the case if an error is reported.
2810 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002811 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002812 * initialize a new server on startup.
2813 *
2814 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2815 * interrupted.
2816 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002817static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2818 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002819 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002820{
2821 int err_code = 0;
2822 struct srv_kw *kw;
2823 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002824 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002825
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002826 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002827 if (!kw) {
2828 best = srv_find_best_kw(args[*cur_arg]);
2829 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002830 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2831 args[*cur_arg], best,
2832 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2833 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002834 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002835 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2836 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2837 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002838
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002839 return ERR_ALERT | ERR_FATAL;
2840 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002841
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002842 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002843 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2844 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002845 err_code = ERR_ALERT | ERR_FATAL;
2846 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002847 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002848
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002849 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002850 ha_alert("'%s' option is not accepted in default-server sections\n",
2851 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002852 err_code = ERR_ALERT;
2853 goto out;
2854 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002855 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002856 ha_alert("'%s' option is not accepted for dynamic server\n",
2857 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002858 err_code |= ERR_ALERT;
2859 goto out;
2860 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002861
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002862 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2863 if (err_code) {
2864 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2865 free(errmsg);
2866 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002867
2868out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002869 if (kw->skip != -1)
2870 *cur_arg += 1 + kw->skip;
2871
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002872 return err_code;
2873}
2874
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002875/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002876 * initialize a new server on startup.
2877 */
2878static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2879 struct server *srv, struct proxy *proxy,
2880 char **errmsg)
2881{
2882 int ret;
2883
2884 if (!srv->sni_expr)
2885 return 0;
2886
2887 ret = server_parse_sni_expr(srv, proxy, errmsg);
2888 if (!ret)
2889 return 0;
2890
2891 return ret;
2892}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002893
2894/* Server initializations finalization.
2895 * Initialize health check, agent check and SNI expression if enabled.
2896 * Must not be called for a default server instance.
2897 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002898 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002899 * initialize a new server on startup.
2900 */
2901static int _srv_parse_finalize(char **args, int cur_arg,
2902 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002903 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002904{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002905 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002906 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002907
2908 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002909 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002910 return ERR_ALERT | ERR_FATAL;
2911 }
2912
2913 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002914 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2915 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002916 return ERR_ALERT | ERR_FATAL;
2917 }
2918
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002919 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2920 if (errmsg) {
2921 ha_alert("%s\n", errmsg);
2922 free(errmsg);
2923 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002924 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002925 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002926
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002927 /* A dynamic server is disabled on startup. It must not be counted as
2928 * an active backend entry.
2929 */
2930 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2931 if (srv->flags & SRV_F_BACKUP)
2932 px->srv_bck++;
2933 else
2934 px->srv_act++;
2935 }
2936
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002937 srv_lb_commit_status(srv);
2938
2939 return 0;
2940}
2941
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002942int parse_server(const char *file, int linenum, char **args,
2943 struct proxy *curproxy, const struct proxy *defproxy,
2944 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002945{
2946 struct server *newsrv = NULL;
2947 int err_code = 0;
2948
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002949 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002950
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002951 set_usermsgs_ctx(file, linenum, NULL);
2952
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002953 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002954 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002955 err_code |= ERR_ALERT | ERR_FATAL;
2956 goto out;
2957 }
2958 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2959 err_code |= ERR_ALERT | ERR_FATAL;
2960 goto out;
2961 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002962
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002963 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2964 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2965 if (!*args[2])
2966 return 0;
2967 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002968
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002969 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002970 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002971
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002972 /* the servers are linked backwards first */
2973 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2974 newsrv->next = curproxy->srv;
2975 curproxy->srv = newsrv;
2976 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002977
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002978 if (err_code & ERR_CODE)
2979 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002980
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002981 newsrv->conf.file = strdup(file);
2982 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002983
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002984 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002985 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002986 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002987 if (err_code & ERR_FATAL)
2988 goto out;
2989 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002990
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002991 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002992 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002993 if (err_code & ERR_FATAL)
2994 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002995 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002996
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002997 if (parse_flags & SRV_PARSE_TEMPLATE)
2998 _srv_parse_tmpl_init(newsrv, curproxy);
2999
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02003000 /* If the server id is fixed, insert it in the proxy used_id tree.
3001 * This is needed to detect a later duplicate id via srv_parse_id.
3002 *
3003 * If no is specified, a dynamic one is generated in
3004 * check_config_validity.
3005 */
3006 if (newsrv->flags & SRV_F_FORCED_ID)
3007 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3008
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02003009 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003010 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02003011
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003012 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003013 return 0;
3014
3015 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003016 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003017 return err_code;
3018}
3019
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003020/* Returns a pointer to the first server matching either id <id>.
3021 * NULL is returned if no match is found.
3022 * the lookup is performed in the backend <bk>
3023 */
3024struct server *server_find_by_id(struct proxy *bk, int id)
3025{
3026 struct eb32_node *eb32;
3027 struct server *curserver;
3028
3029 if (!bk || (id ==0))
3030 return NULL;
3031
3032 /* <bk> has no backend capabilities, so it can't have a server */
3033 if (!(bk->cap & PR_CAP_BE))
3034 return NULL;
3035
3036 curserver = NULL;
3037
3038 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3039 if (eb32)
3040 curserver = container_of(eb32, struct server, conf.id);
3041
3042 return curserver;
3043}
3044
3045/* Returns a pointer to the first server matching either name <name>, or id
3046 * if <name> starts with a '#'. NULL is returned if no match is found.
3047 * the lookup is performed in the backend <bk>
3048 */
3049struct server *server_find_by_name(struct proxy *bk, const char *name)
3050{
3051 struct server *curserver;
3052
3053 if (!bk || !name)
3054 return NULL;
3055
3056 /* <bk> has no backend capabilities, so it can't have a server */
3057 if (!(bk->cap & PR_CAP_BE))
3058 return NULL;
3059
3060 curserver = NULL;
3061 if (*name == '#') {
3062 curserver = server_find_by_id(bk, atoi(name + 1));
3063 if (curserver)
3064 return curserver;
3065 }
3066 else {
3067 curserver = bk->srv;
3068
3069 while (curserver && (strcmp(curserver->id, name) != 0))
3070 curserver = curserver->next;
3071
3072 if (curserver)
3073 return curserver;
3074 }
3075
3076 return NULL;
3077}
3078
3079struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3080{
3081 struct server *byname;
3082 struct server *byid;
3083
3084 if (!name && !id)
3085 return NULL;
3086
3087 if (diff)
3088 *diff = 0;
3089
3090 byname = byid = NULL;
3091
3092 if (name) {
3093 byname = server_find_by_name(bk, name);
3094 if (byname && (!id || byname->puid == id))
3095 return byname;
3096 }
3097
3098 /* remaining possibilities :
3099 * - name not set
3100 * - name set but not found
3101 * - name found but ID doesn't match
3102 */
3103 if (id) {
3104 byid = server_find_by_id(bk, id);
3105 if (byid) {
3106 if (byname) {
3107 /* use id only if forced by configuration */
3108 if (byid->flags & SRV_F_FORCED_ID) {
3109 if (diff)
3110 *diff |= 2;
3111 return byid;
3112 }
3113 else {
3114 if (diff)
3115 *diff |= 1;
3116 return byname;
3117 }
3118 }
3119
3120 /* remaining possibilities:
3121 * - name not set
3122 * - name set but not found
3123 */
3124 if (name && diff)
3125 *diff |= 2;
3126 return byid;
3127 }
3128
3129 /* id bot found */
3130 if (byname) {
3131 if (diff)
3132 *diff |= 1;
3133 return byname;
3134 }
3135 }
3136
3137 return NULL;
3138}
3139
Simon Horman7d09b9a2013-02-12 10:45:51 +09003140/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003141 * update a server's current IP address.
3142 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3143 * ip is in network format.
3144 * updater is a string which contains an information about the requester of the update.
3145 * updater is used if not NULL.
3146 *
3147 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003148 *
3149 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003150 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003151int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003152{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003153 /* save the new IP family & address if necessary */
3154 switch (ip_sin_family) {
3155 case AF_INET:
3156 if (s->addr.ss_family == ip_sin_family &&
3157 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3158 return 0;
3159 break;
3160 case AF_INET6:
3161 if (s->addr.ss_family == ip_sin_family &&
3162 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3163 return 0;
3164 break;
3165 };
3166
Baptiste Assmann14e40142015-04-14 01:13:07 +02003167 /* generates a log line and a warning on stderr */
3168 if (1) {
3169 /* book enough space for both IPv4 and IPv6 */
3170 char oldip[INET6_ADDRSTRLEN];
3171 char newip[INET6_ADDRSTRLEN];
3172
3173 memset(oldip, '\0', INET6_ADDRSTRLEN);
3174 memset(newip, '\0', INET6_ADDRSTRLEN);
3175
3176 /* copy old IP address in a string */
3177 switch (s->addr.ss_family) {
3178 case AF_INET:
3179 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3180 break;
3181 case AF_INET6:
3182 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3183 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003184 default:
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003185 strlcpy2(oldip, "(none)", sizeof(oldip));
Christopher Fauletb0b76072020-09-08 10:38:40 +02003186 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003187 };
3188
3189 /* copy new IP address in a string */
3190 switch (ip_sin_family) {
3191 case AF_INET:
3192 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3193 break;
3194 case AF_INET6:
3195 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3196 break;
3197 };
3198
3199 /* save log line into a buffer */
3200 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3201 s->proxy->id, s->id, oldip, newip, updater);
3202
3203 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003204 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003205
3206 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003207 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003208 }
3209
3210 /* save the new IP family */
3211 s->addr.ss_family = ip_sin_family;
3212 /* save the new IP address */
3213 switch (ip_sin_family) {
3214 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003215 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003216 break;
3217 case AF_INET6:
3218 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3219 break;
3220 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003221 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003222 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003223
3224 return 0;
3225}
3226
William Dauchy7cabc062021-02-11 22:51:24 +01003227/* update agent health check address and port
3228 * addr can be ip4/ip6 or a hostname
3229 * if one error occurs, don't apply anything
3230 * must be called with the server lock held.
3231 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003232const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003233{
3234 struct sockaddr_storage sk;
3235 struct buffer *msg;
3236 int new_port;
3237
3238 msg = get_trash_chunk();
3239 chunk_reset(msg);
3240
3241 if (!(s->agent.state & CHK_ST_ENABLED)) {
3242 chunk_strcat(msg, "agent checks are not enabled on this server");
3243 goto out;
3244 }
3245 if (addr) {
3246 memset(&sk, 0, sizeof(struct sockaddr_storage));
3247 if (str2ip(addr, &sk) == NULL) {
3248 chunk_appendf(msg, "invalid addr '%s'", addr);
3249 goto out;
3250 }
3251 }
3252 if (port) {
3253 if (strl2irc(port, strlen(port), &new_port) != 0) {
3254 chunk_appendf(msg, "provided port is not an integer");
3255 goto out;
3256 }
3257 if (new_port < 0 || new_port > 65535) {
3258 chunk_appendf(msg, "provided port is invalid");
3259 goto out;
3260 }
3261 }
3262out:
3263 if (msg->data)
3264 return msg->area;
3265 else {
3266 if (addr)
3267 set_srv_agent_addr(s, &sk);
3268 if (port)
3269 set_srv_agent_port(s, new_port);
3270 }
3271 return NULL;
3272}
3273
William Dauchyb456e1f2021-02-11 22:51:23 +01003274/* update server health check address and port
3275 * addr must be ip4 or ip6, it won't be resolved
3276 * if one error occurs, don't apply anything
3277 * must be called with the server lock held.
3278 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003279const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003280{
3281 struct sockaddr_storage sk;
3282 struct buffer *msg;
3283 int new_port;
3284
3285 msg = get_trash_chunk();
3286 chunk_reset(msg);
3287
3288 if (!(s->check.state & CHK_ST_ENABLED)) {
3289 chunk_strcat(msg, "health checks are not enabled on this server");
3290 goto out;
3291 }
3292 if (addr) {
3293 memset(&sk, 0, sizeof(struct sockaddr_storage));
3294 if (str2ip2(addr, &sk, 0) == NULL) {
3295 chunk_appendf(msg, "invalid addr '%s'", addr);
3296 goto out;
3297 }
3298 }
3299 if (port) {
3300 if (strl2irc(port, strlen(port), &new_port) != 0) {
3301 chunk_appendf(msg, "provided port is not an integer");
3302 goto out;
3303 }
3304 if (new_port < 0 || new_port > 65535) {
3305 chunk_appendf(msg, "provided port is invalid");
3306 goto out;
3307 }
3308 /* prevent the update of port to 0 if MAPPORTS are in use */
3309 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3310 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3311 goto out;
3312 }
3313 }
3314out:
3315 if (msg->data)
3316 return msg->area;
3317 else {
3318 if (addr)
3319 s->check.addr = sk;
3320 if (port)
3321 s->check.port = new_port;
3322 }
3323 return NULL;
3324}
3325
Baptiste Assmann14e40142015-04-14 01:13:07 +02003326/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003327 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3328 *
3329 * Caller can pass its name through <updater> to get it integrated in the response message
3330 * returned by the function.
3331 *
3332 * The function first does the following, in that order:
3333 * - validates the new addr and/or port
3334 * - checks if an update is required (new IP or port is different than current ones)
3335 * - checks the update is allowed:
3336 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3337 * - allow all changes if no CHECKS are configured
3338 * - if CHECK is configured:
3339 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3340 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3341 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003342 *
3343 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003344 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003345const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003346{
3347 struct sockaddr_storage sa;
3348 int ret, port_change_required;
3349 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003350 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003351 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003352 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003353
3354 msg = get_trash_chunk();
3355 chunk_reset(msg);
3356
3357 if (addr) {
3358 memset(&sa, 0, sizeof(struct sockaddr_storage));
3359 if (str2ip2(addr, &sa, 0) == NULL) {
3360 chunk_printf(msg, "Invalid addr '%s'", addr);
3361 goto out;
3362 }
3363
3364 /* changes are allowed on AF_INET* families only */
3365 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3366 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3367 goto out;
3368 }
3369
3370 /* collecting data currently setup */
3371 memset(current_addr, '\0', sizeof(current_addr));
3372 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3373 /* changes are allowed on AF_INET* families only */
3374 if ((ret != AF_INET) && (ret != AF_INET6)) {
3375 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3376 goto out;
3377 }
3378
3379 /* applying ADDR changes if required and allowed
3380 * ipcmp returns 0 when both ADDR are the same
3381 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003382 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003383 chunk_appendf(msg, "no need to change the addr");
3384 goto port;
3385 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003386 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003387 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003388
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003389 /* update report for caller */
3390 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3391 }
3392
3393 port:
3394 if (port) {
3395 char sign = '\0';
3396 char *endptr;
3397
3398 if (addr)
3399 chunk_appendf(msg, ", ");
3400
3401 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003402 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003403
3404 /* check if PORT change is required */
3405 port_change_required = 0;
3406
3407 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003408 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003409 new_port = strtol(port, &endptr, 10);
3410 if ((errno != 0) || (port == endptr)) {
3411 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3412 goto out;
3413 }
3414
3415 /* check if caller triggers a port mapped or offset */
3416 if (sign == '-' || (sign == '+')) {
3417 /* check if server currently uses port map */
3418 if (!(s->flags & SRV_F_MAPPORTS)) {
3419 /* switch from fixed port to port map mandatorily triggers
3420 * a port change */
3421 port_change_required = 1;
3422 /* check is configured
3423 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3424 * prevent PORT change if check doesn't have it's dedicated port while switching
3425 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003426 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003427 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.");
3428 goto out;
3429 }
3430 }
3431 /* we're already using port maps */
3432 else {
3433 port_change_required = current_port != new_port;
3434 }
3435 }
3436 /* fixed port */
3437 else {
3438 port_change_required = current_port != new_port;
3439 }
3440
3441 /* applying PORT changes if required and update response message */
3442 if (port_change_required) {
3443 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003444 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003445 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003446
3447 /* prepare message */
3448 chunk_appendf(msg, "port changed from '");
3449 if (s->flags & SRV_F_MAPPORTS)
3450 chunk_appendf(msg, "+");
3451 chunk_appendf(msg, "%d' to '", current_port);
3452
3453 if (sign == '-') {
3454 s->flags |= SRV_F_MAPPORTS;
3455 chunk_appendf(msg, "%c", sign);
3456 /* just use for result output */
3457 new_port = -new_port;
3458 }
3459 else if (sign == '+') {
3460 s->flags |= SRV_F_MAPPORTS;
3461 chunk_appendf(msg, "%c", sign);
3462 }
3463 else {
3464 s->flags &= ~SRV_F_MAPPORTS;
3465 }
3466
3467 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003468 }
3469 else {
3470 chunk_appendf(msg, "no need to change the port");
3471 }
3472 }
3473
3474out:
William Dauchy6318d332020-05-02 21:52:36 +02003475 if (changed) {
3476 /* force connection cleanup on the given server */
3477 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003478 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003479 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003480 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003481 if (updater)
3482 chunk_appendf(msg, " by '%s'", updater);
3483 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003484 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003485}
3486
Christopher Faulet5efdef22021-03-11 18:03:21 +01003487/*
3488 * update server status based on result of SRV resolution
3489 * returns:
3490 * 0 if server status is updated
3491 * 1 if server status has not changed
3492 *
3493 * Must be called with the server lock held.
3494 */
3495int srvrq_update_srv_status(struct server *s, int has_no_ip)
3496{
3497 if (!s->srvrq)
3498 return 1;
3499
3500 /* since this server has an IP, it can go back in production */
3501 if (has_no_ip == 0) {
3502 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3503 return 1;
3504 }
3505
3506 if (s->next_admin & SRV_ADMF_RMAINT)
3507 return 1;
3508
3509 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3510 return 0;
3511}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003512
3513/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003514 * update server status based on result of name resolution
3515 * returns:
3516 * 0 if server status is updated
3517 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003518 *
3519 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003520 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003521int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003522{
Emeric Brun750fe792020-12-23 16:51:12 +01003523 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003524 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003525 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003526
Jerome Magnin012261a2020-07-28 13:38:22 +02003527 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003528 if (resolution == NULL)
3529 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003530
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003531 switch (resolution->status) {
3532 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003533 /* status when HAProxy has just (re)started.
3534 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003535 break;
3536
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003537 case RSLV_STATUS_VALID:
3538 /*
3539 * resume health checks
3540 * server will be turned back on if health check is safe
3541 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003542 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003543 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003544 return 1;
3545 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3546 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003547 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003548 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003549
Emeric Brun52a91d32017-08-31 14:41:55 +02003550 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003551 return 1;
3552 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3553 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3554 s->proxy->id, s->id);
3555
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003556 ha_warning("%s.\n", trash.area);
3557 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003558 return 0;
3559
3560 case RSLV_STATUS_NX:
3561 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003562 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3563 if (!tick_is_expired(exp, now_ms))
3564 break;
3565
3566 if (s->next_admin & SRV_ADMF_RMAINT)
3567 return 1;
3568 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3569 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003570
3571 case RSLV_STATUS_TIMEOUT:
3572 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003573 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3574 if (!tick_is_expired(exp, now_ms))
3575 break;
3576
3577 if (s->next_admin & SRV_ADMF_RMAINT)
3578 return 1;
3579 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3580 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003581
3582 case RSLV_STATUS_REFUSED:
3583 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003584 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3585 if (!tick_is_expired(exp, now_ms))
3586 break;
3587
3588 if (s->next_admin & SRV_ADMF_RMAINT)
3589 return 1;
3590 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3591 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003592
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003593 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003594 /* stop server if resolution failed for a long enough period */
3595 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3596 if (!tick_is_expired(exp, now_ms))
3597 break;
3598
3599 if (s->next_admin & SRV_ADMF_RMAINT)
3600 return 1;
3601 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3602 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003603 }
3604
3605 return 1;
3606}
3607
3608/*
3609 * Server Name Resolution valid response callback
3610 * It expects:
3611 * - <nameserver>: the name server which answered the valid response
3612 * - <response>: buffer containing a valid DNS response
3613 * - <response_len>: size of <response>
3614 * It performs the following actions:
3615 * - ignore response if current ip found and server family not met
3616 * - update with first new ip found if family is met and current IP is not found
3617 * returns:
3618 * 0 on error
3619 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003620 *
3621 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003622 */
Emeric Brun08622d32020-12-23 17:41:43 +01003623int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003624{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003625 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003626 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003627 void *serverip, *firstip;
3628 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003629 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003630 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003631 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003632
Christopher Faulet67957bd2017-09-27 11:00:59 +02003633 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003634 if (!s)
3635 return 1;
3636
Christopher Faulet49531e82021-03-10 15:07:27 +01003637 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003638 /* If DNS resolution is disabled ignore it.
3639 * This is the case if the server was associated to
3640 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003641 */
Emeric Brun34067662021-06-11 10:48:45 +02003642 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003643 return 1;
3644 }
3645
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003646 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003647 if (!resolution)
3648 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003649
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003650 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003651 firstip = NULL; /* pointer to the first valid response found */
3652 /* it will be used as the new IP if a change is required */
3653 firstip_sin_family = AF_UNSPEC;
3654 serverip = NULL; /* current server IP address */
3655
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003656 /* initializing server IP pointer */
3657 server_sin_family = s->addr.ss_family;
3658 switch (server_sin_family) {
3659 case AF_INET:
3660 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3661 break;
3662
3663 case AF_INET6:
3664 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3665 break;
3666
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003667 case AF_UNSPEC:
3668 break;
3669
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003670 default:
3671 goto invalid;
3672 }
3673
Emeric Brund30e9a12020-12-23 18:49:16 +01003674 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3675 serverip, server_sin_family, &firstip,
3676 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003677
3678 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003679 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003680 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681
Emeric Brun456de772020-12-23 18:17:31 +01003682 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003683 goto save_ip;
3684
Emeric Brun456de772020-12-23 18:17:31 +01003685 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003686 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003687 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003688
Emeric Brun456de772020-12-23 18:17:31 +01003689 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003690 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003691 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003692 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003693 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003694
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003695 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003696 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003697 goto invalid;
3698
3699 }
3700
3701 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003702 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003703 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003704 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003705 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003706 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707 else
3708 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003709 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003710
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003711 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003712 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3713 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003714 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003715
3716 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003717 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003718 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003719 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003720 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003721 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3722 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003723 return 0;
3724}
3725
3726/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003727 * SRV record error management callback
3728 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003729 * 0 if we can trash answser items.
3730 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003731 *
3732 * Grabs the server's lock.
3733 */
3734int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3735{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003736 struct resolv_srvrq *srvrq;
3737 struct resolv_resolution *res;
3738 struct resolvers *resolvers;
3739 int exp;
3740
3741 /* SRV records */
3742 srvrq = objt_resolv_srvrq(requester->owner);
3743 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003744 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003745
3746 resolvers = srvrq->resolvers;
3747 res = requester->resolution;
3748
3749 switch (res->status) {
3750
3751 case RSLV_STATUS_NX:
3752 /* stop server if resolution is NX for a long enough period */
3753 exp = tick_add(res->last_valid, resolvers->hold.nx);
3754 if (!tick_is_expired(exp, now_ms))
3755 return 1;
3756 break;
3757
3758 case RSLV_STATUS_TIMEOUT:
3759 /* stop server if resolution is TIMEOUT for a long enough period */
3760 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3761 if (!tick_is_expired(exp, now_ms))
3762 return 1;
3763 break;
3764
3765 case RSLV_STATUS_REFUSED:
3766 /* stop server if resolution is REFUSED for a long enough period */
3767 exp = tick_add(res->last_valid, resolvers->hold.refused);
3768 if (!tick_is_expired(exp, now_ms))
3769 return 1;
3770 break;
3771
3772 default:
3773 /* stop server if resolution failed for a long enough period */
3774 exp = tick_add(res->last_valid, resolvers->hold.other);
3775 if (!tick_is_expired(exp, now_ms))
3776 return 1;
3777 }
3778
Emeric Brun34067662021-06-11 10:48:45 +02003779 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003780 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003781
Emeric Brun12ca6582021-06-10 15:25:25 +02003782 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003783}
3784
3785/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003786 * Server Name Resolution error management callback
3787 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003788 * 0 if we can trash answser items.
3789 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003790 *
3791 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792 */
Emeric Brun08622d32020-12-23 17:41:43 +01003793int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003794{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003795 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003796
Christopher Faulet67957bd2017-09-27 11:00:59 +02003797 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003798 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003799 return 0;
3800
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003801 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003802 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003803 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003804 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003805 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003806 return 0;
3807 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003808 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003809
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003810 return 1;
3811}
3812
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003813/*
3814 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003815 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003816 * It returns a pointer to the first server found or NULL if <ip> is not yet
3817 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003818 *
3819 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003820 */
3821struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3822{
3823 struct server *tmpsrv;
3824 struct proxy *be;
3825
3826 if (!srv)
3827 return NULL;
3828
3829 be = srv->proxy;
3830 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003831 /* we found the current server is the same, ignore it */
3832 if (srv == tmpsrv)
3833 continue;
3834
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003835 /* We want to compare the IP in the record with the IP of the servers in the
3836 * same backend, only if:
3837 * * DNS resolution is enabled on the server
3838 * * the hostname used for the resolution by our server is the same than the
3839 * one used for the server found in the backend
3840 * * the server found in the backend is not our current server
3841 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003842 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003843 if ((tmpsrv->hostname_dn == NULL) ||
3844 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003845 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003846 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003847 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003848 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003849 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003850
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003851 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003852 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003853 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003854 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003855 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003856
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003857 /* At this point, we have 2 different servers using the same DNS hostname
3858 * for their respective resolution.
3859 */
3860 if (*ip_family == tmpsrv->addr.ss_family &&
3861 ((tmpsrv->addr.ss_family == AF_INET &&
3862 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3863 (tmpsrv->addr.ss_family == AF_INET6 &&
3864 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003865 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003866 return tmpsrv;
3867 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003868 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003869 }
3870
Emeric Brune9fd6b52017-11-02 17:20:39 +01003871
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003872 return NULL;
3873}
3874
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003875/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3876 * resolver. This is suited for initial address configuration. Returns 0 on
3877 * success otherwise a non-zero error code. In case of error, *err_code, if
3878 * not NULL, is filled up.
3879 */
3880int srv_set_addr_via_libc(struct server *srv, int *err_code)
3881{
3882 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003883 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003884 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003885 return 1;
3886 }
3887 return 0;
3888}
3889
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003890/* Set the server's FDQN (->hostname) from <hostname>.
3891 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003892 *
3893 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003894 */
Emeric Brun08622d32020-12-23 17:41:43 +01003895int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003896{
Emeric Brun08622d32020-12-23 17:41:43 +01003897 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003898 char *hostname_dn;
3899 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003900
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003901 /* Note that the server lock is already held. */
3902 if (!srv->resolvers)
3903 return -1;
3904
Emeric Brun08622d32020-12-23 17:41:43 +01003905 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003906 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003907 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003908 * and we can't enable it at run time for now.
3909 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003910 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003911 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003912
3913 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003914 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003915 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003916 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003917 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003918 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003919 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003920
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003921 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003922 if (resolution &&
3923 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003924 resolution->hostname_dn_len == hostname_dn_len &&
3925 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003926 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003927
Willy Tarreau6878f802021-10-20 14:07:31 +02003928 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003929
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003930 free(srv->hostname);
3931 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003932 srv->hostname = strdup(hostname);
3933 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003934 srv->hostname_dn_len = hostname_dn_len;
3935 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003936 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003937
Baptiste Assmann13a92322019-06-07 09:40:55 +02003938 if (srv->flags & SRV_F_NO_RESOLUTION)
3939 goto end;
3940
Emeric Brund30e9a12020-12-23 18:49:16 +01003941 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003942 goto err;
3943
3944 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003945 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003946 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003947 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003948
3949 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003950 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003951 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003952 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003953}
3954
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003955/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3956 * from the state file. This is suited for initial address configuration.
3957 * Returns 0 on success otherwise a non-zero error code. In case of error,
3958 * *err_code, if not NULL, is filled up.
3959 */
3960static int srv_apply_lastaddr(struct server *srv, int *err_code)
3961{
3962 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3963 if (err_code)
3964 *err_code |= ERR_WARN;
3965 return 1;
3966 }
3967 return 0;
3968}
3969
Willy Tarreau25e51522016-11-04 15:10:17 +01003970/* returns 0 if no error, otherwise a combination of ERR_* flags */
3971static int srv_iterate_initaddr(struct server *srv)
3972{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003973 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003974 int return_code = 0;
3975 int err_code;
3976 unsigned int methods;
3977
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003978 /* If no addr and no hostname set, get the name from the DNS SRV request */
3979 if (!name && srv->srvrq)
3980 name = srv->srvrq->name;
3981
Willy Tarreau25e51522016-11-04 15:10:17 +01003982 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003983 if (!methods) {
3984 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003985 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3986 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003987 if (srv->resolvers_id) {
3988 /* dns resolution is configured, add "none" to not fail on startup */
3989 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3990 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003991 }
3992
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003993 /* "-dr" : always append "none" so that server addresses resolution
3994 * failures are silently ignored, this is convenient to validate some
3995 * configs out of their environment.
3996 */
3997 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3998 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3999
Willy Tarreau25e51522016-11-04 15:10:17 +01004000 while (methods) {
4001 err_code = 0;
4002 switch (srv_get_next_initaddr(&methods)) {
4003 case SRV_IADDR_LAST:
4004 if (!srv->lastaddr)
4005 continue;
4006 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004007 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004008 return_code |= err_code;
4009 break;
4010
4011 case SRV_IADDR_LIBC:
4012 if (!srv->hostname)
4013 continue;
4014 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004015 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004016 return_code |= err_code;
4017 break;
4018
Willy Tarreau37ebe122016-11-04 15:17:58 +01004019 case SRV_IADDR_NONE:
4020 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004021 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004022 ha_warning("could not resolve address '%s', disabling server.\n",
4023 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004024 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004025 return return_code;
4026
Willy Tarreau4310d362016-11-02 15:05:56 +01004027 case SRV_IADDR_IP:
4028 ipcpy(&srv->init_addr, &srv->addr);
4029 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004030 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4031 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004032 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004033 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004034
Willy Tarreau25e51522016-11-04 15:10:17 +01004035 default: /* unhandled method */
4036 break;
4037 }
4038 }
4039
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004040 if (!return_code)
4041 ha_alert("no method found to resolve address '%s'.\n", name);
4042 else
4043 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004044
4045 return_code |= ERR_ALERT | ERR_FATAL;
4046 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004047out:
4048 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004049 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004050 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004051}
4052
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004053/*
4054 * This function parses all backends and all servers within each backend
4055 * and performs servers' addr resolution based on information provided by:
4056 * - configuration file
4057 * - server-state file (states provided by an 'old' haproxy process)
4058 *
4059 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4060 */
4061int srv_init_addr(void)
4062{
4063 struct proxy *curproxy;
4064 int return_code = 0;
4065
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004066 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004067 while (curproxy) {
4068 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004069
4070 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004071 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004072 goto srv_init_addr_next;
4073
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004074 for (srv = curproxy->srv; srv; srv = srv->next) {
4075 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004076 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004077 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004078 reset_usermsgs_ctx();
4079 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004080
4081 srv_init_addr_next:
4082 curproxy = curproxy->next;
4083 }
4084
4085 return return_code;
4086}
4087
Willy Tarreau46b7f532018-08-21 11:54:26 +02004088/*
4089 * Must be called with the server lock held.
4090 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004091const 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 +02004092{
4093
Willy Tarreau83061a82018-07-13 11:56:34 +02004094 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004095
4096 msg = get_trash_chunk();
4097 chunk_reset(msg);
4098
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004099 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004100 chunk_appendf(msg, "no need to change the FDQN");
4101 goto out;
4102 }
4103
4104 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4105 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4106 goto out;
4107 }
4108
4109 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4110 server->proxy->id, server->id, server->hostname, fqdn);
4111
Emeric Brun08622d32020-12-23 17:41:43 +01004112 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004113 chunk_reset(msg);
4114 chunk_appendf(msg, "could not update %s/%s FQDN",
4115 server->proxy->id, server->id);
4116 goto out;
4117 }
4118
4119 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004120 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004121
4122 out:
4123 if (updater)
4124 chunk_appendf(msg, " by '%s'", updater);
4125 chunk_appendf(msg, "\n");
4126
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004127 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004128}
4129
4130
Willy Tarreau21b069d2016-11-23 17:15:08 +01004131/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4132 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004133 * 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 +01004134 * used for CLI commands requiring a server name.
4135 * Important: the <arg> is modified to remove the '/'.
4136 */
4137struct server *cli_find_server(struct appctx *appctx, char *arg)
4138{
4139 struct proxy *px;
4140 struct server *sv;
4141 char *line;
4142
4143 /* split "backend/server" and make <line> point to server */
4144 for (line = arg; *line; line++)
4145 if (*line == '/') {
4146 *line++ = '\0';
4147 break;
4148 }
4149
4150 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004151 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004152 return NULL;
4153 }
4154
4155 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004156 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004157 return NULL;
4158 }
4159
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004160 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004161 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004162 return NULL;
4163 }
4164
4165 return sv;
4166}
4167
William Lallemand222baf22016-11-19 02:00:33 +01004168
Willy Tarreau46b7f532018-08-21 11:54:26 +02004169/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004170static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004171{
4172 struct server *sv;
4173 const char *warning;
4174
4175 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4176 return 1;
4177
4178 sv = cli_find_server(appctx, args[2]);
4179 if (!sv)
4180 return 1;
4181
4182 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004183 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004184 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004185 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004186 if (warning)
4187 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004188 }
4189 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004190 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004191 if (strcmp(args[4], "ready") == 0)
4192 srv_adm_set_ready(sv);
4193 else if (strcmp(args[4], "drain") == 0)
4194 srv_adm_set_drain(sv);
4195 else if (strcmp(args[4], "maint") == 0)
4196 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004197 else
4198 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004199 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004200 }
4201 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004202 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004203 if (sv->track)
4204 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004205 else if (strcmp(args[4], "up") == 0) {
4206 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004207 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004208 }
4209 else if (strcmp(args[4], "stopping") == 0) {
4210 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004211 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004212 }
4213 else if (strcmp(args[4], "down") == 0) {
4214 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004215 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004216 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004217 else
4218 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004219 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004220 }
4221 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004222 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004223 if (!(sv->agent.state & CHK_ST_ENABLED))
4224 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004225 else if (strcmp(args[4], "up") == 0) {
4226 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004227 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004228 }
4229 else if (strcmp(args[4], "down") == 0) {
4230 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004231 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004232 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004233 else
4234 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004235 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004236 }
Misiek2da082d2017-01-09 09:40:42 +01004237 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004238 char *addr = NULL;
4239 char *port = NULL;
4240 if (strlen(args[4]) == 0) {
4241 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4242 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004243 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004244 }
4245 addr = args[4];
4246 if (strcmp(args[5], "port") == 0)
4247 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004248 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004249 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004250 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004251 if (warning)
4252 cli_msg(appctx, LOG_WARNING, warning);
4253 }
4254 else if (strcmp(args[3], "agent-port") == 0) {
4255 char *port = NULL;
4256 if (strlen(args[4]) == 0) {
4257 cli_err(appctx, "set server <b>/<s> agent-port requires"
4258 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004259 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004260 }
4261 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004262 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004263 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004264 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004265 if (warning)
4266 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004267 }
4268 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004269 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004270 if (!(sv->agent.state & CHK_ST_ENABLED))
4271 cli_err(appctx, "agent checks are not enabled on this server.\n");
4272 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004273 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004274 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004275 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004276 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004277 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004278 else if (strcmp(args[3], "check-addr") == 0) {
4279 char *addr = NULL;
4280 char *port = NULL;
4281 if (strlen(args[4]) == 0) {
4282 cli_err(appctx, "set server <b>/<s> check-addr requires"
4283 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004284 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004285 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004286 addr = args[4];
4287 if (strcmp(args[5], "port") == 0)
4288 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004289 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004290 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004291 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004292 if (warning)
4293 cli_msg(appctx, LOG_WARNING, warning);
4294 }
4295 else if (strcmp(args[3], "check-port") == 0) {
4296 char *port = NULL;
4297 if (strlen(args[4]) == 0) {
4298 cli_err(appctx, "set server <b>/<s> check-port requires"
4299 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004300 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004301 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004302 port = args[4];
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_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004305 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004306 if (warning)
4307 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004308 }
4309 else if (strcmp(args[3], "addr") == 0) {
4310 char *addr = NULL;
4311 char *port = NULL;
4312 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004313 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004314 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004315 }
4316 else {
4317 addr = args[4];
4318 }
4319 if (strcmp(args[5], "port") == 0) {
4320 port = args[6];
4321 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004322 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004323 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004324 if (warning)
4325 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004326 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004327 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004328 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004329 else if (strcmp(args[3], "fqdn") == 0) {
4330 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004331 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004332 goto out;
4333 }
4334 if (!sv->resolvers) {
4335 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4336 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004337 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004338 if (sv->srvrq) {
4339 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004340 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004341 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004342 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4343 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004344 /* ensure runtime resolver will process this new fqdn */
4345 if (sv->flags & SRV_F_NO_RESOLUTION) {
4346 sv->flags &= ~SRV_F_NO_RESOLUTION;
4347 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004348 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004349 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004350 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004351 if (warning)
4352 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004353 }
William Dauchyf6370442020-11-14 19:25:33 +01004354 else if (strcmp(args[3], "ssl") == 0) {
4355#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004356 if (sv->flags & SRV_F_DYNAMIC) {
4357 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4358 goto out;
4359 }
4360
William Dauchyf6370442020-11-14 19:25:33 +01004361 if (sv->ssl_ctx.ctx == NULL) {
4362 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4363 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004364 goto out;
4365 }
4366
4367 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4368 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004369 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004370 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004371 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004372 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004373 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004374 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004375 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004376 }
4377 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004378 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004379 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4380#else
4381 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4382#endif
4383 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004384 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004385 "usage: set server <backend>/<server> "
4386 "addr | agent | agent-addr | agent-port | agent-send | "
4387 "check-addr | check-port | fqdn | health | ssl | "
4388 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004389 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004390 out:
William Lallemand222baf22016-11-19 02:00:33 +01004391 return 1;
4392}
4393
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004394static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004395{
William Lallemand6b160942016-11-22 12:34:35 +01004396 struct proxy *px;
4397 struct server *sv;
4398 char *line;
4399
4400
4401 /* split "backend/server" and make <line> point to server */
4402 for (line = args[2]; *line; line++)
4403 if (*line == '/') {
4404 *line++ = '\0';
4405 break;
4406 }
4407
Willy Tarreau9d008692019-08-09 11:21:01 +02004408 if (!*line)
4409 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004410
Willy Tarreau9d008692019-08-09 11:21:01 +02004411 if (!get_backend_server(args[2], line, &px, &sv))
4412 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004413
4414 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004415 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4416 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004417 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004418 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004419 return 1;
4420}
4421
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004422/* Parse a "set weight" command.
4423 *
4424 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004425 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004426static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004427{
4428 struct server *sv;
4429 const char *warning;
4430
4431 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4432 return 1;
4433
4434 sv = cli_find_server(appctx, args[2]);
4435 if (!sv)
4436 return 1;
4437
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004438 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4439
William Lallemand6b160942016-11-22 12:34:35 +01004440 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004441 if (warning)
4442 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004443
4444 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4445
William Lallemand6b160942016-11-22 12:34:35 +01004446 return 1;
4447}
4448
Willy Tarreau46b7f532018-08-21 11:54:26 +02004449/* parse a "set maxconn server" command. It always returns 1.
4450 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004451 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004452 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004453static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004454{
4455 struct server *sv;
4456 const char *warning;
4457
4458 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4459 return 1;
4460
4461 sv = cli_find_server(appctx, args[3]);
4462 if (!sv)
4463 return 1;
4464
Amaury Denoyelle02742862021-06-18 11:11:36 +02004465 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4466
Willy Tarreaub8026272016-11-23 11:26:56 +01004467 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004468 if (warning)
4469 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004470
Amaury Denoyelle02742862021-06-18 11:11:36 +02004471 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4472
Willy Tarreaub8026272016-11-23 11:26:56 +01004473 return 1;
4474}
William Lallemand6b160942016-11-22 12:34:35 +01004475
Willy Tarreau46b7f532018-08-21 11:54:26 +02004476/* parse a "disable agent" command. It always returns 1.
4477 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004478 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004479 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004480static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004481{
4482 struct server *sv;
4483
4484 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4485 return 1;
4486
4487 sv = cli_find_server(appctx, args[2]);
4488 if (!sv)
4489 return 1;
4490
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004491 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004492 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004493 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004494 return 1;
4495}
4496
Willy Tarreau46b7f532018-08-21 11:54:26 +02004497/* parse a "disable health" command. It always returns 1.
4498 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004499 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004500 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004501static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004502{
4503 struct server *sv;
4504
4505 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4506 return 1;
4507
4508 sv = cli_find_server(appctx, args[2]);
4509 if (!sv)
4510 return 1;
4511
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004512 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004513 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004514 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004515 return 1;
4516}
4517
Willy Tarreau46b7f532018-08-21 11:54:26 +02004518/* parse a "disable server" command. It always returns 1.
4519 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004520 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004521 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004522static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004523{
4524 struct server *sv;
4525
4526 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4527 return 1;
4528
4529 sv = cli_find_server(appctx, args[2]);
4530 if (!sv)
4531 return 1;
4532
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004533 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004534 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004535 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004536 return 1;
4537}
4538
Willy Tarreau46b7f532018-08-21 11:54:26 +02004539/* parse a "enable agent" command. It always returns 1.
4540 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004541 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004542 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004543static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004544{
4545 struct server *sv;
4546
4547 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4548 return 1;
4549
4550 sv = cli_find_server(appctx, args[2]);
4551 if (!sv)
4552 return 1;
4553
Willy Tarreau9d008692019-08-09 11:21:01 +02004554 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4555 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004556
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004557 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004558 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004559 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004560 return 1;
4561}
4562
Willy Tarreau46b7f532018-08-21 11:54:26 +02004563/* parse a "enable health" command. It always returns 1.
4564 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004565 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004566 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004567static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004568{
4569 struct server *sv;
4570
4571 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4572 return 1;
4573
4574 sv = cli_find_server(appctx, args[2]);
4575 if (!sv)
4576 return 1;
4577
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004578 if (!(sv->check.state & CHK_ST_CONFIGURED))
4579 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4580
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004581 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004582 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004583 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004584 return 1;
4585}
4586
Willy Tarreau46b7f532018-08-21 11:54:26 +02004587/* parse a "enable server" command. It always returns 1.
4588 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004589 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004590 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004591static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004592{
4593 struct server *sv;
4594
4595 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4596 return 1;
4597
4598 sv = cli_find_server(appctx, args[2]);
4599 if (!sv)
4600 return 1;
4601
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004602 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004603 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004604 if (!(sv->flags & SRV_F_COOKIESET)
4605 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4606 sv->cookie)
4607 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004608 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004609 return 1;
4610}
4611
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004612/* Allocates data structure related to load balancing for the server <sv>. It
4613 * is only required for dynamic servers.
4614 *
4615 * At the moment, the server lock is not used as this function is only called
4616 * for a dynamic server not yet registered.
4617 *
4618 * Returns 1 on success, 0 on allocation failure.
4619 */
4620static int srv_alloc_lb(struct server *sv, struct proxy *be)
4621{
4622 int node;
4623
4624 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4625 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4626 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4627 sv->lb_nodes_now = 0;
4628
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004629 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4630 ((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 +01004631 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4632
4633 if (!sv->lb_nodes)
4634 return 0;
4635
4636 for (node = 0; node < sv->lb_nodes_tot; node++) {
4637 sv->lb_nodes[node].server = sv;
4638 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4639 }
4640 }
4641
4642 return 1;
4643}
4644
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004645/* updates the server's weight during a warmup stage. Once the final weight is
4646 * reached, the task automatically stops. Note that any server status change
4647 * must have updated s->last_change accordingly.
4648 */
4649static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4650{
4651 struct server *s = context;
4652
4653 /* by default, plan on stopping the task */
4654 t->expire = TICK_ETERNITY;
4655 if ((s->next_admin & SRV_ADMF_MAINT) ||
4656 (s->next_state != SRV_ST_STARTING))
4657 return t;
4658
4659 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4660
4661 /* recalculate the weights and update the state */
4662 server_recalc_eweight(s, 1);
4663
4664 /* probably that we can refill this server with a bit more connections */
4665 pendconn_grab_from_px(s);
4666
4667 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4668
4669 /* get back there in 1 second or 1/20th of the slowstart interval,
4670 * whichever is greater, resulting in small 5% steps.
4671 */
4672 if (s->next_state == SRV_ST_STARTING)
4673 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4674 return t;
4675}
4676
4677/* Allocate the slowstart task if the server is configured with a slowstart
4678 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4679 *
4680 * Returns 0 on success else non-zero.
4681 */
4682static int init_srv_slowstart(struct server *srv)
4683{
4684 struct task *t;
4685
4686 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004687 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004688 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4689 return ERR_ALERT | ERR_FATAL;
4690 }
4691
4692 /* We need a warmup task that will be called when the server
4693 * state switches from down to up.
4694 */
4695 srv->warmup = t;
4696 t->process = server_warmup;
4697 t->context = srv;
4698
4699 /* server can be in this state only because of */
4700 if (srv->next_state == SRV_ST_STARTING) {
4701 task_schedule(srv->warmup,
4702 tick_add(now_ms,
4703 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4704 }
4705 }
4706
4707 return ERR_NONE;
4708}
4709REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4710
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004711/* Memory allocation and initialization of the per_thr field.
4712 * Returns 0 if the field has been successfully initialized, -1 on failure.
4713 */
4714int srv_init_per_thr(struct server *srv)
4715{
4716 int i;
4717
4718 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004719 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4720 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004721 return -1;
4722
4723 for (i = 0; i < global.nbthread; i++) {
4724 srv->per_thr[i].idle_conns = EB_ROOT;
4725 srv->per_thr[i].safe_conns = EB_ROOT;
4726 srv->per_thr[i].avail_conns = EB_ROOT;
4727 MT_LIST_INIT(&srv->per_thr[i].streams);
4728 }
4729
4730 return 0;
4731}
4732
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004733/* Parse a "add server" command
4734 * Returns 0 if the server has been successfully initialized, 1 on failure.
4735 */
4736static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4737{
4738 struct proxy *be;
4739 struct server *srv;
4740 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004741 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004742 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004743 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4744
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004745 usermsgs_clr("CLI");
4746
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004747 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4748 return 1;
4749
4750 ++args;
4751
4752 sv_name = be_name = args[1];
4753 /* split backend/server arg */
4754 while (*sv_name && *(++sv_name)) {
4755 if (*sv_name == '/') {
4756 *sv_name = '\0';
4757 ++sv_name;
4758 break;
4759 }
4760 }
4761
4762 if (!*sv_name)
4763 return cli_err(appctx, "Require 'backend/server'.");
4764
Amaury Denoyellecece9182021-04-20 17:09:08 +02004765 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004766 if (!be)
4767 return cli_err(appctx, "No such backend.");
4768
4769 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004770 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004771 return 1;
4772 }
4773
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004774 /* At this point, some operations might not be thread-safe anymore. This
4775 * might be the case for parsing handlers which were designed to run
4776 * only at the starting stage on single-thread mode.
4777 *
4778 * Activate thread isolation to ensure thread-safety.
4779 */
4780 thread_isolate();
4781
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004782 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004783 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4784 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004785 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004786
4787 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004788 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004789
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004790 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004791 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004792 }
4793
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004794 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4795 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004796 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004797
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004798 /* A dynamic server does not currently support resolution.
4799 *
4800 * Initialize it explicitly to the "none" method to ensure no
4801 * resolution will ever be executed.
4802 */
4803 srv->init_addr_methods = SRV_IADDR_NONE;
4804
Amaury Denoyelle30467232021-03-12 18:03:27 +01004805 if (srv->mux_proto) {
4806 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004807 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004808 goto out;
4809 }
4810 }
4811
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004812 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004813 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004814 goto out;
4815 }
4816
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004817 if (srv->max_idle_conns != 0) {
4818 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4819 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004820 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004821 goto out;
4822 }
4823 }
4824
4825 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004826 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004827 goto out;
4828 }
4829
4830 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4831 COUNTERS_SV,
4832 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004833 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004834 goto out;
4835 }
4836
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004837 /* ensure minconn/maxconn consistency */
4838 srv_minmax_conn_apply(srv);
4839
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004840 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4841 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004842 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4843 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4844 goto out;
4845 }
4846 }
4847
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004848 if (srv->trackit) {
4849 if (srv_apply_track(srv, be))
4850 goto out;
4851 }
4852
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004853 /* Init check/agent if configured. The check is manually disabled
4854 * because a dynamic server is started in a disable state. It must be
4855 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004856 */
4857 if (srv->do_check) {
4858 if (init_srv_check(srv))
4859 goto out;
4860
4861 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004862 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004863
4864 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004865 if (init_srv_agent_check(srv))
4866 goto out;
4867
4868 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004869 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004870
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004871 /* Init slowstart if needed. */
4872 if (init_srv_slowstart(srv))
4873 goto out;
4874
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004875 /* Attach the server to the end of the proxy linked list. Note that this
4876 * operation is not thread-safe so this is executed under thread
4877 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004878 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004879 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004880 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004881
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004882 /* TODO use a double-linked list for px->srv */
4883 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004884 struct server *next = be->srv;
4885
4886 while (1) {
4887 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004888 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004889 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004890 goto out;
4891 }
4892
4893 if (!next->next)
4894 break;
4895
4896 next = next->next;
4897 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004898
4899 next->next = srv;
4900 }
4901 else {
4902 srv->next = be->srv;
4903 be->srv = srv;
4904 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004905
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004906 /* generate the server id if not manually specified */
4907 if (!srv->puid) {
4908 next_id = get_next_id(&be->conf.used_server_id, 1);
4909 if (!next_id) {
4910 ha_alert("Cannot attach server : no id left in proxy\n");
4911 goto out;
4912 }
4913
4914 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004915 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004916 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004917
4918 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004919 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4920 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004921 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4922 if (srv->addr_node.key)
4923 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004924
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004925 /* check if LSB bit (odd bit) is set for reuse_cnt */
4926 if (srv_id_reuse_cnt & 1) {
4927 /* cnt must be increased */
4928 srv_id_reuse_cnt++;
4929 }
4930 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4931 * save some space
4932 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
4933 */
4934 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
4935
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01004936 /* adding server cannot fail when we reach this:
4937 * publishing EVENT_HDL_SUB_SERVER_ADD
4938 */
4939 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
4940
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004941 thread_release();
4942
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004943 /* Start the check task. The server must be fully initialized.
4944 *
4945 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4946 * need to randomly spread the task interval for dynamic servers.
4947 */
4948 if (srv->check.state & CHK_ST_CONFIGURED) {
4949 if (!start_check_task(&srv->check, 0, 1, 1))
4950 ha_alert("System might be unstable, consider to execute a reload");
4951 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004952 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004953 if (!start_check_task(&srv->agent, 0, 1, 1))
4954 ha_alert("System might be unstable, consider to execute a reload");
4955 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004956
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004957 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004958 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004959
4960 return 0;
4961
4962out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004963 if (srv) {
4964 if (srv->track)
4965 release_server_track(srv);
4966
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004967 if (srv->check.state & CHK_ST_CONFIGURED)
4968 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004969 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004970 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004971
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004972 /* remove the server from the proxy linked list */
4973 if (be->srv == srv) {
4974 be->srv = srv->next;
4975 }
4976 else {
4977 struct server *prev;
4978 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4979 ;
4980 if (prev)
4981 prev->next = srv->next;
4982 }
4983
4984 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004985
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004986 thread_release();
4987
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004988 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004989 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004990
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004991 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004992 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004993
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004994 return 1;
4995}
4996
Amaury Denoyellee5580432021-04-15 14:41:20 +02004997/* Parse a "del server" command
4998 * Returns 0 if the server has been successfully initialized, 1 on failure.
4999 */
5000static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
5001{
5002 struct proxy *be;
5003 struct server *srv;
5004 char *be_name, *sv_name;
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005005 struct server *prev_del;
Amaury Denoyellee5580432021-04-15 14:41:20 +02005006
5007 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5008 return 1;
5009
5010 ++args;
5011
5012 sv_name = be_name = args[1];
5013 /* split backend/server arg */
5014 while (*sv_name && *(++sv_name)) {
5015 if (*sv_name == '/') {
5016 *sv_name = '\0';
5017 ++sv_name;
5018 break;
5019 }
5020 }
5021
5022 if (!*sv_name)
5023 return cli_err(appctx, "Require 'backend/server'.");
5024
5025 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005026 * requires thread isolation. In addition, any place referencing the
5027 * server about to be deleted would be unsafe after our operation, so
5028 * we must be certain to be alone so that no other thread has even
5029 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005030 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005031 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005032
5033 get_backend_server(be_name, sv_name, &be, &srv);
5034 if (!be) {
5035 cli_err(appctx, "No such backend.");
5036 goto out;
5037 }
5038
5039 if (!srv) {
5040 cli_err(appctx, "No such server.");
5041 goto out;
5042 }
5043
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005044 if (srv->flags & SRV_F_NON_PURGEABLE) {
5045 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 +02005046 goto out;
5047 }
5048
5049 /* Only servers in maintenance can be deleted. This ensures that the
5050 * server is not present anymore in the lb structures (through
5051 * lbprm.set_server_status_down).
5052 */
5053 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5054 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5055 goto out;
5056 }
5057
5058 /* Ensure that there is no active/idle/pending connection on the server.
5059 *
5060 * TODO idle connections should not prevent server deletion. A proper
5061 * cleanup function should be implemented to be used here.
5062 */
5063 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005064 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005065 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5066 goto out;
5067 }
5068
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005069 /* removing cannot fail anymore when we reach this:
5070 * publishing EVENT_HDL_SUB_SERVER_DEL
5071 */
5072 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5073
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005074 /* remove srv from tracking list */
5075 if (srv->track)
5076 release_server_track(srv);
5077
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005078 /* stop the check task if running */
5079 if (srv->check.state & CHK_ST_CONFIGURED)
5080 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005081 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005082 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005083
5084 /* detach the server from the proxy linked list
5085 * The proxy servers list is currently not protected by a lock, so this
5086 * requires thread_isolate/release.
5087 */
5088
5089 /* be->srv cannot be empty since we have already found the server with
5090 * get_backend_server */
5091 BUG_ON(!be->srv);
5092 if (be->srv == srv) {
5093 be->srv = srv->next;
5094 }
5095 else {
5096 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005097 for (next = be->srv; srv != next->next; next = next->next) {
5098 /* srv cannot be not found since we have already found
5099 * it with get_backend_server */
5100 BUG_ON(!next);
5101 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005102
Amaury Denoyellee5580432021-04-15 14:41:20 +02005103 next->next = srv->next;
5104 }
5105
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005106 /* Some deleted servers could still point to us using their 'next',
5107 * update them as needed
5108 * Please note the small race between the POP and APPEND, although in
5109 * this situation this is not an issue as we are under full thread
5110 * isolation
5111 */
5112 while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) {
5113 /* update its 'next' ptr */
5114 prev_del->next = srv->next;
5115 if (srv->next) {
5116 /* now it is our 'next' responsibility */
5117 MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted);
5118 }
5119 }
5120
5121 /* we ourselves need to inform our 'next' that we will still point it */
5122 if (srv->next)
5123 MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted);
5124
Amaury Denoyellee5580432021-04-15 14:41:20 +02005125 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005126 eb32_delete(&srv->conf.id);
5127 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005128 if (srv->addr_node.key)
5129 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005130
5131 /* remove srv from idle_node tree for idle conn cleanup */
5132 eb32_delete(&srv->idle_node);
5133
Aurelien DARRAGON75b9d1c2023-01-24 14:40:01 +01005134 /* flag the server as deleted
5135 * (despite the server being removed from primary server list,
5136 * one could still access the server data from a valid ptr)
5137 * Deleted flag helps detecting when a server is in transient removal
5138 * state.
5139 * ie: removed from the list but not yet freed/purged from memory.
5140 */
5141 srv->flags |= SRV_F_DELETED;
5142
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005143 /* set LSB bit (odd bit) for reuse_cnt */
5144 srv_id_reuse_cnt |= 1;
5145
Amaury Denoyellee5580432021-04-15 14:41:20 +02005146 thread_release();
5147
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005148 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005149 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005150
5151 cli_msg(appctx, LOG_INFO, "Server deleted.");
5152
5153 return 0;
5154
5155out:
5156 thread_release();
5157
5158 return 1;
5159}
5160
William Lallemand222baf22016-11-19 02:00:33 +01005161/* register cli keywords */
5162static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005163 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5164 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005165 { { "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 +02005166 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5167 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005168 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5169 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5170 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5171 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5172 { { "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 +01005173 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5174 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005175 {{},}
5176}};
5177
Willy Tarreau0108d902018-11-25 19:14:37 +01005178INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005179
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005180/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5181 * field is used to retrieve the identifier of the tracked server, either with
5182 * the format "proxy/server" or just "server". <curproxy> must point to the
5183 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5184 * to find the tracked server.
5185 *
5186 * Returns 0 if the server track has been activated else non-zero.
5187 *
5188 * Not thread-safe.
5189 */
5190int srv_apply_track(struct server *srv, struct proxy *curproxy)
5191{
5192 struct proxy *px;
5193 struct server *strack, *loop;
5194 char *pname, *sname;
5195
5196 if (!srv->trackit)
5197 return 1;
5198
5199 pname = srv->trackit;
5200 sname = strrchr(pname, '/');
5201
5202 if (sname) {
5203 *sname++ = '\0';
5204 }
5205 else {
5206 sname = pname;
5207 pname = NULL;
5208 }
5209
5210 if (pname) {
5211 px = proxy_be_by_name(pname);
5212 if (!px) {
5213 ha_alert("unable to find required proxy '%s' for tracking.\n",
5214 pname);
5215 return 1;
5216 }
5217 }
5218 else {
5219 px = curproxy;
5220 }
5221
5222 strack = findserver(px, sname);
5223 if (!strack) {
5224 ha_alert("unable to find required server '%s' for tracking.\n",
5225 sname);
5226 return 1;
5227 }
5228
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005229 if (strack->flags & SRV_F_DYNAMIC) {
5230 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5231 px->id, strack->id);
5232 return 1;
5233 }
5234
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005235 if (!strack->do_check && !strack->do_agent && !strack->track &&
5236 !strack->trackit) {
5237 ha_alert("unable to use %s/%s for "
5238 "tracking as it does not have any check nor agent enabled.\n",
5239 px->id, strack->id);
5240 return 1;
5241 }
5242
5243 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5244 ;
5245
5246 if (srv == strack || loop) {
5247 ha_alert("unable to track %s/%s as it "
5248 "belongs to a tracking chain looping back to %s/%s.\n",
5249 px->id, strack->id, px->id,
5250 srv == strack ? strack->id : loop->id);
5251 return 1;
5252 }
5253
5254 if (curproxy != px &&
5255 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5256 ha_alert("unable to use %s/%s for"
5257 "tracking: disable-on-404 option inconsistency.\n",
5258 px->id, strack->id);
5259 return 1;
5260 }
5261
5262 srv->track = strack;
5263 srv->tracknext = strack->trackers;
5264 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005265 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005266
5267 ha_free(&srv->trackit);
5268
5269 return 0;
5270}
5271
Emeric Brun64cc49c2017-10-03 14:46:45 +02005272/*
Aurelien DARRAGON0ddf0522023-03-27 11:57:28 +02005273 * This function applies server's status changes.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005274 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005275 * Must be called with the server lock held. This may also be called at init
5276 * time as the result of parsing the state file, in which case no lock will be
5277 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005278 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005279static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005280{
5281 struct check *check = &s->check;
5282 int xferred;
5283 struct proxy *px = s->proxy;
5284 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5285 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5286 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005287 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005288
Emeric Brun64cc49c2017-10-03 14:46:45 +02005289 /* If currently main is not set we try to apply pending state changes */
5290 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5291 int next_admin;
5292
5293 /* Backup next admin */
5294 next_admin = s->next_admin;
5295
5296 /* restore current admin state */
5297 s->next_admin = s->cur_admin;
5298
5299 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005300 /* no maintenance + server DOWN: publish event SERVER DOWN */
5301 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5302
Emeric Brun64cc49c2017-10-03 14:46:45 +02005303 s->last_change = now.tv_sec;
5304 if (s->proxy->lbprm.set_server_status_down)
5305 s->proxy->lbprm.set_server_status_down(s);
5306
5307 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5308 srv_shutdown_streams(s, SF_ERR_DOWN);
5309
5310 /* we might have streams queued on this server and waiting for
5311 * a connection. Those which are redispatchable will be queued
5312 * to another server or to the proxy itself.
5313 */
5314 xferred = pendconn_redistribute(s);
5315
5316 tmptrash = alloc_trash_chunk();
5317 if (tmptrash) {
5318 chunk_printf(tmptrash,
5319 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5320 s->proxy->id, s->id);
5321
Emeric Brun5a133512017-10-19 14:42:30 +02005322 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005323 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005324
5325 /* we don't send an alert if the server was previously paused */
5326 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005327 send_log(s->proxy, log_level, "%s.\n",
5328 tmptrash->area);
5329 send_email_alert(s, log_level, "%s",
5330 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005331 free_trash_chunk(tmptrash);
5332 tmptrash = NULL;
5333 }
5334 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5335 set_backend_down(s->proxy);
5336
5337 s->counters.down_trans++;
5338 }
5339 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5340 s->last_change = now.tv_sec;
5341 if (s->proxy->lbprm.set_server_status_down)
5342 s->proxy->lbprm.set_server_status_down(s);
5343
5344 /* we might have streams queued on this server and waiting for
5345 * a connection. Those which are redispatchable will be queued
5346 * to another server or to the proxy itself.
5347 */
5348 xferred = pendconn_redistribute(s);
5349
5350 tmptrash = alloc_trash_chunk();
5351 if (tmptrash) {
5352 chunk_printf(tmptrash,
5353 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5354 s->proxy->id, s->id);
5355
Emeric Brun5a133512017-10-19 14:42:30 +02005356 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005357
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005358 ha_warning("%s.\n", tmptrash->area);
5359 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5360 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005361 free_trash_chunk(tmptrash);
5362 tmptrash = NULL;
5363 }
5364
5365 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5366 set_backend_down(s->proxy);
5367 }
5368 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5369 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005370 /* no maintenance + server going UP: publish event SERVER UP */
5371 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5372
Emeric Brun64cc49c2017-10-03 14:46:45 +02005373 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5374 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5375 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5376 s->proxy->last_change = now.tv_sec;
5377 }
5378
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01005379 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02005380 s->down_time += now.tv_sec - s->last_change;
5381
5382 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005383 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005384 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5385
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005386 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005387 /* now propagate the status change to any LB algorithms */
5388 if (px->lbprm.update_server_eweight)
5389 px->lbprm.update_server_eweight(s);
5390 else if (srv_willbe_usable(s)) {
5391 if (px->lbprm.set_server_status_up)
5392 px->lbprm.set_server_status_up(s);
5393 }
5394 else {
5395 if (px->lbprm.set_server_status_down)
5396 px->lbprm.set_server_status_down(s);
5397 }
5398
5399 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5400 * and it's not a backup server and its effective weight is > 0,
5401 * then it can accept new connections, so we shut down all streams
5402 * on all backup servers.
5403 */
5404 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5405 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5406 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5407
5408 /* check if we can handle some connections queued at the proxy. We
5409 * will take as many as we can handle.
5410 */
5411 xferred = pendconn_grab_from_px(s);
5412
5413 tmptrash = alloc_trash_chunk();
5414 if (tmptrash) {
5415 chunk_printf(tmptrash,
5416 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5417 s->proxy->id, s->id);
5418
Emeric Brun5a133512017-10-19 14:42:30 +02005419 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005420 ha_warning("%s.\n", tmptrash->area);
5421 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5422 tmptrash->area);
5423 send_email_alert(s, LOG_NOTICE, "%s",
5424 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005425 free_trash_chunk(tmptrash);
5426 tmptrash = NULL;
5427 }
5428
5429 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5430 set_backend_down(s->proxy);
5431 }
5432 else if (s->cur_eweight != s->next_eweight) {
5433 /* now propagate the status change to any LB algorithms */
5434 if (px->lbprm.update_server_eweight)
5435 px->lbprm.update_server_eweight(s);
5436 else if (srv_willbe_usable(s)) {
5437 if (px->lbprm.set_server_status_up)
5438 px->lbprm.set_server_status_up(s);
5439 }
5440 else {
5441 if (px->lbprm.set_server_status_down)
5442 px->lbprm.set_server_status_down(s);
5443 }
5444
5445 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5446 set_backend_down(s->proxy);
5447 }
5448
5449 s->next_admin = next_admin;
5450 }
5451
Emeric Brun5a133512017-10-19 14:42:30 +02005452 /* reset operational state change */
5453 *s->op_st_chg.reason = 0;
5454 s->op_st_chg.status = s->op_st_chg.code = -1;
5455 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005456
5457 /* Now we try to apply pending admin changes */
5458
5459 /* Maintenance must also disable health checks */
5460 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5461 if (s->check.state & CHK_ST_ENABLED) {
5462 s->check.state |= CHK_ST_PAUSED;
5463 check->health = 0;
5464 }
5465
5466 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005467 tmptrash = alloc_trash_chunk();
5468 if (tmptrash) {
5469 chunk_printf(tmptrash,
5470 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5471 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5472 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005473
Olivier Houchard796a2b32017-10-24 17:42:47 +02005474 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005475
Olivier Houchard796a2b32017-10-24 17:42:47 +02005476 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005477 ha_warning("%s.\n", tmptrash->area);
5478 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5479 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005480 }
5481 free_trash_chunk(tmptrash);
5482 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005483 }
Emeric Brun8f298292017-12-06 16:47:17 +01005484 /* commit new admin status */
5485
5486 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005487 }
5488 else { /* server was still running */
5489 check->health = 0; /* failure */
5490 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005491
5492 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005493 if (s->proxy->lbprm.set_server_status_down)
5494 s->proxy->lbprm.set_server_status_down(s);
5495
Emeric Brun64cc49c2017-10-03 14:46:45 +02005496 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5497 srv_shutdown_streams(s, SF_ERR_DOWN);
5498
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005499 /* maintenance on previously running server: publish event SERVER DOWN */
5500 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5501
William Dauchy6318d332020-05-02 21:52:36 +02005502 /* force connection cleanup on the given server */
5503 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005504 /* we might have streams queued on this server and waiting for
5505 * a connection. Those which are redispatchable will be queued
5506 * to another server or to the proxy itself.
5507 */
5508 xferred = pendconn_redistribute(s);
5509
5510 tmptrash = alloc_trash_chunk();
5511 if (tmptrash) {
5512 chunk_printf(tmptrash,
5513 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5514 s->flags & SRV_F_BACKUP ? "Backup " : "",
5515 s->proxy->id, s->id,
5516 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5517
5518 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5519
5520 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005521 ha_warning("%s.\n", tmptrash->area);
5522 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5523 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005524 }
5525 free_trash_chunk(tmptrash);
5526 tmptrash = NULL;
5527 }
5528 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5529 set_backend_down(s->proxy);
5530
5531 s->counters.down_trans++;
5532 }
5533 }
5534 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5535 /* OK here we're leaving maintenance, we have many things to check,
5536 * because the server might possibly be coming back up depending on
5537 * its state. In practice, leaving maintenance means that we should
5538 * immediately turn to UP (more or less the slowstart) under the
5539 * following conditions :
5540 * - server is neither checked nor tracked
5541 * - server tracks another server which is not checked
5542 * - server tracks another server which is already up
5543 * Which sums up as something simpler :
5544 * "either the tracking server is up or the server's checks are disabled
5545 * or up". Otherwise we only re-enable health checks. There's a special
5546 * case associated to the stopping state which can be inherited. Note
5547 * that the server might still be in drain mode, which is naturally dealt
5548 * with by the lower level functions.
5549 */
5550
5551 if (s->check.state & CHK_ST_ENABLED) {
5552 s->check.state &= ~CHK_ST_PAUSED;
5553 check->health = check->rise; /* start OK but check immediately */
5554 }
5555
5556 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5557 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5558 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5559 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5560 s->next_state = SRV_ST_STOPPING;
5561 }
5562 else {
Willy Tarreaud332f132021-08-04 19:35:13 +02005563 s->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005564 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005565 if (s->slowstart > 0) {
5566 if (s->warmup)
5567 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5568 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005569 else
5570 s->next_state = SRV_ST_RUNNING;
5571 }
5572
5573 }
5574
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005575 /* ignore if server stays down when leaving maintenance mode */
5576 if (s->next_state != SRV_ST_STOPPED) {
5577 /* leaving maintenance + server UP: publish event SERVER UP */
5578 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5579 }
5580
Emeric Brun64cc49c2017-10-03 14:46:45 +02005581 tmptrash = alloc_trash_chunk();
5582 if (tmptrash) {
5583 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5584 chunk_printf(tmptrash,
5585 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5586 s->flags & SRV_F_BACKUP ? "Backup " : "",
5587 s->proxy->id, s->id,
5588 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5589 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5590 }
5591 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5592 chunk_printf(tmptrash,
5593 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5594 s->flags & SRV_F_BACKUP ? "Backup " : "",
5595 s->proxy->id, s->id, s->hostname,
5596 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5597 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5598 }
5599 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5600 chunk_printf(tmptrash,
5601 "%sServer %s/%s is %s/%s (leaving maintenance)",
5602 s->flags & SRV_F_BACKUP ? "Backup " : "",
5603 s->proxy->id, s->id,
5604 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5605 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5606 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005607 ha_warning("%s.\n", tmptrash->area);
5608 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5609 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005610 free_trash_chunk(tmptrash);
5611 tmptrash = NULL;
5612 }
5613
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005614 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005615 /* now propagate the status change to any LB algorithms */
5616 if (px->lbprm.update_server_eweight)
5617 px->lbprm.update_server_eweight(s);
5618 else if (srv_willbe_usable(s)) {
5619 if (px->lbprm.set_server_status_up)
5620 px->lbprm.set_server_status_up(s);
5621 }
5622 else {
5623 if (px->lbprm.set_server_status_down)
5624 px->lbprm.set_server_status_down(s);
5625 }
5626
5627 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5628 set_backend_down(s->proxy);
Willy Tarreaud332f132021-08-04 19:35:13 +02005629 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act))
5630 s->proxy->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005631
Willy Tarreau6a78e612018-08-07 10:14:53 +02005632 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5633 * and it's not a backup server and its effective weight is > 0,
5634 * then it can accept new connections, so we shut down all streams
5635 * on all backup servers.
5636 */
5637 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5638 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5639 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5640
5641 /* check if we can handle some connections queued at the proxy. We
5642 * will take as many as we can handle.
5643 */
5644 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005645 }
5646 else if (s->next_admin & SRV_ADMF_MAINT) {
5647 /* remaining in maintenance mode, let's inform precisely about the
5648 * situation.
5649 */
5650 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5651 tmptrash = alloc_trash_chunk();
5652 if (tmptrash) {
5653 chunk_printf(tmptrash,
5654 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5655 s->flags & SRV_F_BACKUP ? "Backup " : "",
5656 s->proxy->id, s->id);
5657
5658 if (s->track) /* normally it's mandatory here */
5659 chunk_appendf(tmptrash, " via %s/%s",
5660 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005661 ha_warning("%s.\n", tmptrash->area);
5662 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5663 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005664 free_trash_chunk(tmptrash);
5665 tmptrash = NULL;
5666 }
5667 }
5668 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5669 tmptrash = alloc_trash_chunk();
5670 if (tmptrash) {
5671 chunk_printf(tmptrash,
5672 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5673 s->flags & SRV_F_BACKUP ? "Backup " : "",
5674 s->proxy->id, s->id, s->hostname);
5675
5676 if (s->track) /* normally it's mandatory here */
5677 chunk_appendf(tmptrash, " via %s/%s",
5678 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005679 ha_warning("%s.\n", tmptrash->area);
5680 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5681 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005682 free_trash_chunk(tmptrash);
5683 tmptrash = NULL;
5684 }
5685 }
5686 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5687 tmptrash = alloc_trash_chunk();
5688 if (tmptrash) {
5689 chunk_printf(tmptrash,
5690 "%sServer %s/%s remains in forced maintenance",
5691 s->flags & SRV_F_BACKUP ? "Backup " : "",
5692 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005693 ha_warning("%s.\n", tmptrash->area);
5694 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5695 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005696 free_trash_chunk(tmptrash);
5697 tmptrash = NULL;
5698 }
5699 }
5700 /* don't report anything when leaving drain mode and remaining in maintenance */
5701
5702 s->cur_admin = s->next_admin;
5703 }
5704
5705 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5706 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5707 /* drain state is applied only if not yet in maint */
5708
5709 s->last_change = now.tv_sec;
5710 if (px->lbprm.set_server_status_down)
5711 px->lbprm.set_server_status_down(s);
5712
5713 /* we might have streams queued on this server and waiting for
5714 * a connection. Those which are redispatchable will be queued
5715 * to another server or to the proxy itself.
5716 */
5717 xferred = pendconn_redistribute(s);
5718
5719 tmptrash = alloc_trash_chunk();
5720 if (tmptrash) {
5721 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5722 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5723 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5724
5725 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5726
5727 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005728 ha_warning("%s.\n", tmptrash->area);
5729 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5730 tmptrash->area);
5731 send_email_alert(s, LOG_NOTICE, "%s",
5732 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005733 }
5734 free_trash_chunk(tmptrash);
5735 tmptrash = NULL;
5736 }
5737
5738 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5739 set_backend_down(s->proxy);
5740 }
5741 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5742 /* OK completely leaving drain mode */
5743 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5744 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5745 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5746 s->proxy->last_change = now.tv_sec;
5747 }
5748
5749 if (s->last_change < now.tv_sec) // ignore negative times
5750 s->down_time += now.tv_sec - s->last_change;
5751 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005752 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005753
5754 tmptrash = alloc_trash_chunk();
5755 if (tmptrash) {
5756 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5757 chunk_printf(tmptrash,
5758 "%sServer %s/%s is %s (leaving forced drain)",
5759 s->flags & SRV_F_BACKUP ? "Backup " : "",
5760 s->proxy->id, s->id,
5761 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5762 }
5763 else {
5764 chunk_printf(tmptrash,
5765 "%sServer %s/%s is %s (leaving drain)",
5766 s->flags & SRV_F_BACKUP ? "Backup " : "",
5767 s->proxy->id, s->id,
5768 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5769 if (s->track) /* normally it's mandatory here */
5770 chunk_appendf(tmptrash, " via %s/%s",
5771 s->track->proxy->id, s->track->id);
5772 }
5773
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005774 ha_warning("%s.\n", tmptrash->area);
5775 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5776 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005777 free_trash_chunk(tmptrash);
5778 tmptrash = NULL;
5779 }
5780
5781 /* now propagate the status change to any LB algorithms */
5782 if (px->lbprm.update_server_eweight)
5783 px->lbprm.update_server_eweight(s);
5784 else if (srv_willbe_usable(s)) {
5785 if (px->lbprm.set_server_status_up)
5786 px->lbprm.set_server_status_up(s);
5787 }
5788 else {
5789 if (px->lbprm.set_server_status_down)
5790 px->lbprm.set_server_status_down(s);
5791 }
5792 }
5793 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5794 /* remaining in drain mode after removing one of its flags */
5795
5796 tmptrash = alloc_trash_chunk();
5797 if (tmptrash) {
5798 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5799 chunk_printf(tmptrash,
5800 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5801 s->flags & SRV_F_BACKUP ? "Backup " : "",
5802 s->proxy->id, s->id);
5803
5804 if (s->track) /* normally it's mandatory here */
5805 chunk_appendf(tmptrash, " via %s/%s",
5806 s->track->proxy->id, s->track->id);
5807 }
5808 else {
5809 chunk_printf(tmptrash,
5810 "%sServer %s/%s remains in forced drain mode",
5811 s->flags & SRV_F_BACKUP ? "Backup " : "",
5812 s->proxy->id, s->id);
5813 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005814 ha_warning("%s.\n", tmptrash->area);
5815 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5816 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005817 free_trash_chunk(tmptrash);
5818 tmptrash = NULL;
5819 }
5820
5821 /* commit new admin status */
5822
5823 s->cur_admin = s->next_admin;
5824 }
5825 }
5826
5827 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005828 *s->adm_st_chg_cause = 0;
5829}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005830
Willy Tarreau144f84a2021-03-02 16:09:26 +01005831struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005832{
5833 struct connection *conn;
5834
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005835 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005836 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005837 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005838 }
5839
5840 return task;
5841}
5842
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005843/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005844 * moving them all.
5845 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005846 *
5847 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005848 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005849static 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 +02005850{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005851 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005852 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005853 int i = 0;
5854
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005855 node = eb_first(idle_tree);
5856 while (node) {
5857 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005858 if (toremove_nb != -1 && i >= toremove_nb)
5859 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005860
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005861 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005862 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005863 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005864 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005865
5866 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005867 }
5868 return i;
5869}
William Dauchy6318d332020-05-02 21:52:36 +02005870/* cleanup connections for a given server
5871 * might be useful when going on forced maintenance or live changing ip/port
5872 */
William Dauchy707ad322020-05-04 13:52:40 +02005873static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005874{
William Dauchy6318d332020-05-02 21:52:36 +02005875 int did_remove;
5876 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005877
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005878 /* nothing to do if pool-max-conn is null */
5879 if (!srv->max_idle_conns)
5880 return;
5881
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005882 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005883 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005884 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005885 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005886 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 +02005887 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005888 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 +02005889 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005890 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005891 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005892 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005893
5894 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5895 break;
William Dauchy6318d332020-05-02 21:52:36 +02005896 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005897}
5898
5899/* removes an idle conn after updating the server idle conns counters */
5900void srv_release_conn(struct server *srv, struct connection *conn)
5901{
5902 if (conn->flags & CO_FL_LIST_MASK) {
5903 /* The connection is currently in the server's idle list, so tell it
5904 * there's one less connection available in that list.
5905 */
5906 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5907 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5908 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5909 }
5910 else {
5911 /* The connection is not private and not in any server's idle
5912 * list, so decrement the current number of used connections
5913 */
5914 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5915 }
5916
5917 /* Remove the connection from any tree (safe, idle or available) */
5918 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5919 conn_delete_from_tree(&conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01005920 conn->flags &= ~CO_FL_LIST_MASK;
Willy Tarreau260f3242021-10-06 18:30:04 +02005921 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5922}
5923
5924/* retrieve a connection from its <hash> in <tree>
5925 * returns NULL if no connection found
5926 */
5927struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5928{
Willy Tarreau85223482022-09-29 20:32:43 +02005929 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005930 struct connection *conn = NULL;
5931 struct conn_hash_node *hash_node = NULL;
5932
Willy Tarreau85223482022-09-29 20:32:43 +02005933 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005934 if (node) {
5935 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5936 conn = hash_node->conn;
5937 }
5938
5939 return conn;
5940}
5941
5942/* retrieve the next connection sharing the same hash as <conn>
5943 * returns NULL if no connection found
5944 */
5945struct connection *srv_lookup_conn_next(struct connection *conn)
5946{
Willy Tarreau85223482022-09-29 20:32:43 +02005947 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005948 struct connection *next_conn = NULL;
5949 struct conn_hash_node *hash_node = NULL;
5950
Willy Tarreau85223482022-09-29 20:32:43 +02005951 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005952 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005953 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005954 next_conn = hash_node->conn;
5955 }
5956
5957 return next_conn;
5958}
5959
5960/* This adds an idle connection to the server's list if the connection is
5961 * reusable, not held by any owner anymore, but still has available streams.
5962 */
5963int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5964{
5965 /* we try to keep the connection in the server's idle list
5966 * if we don't have too many FD in use, and if the number of
5967 * idle+current conns is lower than what was observed before
5968 * last purge, or if we already don't have idle conns for the
5969 * current thread and we don't exceed last count by global.nbthread.
5970 */
5971 if (!(conn->flags & CO_FL_PRIVATE) &&
5972 srv && srv->pool_purge_delay > 0 &&
5973 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
5974 ha_used_fds < global.tune.pool_high_count &&
5975 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
5976 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
5977 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
5978 (ha_used_fds < global.tune.pool_low_count &&
5979 (srv->curr_used_conns + srv->curr_idle_conns <=
5980 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
5981 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
5982 int retadd;
5983
5984 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
5985 if (retadd > srv->max_idle_conns) {
5986 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5987 return 0;
5988 }
5989 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5990
5991 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5992 conn_delete_from_tree(&conn->hash_node->node);
5993
5994 if (is_safe) {
5995 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005996 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005997 _HA_ATOMIC_INC(&srv->curr_safe_nb);
5998 } else {
5999 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02006000 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02006001 _HA_ATOMIC_INC(&srv->curr_idle_nb);
6002 }
6003 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
6004 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
6005
6006 __ha_barrier_full();
6007 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
6008 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6009 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
6010 srv->idle_node.key = tick_add(srv->pool_purge_delay,
6011 now_ms);
6012 eb32_insert(&idle_conn_srv, &srv->idle_node);
6013 if (!task_in_wq(idle_conn_task) && !
6014 task_in_rq(idle_conn_task)) {
6015 task_schedule(idle_conn_task,
6016 srv->idle_node.key);
6017 }
6018
6019 }
6020 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6021 }
6022 return 1;
6023 }
6024 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02006025}
6026
Willy Tarreau144f84a2021-03-02 16:09:26 +01006027struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006028{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006029 struct server *srv;
6030 struct eb32_node *eb;
6031 int i;
6032 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006033
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006034 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006035 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6036 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006037 int exceed_conns;
6038 int to_kill;
6039 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006040
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006041 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
6042 if (!eb) {
6043 /* we might have reached the end of the tree, typically because
6044 * <now_ms> is in the first half and we're first scanning the last
6045 * half. Let's loop back to the beginning of the tree now.
6046 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006047
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006048 eb = eb32_first(&idle_conn_srv);
6049 if (likely(!eb))
6050 break;
6051 }
6052 if (tick_is_lt(now_ms, eb->key)) {
6053 /* timer not expired yet, revisit it later */
6054 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006055 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006056 }
6057 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006058
6059 /* Calculate how many idle connections we want to kill :
6060 * we want to remove half the difference between the total
6061 * of established connections (used or idle) and the max
6062 * number of used connections.
6063 */
6064 curr_idle = srv->curr_idle_conns;
6065 if (curr_idle == 0)
6066 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006067 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006068 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006069
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006070 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006071 if (srv->est_need_conns < srv->max_used_conns)
6072 srv->est_need_conns = srv->max_used_conns;
6073
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006074 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006075
Willy Tarreau18ed7892020-07-02 19:05:30 +02006076 if (exceed_conns <= 0)
6077 goto remove;
6078
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006079 /* check all threads starting with ours */
6080 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006081 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006082 int j;
6083 int did_remove = 0;
6084
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006085 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6086 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006087
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006088 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006089 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 +02006090 if (j > 0)
6091 did_remove = 1;
6092 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006093 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 +01006094 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006095 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006096
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006097 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006098 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006099
6100 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6101 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006102 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006103remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006104 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006105
6106 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006107 /* There are still more idle connections, add the
6108 * server back in the tree.
6109 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006110 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006111 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006112 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006113 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006114 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006115 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6116
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006117 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006118 return task;
6119}
Olivier Houchard88698d92019-04-16 19:07:22 +02006120
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006121/* Close remaining idle connections. This functions is designed to be run on
6122 * process shutdown. This guarantees a proper socket shutdown to avoid
6123 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6124 * bypassed.
6125 *
6126 * This function is not thread-safe so it must only be called via a global
6127 * deinit function.
6128 */
6129static void srv_close_idle_conns(struct server *srv)
6130{
6131 struct eb_root **cleaned_tree;
6132 int i;
6133
6134 for (i = 0; i < global.nbthread; ++i) {
6135 struct eb_root *conn_trees[] = {
6136 &srv->per_thr[i].idle_conns,
6137 &srv->per_thr[i].safe_conns,
6138 &srv->per_thr[i].avail_conns,
6139 NULL
6140 };
6141
6142 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6143 while (!eb_is_empty(*cleaned_tree)) {
6144 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6145 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6146 struct connection *conn = conn_hash_node->conn;
6147
6148 if (conn->ctrl->ctrl_close)
6149 conn->ctrl->ctrl_close(conn);
6150 ebmb_delete(node);
6151 }
6152 }
6153 }
6154}
6155
6156REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6157
Willy Tarreau76cc6992020-07-01 18:49:24 +02006158/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6159static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006160 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006161 char **err)
6162{
6163 if (too_many_args(1, args, err, NULL))
6164 return -1;
6165
6166 if (strcmp(args[1], "on") == 0)
6167 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6168 else if (strcmp(args[1], "off") == 0)
6169 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6170 else {
6171 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6172 return -1;
6173 }
6174 return 0;
6175}
6176
Olivier Houchard88698d92019-04-16 19:07:22 +02006177/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6178static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006179 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006180 char **err)
6181{
6182 int arg = -1;
6183
6184 if (too_many_args(1, args, err, NULL))
6185 return -1;
6186
6187 if (*(args[1]) != 0)
6188 arg = atoi(args[1]);
6189
6190 if (arg < 0 || arg > 100) {
6191 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6192 return -1;
6193 }
6194
6195 if (args[0][10] == 'h')
6196 global.tune.pool_high_ratio = arg;
6197 else
6198 global.tune.pool_low_ratio = arg;
6199 return 0;
6200}
6201
6202/* config keyword parsers */
6203static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006204 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006205 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6206 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6207 { 0, NULL, NULL }
6208}};
6209
6210INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6211
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006212/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006213 * Local variables:
6214 * c-indent-level: 8
6215 * c-basic-offset: 8
6216 * End:
6217 */