blob: 1d1e15216cbecbc83130faf9575de5155e659871 [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);
Aurelien DARRAGONd7142132023-03-22 17:35:47 +0100154 cb_data.safe.proxy_name[0] = '\0';
155 cb_data.safe.proxy_uuid = -1; /* default value */
156 if (srv->proxy) {
157 cb_data.safe.proxy_uuid = srv->proxy->uuid;
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100158 snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id);
Aurelien DARRAGONd7142132023-03-22 17:35:47 +0100159 }
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100160 /* unsafe data assignments */
161 cb_data.unsafe.ptr = srv;
162 cb_data.unsafe.thread_isolate = thread_isolate;
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100163 cb_data.unsafe.srv_lock = !thread_isolate;
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100164 /* publish in server dedicated sub list */
165 event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data));
166 /* publish in global subscription list */
167 event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data));
168}
169
170/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100171 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200172 * Unlikely, but it can happen. The server's proxy must be at least
173 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100174 */
175static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100176{
177 struct proxy *p = s->proxy;
178 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100179
180 for (tmpserv = p->srv; tmpserv != NULL;
181 tmpserv = tmpserv->next) {
182 if (tmpserv == s)
183 continue;
184 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
185 continue;
186 if (tmpserv->cookie &&
187 strcmp(tmpserv->cookie, s->cookie) == 0) {
188 ha_warning("We generated two equal cookies for two different servers.\n"
189 "Please change the secret key for '%s'.\n",
190 s->proxy->id);
191 }
192 }
193
194}
195
Willy Tarreau46b7f532018-08-21 11:54:26 +0200196/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200197 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200198 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100199void srv_set_dyncookie(struct server *s)
200{
201 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100202 char *tmpbuf;
203 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100204 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100205 size_t buffer_len;
206 int addr_len;
207 int port;
208
Willy Tarreau595e7672020-10-20 17:30:08 +0200209 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200210
Olivier Houchard4e694042017-03-14 20:01:29 +0100211 if ((s->flags & SRV_F_COOKIESET) ||
212 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
213 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200214 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100215 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100216
217 if (s->addr.ss_family != AF_INET &&
218 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200219 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100220 /*
221 * Buffer to calculate the cookie value.
222 * The buffer contains the secret key + the server IP address
223 * + the TCP port.
224 */
225 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
226 /*
227 * The TCP port should use only 2 bytes, but is stored in
228 * an unsigned int in struct server, so let's use 4, to be
229 * on the safe side.
230 */
231 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200232 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100233 memcpy(tmpbuf, p->dyncookie_key, key_len);
234 memcpy(&(tmpbuf[key_len]),
235 s->addr.ss_family == AF_INET ?
236 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
237 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
238 addr_len);
239 /*
240 * Make sure it's the same across all the load balancers,
241 * no matter their endianness.
242 */
243 port = htonl(s->svc_port);
244 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
245 hash_value = XXH64(tmpbuf, buffer_len, 0);
246 memprintf(&s->cookie, "%016llx", hash_value);
247 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200248 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100249 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100250
251 /* Don't bother checking if the dyncookie is duplicated if
252 * the server is marked as "disabled", maybe it doesn't have
253 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100254 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100255 if (!(s->next_admin & SRV_ADMF_FMAINT))
256 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200257 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200258 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100259}
260
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200261/* Returns true if it's possible to reuse an idle connection from server <srv>
262 * for a websocket stream. This is the case if server is configured to use the
263 * same protocol for both HTTP and websocket streams. This depends on the value
264 * of "proto", "alpn" and "ws" keywords.
265 */
266int srv_check_reuse_ws(struct server *srv)
267{
268 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
269 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
270 * for mux selection.
271 */
272 const struct ist srv_mux = srv->mux_proto ?
273 srv->mux_proto->token : IST_NULL;
274
275 switch (srv->ws) {
276 /* "auto" means use the same protocol : reuse is possible. */
277 case SRV_WS_AUTO:
278 return 1;
279
280 /* "h2" means use h2 for websocket : reuse is possible if
281 * server mux is h2.
282 */
283 case SRV_WS_H2:
284 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
285 return 1;
286 break;
287
288 /* "h1" means use h1 for websocket : reuse is possible if
289 * server mux is h1.
290 */
291 case SRV_WS_H1:
292 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
293 return 1;
294 break;
295 }
296 }
297 else {
298 /* ALPN selection.
299 * Based on the assumption that only "h2" and "http/1.1" token
300 * are used on server ALPN.
301 */
302 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
303 srv->ssl_ctx.alpn_len);
304
305 switch (srv->ws) {
306 case SRV_WS_AUTO:
307 /* for auto mode, consider reuse as possible if the
308 * server uses a single protocol ALPN
309 */
310 if (!istchr(alpn, ','))
311 return 1;
312 break;
313
314 case SRV_WS_H2:
315 return isteq(alpn, ist("\x02h2"));
316
317 case SRV_WS_H1:
318 return isteq(alpn, ist("\x08http/1.1"));
319 }
320 }
321
322 return 0;
323}
324
325/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
326 * is a valid value indicating to use the fallback mux.
327 */
328const struct mux_ops *srv_get_ws_proto(struct server *srv)
329{
330 const struct mux_proto_list *mux = NULL;
331
332 switch (srv->ws) {
333 case SRV_WS_AUTO:
334 mux = srv->mux_proto;
335 break;
336
337 case SRV_WS_H1:
338 mux = get_mux_proto(ist("h1"));
339 break;
340
341 case SRV_WS_H2:
342 mux = get_mux_proto(ist("h2"));
343 break;
344 }
345
346 return mux ? mux->mux : NULL;
347}
348
Willy Tarreau21faa912012-10-10 08:27:36 +0200349/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200350 * Must be called with the server lock held. The server is first removed from
351 * the proxy tree if it was already attached. If <reattach> is true, the server
352 * will then be attached in the proxy tree. The proxy lock is held to
353 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700354 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200355static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700356{
357 struct proxy *p = s->proxy;
358 char *key;
359
360 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
361
362 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700363 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700364 free(key);
365 return;
366 }
367
368 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
369 ebpt_delete(&s->addr_node);
370 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
371
372 free(s->addr_node.key);
373 }
374
375 s->addr_node.key = key;
376
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200377 if (reattach) {
378 if (s->addr_node.key) {
379 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
380 ebis_insert(&p->used_server_addr, &s->addr_node);
381 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
382 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700383 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700384}
385
386/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200387 * Registers the server keyword list <kwl> as a list of valid keywords for next
388 * parsing sessions.
389 */
390void srv_register_keywords(struct srv_kw_list *kwl)
391{
Willy Tarreau2b718102021-04-21 07:32:39 +0200392 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200393}
394
395/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
396 * keyword is found with a NULL ->parse() function, then an attempt is made to
397 * find one with a valid ->parse() function. This way it is possible to declare
398 * platform-dependant, known keywords as NULL, then only declare them as valid
399 * if some options are met. Note that if the requested keyword contains an
400 * opening parenthesis, everything from this point is ignored.
401 */
402struct srv_kw *srv_find_kw(const char *kw)
403{
404 int index;
405 const char *kwend;
406 struct srv_kw_list *kwl;
407 struct srv_kw *ret = NULL;
408
409 kwend = strchr(kw, '(');
410 if (!kwend)
411 kwend = kw + strlen(kw);
412
413 list_for_each_entry(kwl, &srv_keywords.list, list) {
414 for (index = 0; kwl->kw[index].kw != NULL; index++) {
415 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
416 kwl->kw[index].kw[kwend-kw] == 0) {
417 if (kwl->kw[index].parse)
418 return &kwl->kw[index]; /* found it !*/
419 else
420 ret = &kwl->kw[index]; /* may be OK */
421 }
422 }
423 }
424 return ret;
425}
426
427/* Dumps all registered "server" keywords to the <out> string pointer. The
428 * unsupported keywords are only dumped if their supported form was not
429 * found.
430 */
431void srv_dump_kws(char **out)
432{
433 struct srv_kw_list *kwl;
434 int index;
435
Christopher Faulet784063e2020-05-18 12:14:18 +0200436 if (!out)
437 return;
438
Willy Tarreau21faa912012-10-10 08:27:36 +0200439 *out = NULL;
440 list_for_each_entry(kwl, &srv_keywords.list, list) {
441 for (index = 0; kwl->kw[index].kw != NULL; index++) {
442 if (kwl->kw[index].parse ||
443 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
444 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
445 kwl->scope,
446 kwl->kw[index].kw,
447 kwl->kw[index].skip ? " <arg>" : "",
448 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
449 kwl->kw[index].parse ? "" : " (not supported)");
450 }
451 }
452 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100453}
454
455/* Try to find in srv_keyword the word that looks closest to <word> by counting
456 * transitions between letters, digits and other characters. Will return the
457 * best matching word if found, otherwise NULL. An optional array of extra
458 * words to compare may be passed in <extra>, but it must then be terminated
459 * by a NULL entry. If unused it may be NULL.
460 */
461static const char *srv_find_best_kw(const char *word)
462{
463 uint8_t word_sig[1024];
464 uint8_t list_sig[1024];
465 const struct srv_kw_list *kwl;
466 const char *best_ptr = NULL;
467 int dist, best_dist = INT_MAX;
468 const char **extra;
469 int index;
470
471 make_word_fingerprint(word_sig, word);
472 list_for_each_entry(kwl, &srv_keywords.list, list) {
473 for (index = 0; kwl->kw[index].kw != NULL; index++) {
474 make_word_fingerprint(list_sig, kwl->kw[index].kw);
475 dist = word_fingerprint_distance(word_sig, list_sig);
476 if (dist < best_dist) {
477 best_dist = dist;
478 best_ptr = kwl->kw[index].kw;
479 }
480 }
481 }
482
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100483 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100484 make_word_fingerprint(list_sig, *extra);
485 dist = word_fingerprint_distance(word_sig, list_sig);
486 if (dist < best_dist) {
487 best_dist = dist;
488 best_ptr = *extra;
489 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100490 }
491
492 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
493 best_ptr = NULL;
494
495 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200496}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100497
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100498/* Parse the "backup" server keyword */
499static int srv_parse_backup(char **args, int *cur_arg,
500 struct proxy *curproxy, struct server *newsrv, char **err)
501{
502 newsrv->flags |= SRV_F_BACKUP;
503 return 0;
504}
505
Alexander Liu2a54bb72019-05-22 19:44:48 +0800506
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100507/* Parse the "cookie" server keyword */
508static int srv_parse_cookie(char **args, int *cur_arg,
509 struct proxy *curproxy, struct server *newsrv, char **err)
510{
511 char *arg;
512
513 arg = args[*cur_arg + 1];
514 if (!*arg) {
515 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
516 return ERR_ALERT | ERR_FATAL;
517 }
518
519 free(newsrv->cookie);
520 newsrv->cookie = strdup(arg);
521 newsrv->cklen = strlen(arg);
522 newsrv->flags |= SRV_F_COOKIESET;
523 return 0;
524}
525
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100526/* Parse the "disabled" server keyword */
527static int srv_parse_disabled(char **args, int *cur_arg,
528 struct proxy *curproxy, struct server *newsrv, char **err)
529{
Emeric Brun52a91d32017-08-31 14:41:55 +0200530 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
531 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100532 newsrv->check.state |= CHK_ST_PAUSED;
533 newsrv->check.health = 0;
534 return 0;
535}
536
537/* Parse the "enabled" server keyword */
538static int srv_parse_enabled(char **args, int *cur_arg,
539 struct proxy *curproxy, struct server *newsrv, char **err)
540{
Emeric Brun52a91d32017-08-31 14:41:55 +0200541 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
542 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100543 newsrv->check.state &= ~CHK_ST_PAUSED;
544 newsrv->check.health = newsrv->check.rise;
545 return 0;
546}
547
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100548/* Parse the "error-limit" server keyword */
549static int srv_parse_error_limit(char **args, int *cur_arg,
550 struct proxy *curproxy, struct server *newsrv, char **err)
551{
552 if (!*args[*cur_arg + 1]) {
553 memprintf(err, "'%s' expects an integer argument.",
554 args[*cur_arg]);
555 return ERR_ALERT | ERR_FATAL;
556 }
557
558 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
559
560 if (newsrv->consecutive_errors_limit <= 0) {
561 memprintf(err, "%s has to be > 0.",
562 args[*cur_arg]);
563 return ERR_ALERT | ERR_FATAL;
564 }
565
566 return 0;
567}
568
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200569/* Parse the "ws" keyword */
570static int srv_parse_ws(char **args, int *cur_arg,
571 struct proxy *curproxy, struct server *newsrv, char **err)
572{
573 if (!args[*cur_arg + 1]) {
574 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
575 return ERR_ALERT | ERR_FATAL;
576 }
577
578 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
579 newsrv->ws = SRV_WS_H1;
580 }
581 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
582 newsrv->ws = SRV_WS_H2;
583 }
584 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
585 newsrv->ws = SRV_WS_AUTO;
586 }
587 else {
588 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
589 return ERR_ALERT | ERR_FATAL;
590 }
591
592
593 return 0;
594}
595
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100596/* Parse the "init-addr" server keyword */
597static int srv_parse_init_addr(char **args, int *cur_arg,
598 struct proxy *curproxy, struct server *newsrv, char **err)
599{
600 char *p, *end;
601 int done;
602 struct sockaddr_storage sa;
603
604 newsrv->init_addr_methods = 0;
605 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
606
607 for (p = args[*cur_arg + 1]; *p; p = end) {
608 /* cut on next comma */
609 for (end = p; *end && *end != ','; end++);
610 if (*end)
611 *(end++) = 0;
612
613 memset(&sa, 0, sizeof(sa));
614 if (strcmp(p, "libc") == 0) {
615 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
616 }
617 else if (strcmp(p, "last") == 0) {
618 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
619 }
620 else if (strcmp(p, "none") == 0) {
621 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
622 }
623 else if (str2ip2(p, &sa, 0)) {
624 if (is_addr(&newsrv->init_addr)) {
625 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
626 args[*cur_arg], p);
627 return ERR_ALERT | ERR_FATAL;
628 }
629 newsrv->init_addr = sa;
630 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
631 }
632 else {
633 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
634 args[*cur_arg], p);
635 return ERR_ALERT | ERR_FATAL;
636 }
637 if (!done) {
638 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
639 args[*cur_arg], p);
640 return ERR_ALERT | ERR_FATAL;
641 }
642 }
643
644 return 0;
645}
646
647/* Parse the "log-proto" server keyword */
648static int srv_parse_log_proto(char **args, int *cur_arg,
649 struct proxy *curproxy, struct server *newsrv, char **err)
650{
651 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
652 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
653 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
654 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
655 else {
656 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
657 args[*cur_arg], args[*cur_arg + 1]);
658 return ERR_ALERT | ERR_FATAL;
659 }
660
661 return 0;
662}
663
664/* Parse the "maxconn" server keyword */
665static int srv_parse_maxconn(char **args, int *cur_arg,
666 struct proxy *curproxy, struct server *newsrv, char **err)
667{
668 newsrv->maxconn = atol(args[*cur_arg + 1]);
669 return 0;
670}
671
672/* Parse the "maxqueue" server keyword */
673static int srv_parse_maxqueue(char **args, int *cur_arg,
674 struct proxy *curproxy, struct server *newsrv, char **err)
675{
676 newsrv->maxqueue = atol(args[*cur_arg + 1]);
677 return 0;
678}
679
680/* Parse the "minconn" server keyword */
681static int srv_parse_minconn(char **args, int *cur_arg,
682 struct proxy *curproxy, struct server *newsrv, char **err)
683{
684 newsrv->minconn = atol(args[*cur_arg + 1]);
685 return 0;
686}
687
Willy Tarreau9c538e02019-01-23 10:21:49 +0100688static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
689{
690 char *arg;
691
692 arg = args[*cur_arg + 1];
693 if (!*arg) {
694 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
695 return ERR_ALERT | ERR_FATAL;
696 }
697 newsrv->max_reuse = atoi(arg);
698
699 return 0;
700}
701
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100702static 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 +0100703{
704 const char *res;
705 char *arg;
706 unsigned int time;
707
708 arg = args[*cur_arg + 1];
709 if (!*arg) {
710 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
711 return ERR_ALERT | ERR_FATAL;
712 }
713 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200714 if (res == PARSE_TIME_OVER) {
715 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
716 args[*cur_arg+1], args[*cur_arg]);
717 return ERR_ALERT | ERR_FATAL;
718 }
719 else if (res == PARSE_TIME_UNDER) {
720 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
721 args[*cur_arg+1], args[*cur_arg]);
722 return ERR_ALERT | ERR_FATAL;
723 }
724 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100725 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
726 *res, args[*cur_arg]);
727 return ERR_ALERT | ERR_FATAL;
728 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100729 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100730
731 return 0;
732}
733
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200734static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
735{
736 char *arg;
737
738 arg = args[*cur_arg + 1];
739 if (!*arg) {
740 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
741 return ERR_ALERT | ERR_FATAL;
742 }
743
744 newsrv->low_idle_conns = atoi(arg);
745 return 0;
746}
747
Olivier Houchard006e3102018-12-10 18:30:32 +0100748static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
749{
750 char *arg;
751
752 arg = args[*cur_arg + 1];
753 if (!*arg) {
754 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
755 return ERR_ALERT | ERR_FATAL;
756 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100757
Olivier Houchard006e3102018-12-10 18:30:32 +0100758 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100759 if ((int)newsrv->max_idle_conns < -1) {
760 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
761 return ERR_ALERT | ERR_FATAL;
762 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100763
764 return 0;
765}
766
Willy Tarreaudff55432012-10-10 17:51:05 +0200767/* parse the "id" server keyword */
768static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
769{
770 struct eb32_node *node;
771
772 if (!*args[*cur_arg + 1]) {
773 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
774 return ERR_ALERT | ERR_FATAL;
775 }
776
777 newsrv->puid = atol(args[*cur_arg + 1]);
778 newsrv->conf.id.key = newsrv->puid;
779
780 if (newsrv->puid <= 0) {
781 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
782 return ERR_ALERT | ERR_FATAL;
783 }
784
785 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
786 if (node) {
787 struct server *target = container_of(node, struct server, conf.id);
788 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
789 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
790 target->id);
791 return ERR_ALERT | ERR_FATAL;
792 }
793
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200794 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200795 return 0;
796}
797
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100798/* Parse the "namespace" server keyword */
799static int srv_parse_namespace(char **args, int *cur_arg,
800 struct proxy *curproxy, struct server *newsrv, char **err)
801{
Willy Tarreaue5733232019-05-22 19:24:06 +0200802#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100803 char *arg;
804
805 arg = args[*cur_arg + 1];
806 if (!*arg) {
807 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
808 return ERR_ALERT | ERR_FATAL;
809 }
810
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100811 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100812 /* Use the namespace associated with the connection (if present). */
813 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
814 return 0;
815 }
816
817 /*
818 * As this parser may be called several times for the same 'default-server'
819 * object, or for a new 'server' instance deriving from a 'default-server'
820 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
821 */
822 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
823
824 newsrv->netns = netns_store_lookup(arg, strlen(arg));
825 if (!newsrv->netns)
826 newsrv->netns = netns_store_insert(arg);
827
828 if (!newsrv->netns) {
829 memprintf(err, "Cannot open namespace '%s'", arg);
830 return ERR_ALERT | ERR_FATAL;
831 }
832
833 return 0;
834#else
835 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
836 return ERR_ALERT | ERR_FATAL;
837#endif
838}
839
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100840/* Parse the "no-backup" server keyword */
841static int srv_parse_no_backup(char **args, int *cur_arg,
842 struct proxy *curproxy, struct server *newsrv, char **err)
843{
844 newsrv->flags &= ~SRV_F_BACKUP;
845 return 0;
846}
847
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100848
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100849/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200850static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100851{
852 srv->pp_opts &= ~flags;
853 return 0;
854}
855
856/* Parse the "no-send-proxy" server keyword */
857static int srv_parse_no_send_proxy(char **args, int *cur_arg,
858 struct proxy *curproxy, struct server *newsrv, char **err)
859{
860 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
861}
862
863/* Parse the "no-send-proxy-v2" server keyword */
864static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
865 struct proxy *curproxy, struct server *newsrv, char **err)
866{
867 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
868}
869
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200870/* Parse the "shard" server keyword */
871static int srv_parse_shard(char **args, int *cur_arg,
872 struct proxy *curproxy, struct server *newsrv, char **err)
873{
874 newsrv->shard = atol(args[*cur_arg + 1]);
875 return 0;
876}
877
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200878/* Parse the "no-tfo" server keyword */
879static int srv_parse_no_tfo(char **args, int *cur_arg,
880 struct proxy *curproxy, struct server *newsrv, char **err)
881{
882 newsrv->flags &= ~SRV_F_FASTOPEN;
883 return 0;
884}
885
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100886/* Parse the "non-stick" server keyword */
887static int srv_parse_non_stick(char **args, int *cur_arg,
888 struct proxy *curproxy, struct server *newsrv, char **err)
889{
890 newsrv->flags |= SRV_F_NON_STICK;
891 return 0;
892}
893
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100894/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200895static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100896{
897 srv->pp_opts |= flags;
898 return 0;
899}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200900/* parse the "proto" server keyword */
901static int srv_parse_proto(char **args, int *cur_arg,
902 struct proxy *px, struct server *newsrv, char **err)
903{
904 struct ist proto;
905
906 if (!*args[*cur_arg + 1]) {
907 memprintf(err, "'%s' : missing value", args[*cur_arg]);
908 return ERR_ALERT | ERR_FATAL;
909 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100910 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200911 newsrv->mux_proto = get_mux_proto(proto);
912 if (!newsrv->mux_proto) {
913 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
914 return ERR_ALERT | ERR_FATAL;
915 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200916 return 0;
917}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100918
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100919/* parse the "proxy-v2-options" */
920static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
921 struct proxy *px, struct server *newsrv, char **err)
922{
923 char *p, *n;
924 for (p = args[*cur_arg+1]; p; p = n) {
925 n = strchr(p, ',');
926 if (n)
927 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100928 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100929 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100930 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100931 newsrv->pp_opts |= SRV_PP_V2_SSL;
932 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100933 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100934 newsrv->pp_opts |= SRV_PP_V2_SSL;
935 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100936 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100937 newsrv->pp_opts |= SRV_PP_V2_SSL;
938 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100939 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100940 newsrv->pp_opts |= SRV_PP_V2_SSL;
941 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100942 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100943 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100944 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100945 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100946 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100947 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100948 } else
949 goto fail;
950 }
951 return 0;
952 fail:
953 if (err)
954 memprintf(err, "'%s' : proxy v2 option not implemented", p);
955 return ERR_ALERT | ERR_FATAL;
956}
957
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100958/* Parse the "observe" server keyword */
959static int srv_parse_observe(char **args, int *cur_arg,
960 struct proxy *curproxy, struct server *newsrv, char **err)
961{
962 char *arg;
963
964 arg = args[*cur_arg + 1];
965 if (!*arg) {
966 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
967 return ERR_ALERT | ERR_FATAL;
968 }
969
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100970 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100971 newsrv->observe = HANA_OBS_NONE;
972 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100973 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100974 newsrv->observe = HANA_OBS_LAYER4;
975 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100976 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100977 if (curproxy->mode != PR_MODE_HTTP) {
978 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
979 return ERR_ALERT;
980 }
981 newsrv->observe = HANA_OBS_LAYER7;
982 }
983 else {
984 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
985 "but got '%s'\n", args[*cur_arg], arg);
986 return ERR_ALERT | ERR_FATAL;
987 }
988
989 return 0;
990}
991
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100992/* Parse the "on-error" server keyword */
993static int srv_parse_on_error(char **args, int *cur_arg,
994 struct proxy *curproxy, struct server *newsrv, char **err)
995{
996 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
997 newsrv->onerror = HANA_ONERR_FASTINTER;
998 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
999 newsrv->onerror = HANA_ONERR_FAILCHK;
1000 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
1001 newsrv->onerror = HANA_ONERR_SUDDTH;
1002 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
1003 newsrv->onerror = HANA_ONERR_MARKDWN;
1004 else {
1005 memprintf(err, "'%s' expects one of 'fastinter', "
1006 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1007 args[*cur_arg], args[*cur_arg + 1]);
1008 return ERR_ALERT | ERR_FATAL;
1009 }
1010
1011 return 0;
1012}
1013
1014/* Parse the "on-marked-down" server keyword */
1015static int srv_parse_on_marked_down(char **args, int *cur_arg,
1016 struct proxy *curproxy, struct server *newsrv, char **err)
1017{
1018 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1019 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1020 else {
1021 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1022 args[*cur_arg], args[*cur_arg + 1]);
1023 return ERR_ALERT | ERR_FATAL;
1024 }
1025
1026 return 0;
1027}
1028
1029/* Parse the "on-marked-up" server keyword */
1030static int srv_parse_on_marked_up(char **args, int *cur_arg,
1031 struct proxy *curproxy, struct server *newsrv, char **err)
1032{
1033 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1034 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1035 else {
1036 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1037 args[*cur_arg], args[*cur_arg + 1]);
1038 return ERR_ALERT | ERR_FATAL;
1039 }
1040
1041 return 0;
1042}
1043
Frédéric Lécaille16186232017-03-14 16:42:49 +01001044/* Parse the "redir" server keyword */
1045static int srv_parse_redir(char **args, int *cur_arg,
1046 struct proxy *curproxy, struct server *newsrv, char **err)
1047{
1048 char *arg;
1049
1050 arg = args[*cur_arg + 1];
1051 if (!*arg) {
1052 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1053 return ERR_ALERT | ERR_FATAL;
1054 }
1055
1056 free(newsrv->rdr_pfx);
1057 newsrv->rdr_pfx = strdup(arg);
1058 newsrv->rdr_len = strlen(arg);
1059
1060 return 0;
1061}
1062
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001063/* Parse the "resolvers" server keyword */
1064static int srv_parse_resolvers(char **args, int *cur_arg,
1065 struct proxy *curproxy, struct server *newsrv, char **err)
1066{
1067 free(newsrv->resolvers_id);
1068 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1069 return 0;
1070}
1071
1072/* Parse the "resolve-net" server keyword */
1073static int srv_parse_resolve_net(char **args, int *cur_arg,
1074 struct proxy *curproxy, struct server *newsrv, char **err)
1075{
1076 char *p, *e;
1077 unsigned char mask;
1078 struct resolv_options *opt;
1079
1080 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1081 memprintf(err, "'%s' expects a list of networks.",
1082 args[*cur_arg]);
1083 return ERR_ALERT | ERR_FATAL;
1084 }
1085
1086 opt = &newsrv->resolv_opts;
1087
1088 /* Split arguments by comma, and convert it from ipv4 or ipv6
1089 * string network in in_addr or in6_addr.
1090 */
1091 p = args[*cur_arg + 1];
1092 e = p;
1093 while (*p != '\0') {
1094 /* If no room available, return error. */
1095 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1096 memprintf(err, "'%s' exceed %d networks.",
1097 args[*cur_arg], SRV_MAX_PREF_NET);
1098 return ERR_ALERT | ERR_FATAL;
1099 }
1100 /* look for end or comma. */
1101 while (*e != ',' && *e != '\0')
1102 e++;
1103 if (*e == ',') {
1104 *e = '\0';
1105 e++;
1106 }
1107 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1108 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1109 /* Try to convert input string from ipv4 or ipv6 network. */
1110 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1111 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1112 &mask)) {
1113 /* Try to convert input string from ipv6 network. */
1114 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1115 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1116 } else {
1117 /* All network conversions fail, return error. */
1118 memprintf(err, "'%s' invalid network '%s'.",
1119 args[*cur_arg], p);
1120 return ERR_ALERT | ERR_FATAL;
1121 }
1122 opt->pref_net_nb++;
1123 p = e;
1124 }
1125
1126 return 0;
1127}
1128
1129/* Parse the "resolve-opts" server keyword */
1130static int srv_parse_resolve_opts(char **args, int *cur_arg,
1131 struct proxy *curproxy, struct server *newsrv, char **err)
1132{
1133 char *p, *end;
1134
1135 for (p = args[*cur_arg + 1]; *p; p = end) {
1136 /* cut on next comma */
1137 for (end = p; *end && *end != ','; end++);
1138 if (*end)
1139 *(end++) = 0;
1140
1141 if (strcmp(p, "allow-dup-ip") == 0) {
1142 newsrv->resolv_opts.accept_duplicate_ip = 1;
1143 }
1144 else if (strcmp(p, "ignore-weight") == 0) {
1145 newsrv->resolv_opts.ignore_weight = 1;
1146 }
1147 else if (strcmp(p, "prevent-dup-ip") == 0) {
1148 newsrv->resolv_opts.accept_duplicate_ip = 0;
1149 }
1150 else {
1151 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1152 args[*cur_arg], p);
1153 return ERR_ALERT | ERR_FATAL;
1154 }
1155 }
1156
1157 return 0;
1158}
1159
1160/* Parse the "resolve-prefer" server keyword */
1161static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1162 struct proxy *curproxy, struct server *newsrv, char **err)
1163{
1164 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1165 newsrv->resolv_opts.family_prio = AF_INET;
1166 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1167 newsrv->resolv_opts.family_prio = AF_INET6;
1168 else {
1169 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1170 args[*cur_arg]);
1171 return ERR_ALERT | ERR_FATAL;
1172 }
1173
1174 return 0;
1175}
1176
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001177/* Parse the "send-proxy" server keyword */
1178static int srv_parse_send_proxy(char **args, int *cur_arg,
1179 struct proxy *curproxy, struct server *newsrv, char **err)
1180{
1181 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1182}
1183
1184/* Parse the "send-proxy-v2" server keyword */
1185static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1186 struct proxy *curproxy, struct server *newsrv, char **err)
1187{
1188 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1189}
1190
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001191/* Parse the "slowstart" server keyword */
1192static int srv_parse_slowstart(char **args, int *cur_arg,
1193 struct proxy *curproxy, struct server *newsrv, char **err)
1194{
1195 /* slowstart is stored in seconds */
1196 unsigned int val;
1197 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1198
1199 if (time_err == PARSE_TIME_OVER) {
1200 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1201 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1202 return ERR_ALERT | ERR_FATAL;
1203 }
1204 else if (time_err == PARSE_TIME_UNDER) {
1205 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1206 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1207 return ERR_ALERT | ERR_FATAL;
1208 }
1209 else if (time_err) {
1210 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1211 *time_err, newsrv->id);
1212 return ERR_ALERT | ERR_FATAL;
1213 }
1214 newsrv->slowstart = (val + 999) / 1000;
1215
1216 return 0;
1217}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001218
1219/* Parse the "source" server keyword */
1220static int srv_parse_source(char **args, int *cur_arg,
1221 struct proxy *curproxy, struct server *newsrv, char **err)
1222{
1223 char *errmsg;
1224 int port_low, port_high;
1225 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001226
1227 errmsg = NULL;
1228
1229 if (!*args[*cur_arg + 1]) {
1230 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1231 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1232 goto err;
1233 }
1234
1235 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001236 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1237 &errmsg, NULL, NULL,
1238 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 +01001239 if (!sk) {
1240 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1241 goto err;
1242 }
1243
Frédéric Lécailledba97072017-03-17 15:33:50 +01001244 newsrv->conn_src.opts |= CO_SRC_BIND;
1245 newsrv->conn_src.source_addr = *sk;
1246
1247 if (port_low != port_high) {
1248 int i;
1249
Frédéric Lécailledba97072017-03-17 15:33:50 +01001250 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001251 if (!newsrv->conn_src.sport_range) {
1252 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1253 goto err;
1254 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001255 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1256 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1257 }
1258
1259 *cur_arg += 2;
1260 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001261 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001262#if defined(CONFIG_HAP_TRANSPARENT)
1263 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001264 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1265 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001266 goto err;
1267 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001268 if (strcmp(args[*cur_arg + 1], "client") == 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_CLI;
1271 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001272 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001273 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1274 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1275 }
1276 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1277 char *name, *end;
1278
1279 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001280 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001281 name++;
1282
1283 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001284 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001285 end++;
1286
1287 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1288 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1289 free(newsrv->conn_src.bind_hdr_name);
1290 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001291 if (!newsrv->conn_src.bind_hdr_name) {
1292 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1293 goto err;
1294 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001295 newsrv->conn_src.bind_hdr_len = end - name;
1296 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1297 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1298 newsrv->conn_src.bind_hdr_occ = -1;
1299
1300 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001301 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001302 end++;
1303 if (*end == ',') {
1304 end++;
1305 name = end;
1306 if (*end == '-')
1307 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001308 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001309 end++;
1310 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1311 }
1312
1313 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001314 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1315 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001316 goto err;
1317 }
1318 }
1319 else {
1320 struct sockaddr_storage *sk;
1321 int port1, port2;
1322
1323 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001324 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1325 &errmsg, NULL, NULL,
1326 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001327 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001328 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001329 goto err;
1330 }
1331
Frédéric Lécailledba97072017-03-17 15:33:50 +01001332 newsrv->conn_src.tproxy_addr = *sk;
1333 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1334 }
1335 global.last_checks |= LSTCHK_NETADM;
1336 *cur_arg += 2;
1337 continue;
1338#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001339 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 +01001340 goto err;
1341#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1342 } /* "usesrc" */
1343
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001344 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001345#ifdef SO_BINDTODEVICE
1346 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001347 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001348 goto err;
1349 }
1350 free(newsrv->conn_src.iface_name);
1351 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1352 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1353 global.last_checks |= LSTCHK_NETADM;
1354#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001355 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001356 goto err;
1357#endif
1358 *cur_arg += 2;
1359 continue;
1360 }
1361 /* this keyword in not an option of "source" */
1362 break;
1363 } /* while */
1364
1365 return 0;
1366
1367 err:
1368 free(errmsg);
1369 return ERR_ALERT | ERR_FATAL;
1370}
1371
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001372/* Parse the "stick" server keyword */
1373static int srv_parse_stick(char **args, int *cur_arg,
1374 struct proxy *curproxy, struct server *newsrv, char **err)
1375{
1376 newsrv->flags &= ~SRV_F_NON_STICK;
1377 return 0;
1378}
1379
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001380/* Parse the "track" server keyword */
1381static int srv_parse_track(char **args, int *cur_arg,
1382 struct proxy *curproxy, struct server *newsrv, char **err)
1383{
1384 char *arg;
1385
1386 arg = args[*cur_arg + 1];
1387 if (!*arg) {
1388 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1389 return ERR_ALERT | ERR_FATAL;
1390 }
1391
1392 free(newsrv->trackit);
1393 newsrv->trackit = strdup(arg);
1394
1395 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001396}
1397
1398/* Parse the "socks4" server keyword */
1399static int srv_parse_socks4(char **args, int *cur_arg,
1400 struct proxy *curproxy, struct server *newsrv, char **err)
1401{
1402 char *errmsg;
1403 int port_low, port_high;
1404 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001405
1406 errmsg = NULL;
1407
1408 if (!*args[*cur_arg + 1]) {
1409 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1410 goto err;
1411 }
1412
1413 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001414 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1415 &errmsg, NULL, NULL,
1416 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001417 if (!sk) {
1418 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1419 goto err;
1420 }
1421
Alexander Liu2a54bb72019-05-22 19:44:48 +08001422 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1423 newsrv->socks4_addr = *sk;
1424
Alexander Liu2a54bb72019-05-22 19:44:48 +08001425 return 0;
1426
1427 err:
1428 free(errmsg);
1429 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001430}
1431
Frédéric Lécailledba97072017-03-17 15:33:50 +01001432
Willy Tarreau034c88c2017-01-23 23:36:45 +01001433/* parse the "tfo" server keyword */
1434static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1435{
1436 newsrv->flags |= SRV_F_FASTOPEN;
1437 return 0;
1438}
1439
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001440/* parse the "usesrc" server keyword */
1441static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1442{
1443 memprintf(err, "'%s' only allowed after a '%s' statement.",
1444 "usesrc", "source");
1445 return ERR_ALERT | ERR_FATAL;
1446}
1447
1448/* parse the "weight" server keyword */
1449static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1450{
1451 int w;
1452
1453 w = atol(args[*cur_arg + 1]);
1454 if (w < 0 || w > SRV_UWGHT_MAX) {
1455 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1456 newsrv->id, SRV_UWGHT_MAX, w);
1457 return ERR_ALERT | ERR_FATAL;
1458 }
1459 newsrv->uweight = newsrv->iweight = w;
1460
1461 return 0;
1462}
1463
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001464/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001465 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001466 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001467 *
1468 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001469 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001470void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001471{
Willy Tarreau751153e2021-02-17 13:33:24 +01001472 struct stream *stream;
1473 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001474 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001475
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001476 for (thr = 0; thr < global.nbthread; thr++)
1477 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1478 if (stream->srv_conn == srv)
1479 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001480}
1481
1482/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001483 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001484 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001485 *
1486 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001487 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001488void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001489{
1490 struct server *srv;
1491
1492 for (srv = px->srv; srv != NULL; srv = srv->next)
1493 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001494 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001495}
1496
Willy Tarreaubda92272014-05-20 21:55:30 +02001497/* Appends some information to a message string related to a server going UP or
1498 * DOWN. If both <forced> and <reason> are null and the server tracks another
1499 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001500 * If <check> is non-null, an entire string describing the check result will be
1501 * appended after a comma and a space (eg: to report some information from the
1502 * check that changed the state). In the other case, the string will be built
1503 * using the check results stored into the struct server if present.
1504 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001505 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001506 *
1507 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001508 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001509void srv_append_status(struct buffer *msg, struct server *s,
1510 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001511{
Emeric Brun5a133512017-10-19 14:42:30 +02001512 short status = s->op_st_chg.status;
1513 short code = s->op_st_chg.code;
1514 long duration = s->op_st_chg.duration;
1515 char *desc = s->op_st_chg.reason;
1516
1517 if (check) {
1518 status = check->status;
1519 code = check->code;
1520 duration = check->duration;
1521 desc = check->desc;
1522 }
1523
1524 if (status != -1) {
1525 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1526
1527 if (status >= HCHK_STATUS_L57DATA)
1528 chunk_appendf(msg, ", code: %d", code);
1529
1530 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001531 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001532
1533 chunk_appendf(msg, ", info: \"");
1534
1535 chunk_initlen(&src, desc, 0, strlen(desc));
1536 chunk_asciiencode(msg, &src, '"');
1537
1538 chunk_appendf(msg, "\"");
1539 }
1540
1541 if (duration >= 0)
1542 chunk_appendf(msg, ", check duration: %ldms", duration);
1543 }
1544 else if (desc && *desc) {
1545 chunk_appendf(msg, ", %s", desc);
1546 }
1547 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001548 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001549 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001550
1551 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001552 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001553 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1554 " %d sessions active, %d requeued, %d remaining in queue",
1555 s->proxy->srv_act, s->proxy->srv_bck,
1556 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001557 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001558 else
1559 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1560 " %d sessions requeued, %d total in queue",
1561 s->proxy->srv_act, s->proxy->srv_bck,
1562 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001563 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001564 }
1565}
1566
Emeric Brun5a133512017-10-19 14:42:30 +02001567/* Marks server <s> down, regardless of its checks' statuses. The server is
1568 * registered in a list to postpone the counting of the remaining servers on
1569 * the proxy and transfers queued streams whenever possible to other servers at
1570 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1571 * non-null as the reason for going down or the available data from the check
1572 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001573 *
1574 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001575 */
Emeric Brun5a133512017-10-19 14:42:30 +02001576void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001577{
1578 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001579
Emeric Brun64cc49c2017-10-03 14:46:45 +02001580 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001581 return;
1582
Emeric Brun52a91d32017-08-31 14:41:55 +02001583 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001584 *s->op_st_chg.reason = 0;
1585 s->op_st_chg.status = -1;
1586 if (reason) {
1587 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1588 }
1589 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001590 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001591 s->op_st_chg.code = check->code;
1592 s->op_st_chg.status = check->status;
1593 s->op_st_chg.duration = check->duration;
1594 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001595
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001596 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001597 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001598
Emeric Brun9f0b4582017-10-23 14:39:51 +02001599 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001600 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001601 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001602 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001603 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001604}
1605
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001606/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001607 * in maintenance. The server is registered in a list to postpone the counting
1608 * of the remaining servers on the proxy and tries to grab requests from the
1609 * proxy at a sync point. Maintenance servers are ignored. It stores the
1610 * <reason> if non-null as the reason for going down or the available data
1611 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001612 *
1613 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001614 */
Emeric Brun5a133512017-10-19 14:42:30 +02001615void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001616{
1617 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001618
Emeric Brun64cc49c2017-10-03 14:46:45 +02001619 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001620 return;
1621
Emeric Brun52a91d32017-08-31 14:41:55 +02001622 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001623 return;
1624
Emeric Brun52a91d32017-08-31 14:41:55 +02001625 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001626 *s->op_st_chg.reason = 0;
1627 s->op_st_chg.status = -1;
1628 if (reason) {
1629 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1630 }
1631 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001632 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001633 s->op_st_chg.code = check->code;
1634 s->op_st_chg.status = check->status;
1635 s->op_st_chg.duration = check->duration;
1636 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001637
Emeric Brun64cc49c2017-10-03 14:46:45 +02001638 if (s->slowstart <= 0)
1639 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001640
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001641 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001642 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001643
Emeric Brun9f0b4582017-10-23 14:39:51 +02001644 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001645 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001646 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001647 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001648 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001649}
1650
Willy Tarreau8eb77842014-05-21 13:54:57 +02001651/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001652 * isn't in maintenance. The server is registered in a list to postpone the
1653 * counting of the remaining servers on the proxy and tries to grab requests
1654 * from the proxy. Maintenance servers are ignored. It stores the
1655 * <reason> if non-null as the reason for going down or the available data
1656 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001657 * up. Note that it makes use of the trash to build the log strings, so <reason>
1658 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001659 *
1660 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001661 */
Emeric Brun5a133512017-10-19 14:42:30 +02001662void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001663{
1664 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001665
Emeric Brun64cc49c2017-10-03 14:46:45 +02001666 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001667 return;
1668
Emeric Brun52a91d32017-08-31 14:41:55 +02001669 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001670 return;
1671
Emeric Brun52a91d32017-08-31 14:41:55 +02001672 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001673 *s->op_st_chg.reason = 0;
1674 s->op_st_chg.status = -1;
1675 if (reason) {
1676 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1677 }
1678 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001679 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001680 s->op_st_chg.code = check->code;
1681 s->op_st_chg.status = check->status;
1682 s->op_st_chg.duration = check->duration;
1683 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001684
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001685 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001686 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001687
Emeric Brun9f0b4582017-10-23 14:39:51 +02001688 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001689 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001690 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001691 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001692 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001693}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001694
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001695/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1696 * enforce either maint mode or drain mode. It is not allowed to set more than
1697 * one flag at once. The equivalent "inherited" flag is propagated to all
1698 * tracking servers. Maintenance mode disables health checks (but not agent
1699 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001700 * is done. If <cause> is non-null, it will be displayed at the end of the log
1701 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001702 *
1703 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001704 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001705void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001706{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001707 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001708
1709 if (!mode)
1710 return;
1711
1712 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001713 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001714 return;
1715
Emeric Brun52a91d32017-08-31 14:41:55 +02001716 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001717 if (cause)
1718 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1719
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001720 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001721 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001722
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001723 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001724 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1725 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001726 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001727
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001728 /* compute the inherited flag to propagate */
1729 if (mode & SRV_ADMF_MAINT)
1730 mode = SRV_ADMF_IMAINT;
1731 else if (mode & SRV_ADMF_DRAIN)
1732 mode = SRV_ADMF_IDRAIN;
1733
Emeric Brun9f0b4582017-10-23 14:39:51 +02001734 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001735 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001736 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001737 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001738 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001739}
1740
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001741/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1742 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1743 * than one flag at once. The equivalent "inherited" flag is propagated to all
1744 * tracking servers. Leaving maintenance mode re-enables health checks. When
1745 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001746 *
1747 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001748 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001749void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001750{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001751 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001752
1753 if (!mode)
1754 return;
1755
1756 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001757 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001758 return;
1759
Emeric Brun52a91d32017-08-31 14:41:55 +02001760 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001761
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001762 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001763 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001764
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001765 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001766 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1767 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001768 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001769
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001770 if (mode & SRV_ADMF_MAINT)
1771 mode = SRV_ADMF_IMAINT;
1772 else if (mode & SRV_ADMF_DRAIN)
1773 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001774
Emeric Brun9f0b4582017-10-23 14:39:51 +02001775 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001776 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001777 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001778 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001779 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001780}
1781
Willy Tarreau757478e2016-11-03 19:22:19 +01001782/* principle: propagate maint and drain to tracking servers. This is useful
1783 * upon startup so that inherited states are correct.
1784 */
1785static void srv_propagate_admin_state(struct server *srv)
1786{
1787 struct server *srv2;
1788
1789 if (!srv->trackers)
1790 return;
1791
1792 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001793 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001794 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001795 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001796
Emeric Brun52a91d32017-08-31 14:41:55 +02001797 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001798 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001799 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001800 }
1801}
1802
1803/* Compute and propagate the admin states for all servers in proxy <px>.
1804 * Only servers *not* tracking another one are considered, because other
1805 * ones will be handled when the server they track is visited.
1806 */
1807void srv_compute_all_admin_states(struct proxy *px)
1808{
1809 struct server *srv;
1810
1811 for (srv = px->srv; srv; srv = srv->next) {
1812 if (srv->track)
1813 continue;
1814 srv_propagate_admin_state(srv);
1815 }
1816}
1817
Willy Tarreaudff55432012-10-10 17:51:05 +02001818/* Note: must not be declared <const> as its list will be overwritten.
1819 * Please take care of keeping this list alphabetically sorted, doing so helps
1820 * all code contributors.
1821 * Optional keywords are also declared with a NULL ->parse() function so that
1822 * the config parser can report an appropriate error when a known keyword was
1823 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001824 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001825 */
1826static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001827 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001828 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001829 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1830 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001831 { "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 +02001832 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001833 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001834 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1835 { "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 +01001836 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1837 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001838 { "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 +01001839 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001840 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001841 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1842 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1843 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1844 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001845 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001846 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1847 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1848 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1849 { "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 +01001850 { "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 */
1851 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1852 { "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 +01001853 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001854 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001855 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001856 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001857 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001858 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001859 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001860 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1861 { "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 +02001862 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001863 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001864 { "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 +01001865 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001866 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001867 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001868 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001869 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1870 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001871 { NULL, NULL, 0 },
1872}};
1873
Willy Tarreau0108d902018-11-25 19:14:37 +01001874INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001875
Willy Tarreau004e0452013-11-21 11:22:01 +01001876/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001877 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001878 * state is automatically disabled if the time is elapsed. If <must_update> is
1879 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001880 *
1881 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001882 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001883void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001884{
1885 struct proxy *px = sv->proxy;
1886 unsigned w;
1887
1888 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1889 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001890 if (sv->next_state == SRV_ST_STARTING)
1891 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001892 }
1893
1894 /* We must take care of not pushing the server to full throttle during slow starts.
1895 * It must also start immediately, at least at the minimal step when leaving maintenance.
1896 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001897 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001898 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1899 else
1900 w = px->lbprm.wdiv;
1901
Emeric Brun52a91d32017-08-31 14:41:55 +02001902 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001903
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001904 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001905 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001906 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001907}
1908
Willy Tarreaubaaee002006-06-26 02:48:02 +02001909/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001910 * Parses weight_str and configures sv accordingly.
1911 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001912 *
1913 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001914 */
1915const char *server_parse_weight_change_request(struct server *sv,
1916 const char *weight_str)
1917{
1918 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001919 long int w;
1920 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001921
1922 px = sv->proxy;
1923
1924 /* if the weight is terminated with '%', it is set relative to
1925 * the initial weight, otherwise it is absolute.
1926 */
1927 if (!*weight_str)
1928 return "Require <weight> or <weight%>.\n";
1929
Simon Hormanb796afa2013-02-12 10:45:53 +09001930 w = strtol(weight_str, &end, 10);
1931 if (end == weight_str)
1932 return "Empty weight string empty or preceded by garbage";
1933 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001934 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001935 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001936 /* Avoid integer overflow */
1937 if (w > 25600)
1938 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001939 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001940 if (w > 256)
1941 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001942 }
1943 else if (w < 0 || w > 256)
1944 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001945 else if (end[0] != '\0')
1946 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001947
1948 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1949 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1950
1951 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001952 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001953
1954 return NULL;
1955}
1956
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001957/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001958 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1959 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001960 * Returns:
1961 * - error string on error
1962 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001963 *
1964 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001965 */
1966const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001967 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001968{
1969 unsigned char ip[INET6_ADDRSTRLEN];
1970
1971 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001972 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001973 return NULL;
1974 }
1975 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001976 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001977 return NULL;
1978 }
1979
1980 return "Could not understand IP address format.\n";
1981}
1982
Willy Tarreau46b7f532018-08-21 11:54:26 +02001983/*
1984 * Must be called with the server lock held.
1985 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001986const char *server_parse_maxconn_change_request(struct server *sv,
1987 const char *maxconn_str)
1988{
1989 long int v;
1990 char *end;
1991
1992 if (!*maxconn_str)
1993 return "Require <maxconn>.\n";
1994
1995 v = strtol(maxconn_str, &end, 10);
1996 if (end == maxconn_str)
1997 return "maxconn string empty or preceded by garbage";
1998 else if (end[0] != '\0')
1999 return "Trailing garbage in maxconn string";
2000
2001 if (sv->maxconn == sv->minconn) { // static maxconn
2002 sv->maxconn = sv->minconn = v;
2003 } else { // dynamic maxconn
2004 sv->maxconn = v;
2005 }
2006
2007 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002008 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002009
2010 return NULL;
2011}
2012
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002013static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
2014 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002015{
2016 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002017 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002018 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002019 NULL,
2020 };
2021
2022 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002023 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002024
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002025 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002026}
2027
William Lallemand0d058672022-03-16 15:44:42 +01002028int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002029{
2030 struct sample_expr *expr;
2031
2032 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 +01002033 if (!expr) {
2034 memprintf(err, "error detected while parsing sni expression : %s", *err);
2035 return ERR_ALERT | ERR_FATAL;
2036 }
2037
2038 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2039 memprintf(err, "error detected while parsing sni expression : "
2040 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002041 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002042 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002043 return ERR_ALERT | ERR_FATAL;
2044 }
2045
2046 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2047 release_sample_expr(newsrv->ssl_ctx.sni);
2048 newsrv->ssl_ctx.sni = expr;
2049
2050 return 0;
2051}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002052
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002053static 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 +01002054{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002055 char *msg = "error encountered while processing ";
2056 char *quote = "'";
2057 char *token = args[cur_arg];
2058
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002059 if (err && *err) {
2060 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002061 msg = *err;
2062 quote = "";
2063 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002064 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002065
2066 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002067 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002068 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002069 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002070}
2071
Christopher Faulet0b365e32022-08-03 11:21:14 +02002072static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002073{
2074 int range_sz;
2075
2076 range_sz = src->conn_src.sport_range->size;
2077 if (range_sz > 0) {
2078 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2079 if (srv->conn_src.sport_range != NULL) {
2080 int i;
2081
2082 for (i = 0; i < range_sz; i++) {
2083 srv->conn_src.sport_range->ports[i] =
2084 src->conn_src.sport_range->ports[i];
2085 }
2086 }
2087 }
2088}
2089
2090/*
2091 * Copy <src> server connection source settings to <srv> server everything needed.
2092 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002093static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002094{
2095 srv->conn_src.opts = src->conn_src.opts;
2096 srv->conn_src.source_addr = src->conn_src.source_addr;
2097
2098 /* Source port range copy. */
2099 if (src->conn_src.sport_range != NULL)
2100 srv_conn_src_sport_range_cpy(srv, src);
2101
2102#ifdef CONFIG_HAP_TRANSPARENT
2103 if (src->conn_src.bind_hdr_name != NULL) {
2104 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2105 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2106 }
2107 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2108 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2109#endif
2110 if (src->conn_src.iface_name != NULL)
2111 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2112}
2113
2114/*
2115 * Copy <src> server SSL settings to <srv> server allocating
2116 * everything needed.
2117 */
2118#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002119static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002120{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002121 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002122 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2123
Christopher Faulet4ab26792021-12-01 09:50:41 +01002124 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2125 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2126
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002127 if (src->ssl_ctx.ca_file != NULL)
2128 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2129 if (src->ssl_ctx.crl_file != NULL)
2130 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002131 if (src->ssl_ctx.client_crt != NULL)
2132 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002133
2134 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2135
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002136
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002137 if (src->ssl_ctx.verify_host != NULL)
2138 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2139 if (src->ssl_ctx.ciphers != NULL)
2140 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002141 if (src->ssl_ctx.options)
2142 srv->ssl_ctx.options = src->ssl_ctx.options;
2143 if (src->ssl_ctx.methods.flags)
2144 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2145 if (src->ssl_ctx.methods.min)
2146 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2147 if (src->ssl_ctx.methods.max)
2148 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2149
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002150 if (src->ssl_ctx.ciphersuites != NULL)
2151 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002152 if (src->sni_expr != NULL)
2153 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002154
Olivier Houchardc7566002018-11-20 23:33:50 +01002155 if (src->ssl_ctx.alpn_str) {
2156 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2157 if (srv->ssl_ctx.alpn_str) {
2158 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2159 src->ssl_ctx.alpn_len);
2160 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2161 }
2162 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002163
Olivier Houchardc7566002018-11-20 23:33:50 +01002164 if (src->ssl_ctx.npn_str) {
2165 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2166 if (srv->ssl_ctx.npn_str) {
2167 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2168 src->ssl_ctx.npn_len);
2169 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2170 }
2171 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002172}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002173
2174/* Activate ssl on server <s>.
2175 * do nothing if there is no change to apply
2176 *
2177 * Must be called with the server lock held.
2178 */
2179void srv_set_ssl(struct server *s, int use_ssl)
2180{
2181 if (s->use_ssl == use_ssl)
2182 return;
2183
2184 s->use_ssl = use_ssl;
2185 if (s->use_ssl)
2186 s->xprt = xprt_get(XPRT_SSL);
2187 else
William Dauchya087f872022-01-06 16:57:15 +01002188 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002189}
2190
2191#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002192
2193/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002194 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002195 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002196 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002197 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002198int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002199{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002200 char *hostname_dn;
2201 int hostname_len, hostname_dn_len;
2202
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002203 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002204 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002205
Christopher Faulet67957bd2017-09-27 11:00:59 +02002206 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002207 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002208 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002209 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002210 if (hostname_dn_len == -1)
2211 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002212
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002213
Christopher Faulet67957bd2017-09-27 11:00:59 +02002214 free(srv->hostname);
2215 free(srv->hostname_dn);
2216 srv->hostname = strdup(hostname);
2217 srv->hostname_dn = strdup(hostname_dn);
2218 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002219 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002220 goto err;
2221
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002222 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002223
2224 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002225 ha_free(&srv->hostname);
2226 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002227 return -1;
2228}
2229
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002230/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002231 * Copy <src> server settings to <srv> server allocating
2232 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002233 * This function is not supposed to be called at any time, but only
2234 * during server settings parsing or during server allocations from
2235 * a server template, and just after having calloc()'ed a new server.
2236 * So, <src> may only be a default server (when parsing server settings)
2237 * or a server template (during server allocations from a server template).
2238 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2239 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002240 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002241void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002242{
2243 /* Connection source settings copy */
2244 srv_conn_src_cpy(srv, src);
2245
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002246 if (srv_tmpl) {
2247 srv->addr = src->addr;
2248 srv->svc_port = src->svc_port;
2249 }
2250
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002251 srv->pp_opts = src->pp_opts;
2252 if (src->rdr_pfx != NULL) {
2253 srv->rdr_pfx = strdup(src->rdr_pfx);
2254 srv->rdr_len = src->rdr_len;
2255 }
2256 if (src->cookie != NULL) {
2257 srv->cookie = strdup(src->cookie);
2258 srv->cklen = src->cklen;
2259 }
2260 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002261 srv->check.addr = src->check.addr;
2262 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002263 srv->check.use_ssl = src->check.use_ssl;
2264 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002265 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002266 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002267 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002268 /* Note: 'flags' field has potentially been already initialized. */
2269 srv->flags |= src->flags;
2270 srv->do_check = src->do_check;
2271 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002272 srv->check.inter = src->check.inter;
2273 srv->check.fastinter = src->check.fastinter;
2274 srv->check.downinter = src->check.downinter;
2275 srv->agent.use_ssl = src->agent.use_ssl;
2276 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002277
2278 if (src->agent.tcpcheck_rules) {
2279 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2280 if (srv->agent.tcpcheck_rules) {
2281 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2282 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2283 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2284 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2285 &src->agent.tcpcheck_rules->preset_vars);
2286 }
2287 }
2288
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002289 srv->agent.inter = src->agent.inter;
2290 srv->agent.fastinter = src->agent.fastinter;
2291 srv->agent.downinter = src->agent.downinter;
2292 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002293 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002294 srv->minconn = src->minconn;
2295 srv->maxconn = src->maxconn;
2296 srv->slowstart = src->slowstart;
2297 srv->observe = src->observe;
2298 srv->onerror = src->onerror;
2299 srv->onmarkeddown = src->onmarkeddown;
2300 srv->onmarkedup = src->onmarkedup;
2301 if (src->trackit != NULL)
2302 srv->trackit = strdup(src->trackit);
2303 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2304 srv->uweight = srv->iweight = src->iweight;
2305
2306 srv->check.send_proxy = src->check.send_proxy;
2307 /* health: up, but will fall down at first failure */
2308 srv->check.rise = srv->check.health = src->check.rise;
2309 srv->check.fall = src->check.fall;
2310
2311 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002312 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2313 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2314 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002315 srv->check.state |= CHK_ST_PAUSED;
2316 srv->check.health = 0;
2317 }
2318
2319 /* health: up but will fall down at first failure */
2320 srv->agent.rise = srv->agent.health = src->agent.rise;
2321 srv->agent.fall = src->agent.fall;
2322
2323 if (src->resolvers_id != NULL)
2324 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002325 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2326 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2327 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2328 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2329 srv->resolv_opts.family_prio = AF_INET6;
2330 memcpy(srv->resolv_opts.pref_net,
2331 src->resolv_opts.pref_net,
2332 sizeof srv->resolv_opts.pref_net);
2333 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002334
2335 srv->init_addr_methods = src->init_addr_methods;
2336 srv->init_addr = src->init_addr;
2337#if defined(USE_OPENSSL)
2338 srv_ssl_settings_cpy(srv, src);
2339#endif
2340#ifdef TCP_USER_TIMEOUT
2341 srv->tcp_ut = src->tcp_ut;
2342#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002343 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002344 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002345 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002346 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002347 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002348
Olivier Houchard8da5f982017-08-04 18:35:36 +02002349 if (srv_tmpl)
2350 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002351
2352 srv->check.via_socks4 = src->check.via_socks4;
2353 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002354}
2355
Willy Tarreau198e92a2021-03-05 10:23:32 +01002356/* allocate a server and attach it to the global servers_list. Returns
2357 * the server on success, otherwise NULL.
2358 */
William Lallemand313bfd12018-10-26 14:47:32 +02002359struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002360{
2361 struct server *srv;
2362
2363 srv = calloc(1, sizeof *srv);
2364 if (!srv)
2365 return NULL;
2366
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002367 srv_take(srv);
2368
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002369 srv->obj_type = OBJ_TYPE_SERVER;
2370 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002371 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002372 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002373 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002374 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002375 MT_LIST_INIT(&srv->prev_deleted);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002376 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002377
Emeric Brun52a91d32017-08-31 14:41:55 +02002378 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002379 srv->last_change = now.tv_sec;
2380
Christopher Faulet38290462020-04-21 11:46:40 +02002381 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002382 srv->check.status = HCHK_STATUS_INI;
2383 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002384 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002385 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002386
Christopher Faulet38290462020-04-21 11:46:40 +02002387 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002388 srv->agent.status = HCHK_STATUS_INI;
2389 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002390 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002391 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002392#if defined(USE_QUIC)
2393 srv->cids = EB_ROOT_UNIQUE;
2394#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002395
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002396 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002397#ifdef USE_OPENSSL
2398 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2399#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002400
Willy Tarreau975b1552019-06-06 16:25:55 +02002401 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002402 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002403 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002404 return srv;
2405}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002406
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002407/* Increment the server refcount. */
2408void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002409{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002410 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002411}
2412
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002413/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2414 * dynamic servers, its refcount is decremented first. The free operations are
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002415 * conducted only if the refcount is nul.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002416 *
2417 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002418 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002419 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002420struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002421{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002422 struct server *next = NULL;
2423
William Lallemand4c395fc2021-08-20 10:10:15 +02002424 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002425 goto end;
2426
2427 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002428
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002429 /* For dynamic servers, decrement the reference counter. Only free the
2430 * server when reaching zero.
2431 */
Aurelien DARRAGON32483ec2023-03-09 11:56:14 +01002432 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2433 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002434
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01002435 /* make sure we are removed from our 'next->prev_deleted' list
2436 * This doesn't require full thread isolation as we're using mt lists
2437 * However this could easily be turned into regular list if required
2438 * (with the proper use of thread isolation)
2439 */
2440 MT_LIST_DELETE(&srv->prev_deleted);
2441
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002442 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002443 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002444
2445 free(srv->id);
2446 free(srv->cookie);
2447 free(srv->hostname);
2448 free(srv->hostname_dn);
2449 free((char*)srv->conf.file);
2450 free(srv->per_thr);
Willy Tarreauc21a1872022-11-21 14:14:06 +01002451 free(srv->per_tgrp);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002452 free(srv->curr_idle_thr);
2453 free(srv->resolvers_id);
2454 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002455 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002456
Aurelien DARRAGONb5ee8be2023-03-09 14:28:00 +01002457 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2458 xprt_get(XPRT_SSL)->destroy_srv(srv);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002459 HA_SPIN_DESTROY(&srv->lock);
2460
Willy Tarreau2b718102021-04-21 07:32:39 +02002461 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002462 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002463
2464 EXTRA_COUNTERS_FREE(srv->extra_counters);
2465
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002466 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002467
2468 end:
2469 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002470}
2471
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002472/* Remove a server <srv> from a tracking list if <srv> is tracking another
2473 * server. No special care is taken if <srv> is tracked itself by another one :
2474 * this situation should be avoided by the caller.
2475 *
2476 * Not thread-safe.
2477 */
2478static void release_server_track(struct server *srv)
2479{
2480 struct server *strack = srv->track;
2481 struct server **base;
2482
2483 if (!strack)
2484 return;
2485
2486 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2487 if (*base == srv) {
2488 *base = srv->tracknext;
2489 return;
2490 }
2491 }
2492
2493 /* srv not found on the tracking list, this should never happen */
2494 BUG_ON(!*base);
2495}
2496
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002497/*
2498 * Parse as much as possible such a range string argument: low[-high]
2499 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2500 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2501 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2502 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002503 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002504static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2505 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002506{
2507 char *nb_high_arg;
2508
2509 *nb_high = 0;
2510 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002511 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002512
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002513 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002514 *nb_high_arg++ = '\0';
2515 *nb_high = atoi(nb_high_arg);
2516 }
2517 else {
2518 *nb_high += *nb_low;
2519 *nb_low = 1;
2520 }
2521
2522 if (*nb_low < 0 || *nb_high < *nb_low)
2523 return -1;
2524
2525 return 0;
2526}
2527
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002528/* Parse as much as possible such a range string argument: low[-high]
2529 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2530 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2531 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002532 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002533 * initialize a new server on startup.
2534 *
2535 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2536 * Returns 0 if succeeded, -1 if not.
2537 */
2538static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2539 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002540{
2541 chunk_printf(&trash, "%s%d", prefix, nb);
2542 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002543 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002544}
2545
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002546/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002547 * Note that a server template is a special server with
2548 * a few different parameters than a server which has
2549 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002550 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002551 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002552 * initialize a new server on startup.
2553 *
Joseph Herlant44466822018-11-15 08:57:51 -08002554 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002555 * 'srv' template included.
2556 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002557static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002558{
2559 int i;
2560 struct server *newsrv;
2561
2562 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 +02002563 newsrv = new_server(px);
2564 if (!newsrv)
2565 goto err;
2566
Christopher Faulet75bef002020-11-02 22:04:55 +01002567 newsrv->conf.file = strdup(srv->conf.file);
2568 newsrv->conf.line = srv->conf.line;
2569
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002570 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002571 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002572
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002573 if (newsrv->sni_expr) {
2574 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2575 if (!newsrv->ssl_ctx.sni)
2576 goto err;
2577 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002578
Emeric Brun34067662021-06-11 10:48:45 +02002579 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002580 if (newsrv->srvrq)
2581 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002582
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002583 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002584 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002585
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002586 /* Linked backwards first. This will be restablished after parsing. */
2587 newsrv->next = px->srv;
2588 px->srv = newsrv;
2589 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002590 _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 +02002591
2592 return i - srv->tmpl_info.nb_low;
2593
2594 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002595 _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 +02002596 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002597 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002598 free_check(&newsrv->agent);
2599 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002600 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002601 }
2602 free(newsrv);
2603 return i - srv->tmpl_info.nb_low;
2604}
2605
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002606/* Allocate a new server pointed by <srv> and try to parse the first arguments
2607 * in <args> as an address for a server or an address-range for a template or
2608 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2609 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002610 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002611 * initialize a new server on startup.
2612 *
2613 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2614 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2615 * <srv> will be set to NULL.
2616 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002617static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2618 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002619 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002620{
2621 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002622 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002623 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002624 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002625 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002626 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002627
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002628 *srv = NULL;
2629
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002630 /* There is no mandatory first arguments for default server. */
2631 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2632 if (parse_flags & SRV_PARSE_TEMPLATE) {
2633 if (!*args[3]) {
2634 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002635 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2636 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002637 err_code |= ERR_ALERT | ERR_FATAL;
2638 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002639 }
2640
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002641 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002642 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002643 else {
2644 if (!*args[2]) {
2645 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002646 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2647 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002648 err_code |= ERR_ALERT | ERR_FATAL;
2649 goto out;
2650 }
2651
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002652 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002653 }
2654
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002655 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002656 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2657 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002658 err_code |= ERR_ALERT | ERR_FATAL;
2659 goto out;
2660 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002661 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002662
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002663 *cur_arg = 2;
2664 if (parse_flags & SRV_PARSE_TEMPLATE) {
2665 /* Parse server-template <nb | range> arg. */
2666 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002667 ha_alert("Wrong %s number or range arg '%s'.\n",
2668 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002669 err_code |= ERR_ALERT | ERR_FATAL;
2670 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002671 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002672 (*cur_arg)++;
2673 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002674
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002675 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2676 struct sockaddr_storage *sk;
2677 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002678
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002679 *srv = newsrv = new_server(curproxy);
2680 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002681 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002682 err_code |= ERR_ALERT | ERR_ABORT;
2683 goto out;
2684 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002685 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002686
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002687 if (parse_flags & SRV_PARSE_TEMPLATE) {
2688 newsrv->tmpl_info.nb_low = tmpl_range_low;
2689 newsrv->tmpl_info.nb_high = tmpl_range_high;
2690 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002691
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002692 if (parse_flags & SRV_PARSE_DYNAMIC)
2693 newsrv->flags |= SRV_F_DYNAMIC;
2694
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002695 /* Note: for a server template, its id is its prefix.
2696 * This is a temporary id which will be used for server allocations to come
2697 * after parsing.
2698 */
2699 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2700 newsrv->id = strdup(args[1]);
2701 else
2702 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002703
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002704 /* revision defaults to 0 */
2705 newsrv->rid = 0;
2706
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002707 /* several ways to check the port component :
2708 * - IP => port=+0, relative (IPv4 only)
2709 * - IP: => port=+0, relative
2710 * - IP:N => port=N, absolute
2711 * - IP:+N => port=+N, relative
2712 * - IP:-N => port=-N, relative
2713 */
2714 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2715 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002716
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002717 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002718 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002719 (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);
2720 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002721 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002722 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002723 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002724 goto out;
2725 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002726
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002727 if (!port1 || !port2) {
2728 /* no port specified, +offset, -offset */
2729 newsrv->flags |= SRV_F_MAPPORTS;
2730 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002731
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002732 /* save hostname and create associated name resolution */
2733 if (fqdn) {
2734 if (fqdn[0] == '_') { /* SRV record */
2735 /* Check if a SRV request already exists, and if not, create it */
2736 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2737 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2738 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002739 err_code |= ERR_ALERT | ERR_FATAL;
2740 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002741 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002742 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002743 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002744 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002745 ha_alert("Can't create DNS resolution for server '%s'\n",
2746 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002747 err_code |= ERR_ALERT | ERR_FATAL;
2748 goto out;
2749 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002750 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002751
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002752 newsrv->addr = *sk;
2753 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002754 /*
2755 * we don't need to lock the server here, because
2756 * we are in the process of initializing.
2757 *
2758 * Note that the server is not attached into the proxy tree if
2759 * this is a dynamic server.
2760 */
2761 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002762
Willy Tarreau14e7f292021-10-27 17:41:07 +02002763 if (!newsrv->srvrq && !newsrv->hostname &&
2764 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002765 ha_alert("Unknown protocol family %d '%s'\n",
2766 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002767 err_code |= ERR_ALERT | ERR_FATAL;
2768 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002769 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002770
2771 (*cur_arg)++;
2772 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002773 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2774 /* Copy default server settings to new server */
2775 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2776 } else {
2777 /* Initialize dynamic server weight to 1 */
2778 newsrv->uweight = newsrv->iweight = 1;
2779
2780 /* A dynamic server is disabled on startup */
2781 newsrv->next_admin = SRV_ADMF_FMAINT;
2782 newsrv->next_state = SRV_ST_STOPPED;
2783 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002784
2785 /* Set default values for checks */
2786 newsrv->check.inter = DEF_CHKINTR;
2787 newsrv->check.rise = DEF_RISETIME;
2788 newsrv->check.fall = DEF_FALLTIME;
2789
2790 newsrv->agent.inter = DEF_CHKINTR;
2791 newsrv->agent.rise = DEF_AGENT_RISETIME;
2792 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002793 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002794 HA_SPIN_INIT(&newsrv->lock);
2795 }
2796 else {
2797 *srv = newsrv = &curproxy->defsrv;
2798 *cur_arg = 1;
2799 newsrv->resolv_opts.family_prio = AF_INET6;
2800 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002801 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002802
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002803 free(fqdn);
2804 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002805
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002806out:
2807 free(fqdn);
2808 return err_code;
2809}
Willy Tarreau272adea2014-03-31 10:39:59 +02002810
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002811/* Parse the server keyword in <args>.
2812 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2813 * might not be the case if an error is reported.
2814 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002815 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002816 * initialize a new server on startup.
2817 *
2818 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2819 * interrupted.
2820 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002821static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2822 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002823 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002824{
2825 int err_code = 0;
2826 struct srv_kw *kw;
2827 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002828 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002829
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002830 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002831 if (!kw) {
2832 best = srv_find_best_kw(args[*cur_arg]);
2833 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002834 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2835 args[*cur_arg], best,
2836 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2837 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002838 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002839 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2840 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2841 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002842
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002843 return ERR_ALERT | ERR_FATAL;
2844 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002845
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002846 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002847 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2848 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002849 err_code = ERR_ALERT | ERR_FATAL;
2850 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002851 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002852
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002853 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002854 ha_alert("'%s' option is not accepted in default-server sections\n",
2855 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002856 err_code = ERR_ALERT;
2857 goto out;
2858 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002859 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002860 ha_alert("'%s' option is not accepted for dynamic server\n",
2861 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002862 err_code |= ERR_ALERT;
2863 goto out;
2864 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002865
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002866 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2867 if (err_code) {
2868 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2869 free(errmsg);
2870 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002871
2872out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002873 if (kw->skip != -1)
2874 *cur_arg += 1 + kw->skip;
2875
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002876 return err_code;
2877}
2878
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002879/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002880 * initialize a new server on startup.
2881 */
2882static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2883 struct server *srv, struct proxy *proxy,
2884 char **errmsg)
2885{
2886 int ret;
2887
2888 if (!srv->sni_expr)
2889 return 0;
2890
2891 ret = server_parse_sni_expr(srv, proxy, errmsg);
2892 if (!ret)
2893 return 0;
2894
2895 return ret;
2896}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002897
2898/* Server initializations finalization.
2899 * Initialize health check, agent check and SNI expression if enabled.
2900 * Must not be called for a default server instance.
2901 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002902 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002903 * initialize a new server on startup.
2904 */
2905static int _srv_parse_finalize(char **args, int cur_arg,
2906 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002907 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002908{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002909 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002910 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002911
2912 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002913 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002914 return ERR_ALERT | ERR_FATAL;
2915 }
2916
2917 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002918 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2919 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002920 return ERR_ALERT | ERR_FATAL;
2921 }
2922
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002923 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2924 if (errmsg) {
2925 ha_alert("%s\n", errmsg);
2926 free(errmsg);
2927 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002928 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002929 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002930
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002931 /* A dynamic server is disabled on startup. It must not be counted as
2932 * an active backend entry.
2933 */
2934 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2935 if (srv->flags & SRV_F_BACKUP)
2936 px->srv_bck++;
2937 else
2938 px->srv_act++;
2939 }
2940
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002941 srv_lb_commit_status(srv);
2942
2943 return 0;
2944}
2945
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002946int parse_server(const char *file, int linenum, char **args,
2947 struct proxy *curproxy, const struct proxy *defproxy,
2948 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002949{
2950 struct server *newsrv = NULL;
2951 int err_code = 0;
2952
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002953 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002954
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002955 set_usermsgs_ctx(file, linenum, NULL);
2956
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002957 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002958 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002959 err_code |= ERR_ALERT | ERR_FATAL;
2960 goto out;
2961 }
2962 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2963 err_code |= ERR_ALERT | ERR_FATAL;
2964 goto out;
2965 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002966
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002967 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2968 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2969 if (!*args[2])
2970 return 0;
2971 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002972
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002973 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002974 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002975
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002976 /* the servers are linked backwards first */
2977 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2978 newsrv->next = curproxy->srv;
2979 curproxy->srv = newsrv;
2980 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002981
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002982 if (err_code & ERR_CODE)
2983 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002984
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002985 newsrv->conf.file = strdup(file);
2986 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002987
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002988 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002989 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002990 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002991 if (err_code & ERR_FATAL)
2992 goto out;
2993 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002994
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002995 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002996 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002997 if (err_code & ERR_FATAL)
2998 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002999 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01003000
Amaury Denoyelle30c05372021-03-08 16:36:46 +01003001 if (parse_flags & SRV_PARSE_TEMPLATE)
3002 _srv_parse_tmpl_init(newsrv, curproxy);
3003
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02003004 /* If the server id is fixed, insert it in the proxy used_id tree.
3005 * This is needed to detect a later duplicate id via srv_parse_id.
3006 *
3007 * If no is specified, a dynamic one is generated in
3008 * check_config_validity.
3009 */
3010 if (newsrv->flags & SRV_F_FORCED_ID)
3011 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3012
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02003013 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003014 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02003015
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003016 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003017 return 0;
3018
3019 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003020 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003021 return err_code;
3022}
3023
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003024/* Returns a pointer to the first server matching either id <id>.
3025 * NULL is returned if no match is found.
3026 * the lookup is performed in the backend <bk>
3027 */
3028struct server *server_find_by_id(struct proxy *bk, int id)
3029{
3030 struct eb32_node *eb32;
3031 struct server *curserver;
3032
3033 if (!bk || (id ==0))
3034 return NULL;
3035
3036 /* <bk> has no backend capabilities, so it can't have a server */
3037 if (!(bk->cap & PR_CAP_BE))
3038 return NULL;
3039
3040 curserver = NULL;
3041
3042 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3043 if (eb32)
3044 curserver = container_of(eb32, struct server, conf.id);
3045
3046 return curserver;
3047}
3048
3049/* Returns a pointer to the first server matching either name <name>, or id
3050 * if <name> starts with a '#'. NULL is returned if no match is found.
3051 * the lookup is performed in the backend <bk>
3052 */
3053struct server *server_find_by_name(struct proxy *bk, const char *name)
3054{
3055 struct server *curserver;
3056
3057 if (!bk || !name)
3058 return NULL;
3059
3060 /* <bk> has no backend capabilities, so it can't have a server */
3061 if (!(bk->cap & PR_CAP_BE))
3062 return NULL;
3063
3064 curserver = NULL;
3065 if (*name == '#') {
3066 curserver = server_find_by_id(bk, atoi(name + 1));
3067 if (curserver)
3068 return curserver;
3069 }
3070 else {
3071 curserver = bk->srv;
3072
3073 while (curserver && (strcmp(curserver->id, name) != 0))
3074 curserver = curserver->next;
3075
3076 if (curserver)
3077 return curserver;
3078 }
3079
3080 return NULL;
3081}
3082
3083struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3084{
3085 struct server *byname;
3086 struct server *byid;
3087
3088 if (!name && !id)
3089 return NULL;
3090
3091 if (diff)
3092 *diff = 0;
3093
3094 byname = byid = NULL;
3095
3096 if (name) {
3097 byname = server_find_by_name(bk, name);
3098 if (byname && (!id || byname->puid == id))
3099 return byname;
3100 }
3101
3102 /* remaining possibilities :
3103 * - name not set
3104 * - name set but not found
3105 * - name found but ID doesn't match
3106 */
3107 if (id) {
3108 byid = server_find_by_id(bk, id);
3109 if (byid) {
3110 if (byname) {
3111 /* use id only if forced by configuration */
3112 if (byid->flags & SRV_F_FORCED_ID) {
3113 if (diff)
3114 *diff |= 2;
3115 return byid;
3116 }
3117 else {
3118 if (diff)
3119 *diff |= 1;
3120 return byname;
3121 }
3122 }
3123
3124 /* remaining possibilities:
3125 * - name not set
3126 * - name set but not found
3127 */
3128 if (name && diff)
3129 *diff |= 2;
3130 return byid;
3131 }
3132
3133 /* id bot found */
3134 if (byname) {
3135 if (diff)
3136 *diff |= 1;
3137 return byname;
3138 }
3139 }
3140
3141 return NULL;
3142}
3143
Simon Horman7d09b9a2013-02-12 10:45:51 +09003144/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003145 * update a server's current IP address.
3146 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3147 * ip is in network format.
3148 * updater is a string which contains an information about the requester of the update.
3149 * updater is used if not NULL.
3150 *
3151 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003152 *
3153 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003154 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003155int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003156{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003157 /* save the new IP family & address if necessary */
3158 switch (ip_sin_family) {
3159 case AF_INET:
3160 if (s->addr.ss_family == ip_sin_family &&
3161 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3162 return 0;
3163 break;
3164 case AF_INET6:
3165 if (s->addr.ss_family == ip_sin_family &&
3166 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3167 return 0;
3168 break;
3169 };
3170
Baptiste Assmann14e40142015-04-14 01:13:07 +02003171 /* generates a log line and a warning on stderr */
3172 if (1) {
3173 /* book enough space for both IPv4 and IPv6 */
3174 char oldip[INET6_ADDRSTRLEN];
3175 char newip[INET6_ADDRSTRLEN];
3176
3177 memset(oldip, '\0', INET6_ADDRSTRLEN);
3178 memset(newip, '\0', INET6_ADDRSTRLEN);
3179
3180 /* copy old IP address in a string */
3181 switch (s->addr.ss_family) {
3182 case AF_INET:
3183 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3184 break;
3185 case AF_INET6:
3186 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3187 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003188 default:
Willy Tarreaufc458ec2023-04-07 18:11:39 +02003189 strlcpy2(oldip, "(none)", sizeof(oldip));
Christopher Fauletb0b76072020-09-08 10:38:40 +02003190 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003191 };
3192
3193 /* copy new IP address in a string */
3194 switch (ip_sin_family) {
3195 case AF_INET:
3196 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3197 break;
3198 case AF_INET6:
3199 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3200 break;
3201 };
3202
3203 /* save log line into a buffer */
3204 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3205 s->proxy->id, s->id, oldip, newip, updater);
3206
3207 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003208 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003209
3210 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003211 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003212 }
3213
3214 /* save the new IP family */
3215 s->addr.ss_family = ip_sin_family;
3216 /* save the new IP address */
3217 switch (ip_sin_family) {
3218 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003219 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003220 break;
3221 case AF_INET6:
3222 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3223 break;
3224 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003225 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003226 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003227
3228 return 0;
3229}
3230
William Dauchy7cabc062021-02-11 22:51:24 +01003231/* update agent health check address and port
3232 * addr can be ip4/ip6 or a hostname
3233 * if one error occurs, don't apply anything
3234 * must be called with the server lock held.
3235 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003236const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003237{
3238 struct sockaddr_storage sk;
3239 struct buffer *msg;
3240 int new_port;
3241
3242 msg = get_trash_chunk();
3243 chunk_reset(msg);
3244
3245 if (!(s->agent.state & CHK_ST_ENABLED)) {
3246 chunk_strcat(msg, "agent checks are not enabled on this server");
3247 goto out;
3248 }
3249 if (addr) {
3250 memset(&sk, 0, sizeof(struct sockaddr_storage));
3251 if (str2ip(addr, &sk) == NULL) {
3252 chunk_appendf(msg, "invalid addr '%s'", addr);
3253 goto out;
3254 }
3255 }
3256 if (port) {
3257 if (strl2irc(port, strlen(port), &new_port) != 0) {
3258 chunk_appendf(msg, "provided port is not an integer");
3259 goto out;
3260 }
3261 if (new_port < 0 || new_port > 65535) {
3262 chunk_appendf(msg, "provided port is invalid");
3263 goto out;
3264 }
3265 }
3266out:
3267 if (msg->data)
3268 return msg->area;
3269 else {
3270 if (addr)
3271 set_srv_agent_addr(s, &sk);
3272 if (port)
3273 set_srv_agent_port(s, new_port);
3274 }
3275 return NULL;
3276}
3277
William Dauchyb456e1f2021-02-11 22:51:23 +01003278/* update server health check address and port
3279 * addr must be ip4 or ip6, it won't be resolved
3280 * if one error occurs, don't apply anything
3281 * must be called with the server lock held.
3282 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003283const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003284{
3285 struct sockaddr_storage sk;
3286 struct buffer *msg;
3287 int new_port;
3288
3289 msg = get_trash_chunk();
3290 chunk_reset(msg);
3291
3292 if (!(s->check.state & CHK_ST_ENABLED)) {
3293 chunk_strcat(msg, "health checks are not enabled on this server");
3294 goto out;
3295 }
3296 if (addr) {
3297 memset(&sk, 0, sizeof(struct sockaddr_storage));
3298 if (str2ip2(addr, &sk, 0) == NULL) {
3299 chunk_appendf(msg, "invalid addr '%s'", addr);
3300 goto out;
3301 }
3302 }
3303 if (port) {
3304 if (strl2irc(port, strlen(port), &new_port) != 0) {
3305 chunk_appendf(msg, "provided port is not an integer");
3306 goto out;
3307 }
3308 if (new_port < 0 || new_port > 65535) {
3309 chunk_appendf(msg, "provided port is invalid");
3310 goto out;
3311 }
3312 /* prevent the update of port to 0 if MAPPORTS are in use */
3313 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3314 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3315 goto out;
3316 }
3317 }
3318out:
3319 if (msg->data)
3320 return msg->area;
3321 else {
3322 if (addr)
3323 s->check.addr = sk;
3324 if (port)
3325 s->check.port = new_port;
3326 }
3327 return NULL;
3328}
3329
Baptiste Assmann14e40142015-04-14 01:13:07 +02003330/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003331 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3332 *
3333 * Caller can pass its name through <updater> to get it integrated in the response message
3334 * returned by the function.
3335 *
3336 * The function first does the following, in that order:
3337 * - validates the new addr and/or port
3338 * - checks if an update is required (new IP or port is different than current ones)
3339 * - checks the update is allowed:
3340 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3341 * - allow all changes if no CHECKS are configured
3342 * - if CHECK is configured:
3343 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3344 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3345 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003346 *
3347 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003348 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003349const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003350{
3351 struct sockaddr_storage sa;
3352 int ret, port_change_required;
3353 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003354 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003355 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003356 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003357
3358 msg = get_trash_chunk();
3359 chunk_reset(msg);
3360
3361 if (addr) {
3362 memset(&sa, 0, sizeof(struct sockaddr_storage));
3363 if (str2ip2(addr, &sa, 0) == NULL) {
3364 chunk_printf(msg, "Invalid addr '%s'", addr);
3365 goto out;
3366 }
3367
3368 /* changes are allowed on AF_INET* families only */
3369 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3370 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3371 goto out;
3372 }
3373
3374 /* collecting data currently setup */
3375 memset(current_addr, '\0', sizeof(current_addr));
3376 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3377 /* changes are allowed on AF_INET* families only */
3378 if ((ret != AF_INET) && (ret != AF_INET6)) {
3379 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3380 goto out;
3381 }
3382
3383 /* applying ADDR changes if required and allowed
3384 * ipcmp returns 0 when both ADDR are the same
3385 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003386 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003387 chunk_appendf(msg, "no need to change the addr");
3388 goto port;
3389 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003390 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003391 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003392
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003393 /* update report for caller */
3394 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3395 }
3396
3397 port:
3398 if (port) {
3399 char sign = '\0';
3400 char *endptr;
3401
3402 if (addr)
3403 chunk_appendf(msg, ", ");
3404
3405 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003406 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003407
3408 /* check if PORT change is required */
3409 port_change_required = 0;
3410
3411 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003412 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003413 new_port = strtol(port, &endptr, 10);
3414 if ((errno != 0) || (port == endptr)) {
3415 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3416 goto out;
3417 }
3418
3419 /* check if caller triggers a port mapped or offset */
3420 if (sign == '-' || (sign == '+')) {
3421 /* check if server currently uses port map */
3422 if (!(s->flags & SRV_F_MAPPORTS)) {
3423 /* switch from fixed port to port map mandatorily triggers
3424 * a port change */
3425 port_change_required = 1;
3426 /* check is configured
3427 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3428 * prevent PORT change if check doesn't have it's dedicated port while switching
3429 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003430 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003431 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.");
3432 goto out;
3433 }
3434 }
3435 /* we're already using port maps */
3436 else {
3437 port_change_required = current_port != new_port;
3438 }
3439 }
3440 /* fixed port */
3441 else {
3442 port_change_required = current_port != new_port;
3443 }
3444
3445 /* applying PORT changes if required and update response message */
3446 if (port_change_required) {
3447 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003448 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003449 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003450
3451 /* prepare message */
3452 chunk_appendf(msg, "port changed from '");
3453 if (s->flags & SRV_F_MAPPORTS)
3454 chunk_appendf(msg, "+");
3455 chunk_appendf(msg, "%d' to '", current_port);
3456
3457 if (sign == '-') {
3458 s->flags |= SRV_F_MAPPORTS;
3459 chunk_appendf(msg, "%c", sign);
3460 /* just use for result output */
3461 new_port = -new_port;
3462 }
3463 else if (sign == '+') {
3464 s->flags |= SRV_F_MAPPORTS;
3465 chunk_appendf(msg, "%c", sign);
3466 }
3467 else {
3468 s->flags &= ~SRV_F_MAPPORTS;
3469 }
3470
3471 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003472 }
3473 else {
3474 chunk_appendf(msg, "no need to change the port");
3475 }
3476 }
3477
3478out:
William Dauchy6318d332020-05-02 21:52:36 +02003479 if (changed) {
3480 /* force connection cleanup on the given server */
3481 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003482 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003483 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003484 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003485 if (updater)
3486 chunk_appendf(msg, " by '%s'", updater);
3487 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003488 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003489}
3490
Christopher Faulet5efdef22021-03-11 18:03:21 +01003491/*
3492 * update server status based on result of SRV resolution
3493 * returns:
3494 * 0 if server status is updated
3495 * 1 if server status has not changed
3496 *
3497 * Must be called with the server lock held.
3498 */
3499int srvrq_update_srv_status(struct server *s, int has_no_ip)
3500{
3501 if (!s->srvrq)
3502 return 1;
3503
3504 /* since this server has an IP, it can go back in production */
3505 if (has_no_ip == 0) {
3506 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3507 return 1;
3508 }
3509
3510 if (s->next_admin & SRV_ADMF_RMAINT)
3511 return 1;
3512
3513 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3514 return 0;
3515}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003516
3517/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003518 * update server status based on result of name resolution
3519 * returns:
3520 * 0 if server status is updated
3521 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003522 *
3523 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003524 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003525int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003526{
Emeric Brun750fe792020-12-23 16:51:12 +01003527 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003528 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003529 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003530
Jerome Magnin012261a2020-07-28 13:38:22 +02003531 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003532 if (resolution == NULL)
3533 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003534
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003535 switch (resolution->status) {
3536 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003537 /* status when HAProxy has just (re)started.
3538 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003539 break;
3540
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003541 case RSLV_STATUS_VALID:
3542 /*
3543 * resume health checks
3544 * server will be turned back on if health check is safe
3545 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003546 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003547 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003548 return 1;
3549 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3550 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003551 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003552 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003553
Emeric Brun52a91d32017-08-31 14:41:55 +02003554 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003555 return 1;
3556 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3557 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3558 s->proxy->id, s->id);
3559
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003560 ha_warning("%s.\n", trash.area);
3561 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003562 return 0;
3563
3564 case RSLV_STATUS_NX:
3565 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003566 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3567 if (!tick_is_expired(exp, now_ms))
3568 break;
3569
3570 if (s->next_admin & SRV_ADMF_RMAINT)
3571 return 1;
3572 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3573 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003574
3575 case RSLV_STATUS_TIMEOUT:
3576 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003577 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3578 if (!tick_is_expired(exp, now_ms))
3579 break;
3580
3581 if (s->next_admin & SRV_ADMF_RMAINT)
3582 return 1;
3583 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3584 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003585
3586 case RSLV_STATUS_REFUSED:
3587 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003588 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3589 if (!tick_is_expired(exp, now_ms))
3590 break;
3591
3592 if (s->next_admin & SRV_ADMF_RMAINT)
3593 return 1;
3594 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3595 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003596
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003597 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003598 /* stop server if resolution failed for a long enough period */
3599 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3600 if (!tick_is_expired(exp, now_ms))
3601 break;
3602
3603 if (s->next_admin & SRV_ADMF_RMAINT)
3604 return 1;
3605 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3606 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003607 }
3608
3609 return 1;
3610}
3611
3612/*
3613 * Server Name Resolution valid response callback
3614 * It expects:
3615 * - <nameserver>: the name server which answered the valid response
3616 * - <response>: buffer containing a valid DNS response
3617 * - <response_len>: size of <response>
3618 * It performs the following actions:
3619 * - ignore response if current ip found and server family not met
3620 * - update with first new ip found if family is met and current IP is not found
3621 * returns:
3622 * 0 on error
3623 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003624 *
3625 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003626 */
Emeric Brun08622d32020-12-23 17:41:43 +01003627int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003628{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003629 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003630 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003631 void *serverip, *firstip;
3632 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003633 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003634 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003635 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003636
Christopher Faulet67957bd2017-09-27 11:00:59 +02003637 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003638 if (!s)
3639 return 1;
3640
Christopher Faulet49531e82021-03-10 15:07:27 +01003641 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003642 /* If DNS resolution is disabled ignore it.
3643 * This is the case if the server was associated to
3644 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003645 */
Emeric Brun34067662021-06-11 10:48:45 +02003646 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003647 return 1;
3648 }
3649
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003650 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003651 if (!resolution)
3652 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003653
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003654 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003655 firstip = NULL; /* pointer to the first valid response found */
3656 /* it will be used as the new IP if a change is required */
3657 firstip_sin_family = AF_UNSPEC;
3658 serverip = NULL; /* current server IP address */
3659
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003660 /* initializing server IP pointer */
3661 server_sin_family = s->addr.ss_family;
3662 switch (server_sin_family) {
3663 case AF_INET:
3664 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3665 break;
3666
3667 case AF_INET6:
3668 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3669 break;
3670
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003671 case AF_UNSPEC:
3672 break;
3673
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003674 default:
3675 goto invalid;
3676 }
3677
Emeric Brund30e9a12020-12-23 18:49:16 +01003678 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3679 serverip, server_sin_family, &firstip,
3680 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681
3682 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003683 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003684 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003685
Emeric Brun456de772020-12-23 18:17:31 +01003686 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687 goto save_ip;
3688
Emeric Brun456de772020-12-23 18:17:31 +01003689 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003690 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003691 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003692
Emeric Brun456de772020-12-23 18:17:31 +01003693 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003694 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003695 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003696 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003697 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003698
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003699 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003700 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003701 goto invalid;
3702
3703 }
3704
3705 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003706 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003707 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003708 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003709 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003710 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003711 else
3712 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003713 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003714
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003715 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003716 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3717 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003718 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003719
3720 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003721 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003722 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003723 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003724 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003725 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3726 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003727 return 0;
3728}
3729
3730/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003731 * SRV record error management callback
3732 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003733 * 0 if we can trash answser items.
3734 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003735 *
3736 * Grabs the server's lock.
3737 */
3738int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3739{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003740 struct resolv_srvrq *srvrq;
3741 struct resolv_resolution *res;
3742 struct resolvers *resolvers;
3743 int exp;
3744
3745 /* SRV records */
3746 srvrq = objt_resolv_srvrq(requester->owner);
3747 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003748 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003749
3750 resolvers = srvrq->resolvers;
3751 res = requester->resolution;
3752
3753 switch (res->status) {
3754
3755 case RSLV_STATUS_NX:
3756 /* stop server if resolution is NX for a long enough period */
3757 exp = tick_add(res->last_valid, resolvers->hold.nx);
3758 if (!tick_is_expired(exp, now_ms))
3759 return 1;
3760 break;
3761
3762 case RSLV_STATUS_TIMEOUT:
3763 /* stop server if resolution is TIMEOUT for a long enough period */
3764 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3765 if (!tick_is_expired(exp, now_ms))
3766 return 1;
3767 break;
3768
3769 case RSLV_STATUS_REFUSED:
3770 /* stop server if resolution is REFUSED for a long enough period */
3771 exp = tick_add(res->last_valid, resolvers->hold.refused);
3772 if (!tick_is_expired(exp, now_ms))
3773 return 1;
3774 break;
3775
3776 default:
3777 /* stop server if resolution failed for a long enough period */
3778 exp = tick_add(res->last_valid, resolvers->hold.other);
3779 if (!tick_is_expired(exp, now_ms))
3780 return 1;
3781 }
3782
Emeric Brun34067662021-06-11 10:48:45 +02003783 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003784 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003785
Emeric Brun12ca6582021-06-10 15:25:25 +02003786 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003787}
3788
3789/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003790 * Server Name Resolution error management callback
3791 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003792 * 0 if we can trash answser items.
3793 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003794 *
3795 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003796 */
Emeric Brun08622d32020-12-23 17:41:43 +01003797int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003798{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003799 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003800
Christopher Faulet67957bd2017-09-27 11:00:59 +02003801 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003802 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003803 return 0;
3804
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003805 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003806 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003807 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003808 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003809 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003810 return 0;
3811 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003812 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003813
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003814 return 1;
3815}
3816
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003817/*
3818 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003819 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003820 * It returns a pointer to the first server found or NULL if <ip> is not yet
3821 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003822 *
3823 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003824 */
3825struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3826{
3827 struct server *tmpsrv;
3828 struct proxy *be;
3829
3830 if (!srv)
3831 return NULL;
3832
3833 be = srv->proxy;
3834 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003835 /* we found the current server is the same, ignore it */
3836 if (srv == tmpsrv)
3837 continue;
3838
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003839 /* We want to compare the IP in the record with the IP of the servers in the
3840 * same backend, only if:
3841 * * DNS resolution is enabled on the server
3842 * * the hostname used for the resolution by our server is the same than the
3843 * one used for the server found in the backend
3844 * * the server found in the backend is not our current server
3845 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003846 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003847 if ((tmpsrv->hostname_dn == NULL) ||
3848 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003849 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003850 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003851 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003852 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003853 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003854
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003855 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003856 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003857 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003858 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003859 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003860
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003861 /* At this point, we have 2 different servers using the same DNS hostname
3862 * for their respective resolution.
3863 */
3864 if (*ip_family == tmpsrv->addr.ss_family &&
3865 ((tmpsrv->addr.ss_family == AF_INET &&
3866 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3867 (tmpsrv->addr.ss_family == AF_INET6 &&
3868 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003869 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003870 return tmpsrv;
3871 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003872 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003873 }
3874
Emeric Brune9fd6b52017-11-02 17:20:39 +01003875
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003876 return NULL;
3877}
3878
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003879/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3880 * resolver. This is suited for initial address configuration. Returns 0 on
3881 * success otherwise a non-zero error code. In case of error, *err_code, if
3882 * not NULL, is filled up.
3883 */
3884int srv_set_addr_via_libc(struct server *srv, int *err_code)
3885{
3886 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003887 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003888 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003889 return 1;
3890 }
3891 return 0;
3892}
3893
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003894/* Set the server's FDQN (->hostname) from <hostname>.
3895 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003896 *
3897 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003898 */
Emeric Brun08622d32020-12-23 17:41:43 +01003899int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003900{
Emeric Brun08622d32020-12-23 17:41:43 +01003901 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003902 char *hostname_dn;
3903 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003904
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003905 /* Note that the server lock is already held. */
3906 if (!srv->resolvers)
3907 return -1;
3908
Emeric Brun08622d32020-12-23 17:41:43 +01003909 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003910 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003911 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003912 * and we can't enable it at run time for now.
3913 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003914 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003915 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003916
3917 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003918 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003919 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003920 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003921 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003922 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003923 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003924
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003925 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003926 if (resolution &&
3927 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003928 resolution->hostname_dn_len == hostname_dn_len &&
3929 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003930 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003931
Willy Tarreau6878f802021-10-20 14:07:31 +02003932 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003933
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003934 free(srv->hostname);
3935 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003936 srv->hostname = strdup(hostname);
3937 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003938 srv->hostname_dn_len = hostname_dn_len;
3939 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003940 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003941
Baptiste Assmann13a92322019-06-07 09:40:55 +02003942 if (srv->flags & SRV_F_NO_RESOLUTION)
3943 goto end;
3944
Emeric Brund30e9a12020-12-23 18:49:16 +01003945 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003946 goto err;
3947
3948 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003949 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003950 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003951 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003952
3953 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003954 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003955 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003956 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003957}
3958
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003959/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3960 * from the state file. This is suited for initial address configuration.
3961 * Returns 0 on success otherwise a non-zero error code. In case of error,
3962 * *err_code, if not NULL, is filled up.
3963 */
3964static int srv_apply_lastaddr(struct server *srv, int *err_code)
3965{
3966 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3967 if (err_code)
3968 *err_code |= ERR_WARN;
3969 return 1;
3970 }
3971 return 0;
3972}
3973
Willy Tarreau25e51522016-11-04 15:10:17 +01003974/* returns 0 if no error, otherwise a combination of ERR_* flags */
3975static int srv_iterate_initaddr(struct server *srv)
3976{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003977 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003978 int return_code = 0;
3979 int err_code;
3980 unsigned int methods;
3981
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003982 /* If no addr and no hostname set, get the name from the DNS SRV request */
3983 if (!name && srv->srvrq)
3984 name = srv->srvrq->name;
3985
Willy Tarreau25e51522016-11-04 15:10:17 +01003986 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003987 if (!methods) {
3988 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003989 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3990 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003991 if (srv->resolvers_id) {
3992 /* dns resolution is configured, add "none" to not fail on startup */
3993 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3994 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003995 }
3996
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003997 /* "-dr" : always append "none" so that server addresses resolution
3998 * failures are silently ignored, this is convenient to validate some
3999 * configs out of their environment.
4000 */
4001 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4002 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4003
Willy Tarreau25e51522016-11-04 15:10:17 +01004004 while (methods) {
4005 err_code = 0;
4006 switch (srv_get_next_initaddr(&methods)) {
4007 case SRV_IADDR_LAST:
4008 if (!srv->lastaddr)
4009 continue;
4010 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004011 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004012 return_code |= err_code;
4013 break;
4014
4015 case SRV_IADDR_LIBC:
4016 if (!srv->hostname)
4017 continue;
4018 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004019 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004020 return_code |= err_code;
4021 break;
4022
Willy Tarreau37ebe122016-11-04 15:17:58 +01004023 case SRV_IADDR_NONE:
4024 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004025 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004026 ha_warning("could not resolve address '%s', disabling server.\n",
4027 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004028 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004029 return return_code;
4030
Willy Tarreau4310d362016-11-02 15:05:56 +01004031 case SRV_IADDR_IP:
4032 ipcpy(&srv->init_addr, &srv->addr);
4033 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004034 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4035 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004036 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004037 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004038
Willy Tarreau25e51522016-11-04 15:10:17 +01004039 default: /* unhandled method */
4040 break;
4041 }
4042 }
4043
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004044 if (!return_code)
4045 ha_alert("no method found to resolve address '%s'.\n", name);
4046 else
4047 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004048
4049 return_code |= ERR_ALERT | ERR_FATAL;
4050 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004051out:
4052 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004053 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004054 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004055}
4056
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004057/*
4058 * This function parses all backends and all servers within each backend
4059 * and performs servers' addr resolution based on information provided by:
4060 * - configuration file
4061 * - server-state file (states provided by an 'old' haproxy process)
4062 *
4063 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4064 */
4065int srv_init_addr(void)
4066{
4067 struct proxy *curproxy;
4068 int return_code = 0;
4069
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004070 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004071 while (curproxy) {
4072 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004073
4074 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004075 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004076 goto srv_init_addr_next;
4077
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004078 for (srv = curproxy->srv; srv; srv = srv->next) {
4079 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004080 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004081 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004082 reset_usermsgs_ctx();
4083 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004084
4085 srv_init_addr_next:
4086 curproxy = curproxy->next;
4087 }
4088
4089 return return_code;
4090}
4091
Willy Tarreau46b7f532018-08-21 11:54:26 +02004092/*
4093 * Must be called with the server lock held.
4094 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004095const 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 +02004096{
4097
Willy Tarreau83061a82018-07-13 11:56:34 +02004098 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004099
4100 msg = get_trash_chunk();
4101 chunk_reset(msg);
4102
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004103 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004104 chunk_appendf(msg, "no need to change the FDQN");
4105 goto out;
4106 }
4107
4108 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4109 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4110 goto out;
4111 }
4112
4113 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4114 server->proxy->id, server->id, server->hostname, fqdn);
4115
Emeric Brun08622d32020-12-23 17:41:43 +01004116 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004117 chunk_reset(msg);
4118 chunk_appendf(msg, "could not update %s/%s FQDN",
4119 server->proxy->id, server->id);
4120 goto out;
4121 }
4122
4123 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004124 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004125
4126 out:
4127 if (updater)
4128 chunk_appendf(msg, " by '%s'", updater);
4129 chunk_appendf(msg, "\n");
4130
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004131 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004132}
4133
4134
Willy Tarreau21b069d2016-11-23 17:15:08 +01004135/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4136 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004137 * 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 +01004138 * used for CLI commands requiring a server name.
4139 * Important: the <arg> is modified to remove the '/'.
4140 */
4141struct server *cli_find_server(struct appctx *appctx, char *arg)
4142{
4143 struct proxy *px;
4144 struct server *sv;
4145 char *line;
4146
4147 /* split "backend/server" and make <line> point to server */
4148 for (line = arg; *line; line++)
4149 if (*line == '/') {
4150 *line++ = '\0';
4151 break;
4152 }
4153
4154 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004155 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004156 return NULL;
4157 }
4158
4159 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004160 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004161 return NULL;
4162 }
4163
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004164 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004165 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004166 return NULL;
4167 }
4168
4169 return sv;
4170}
4171
William Lallemand222baf22016-11-19 02:00:33 +01004172
Willy Tarreau46b7f532018-08-21 11:54:26 +02004173/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004174static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004175{
4176 struct server *sv;
4177 const char *warning;
4178
4179 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4180 return 1;
4181
4182 sv = cli_find_server(appctx, args[2]);
4183 if (!sv)
4184 return 1;
4185
4186 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004187 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004188 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004189 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004190 if (warning)
4191 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004192 }
4193 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004194 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004195 if (strcmp(args[4], "ready") == 0)
4196 srv_adm_set_ready(sv);
4197 else if (strcmp(args[4], "drain") == 0)
4198 srv_adm_set_drain(sv);
4199 else if (strcmp(args[4], "maint") == 0)
4200 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004201 else
4202 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004203 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004204 }
4205 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004206 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004207 if (sv->track)
4208 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004209 else if (strcmp(args[4], "up") == 0) {
4210 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004211 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004212 }
4213 else if (strcmp(args[4], "stopping") == 0) {
4214 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004215 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004216 }
4217 else if (strcmp(args[4], "down") == 0) {
4218 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004219 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004220 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004221 else
4222 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004223 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004224 }
4225 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004226 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004227 if (!(sv->agent.state & CHK_ST_ENABLED))
4228 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004229 else if (strcmp(args[4], "up") == 0) {
4230 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004231 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004232 }
4233 else if (strcmp(args[4], "down") == 0) {
4234 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004235 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004236 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004237 else
4238 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004239 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004240 }
Misiek2da082d2017-01-09 09:40:42 +01004241 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004242 char *addr = NULL;
4243 char *port = NULL;
4244 if (strlen(args[4]) == 0) {
4245 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4246 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004247 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004248 }
4249 addr = args[4];
4250 if (strcmp(args[5], "port") == 0)
4251 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004252 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004253 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004254 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004255 if (warning)
4256 cli_msg(appctx, LOG_WARNING, warning);
4257 }
4258 else if (strcmp(args[3], "agent-port") == 0) {
4259 char *port = NULL;
4260 if (strlen(args[4]) == 0) {
4261 cli_err(appctx, "set server <b>/<s> agent-port requires"
4262 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004263 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004264 }
4265 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004266 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004267 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004268 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004269 if (warning)
4270 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004271 }
4272 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004273 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004274 if (!(sv->agent.state & CHK_ST_ENABLED))
4275 cli_err(appctx, "agent checks are not enabled on this server.\n");
4276 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004277 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004278 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004279 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004280 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004281 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004282 else if (strcmp(args[3], "check-addr") == 0) {
4283 char *addr = NULL;
4284 char *port = NULL;
4285 if (strlen(args[4]) == 0) {
4286 cli_err(appctx, "set server <b>/<s> check-addr requires"
4287 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004288 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004289 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004290 addr = args[4];
4291 if (strcmp(args[5], "port") == 0)
4292 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004293 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004294 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004295 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004296 if (warning)
4297 cli_msg(appctx, LOG_WARNING, warning);
4298 }
4299 else if (strcmp(args[3], "check-port") == 0) {
4300 char *port = NULL;
4301 if (strlen(args[4]) == 0) {
4302 cli_err(appctx, "set server <b>/<s> check-port requires"
4303 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004304 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004305 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004306 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004307 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004308 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004309 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004310 if (warning)
4311 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004312 }
4313 else if (strcmp(args[3], "addr") == 0) {
4314 char *addr = NULL;
4315 char *port = NULL;
4316 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004317 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004318 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004319 }
4320 else {
4321 addr = args[4];
4322 }
4323 if (strcmp(args[5], "port") == 0) {
4324 port = args[6];
4325 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004326 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004327 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004328 if (warning)
4329 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004330 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004331 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004332 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004333 else if (strcmp(args[3], "fqdn") == 0) {
4334 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004335 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004336 goto out;
4337 }
4338 if (!sv->resolvers) {
4339 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4340 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004341 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004342 if (sv->srvrq) {
4343 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004344 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004345 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004346 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4347 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004348 /* ensure runtime resolver will process this new fqdn */
4349 if (sv->flags & SRV_F_NO_RESOLUTION) {
4350 sv->flags &= ~SRV_F_NO_RESOLUTION;
4351 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004352 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004353 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004354 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004355 if (warning)
4356 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004357 }
William Dauchyf6370442020-11-14 19:25:33 +01004358 else if (strcmp(args[3], "ssl") == 0) {
4359#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004360 if (sv->flags & SRV_F_DYNAMIC) {
4361 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4362 goto out;
4363 }
4364
William Dauchyf6370442020-11-14 19:25:33 +01004365 if (sv->ssl_ctx.ctx == NULL) {
4366 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4367 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004368 goto out;
4369 }
4370
4371 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4372 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004373 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004374 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004375 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004376 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004377 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004378 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004379 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004380 }
4381 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004382 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004383 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4384#else
4385 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4386#endif
4387 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004388 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004389 "usage: set server <backend>/<server> "
4390 "addr | agent | agent-addr | agent-port | agent-send | "
4391 "check-addr | check-port | fqdn | health | ssl | "
4392 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004393 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004394 out:
William Lallemand222baf22016-11-19 02:00:33 +01004395 return 1;
4396}
4397
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004398static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004399{
William Lallemand6b160942016-11-22 12:34:35 +01004400 struct proxy *px;
4401 struct server *sv;
4402 char *line;
4403
4404
4405 /* split "backend/server" and make <line> point to server */
4406 for (line = args[2]; *line; line++)
4407 if (*line == '/') {
4408 *line++ = '\0';
4409 break;
4410 }
4411
Willy Tarreau9d008692019-08-09 11:21:01 +02004412 if (!*line)
4413 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004414
Willy Tarreau9d008692019-08-09 11:21:01 +02004415 if (!get_backend_server(args[2], line, &px, &sv))
4416 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004417
4418 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004419 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4420 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004421 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004422 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004423 return 1;
4424}
4425
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004426/* Parse a "set weight" command.
4427 *
4428 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004429 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004430static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004431{
4432 struct server *sv;
4433 const char *warning;
4434
4435 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4436 return 1;
4437
4438 sv = cli_find_server(appctx, args[2]);
4439 if (!sv)
4440 return 1;
4441
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004442 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4443
William Lallemand6b160942016-11-22 12:34:35 +01004444 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004445 if (warning)
4446 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004447
4448 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4449
William Lallemand6b160942016-11-22 12:34:35 +01004450 return 1;
4451}
4452
Willy Tarreau46b7f532018-08-21 11:54:26 +02004453/* parse a "set maxconn server" command. It always returns 1.
4454 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004455 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004456 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004457static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004458{
4459 struct server *sv;
4460 const char *warning;
4461
4462 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4463 return 1;
4464
4465 sv = cli_find_server(appctx, args[3]);
4466 if (!sv)
4467 return 1;
4468
Amaury Denoyelle02742862021-06-18 11:11:36 +02004469 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4470
Willy Tarreaub8026272016-11-23 11:26:56 +01004471 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004472 if (warning)
4473 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004474
Amaury Denoyelle02742862021-06-18 11:11:36 +02004475 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4476
Willy Tarreaub8026272016-11-23 11:26:56 +01004477 return 1;
4478}
William Lallemand6b160942016-11-22 12:34:35 +01004479
Willy Tarreau46b7f532018-08-21 11:54:26 +02004480/* parse a "disable agent" command. It always returns 1.
4481 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004482 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004483 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004484static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004485{
4486 struct server *sv;
4487
4488 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4489 return 1;
4490
4491 sv = cli_find_server(appctx, args[2]);
4492 if (!sv)
4493 return 1;
4494
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004495 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004496 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004497 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004498 return 1;
4499}
4500
Willy Tarreau46b7f532018-08-21 11:54:26 +02004501/* parse a "disable health" command. It always returns 1.
4502 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004503 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004504 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004505static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004506{
4507 struct server *sv;
4508
4509 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4510 return 1;
4511
4512 sv = cli_find_server(appctx, args[2]);
4513 if (!sv)
4514 return 1;
4515
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004516 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004517 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004518 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004519 return 1;
4520}
4521
Willy Tarreau46b7f532018-08-21 11:54:26 +02004522/* parse a "disable server" command. It always returns 1.
4523 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004524 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004525 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004526static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004527{
4528 struct server *sv;
4529
4530 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4531 return 1;
4532
4533 sv = cli_find_server(appctx, args[2]);
4534 if (!sv)
4535 return 1;
4536
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004537 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004538 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004539 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004540 return 1;
4541}
4542
Willy Tarreau46b7f532018-08-21 11:54:26 +02004543/* parse a "enable agent" command. It always returns 1.
4544 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004545 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004546 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004547static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004548{
4549 struct server *sv;
4550
4551 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4552 return 1;
4553
4554 sv = cli_find_server(appctx, args[2]);
4555 if (!sv)
4556 return 1;
4557
Willy Tarreau9d008692019-08-09 11:21:01 +02004558 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4559 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004560
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004561 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004562 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004563 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004564 return 1;
4565}
4566
Willy Tarreau46b7f532018-08-21 11:54:26 +02004567/* parse a "enable health" command. It always returns 1.
4568 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004569 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004570 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004571static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004572{
4573 struct server *sv;
4574
4575 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4576 return 1;
4577
4578 sv = cli_find_server(appctx, args[2]);
4579 if (!sv)
4580 return 1;
4581
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004582 if (!(sv->check.state & CHK_ST_CONFIGURED))
4583 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4584
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004585 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004586 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004587 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004588 return 1;
4589}
4590
Willy Tarreau46b7f532018-08-21 11:54:26 +02004591/* parse a "enable server" command. It always returns 1.
4592 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004593 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004594 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004595static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004596{
4597 struct server *sv;
4598
4599 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4600 return 1;
4601
4602 sv = cli_find_server(appctx, args[2]);
4603 if (!sv)
4604 return 1;
4605
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004606 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004607 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004608 if (!(sv->flags & SRV_F_COOKIESET)
4609 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4610 sv->cookie)
4611 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004612 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004613 return 1;
4614}
4615
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004616/* Allocates data structure related to load balancing for the server <sv>. It
4617 * is only required for dynamic servers.
4618 *
4619 * At the moment, the server lock is not used as this function is only called
4620 * for a dynamic server not yet registered.
4621 *
4622 * Returns 1 on success, 0 on allocation failure.
4623 */
4624static int srv_alloc_lb(struct server *sv, struct proxy *be)
4625{
4626 int node;
4627
4628 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4629 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4630 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4631 sv->lb_nodes_now = 0;
4632
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004633 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4634 ((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 +01004635 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4636
4637 if (!sv->lb_nodes)
4638 return 0;
4639
4640 for (node = 0; node < sv->lb_nodes_tot; node++) {
4641 sv->lb_nodes[node].server = sv;
4642 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4643 }
4644 }
4645
4646 return 1;
4647}
4648
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004649/* updates the server's weight during a warmup stage. Once the final weight is
4650 * reached, the task automatically stops. Note that any server status change
4651 * must have updated s->last_change accordingly.
4652 */
4653static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4654{
4655 struct server *s = context;
4656
4657 /* by default, plan on stopping the task */
4658 t->expire = TICK_ETERNITY;
4659 if ((s->next_admin & SRV_ADMF_MAINT) ||
4660 (s->next_state != SRV_ST_STARTING))
4661 return t;
4662
4663 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4664
4665 /* recalculate the weights and update the state */
4666 server_recalc_eweight(s, 1);
4667
4668 /* probably that we can refill this server with a bit more connections */
4669 pendconn_grab_from_px(s);
4670
4671 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4672
4673 /* get back there in 1 second or 1/20th of the slowstart interval,
4674 * whichever is greater, resulting in small 5% steps.
4675 */
4676 if (s->next_state == SRV_ST_STARTING)
4677 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4678 return t;
4679}
4680
4681/* Allocate the slowstart task if the server is configured with a slowstart
4682 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4683 *
4684 * Returns 0 on success else non-zero.
4685 */
4686static int init_srv_slowstart(struct server *srv)
4687{
4688 struct task *t;
4689
4690 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004691 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004692 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4693 return ERR_ALERT | ERR_FATAL;
4694 }
4695
4696 /* We need a warmup task that will be called when the server
4697 * state switches from down to up.
4698 */
4699 srv->warmup = t;
4700 t->process = server_warmup;
4701 t->context = srv;
4702
4703 /* server can be in this state only because of */
4704 if (srv->next_state == SRV_ST_STARTING) {
4705 task_schedule(srv->warmup,
4706 tick_add(now_ms,
4707 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4708 }
4709 }
4710
4711 return ERR_NONE;
4712}
4713REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4714
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004715/* Memory allocation and initialization of the per_thr field.
4716 * Returns 0 if the field has been successfully initialized, -1 on failure.
4717 */
4718int srv_init_per_thr(struct server *srv)
4719{
4720 int i;
4721
4722 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004723 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4724 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004725 return -1;
4726
4727 for (i = 0; i < global.nbthread; i++) {
4728 srv->per_thr[i].idle_conns = EB_ROOT;
4729 srv->per_thr[i].safe_conns = EB_ROOT;
4730 srv->per_thr[i].avail_conns = EB_ROOT;
4731 MT_LIST_INIT(&srv->per_thr[i].streams);
4732 }
4733
4734 return 0;
4735}
4736
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004737/* Parse a "add server" command
4738 * Returns 0 if the server has been successfully initialized, 1 on failure.
4739 */
4740static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4741{
4742 struct proxy *be;
4743 struct server *srv;
4744 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004745 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004746 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004747 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4748
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004749 usermsgs_clr("CLI");
4750
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004751 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4752 return 1;
4753
4754 ++args;
4755
4756 sv_name = be_name = args[1];
4757 /* split backend/server arg */
4758 while (*sv_name && *(++sv_name)) {
4759 if (*sv_name == '/') {
4760 *sv_name = '\0';
4761 ++sv_name;
4762 break;
4763 }
4764 }
4765
4766 if (!*sv_name)
4767 return cli_err(appctx, "Require 'backend/server'.");
4768
Amaury Denoyellecece9182021-04-20 17:09:08 +02004769 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004770 if (!be)
4771 return cli_err(appctx, "No such backend.");
4772
4773 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004774 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004775 return 1;
4776 }
4777
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004778 /* At this point, some operations might not be thread-safe anymore. This
4779 * might be the case for parsing handlers which were designed to run
4780 * only at the starting stage on single-thread mode.
4781 *
4782 * Activate thread isolation to ensure thread-safety.
4783 */
4784 thread_isolate();
4785
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004786 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004787 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4788 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004789 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004790
4791 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004792 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004793
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004794 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004795 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004796 }
4797
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004798 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4799 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004800 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004801
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004802 /* A dynamic server does not currently support resolution.
4803 *
4804 * Initialize it explicitly to the "none" method to ensure no
4805 * resolution will ever be executed.
4806 */
4807 srv->init_addr_methods = SRV_IADDR_NONE;
4808
Amaury Denoyelle30467232021-03-12 18:03:27 +01004809 if (srv->mux_proto) {
4810 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004811 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004812 goto out;
4813 }
4814 }
4815
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004816 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004817 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004818 goto out;
4819 }
4820
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004821 if (srv->max_idle_conns != 0) {
4822 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4823 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004824 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004825 goto out;
4826 }
4827 }
4828
4829 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004830 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004831 goto out;
4832 }
4833
4834 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4835 COUNTERS_SV,
4836 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004837 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004838 goto out;
4839 }
4840
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004841 /* ensure minconn/maxconn consistency */
4842 srv_minmax_conn_apply(srv);
4843
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004844 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4845 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004846 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4847 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4848 goto out;
4849 }
4850 }
4851
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004852 if (srv->trackit) {
4853 if (srv_apply_track(srv, be))
4854 goto out;
4855 }
4856
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004857 /* Init check/agent if configured. The check is manually disabled
4858 * because a dynamic server is started in a disable state. It must be
4859 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004860 */
4861 if (srv->do_check) {
4862 if (init_srv_check(srv))
4863 goto out;
4864
4865 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004866 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004867
4868 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004869 if (init_srv_agent_check(srv))
4870 goto out;
4871
4872 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004873 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004874
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004875 /* Init slowstart if needed. */
4876 if (init_srv_slowstart(srv))
4877 goto out;
4878
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004879 /* Attach the server to the end of the proxy linked list. Note that this
4880 * operation is not thread-safe so this is executed under thread
4881 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004882 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004883 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004884 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004885
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004886 /* TODO use a double-linked list for px->srv */
4887 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004888 struct server *next = be->srv;
4889
4890 while (1) {
4891 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004892 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004893 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004894 goto out;
4895 }
4896
4897 if (!next->next)
4898 break;
4899
4900 next = next->next;
4901 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004902
4903 next->next = srv;
4904 }
4905 else {
4906 srv->next = be->srv;
4907 be->srv = srv;
4908 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004909
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004910 /* generate the server id if not manually specified */
4911 if (!srv->puid) {
4912 next_id = get_next_id(&be->conf.used_server_id, 1);
4913 if (!next_id) {
4914 ha_alert("Cannot attach server : no id left in proxy\n");
4915 goto out;
4916 }
4917
4918 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004919 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004920 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004921
4922 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004923 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4924 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004925 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4926 if (srv->addr_node.key)
4927 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004928
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004929 /* check if LSB bit (odd bit) is set for reuse_cnt */
4930 if (srv_id_reuse_cnt & 1) {
4931 /* cnt must be increased */
4932 srv_id_reuse_cnt++;
4933 }
4934 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4935 * save some space
4936 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
4937 */
4938 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
4939
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01004940 /* adding server cannot fail when we reach this:
4941 * publishing EVENT_HDL_SUB_SERVER_ADD
4942 */
4943 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
4944
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004945 thread_release();
4946
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004947 /* Start the check task. The server must be fully initialized.
4948 *
4949 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4950 * need to randomly spread the task interval for dynamic servers.
4951 */
4952 if (srv->check.state & CHK_ST_CONFIGURED) {
4953 if (!start_check_task(&srv->check, 0, 1, 1))
4954 ha_alert("System might be unstable, consider to execute a reload");
4955 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004956 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004957 if (!start_check_task(&srv->agent, 0, 1, 1))
4958 ha_alert("System might be unstable, consider to execute a reload");
4959 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004960
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004961 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004962 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004963
4964 return 0;
4965
4966out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004967 if (srv) {
4968 if (srv->track)
4969 release_server_track(srv);
4970
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004971 if (srv->check.state & CHK_ST_CONFIGURED)
4972 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004973 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004974 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004975
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004976 /* remove the server from the proxy linked list */
4977 if (be->srv == srv) {
4978 be->srv = srv->next;
4979 }
4980 else {
4981 struct server *prev;
4982 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4983 ;
4984 if (prev)
4985 prev->next = srv->next;
4986 }
4987
4988 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004989
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004990 thread_release();
4991
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004992 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004993 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004994
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004995 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004996 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004997
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004998 return 1;
4999}
5000
Amaury Denoyellee5580432021-04-15 14:41:20 +02005001/* Parse a "del server" command
5002 * Returns 0 if the server has been successfully initialized, 1 on failure.
5003 */
5004static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
5005{
5006 struct proxy *be;
5007 struct server *srv;
5008 char *be_name, *sv_name;
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005009 struct server *prev_del;
Amaury Denoyellee5580432021-04-15 14:41:20 +02005010
5011 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5012 return 1;
5013
5014 ++args;
5015
5016 sv_name = be_name = args[1];
5017 /* split backend/server arg */
5018 while (*sv_name && *(++sv_name)) {
5019 if (*sv_name == '/') {
5020 *sv_name = '\0';
5021 ++sv_name;
5022 break;
5023 }
5024 }
5025
5026 if (!*sv_name)
5027 return cli_err(appctx, "Require 'backend/server'.");
5028
5029 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005030 * requires thread isolation. In addition, any place referencing the
5031 * server about to be deleted would be unsafe after our operation, so
5032 * we must be certain to be alone so that no other thread has even
5033 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005034 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005035 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005036
5037 get_backend_server(be_name, sv_name, &be, &srv);
5038 if (!be) {
5039 cli_err(appctx, "No such backend.");
5040 goto out;
5041 }
5042
5043 if (!srv) {
5044 cli_err(appctx, "No such server.");
5045 goto out;
5046 }
5047
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005048 if (srv->flags & SRV_F_NON_PURGEABLE) {
5049 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 +02005050 goto out;
5051 }
5052
5053 /* Only servers in maintenance can be deleted. This ensures that the
5054 * server is not present anymore in the lb structures (through
5055 * lbprm.set_server_status_down).
5056 */
5057 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5058 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5059 goto out;
5060 }
5061
5062 /* Ensure that there is no active/idle/pending connection on the server.
5063 *
5064 * TODO idle connections should not prevent server deletion. A proper
5065 * cleanup function should be implemented to be used here.
5066 */
5067 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005068 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005069 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5070 goto out;
5071 }
5072
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005073 /* removing cannot fail anymore when we reach this:
5074 * publishing EVENT_HDL_SUB_SERVER_DEL
5075 */
5076 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5077
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005078 /* remove srv from tracking list */
5079 if (srv->track)
5080 release_server_track(srv);
5081
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005082 /* stop the check task if running */
5083 if (srv->check.state & CHK_ST_CONFIGURED)
5084 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005085 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005086 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005087
5088 /* detach the server from the proxy linked list
5089 * The proxy servers list is currently not protected by a lock, so this
5090 * requires thread_isolate/release.
5091 */
5092
5093 /* be->srv cannot be empty since we have already found the server with
5094 * get_backend_server */
5095 BUG_ON(!be->srv);
5096 if (be->srv == srv) {
5097 be->srv = srv->next;
5098 }
5099 else {
5100 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005101 for (next = be->srv; srv != next->next; next = next->next) {
5102 /* srv cannot be not found since we have already found
5103 * it with get_backend_server */
5104 BUG_ON(!next);
5105 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005106
Amaury Denoyellee5580432021-04-15 14:41:20 +02005107 next->next = srv->next;
5108 }
5109
Aurelien DARRAGONf175b082023-02-01 17:22:32 +01005110 /* Some deleted servers could still point to us using their 'next',
5111 * update them as needed
5112 * Please note the small race between the POP and APPEND, although in
5113 * this situation this is not an issue as we are under full thread
5114 * isolation
5115 */
5116 while ((prev_del = MT_LIST_POP(&srv->prev_deleted, struct server *, prev_deleted))) {
5117 /* update its 'next' ptr */
5118 prev_del->next = srv->next;
5119 if (srv->next) {
5120 /* now it is our 'next' responsibility */
5121 MT_LIST_APPEND(&srv->next->prev_deleted, &prev_del->prev_deleted);
5122 }
5123 }
5124
5125 /* we ourselves need to inform our 'next' that we will still point it */
5126 if (srv->next)
5127 MT_LIST_APPEND(&srv->next->prev_deleted, &srv->prev_deleted);
5128
Amaury Denoyellee5580432021-04-15 14:41:20 +02005129 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005130 eb32_delete(&srv->conf.id);
5131 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005132 if (srv->addr_node.key)
5133 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005134
5135 /* remove srv from idle_node tree for idle conn cleanup */
5136 eb32_delete(&srv->idle_node);
5137
Aurelien DARRAGON75b9d1c2023-01-24 14:40:01 +01005138 /* flag the server as deleted
5139 * (despite the server being removed from primary server list,
5140 * one could still access the server data from a valid ptr)
5141 * Deleted flag helps detecting when a server is in transient removal
5142 * state.
5143 * ie: removed from the list but not yet freed/purged from memory.
5144 */
5145 srv->flags |= SRV_F_DELETED;
5146
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005147 /* set LSB bit (odd bit) for reuse_cnt */
5148 srv_id_reuse_cnt |= 1;
5149
Amaury Denoyellee5580432021-04-15 14:41:20 +02005150 thread_release();
5151
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005152 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005153 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005154
5155 cli_msg(appctx, LOG_INFO, "Server deleted.");
5156
5157 return 0;
5158
5159out:
5160 thread_release();
5161
5162 return 1;
5163}
5164
William Lallemand222baf22016-11-19 02:00:33 +01005165/* register cli keywords */
5166static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005167 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5168 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005169 { { "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 +02005170 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5171 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005172 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5173 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5174 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5175 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5176 { { "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 +01005177 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5178 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005179 {{},}
5180}};
5181
Willy Tarreau0108d902018-11-25 19:14:37 +01005182INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005183
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005184/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5185 * field is used to retrieve the identifier of the tracked server, either with
5186 * the format "proxy/server" or just "server". <curproxy> must point to the
5187 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5188 * to find the tracked server.
5189 *
5190 * Returns 0 if the server track has been activated else non-zero.
5191 *
5192 * Not thread-safe.
5193 */
5194int srv_apply_track(struct server *srv, struct proxy *curproxy)
5195{
5196 struct proxy *px;
5197 struct server *strack, *loop;
5198 char *pname, *sname;
5199
5200 if (!srv->trackit)
5201 return 1;
5202
5203 pname = srv->trackit;
5204 sname = strrchr(pname, '/');
5205
5206 if (sname) {
5207 *sname++ = '\0';
5208 }
5209 else {
5210 sname = pname;
5211 pname = NULL;
5212 }
5213
5214 if (pname) {
5215 px = proxy_be_by_name(pname);
5216 if (!px) {
5217 ha_alert("unable to find required proxy '%s' for tracking.\n",
5218 pname);
5219 return 1;
5220 }
5221 }
5222 else {
5223 px = curproxy;
5224 }
5225
5226 strack = findserver(px, sname);
5227 if (!strack) {
5228 ha_alert("unable to find required server '%s' for tracking.\n",
5229 sname);
5230 return 1;
5231 }
5232
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005233 if (strack->flags & SRV_F_DYNAMIC) {
5234 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5235 px->id, strack->id);
5236 return 1;
5237 }
5238
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005239 if (!strack->do_check && !strack->do_agent && !strack->track &&
5240 !strack->trackit) {
5241 ha_alert("unable to use %s/%s for "
5242 "tracking as it does not have any check nor agent enabled.\n",
5243 px->id, strack->id);
5244 return 1;
5245 }
5246
5247 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5248 ;
5249
5250 if (srv == strack || loop) {
5251 ha_alert("unable to track %s/%s as it "
5252 "belongs to a tracking chain looping back to %s/%s.\n",
5253 px->id, strack->id, px->id,
5254 srv == strack ? strack->id : loop->id);
5255 return 1;
5256 }
5257
5258 if (curproxy != px &&
5259 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5260 ha_alert("unable to use %s/%s for"
5261 "tracking: disable-on-404 option inconsistency.\n",
5262 px->id, strack->id);
5263 return 1;
5264 }
5265
5266 srv->track = strack;
5267 srv->tracknext = strack->trackers;
5268 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005269 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005270
5271 ha_free(&srv->trackit);
5272
5273 return 0;
5274}
5275
Emeric Brun64cc49c2017-10-03 14:46:45 +02005276/*
Aurelien DARRAGON0ddf0522023-03-27 11:57:28 +02005277 * This function applies server's status changes.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005278 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005279 * Must be called with the server lock held. This may also be called at init
5280 * time as the result of parsing the state file, in which case no lock will be
5281 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005282 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005283static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005284{
5285 struct check *check = &s->check;
5286 int xferred;
5287 struct proxy *px = s->proxy;
5288 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5289 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5290 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005291 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005292
Emeric Brun64cc49c2017-10-03 14:46:45 +02005293 /* If currently main is not set we try to apply pending state changes */
5294 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5295 int next_admin;
5296
5297 /* Backup next admin */
5298 next_admin = s->next_admin;
5299
5300 /* restore current admin state */
5301 s->next_admin = s->cur_admin;
5302
5303 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5304 s->last_change = now.tv_sec;
5305 if (s->proxy->lbprm.set_server_status_down)
5306 s->proxy->lbprm.set_server_status_down(s);
5307
5308 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5309 srv_shutdown_streams(s, SF_ERR_DOWN);
5310
5311 /* we might have streams queued on this server and waiting for
5312 * a connection. Those which are redispatchable will be queued
5313 * to another server or to the proxy itself.
5314 */
5315 xferred = pendconn_redistribute(s);
5316
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005317 /* no maintenance + server DOWN: publish event SERVER DOWN */
5318 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5319
Emeric Brun64cc49c2017-10-03 14:46:45 +02005320 tmptrash = alloc_trash_chunk();
5321 if (tmptrash) {
5322 chunk_printf(tmptrash,
5323 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5324 s->proxy->id, s->id);
5325
Emeric Brun5a133512017-10-19 14:42:30 +02005326 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005327 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005328
5329 /* we don't send an alert if the server was previously paused */
5330 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005331 send_log(s->proxy, log_level, "%s.\n",
5332 tmptrash->area);
5333 send_email_alert(s, log_level, "%s",
5334 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005335 free_trash_chunk(tmptrash);
5336 tmptrash = NULL;
5337 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005338 s->counters.down_trans++;
5339 }
5340 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5341 s->last_change = now.tv_sec;
5342 if (s->proxy->lbprm.set_server_status_down)
5343 s->proxy->lbprm.set_server_status_down(s);
5344
5345 /* we might have streams queued on this server and waiting for
5346 * a connection. Those which are redispatchable will be queued
5347 * to another server or to the proxy itself.
5348 */
5349 xferred = pendconn_redistribute(s);
5350
5351 tmptrash = alloc_trash_chunk();
5352 if (tmptrash) {
5353 chunk_printf(tmptrash,
5354 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5355 s->proxy->id, s->id);
5356
Emeric Brun5a133512017-10-19 14:42:30 +02005357 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005358
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005359 ha_warning("%s.\n", tmptrash->area);
5360 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5361 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005362 free_trash_chunk(tmptrash);
5363 tmptrash = NULL;
5364 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005365 }
5366 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5367 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005368
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01005369 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02005370 s->down_time += now.tv_sec - s->last_change;
5371
5372 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005373 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005374 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5375
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005376 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005377 /* now propagate the status change to any LB algorithms */
5378 if (px->lbprm.update_server_eweight)
5379 px->lbprm.update_server_eweight(s);
5380 else if (srv_willbe_usable(s)) {
5381 if (px->lbprm.set_server_status_up)
5382 px->lbprm.set_server_status_up(s);
5383 }
5384 else {
5385 if (px->lbprm.set_server_status_down)
5386 px->lbprm.set_server_status_down(s);
5387 }
5388
5389 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5390 * and it's not a backup server and its effective weight is > 0,
5391 * then it can accept new connections, so we shut down all streams
5392 * on all backup servers.
5393 */
5394 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5395 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5396 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5397
5398 /* check if we can handle some connections queued at the proxy. We
5399 * will take as many as we can handle.
5400 */
5401 xferred = pendconn_grab_from_px(s);
5402
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005403 /* no maintenance + server going UP: publish event SERVER UP */
5404 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5405
Emeric Brun64cc49c2017-10-03 14:46:45 +02005406 tmptrash = alloc_trash_chunk();
5407 if (tmptrash) {
5408 chunk_printf(tmptrash,
5409 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5410 s->proxy->id, s->id);
5411
Emeric Brun5a133512017-10-19 14:42:30 +02005412 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005413 ha_warning("%s.\n", tmptrash->area);
5414 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5415 tmptrash->area);
5416 send_email_alert(s, LOG_NOTICE, "%s",
5417 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005418 free_trash_chunk(tmptrash);
5419 tmptrash = NULL;
5420 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005421 }
5422 else if (s->cur_eweight != s->next_eweight) {
5423 /* now propagate the status change to any LB algorithms */
5424 if (px->lbprm.update_server_eweight)
5425 px->lbprm.update_server_eweight(s);
5426 else if (srv_willbe_usable(s)) {
5427 if (px->lbprm.set_server_status_up)
5428 px->lbprm.set_server_status_up(s);
5429 }
5430 else {
5431 if (px->lbprm.set_server_status_down)
5432 px->lbprm.set_server_status_down(s);
5433 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005434 }
5435
5436 s->next_admin = next_admin;
5437 }
5438
Emeric Brun5a133512017-10-19 14:42:30 +02005439 /* reset operational state change */
5440 *s->op_st_chg.reason = 0;
5441 s->op_st_chg.status = s->op_st_chg.code = -1;
5442 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005443
5444 /* Now we try to apply pending admin changes */
5445
5446 /* Maintenance must also disable health checks */
5447 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5448 if (s->check.state & CHK_ST_ENABLED) {
5449 s->check.state |= CHK_ST_PAUSED;
5450 check->health = 0;
5451 }
5452
5453 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005454 tmptrash = alloc_trash_chunk();
5455 if (tmptrash) {
5456 chunk_printf(tmptrash,
5457 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5458 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5459 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005460
Olivier Houchard796a2b32017-10-24 17:42:47 +02005461 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005462
Olivier Houchard796a2b32017-10-24 17:42:47 +02005463 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005464 ha_warning("%s.\n", tmptrash->area);
5465 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5466 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005467 }
5468 free_trash_chunk(tmptrash);
5469 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005470 }
5471 }
5472 else { /* server was still running */
5473 check->health = 0; /* failure */
5474 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005475
5476 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005477 if (s->proxy->lbprm.set_server_status_down)
5478 s->proxy->lbprm.set_server_status_down(s);
5479
Emeric Brun64cc49c2017-10-03 14:46:45 +02005480 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5481 srv_shutdown_streams(s, SF_ERR_DOWN);
5482
William Dauchy6318d332020-05-02 21:52:36 +02005483 /* force connection cleanup on the given server */
5484 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005485 /* we might have streams queued on this server and waiting for
5486 * a connection. Those which are redispatchable will be queued
5487 * to another server or to the proxy itself.
5488 */
5489 xferred = pendconn_redistribute(s);
5490
Aurelien DARRAGONa63f4902023-03-23 14:39:51 +01005491 /* maintenance on previously running server: publish event SERVER DOWN */
5492 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5493
Emeric Brun64cc49c2017-10-03 14:46:45 +02005494 tmptrash = alloc_trash_chunk();
5495 if (tmptrash) {
5496 chunk_printf(tmptrash,
5497 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5498 s->flags & SRV_F_BACKUP ? "Backup " : "",
5499 s->proxy->id, s->id,
5500 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5501
5502 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5503
5504 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005505 ha_warning("%s.\n", tmptrash->area);
5506 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5507 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005508 }
5509 free_trash_chunk(tmptrash);
5510 tmptrash = NULL;
5511 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005512 s->counters.down_trans++;
5513 }
5514 }
5515 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5516 /* OK here we're leaving maintenance, we have many things to check,
5517 * because the server might possibly be coming back up depending on
5518 * its state. In practice, leaving maintenance means that we should
5519 * immediately turn to UP (more or less the slowstart) under the
5520 * following conditions :
5521 * - server is neither checked nor tracked
5522 * - server tracks another server which is not checked
5523 * - server tracks another server which is already up
5524 * Which sums up as something simpler :
5525 * "either the tracking server is up or the server's checks are disabled
5526 * or up". Otherwise we only re-enable health checks. There's a special
5527 * case associated to the stopping state which can be inherited. Note
5528 * that the server might still be in drain mode, which is naturally dealt
5529 * with by the lower level functions.
5530 */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005531 if (s->check.state & CHK_ST_ENABLED) {
5532 s->check.state &= ~CHK_ST_PAUSED;
5533 check->health = check->rise; /* start OK but check immediately */
5534 }
5535
5536 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5537 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5538 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5539 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5540 s->next_state = SRV_ST_STOPPING;
5541 }
5542 else {
Willy Tarreaud332f132021-08-04 19:35:13 +02005543 s->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005544 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005545 if (s->slowstart > 0) {
5546 if (s->warmup)
5547 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5548 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005549 else
5550 s->next_state = SRV_ST_RUNNING;
5551 }
5552
5553 }
5554
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005555 /* ignore if server stays down when leaving maintenance mode */
5556 if (s->next_state != SRV_ST_STOPPED) {
5557 /* leaving maintenance + server UP: publish event SERVER UP */
5558 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5559 }
5560
Emeric Brun64cc49c2017-10-03 14:46:45 +02005561 tmptrash = alloc_trash_chunk();
5562 if (tmptrash) {
5563 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5564 chunk_printf(tmptrash,
5565 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5566 s->flags & SRV_F_BACKUP ? "Backup " : "",
5567 s->proxy->id, s->id,
5568 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5569 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5570 }
5571 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5572 chunk_printf(tmptrash,
5573 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5574 s->flags & SRV_F_BACKUP ? "Backup " : "",
5575 s->proxy->id, s->id, s->hostname,
5576 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5577 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5578 }
5579 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5580 chunk_printf(tmptrash,
5581 "%sServer %s/%s is %s/%s (leaving maintenance)",
5582 s->flags & SRV_F_BACKUP ? "Backup " : "",
5583 s->proxy->id, s->id,
5584 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5585 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5586 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005587 ha_warning("%s.\n", tmptrash->area);
5588 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5589 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005590 free_trash_chunk(tmptrash);
5591 tmptrash = NULL;
5592 }
5593
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005594 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005595 /* now propagate the status change to any LB algorithms */
5596 if (px->lbprm.update_server_eweight)
5597 px->lbprm.update_server_eweight(s);
5598 else if (srv_willbe_usable(s)) {
5599 if (px->lbprm.set_server_status_up)
5600 px->lbprm.set_server_status_up(s);
5601 }
5602 else {
5603 if (px->lbprm.set_server_status_down)
5604 px->lbprm.set_server_status_down(s);
5605 }
5606
Willy Tarreau6a78e612018-08-07 10:14:53 +02005607 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5608 * and it's not a backup server and its effective weight is > 0,
5609 * then it can accept new connections, so we shut down all streams
5610 * on all backup servers.
5611 */
5612 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5613 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5614 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5615
5616 /* check if we can handle some connections queued at the proxy. We
5617 * will take as many as we can handle.
5618 */
5619 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005620 }
5621 else if (s->next_admin & SRV_ADMF_MAINT) {
5622 /* remaining in maintenance mode, let's inform precisely about the
5623 * situation.
5624 */
5625 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5626 tmptrash = alloc_trash_chunk();
5627 if (tmptrash) {
5628 chunk_printf(tmptrash,
5629 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5630 s->flags & SRV_F_BACKUP ? "Backup " : "",
5631 s->proxy->id, s->id);
5632
5633 if (s->track) /* normally it's mandatory here */
5634 chunk_appendf(tmptrash, " via %s/%s",
5635 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005636 ha_warning("%s.\n", tmptrash->area);
5637 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5638 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005639 free_trash_chunk(tmptrash);
5640 tmptrash = NULL;
5641 }
5642 }
5643 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5644 tmptrash = alloc_trash_chunk();
5645 if (tmptrash) {
5646 chunk_printf(tmptrash,
5647 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5648 s->flags & SRV_F_BACKUP ? "Backup " : "",
5649 s->proxy->id, s->id, s->hostname);
5650
5651 if (s->track) /* normally it's mandatory here */
5652 chunk_appendf(tmptrash, " via %s/%s",
5653 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005654 ha_warning("%s.\n", tmptrash->area);
5655 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5656 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005657 free_trash_chunk(tmptrash);
5658 tmptrash = NULL;
5659 }
5660 }
5661 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5662 tmptrash = alloc_trash_chunk();
5663 if (tmptrash) {
5664 chunk_printf(tmptrash,
5665 "%sServer %s/%s remains in forced maintenance",
5666 s->flags & SRV_F_BACKUP ? "Backup " : "",
5667 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005668 ha_warning("%s.\n", tmptrash->area);
5669 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5670 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005671 free_trash_chunk(tmptrash);
5672 tmptrash = NULL;
5673 }
5674 }
5675 /* don't report anything when leaving drain mode and remaining in maintenance */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005676 }
5677
5678 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5679 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5680 /* drain state is applied only if not yet in maint */
5681
5682 s->last_change = now.tv_sec;
5683 if (px->lbprm.set_server_status_down)
5684 px->lbprm.set_server_status_down(s);
5685
5686 /* we might have streams queued on this server and waiting for
5687 * a connection. Those which are redispatchable will be queued
5688 * to another server or to the proxy itself.
5689 */
5690 xferred = pendconn_redistribute(s);
5691
5692 tmptrash = alloc_trash_chunk();
5693 if (tmptrash) {
5694 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5695 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5696 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5697
5698 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5699
5700 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005701 ha_warning("%s.\n", tmptrash->area);
5702 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5703 tmptrash->area);
5704 send_email_alert(s, LOG_NOTICE, "%s",
5705 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005706 }
5707 free_trash_chunk(tmptrash);
5708 tmptrash = NULL;
5709 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005710 }
5711 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5712 /* OK completely leaving drain mode */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005713
5714 if (s->last_change < now.tv_sec) // ignore negative times
5715 s->down_time += now.tv_sec - s->last_change;
5716 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005717 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005718
5719 tmptrash = alloc_trash_chunk();
5720 if (tmptrash) {
Aurelien DARRAGON9a1df022023-03-27 10:17:31 +02005721 if (s->cur_admin & SRV_ADMF_FDRAIN) {
Emeric Brun64cc49c2017-10-03 14:46:45 +02005722 chunk_printf(tmptrash,
5723 "%sServer %s/%s is %s (leaving forced drain)",
5724 s->flags & SRV_F_BACKUP ? "Backup " : "",
5725 s->proxy->id, s->id,
5726 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5727 }
5728 else {
5729 chunk_printf(tmptrash,
5730 "%sServer %s/%s is %s (leaving drain)",
5731 s->flags & SRV_F_BACKUP ? "Backup " : "",
5732 s->proxy->id, s->id,
5733 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5734 if (s->track) /* normally it's mandatory here */
5735 chunk_appendf(tmptrash, " via %s/%s",
5736 s->track->proxy->id, s->track->id);
5737 }
5738
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005739 ha_warning("%s.\n", tmptrash->area);
5740 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5741 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005742 free_trash_chunk(tmptrash);
5743 tmptrash = NULL;
5744 }
5745
5746 /* now propagate the status change to any LB algorithms */
5747 if (px->lbprm.update_server_eweight)
5748 px->lbprm.update_server_eweight(s);
5749 else if (srv_willbe_usable(s)) {
5750 if (px->lbprm.set_server_status_up)
5751 px->lbprm.set_server_status_up(s);
5752 }
5753 else {
5754 if (px->lbprm.set_server_status_down)
5755 px->lbprm.set_server_status_down(s);
5756 }
5757 }
5758 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5759 /* remaining in drain mode after removing one of its flags */
5760
5761 tmptrash = alloc_trash_chunk();
5762 if (tmptrash) {
5763 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5764 chunk_printf(tmptrash,
5765 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5766 s->flags & SRV_F_BACKUP ? "Backup " : "",
5767 s->proxy->id, s->id);
5768
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 else {
5774 chunk_printf(tmptrash,
5775 "%sServer %s/%s remains in forced drain mode",
5776 s->flags & SRV_F_BACKUP ? "Backup " : "",
5777 s->proxy->id, s->id);
5778 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005779 ha_warning("%s.\n", tmptrash->area);
5780 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5781 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005782 free_trash_chunk(tmptrash);
5783 tmptrash = NULL;
5784 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005785 }
5786 }
5787
5788 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005789 *s->adm_st_chg_cause = 0;
Aurelien DARRAGON22151c72023-04-17 17:45:58 +02005790
5791 /* explicitly commit state changes (even if it was already applied implicitly
5792 * by some lb state change function), so we don't miss anything
5793 */
5794 srv_lb_commit_status(s);
Aurelien DARRAGONe80ddb12023-04-17 15:30:11 +02005795
5796 /* check if backend stats must be updated due to the server state change */
5797 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5798 set_backend_down(s->proxy); /* backend going down */
5799 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act)) {
5800 /* backend was down and is back up again:
5801 * no helper function, updating last_change and backend downtime stats
5802 */
5803 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5804 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5805 s->proxy->last_change = now.tv_sec;
5806 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005807}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005808
Willy Tarreau144f84a2021-03-02 16:09:26 +01005809struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005810{
5811 struct connection *conn;
5812
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005813 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005814 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005815 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005816 }
5817
5818 return task;
5819}
5820
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005821/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005822 * moving them all.
5823 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005824 *
5825 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005826 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005827static 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 +02005828{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005829 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005830 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005831 int i = 0;
5832
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005833 node = eb_first(idle_tree);
5834 while (node) {
5835 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005836 if (toremove_nb != -1 && i >= toremove_nb)
5837 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005838
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005839 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005840 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005841 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005842 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005843
5844 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005845 }
5846 return i;
5847}
William Dauchy6318d332020-05-02 21:52:36 +02005848/* cleanup connections for a given server
5849 * might be useful when going on forced maintenance or live changing ip/port
5850 */
William Dauchy707ad322020-05-04 13:52:40 +02005851static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005852{
William Dauchy6318d332020-05-02 21:52:36 +02005853 int did_remove;
5854 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005855
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005856 /* nothing to do if pool-max-conn is null */
5857 if (!srv->max_idle_conns)
5858 return;
5859
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005860 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005861 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005862 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005863 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005864 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 +02005865 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005866 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 +02005867 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005868 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005869 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005870 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005871
5872 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5873 break;
William Dauchy6318d332020-05-02 21:52:36 +02005874 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005875}
5876
5877/* removes an idle conn after updating the server idle conns counters */
5878void srv_release_conn(struct server *srv, struct connection *conn)
5879{
5880 if (conn->flags & CO_FL_LIST_MASK) {
5881 /* The connection is currently in the server's idle list, so tell it
5882 * there's one less connection available in that list.
5883 */
5884 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5885 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5886 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5887 }
5888 else {
5889 /* The connection is not private and not in any server's idle
5890 * list, so decrement the current number of used connections
5891 */
5892 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5893 }
5894
5895 /* Remove the connection from any tree (safe, idle or available) */
5896 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5897 conn_delete_from_tree(&conn->hash_node->node);
Christopher Faulet5e1b0e72023-02-28 15:39:38 +01005898 conn->flags &= ~CO_FL_LIST_MASK;
Willy Tarreau260f3242021-10-06 18:30:04 +02005899 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5900}
5901
5902/* retrieve a connection from its <hash> in <tree>
5903 * returns NULL if no connection found
5904 */
5905struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5906{
Willy Tarreau85223482022-09-29 20:32:43 +02005907 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005908 struct connection *conn = NULL;
5909 struct conn_hash_node *hash_node = NULL;
5910
Willy Tarreau85223482022-09-29 20:32:43 +02005911 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005912 if (node) {
5913 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5914 conn = hash_node->conn;
5915 }
5916
5917 return conn;
5918}
5919
5920/* retrieve the next connection sharing the same hash as <conn>
5921 * returns NULL if no connection found
5922 */
5923struct connection *srv_lookup_conn_next(struct connection *conn)
5924{
Willy Tarreau85223482022-09-29 20:32:43 +02005925 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005926 struct connection *next_conn = NULL;
5927 struct conn_hash_node *hash_node = NULL;
5928
Willy Tarreau85223482022-09-29 20:32:43 +02005929 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005930 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005931 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005932 next_conn = hash_node->conn;
5933 }
5934
5935 return next_conn;
5936}
5937
5938/* This adds an idle connection to the server's list if the connection is
5939 * reusable, not held by any owner anymore, but still has available streams.
5940 */
5941int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5942{
5943 /* we try to keep the connection in the server's idle list
5944 * if we don't have too many FD in use, and if the number of
5945 * idle+current conns is lower than what was observed before
5946 * last purge, or if we already don't have idle conns for the
5947 * current thread and we don't exceed last count by global.nbthread.
5948 */
5949 if (!(conn->flags & CO_FL_PRIVATE) &&
5950 srv && srv->pool_purge_delay > 0 &&
5951 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
5952 ha_used_fds < global.tune.pool_high_count &&
5953 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
5954 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
5955 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
5956 (ha_used_fds < global.tune.pool_low_count &&
5957 (srv->curr_used_conns + srv->curr_idle_conns <=
5958 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
5959 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
5960 int retadd;
5961
5962 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
5963 if (retadd > srv->max_idle_conns) {
5964 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5965 return 0;
5966 }
5967 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5968
5969 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5970 conn_delete_from_tree(&conn->hash_node->node);
5971
5972 if (is_safe) {
5973 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005974 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005975 _HA_ATOMIC_INC(&srv->curr_safe_nb);
5976 } else {
5977 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005978 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005979 _HA_ATOMIC_INC(&srv->curr_idle_nb);
5980 }
5981 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5982 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
5983
5984 __ha_barrier_full();
5985 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5986 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5987 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5988 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5989 now_ms);
5990 eb32_insert(&idle_conn_srv, &srv->idle_node);
5991 if (!task_in_wq(idle_conn_task) && !
5992 task_in_rq(idle_conn_task)) {
5993 task_schedule(idle_conn_task,
5994 srv->idle_node.key);
5995 }
5996
5997 }
5998 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5999 }
6000 return 1;
6001 }
6002 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02006003}
6004
Willy Tarreau144f84a2021-03-02 16:09:26 +01006005struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006006{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006007 struct server *srv;
6008 struct eb32_node *eb;
6009 int i;
6010 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006011
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006012 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006013 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6014 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006015 int exceed_conns;
6016 int to_kill;
6017 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006018
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006019 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
6020 if (!eb) {
6021 /* we might have reached the end of the tree, typically because
6022 * <now_ms> is in the first half and we're first scanning the last
6023 * half. Let's loop back to the beginning of the tree now.
6024 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006025
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006026 eb = eb32_first(&idle_conn_srv);
6027 if (likely(!eb))
6028 break;
6029 }
6030 if (tick_is_lt(now_ms, eb->key)) {
6031 /* timer not expired yet, revisit it later */
6032 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006033 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006034 }
6035 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006036
6037 /* Calculate how many idle connections we want to kill :
6038 * we want to remove half the difference between the total
6039 * of established connections (used or idle) and the max
6040 * number of used connections.
6041 */
6042 curr_idle = srv->curr_idle_conns;
6043 if (curr_idle == 0)
6044 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006045 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006046 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006047
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006048 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006049 if (srv->est_need_conns < srv->max_used_conns)
6050 srv->est_need_conns = srv->max_used_conns;
6051
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006052 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006053
Willy Tarreau18ed7892020-07-02 19:05:30 +02006054 if (exceed_conns <= 0)
6055 goto remove;
6056
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006057 /* check all threads starting with ours */
6058 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006059 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006060 int j;
6061 int did_remove = 0;
6062
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006063 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6064 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006065
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006066 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006067 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 +02006068 if (j > 0)
6069 did_remove = 1;
6070 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006071 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 +01006072 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006073 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006074
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006075 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006076 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006077
6078 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6079 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006080 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006081remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006082 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006083
6084 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006085 /* There are still more idle connections, add the
6086 * server back in the tree.
6087 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006088 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006089 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006090 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006091 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006092 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006093 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6094
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006095 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006096 return task;
6097}
Olivier Houchard88698d92019-04-16 19:07:22 +02006098
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006099/* Close remaining idle connections. This functions is designed to be run on
6100 * process shutdown. This guarantees a proper socket shutdown to avoid
6101 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6102 * bypassed.
6103 *
6104 * This function is not thread-safe so it must only be called via a global
6105 * deinit function.
6106 */
6107static void srv_close_idle_conns(struct server *srv)
6108{
6109 struct eb_root **cleaned_tree;
6110 int i;
6111
6112 for (i = 0; i < global.nbthread; ++i) {
6113 struct eb_root *conn_trees[] = {
6114 &srv->per_thr[i].idle_conns,
6115 &srv->per_thr[i].safe_conns,
6116 &srv->per_thr[i].avail_conns,
6117 NULL
6118 };
6119
6120 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6121 while (!eb_is_empty(*cleaned_tree)) {
6122 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6123 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6124 struct connection *conn = conn_hash_node->conn;
6125
6126 if (conn->ctrl->ctrl_close)
6127 conn->ctrl->ctrl_close(conn);
6128 ebmb_delete(node);
6129 }
6130 }
6131 }
6132}
6133
6134REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6135
Willy Tarreau76cc6992020-07-01 18:49:24 +02006136/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6137static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006138 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006139 char **err)
6140{
6141 if (too_many_args(1, args, err, NULL))
6142 return -1;
6143
6144 if (strcmp(args[1], "on") == 0)
6145 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6146 else if (strcmp(args[1], "off") == 0)
6147 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6148 else {
6149 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6150 return -1;
6151 }
6152 return 0;
6153}
6154
Olivier Houchard88698d92019-04-16 19:07:22 +02006155/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6156static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006157 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006158 char **err)
6159{
6160 int arg = -1;
6161
6162 if (too_many_args(1, args, err, NULL))
6163 return -1;
6164
6165 if (*(args[1]) != 0)
6166 arg = atoi(args[1]);
6167
6168 if (arg < 0 || arg > 100) {
6169 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6170 return -1;
6171 }
6172
6173 if (args[0][10] == 'h')
6174 global.tune.pool_high_ratio = arg;
6175 else
6176 global.tune.pool_low_ratio = arg;
6177 return 0;
6178}
6179
6180/* config keyword parsers */
6181static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006182 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006183 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6184 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6185 { 0, NULL, NULL }
6186}};
6187
6188INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6189
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006190/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006191 * Local variables:
6192 * c-indent-level: 8
6193 * c-basic-offset: 8
6194 * End:
6195 */