blob: 6e9d5113fed0ae407a1e47e7549af11c6e42d9e5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau0a4b0ab2020-06-11 11:22:44 +020014#include <sys/types.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020015#include <netinet/tcp.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020017#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020018
Willy Tarreau260f3242021-10-06 18:30:04 +020019#include <import/ebmbtree.h>
20
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020022#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020023#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020024#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020025#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020026#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020027#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020028#include <haproxy/dict-t.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020029#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020030#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020032#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020033#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/port_range.h>
35#include <haproxy/protocol.h>
Willy Tarreaub00a8e32021-05-08 20:18:59 +020036#include <haproxy/proxy.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020037#include <haproxy/queue.h>
Emeric Brunc9437992021-02-12 19:42:55 +010038#include <haproxy/resolvers.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020040#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020041#include <haproxy/server.h>
Amaury Denoyellef99f77a2021-03-08 17:13:32 +010042#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020043#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020044#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020045#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020046#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020047#include <haproxy/time.h>
Willy Tarreauba6300e2021-05-08 14:09:40 +020048#include <haproxy/tools.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020049#include <haproxy/xxhash.h>
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +010050#include <haproxy/event_hdl.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020051
Baptiste Assmannda29fe22019-06-13 13:24:29 +020052
Willy Tarreau3ff577e2018-08-02 11:48:52 +020053static void srv_update_status(struct server *s);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010054static int srv_apply_lastaddr(struct server *srv, int *err_code);
William Dauchy6318d332020-05-02 21:52:36 +020055static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010057/* extra keywords used as value for other arguments. They are used as
58 * suggestions for mistyped words.
Willy Tarreau49c2b452021-03-12 09:58:04 +010059 */
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010060static const char *extra_kw_list[] = {
61 "ipv4", "ipv6", "legacy", "octet-count",
Amaury Denoyelle3b89c112021-03-12 16:04:00 +010062 "fail-check", "sudden-death", "mark-down",
Willy Tarreau49c2b452021-03-12 09:58:04 +010063 NULL /* must be last */
64};
65
Willy Tarreau21faa912012-10-10 08:27:36 +020066/* List head of all known server keywords */
Willy Tarreauca1acd62022-03-29 15:02:44 +020067struct srv_kw_list srv_keywords = {
Willy Tarreau21faa912012-10-10 08:27:36 +020068 .list = LIST_HEAD_INIT(srv_keywords.list)
69};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020070
Willy Tarreauaf613e82020-06-05 08:40:51 +020071__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010072struct eb_root idle_conn_srv = EB_ROOT;
Willy Tarreau14015b82021-04-10 17:33:15 +020073struct task *idle_conn_task __read_mostly = NULL;
Willy Tarreau198e92a2021-03-05 10:23:32 +010074struct list servers_list = LIST_HEAD_INIT(servers_list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010075
Aurelien DARRAGON61e38942022-11-17 16:10:35 +010076/* SERVER DELETE(n)->ADD global tracker:
77 * This is meant to provide srv->rid (revision id) value.
78 * Revision id allows to differentiate between a previously existing
79 * deleted server and a new server reusing deleted server name/id.
80 *
81 * start value is 0 (even value)
82 * LSB is used to specify that one or multiple srv delete in a row
83 * were performed.
84 * When adding a new server, increment by 1 if current
85 * value is odd (odd = LSB set),
86 * because adding a new server after one or
87 * multiple deletions means we could potentially be reusing old names:
88 * Increase the revision id to prevent mixups between old and new names.
89 *
90 * srv->rid is calculated from cnt even values only.
91 * sizeof(srv_id_reuse_cnt) must be twice sizeof(srv->rid)
92 *
93 * Wraparound is expected and should not cause issues
94 * (with current design we allow up to 4 billion unique revisions)
95 *
96 * Counter is only used under thread_isolate (cli_add/cli_del),
97 * no need for atomic ops.
98 */
99static uint64_t srv_id_reuse_cnt = 0;
100
Frédéric Lécaille7da71292019-05-20 09:47:07 +0200101/* The server names dictionary */
Thayne McCombs92149f92020-11-20 01:28:26 -0700102struct dict server_key_dict = {
103 .name = "server keys",
Frédéric Lécaille7da71292019-05-20 09:47:07 +0200104 .values = EB_ROOT_UNIQUE,
105};
106
Simon Hormana3608442013-11-01 16:46:15 +0900107int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +0200108{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +0100109 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200110 return s->down_time;
111
112 return now.tv_sec - s->last_change + s->down_time;
113}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500115int srv_lastsession(const struct server *s)
116{
117 if (s->counters.last_sess)
118 return now.tv_sec - s->counters.last_sess;
119
120 return -1;
121}
122
Simon Horman4a741432013-02-23 15:35:38 +0900123int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +0200124{
Simon Horman4a741432013-02-23 15:35:38 +0900125 const struct server *s = check->server;
126
Aurelien DARRAGON7d541a92022-12-07 12:17:24 +0100127 if ((check->state & (CHK_ST_CONFIGURED|CHK_ST_FASTINTER)) == CHK_ST_CONFIGURED &&
128 (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900129 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100130
Emeric Brun52a91d32017-08-31 14:41:55 +0200131 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900132 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100133
Simon Horman4a741432013-02-23 15:35:38 +0900134 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100135}
136
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137/*
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100138 * Use this to publish EVENT_HDL_SUB_SERVER family type event
139 * from srv facility
140 * Event will be published in both global subscription list and
141 * server dedicated subscription list
142 * server ptr must be valid
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100143 * must be called with srv lock or under thread_isolate
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100144 */
145static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate)
146{
147 struct event_hdl_cb_data_server cb_data;
148
149 /* safe data assignments */
150 cb_data.safe.puid = srv->puid;
151 cb_data.safe.rid = srv->rid;
152 cb_data.safe.flags = srv->flags;
153 snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id);
154 if (srv->proxy)
155 snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id);
156 /* unsafe data assignments */
157 cb_data.unsafe.ptr = srv;
158 cb_data.unsafe.thread_isolate = thread_isolate;
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +0100159 cb_data.unsafe.srv_lock = !thread_isolate;
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100160 /* publish in server dedicated sub list */
161 event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data));
162 /* publish in global subscription list */
163 event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data));
164}
165
166/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100167 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200168 * Unlikely, but it can happen. The server's proxy must be at least
169 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100170 */
171static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100172{
173 struct proxy *p = s->proxy;
174 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100175
176 for (tmpserv = p->srv; tmpserv != NULL;
177 tmpserv = tmpserv->next) {
178 if (tmpserv == s)
179 continue;
180 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
181 continue;
182 if (tmpserv->cookie &&
183 strcmp(tmpserv->cookie, s->cookie) == 0) {
184 ha_warning("We generated two equal cookies for two different servers.\n"
185 "Please change the secret key for '%s'.\n",
186 s->proxy->id);
187 }
188 }
189
190}
191
Willy Tarreau46b7f532018-08-21 11:54:26 +0200192/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200193 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200194 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100195void srv_set_dyncookie(struct server *s)
196{
197 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100198 char *tmpbuf;
199 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100200 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100201 size_t buffer_len;
202 int addr_len;
203 int port;
204
Willy Tarreau595e7672020-10-20 17:30:08 +0200205 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200206
Olivier Houchard4e694042017-03-14 20:01:29 +0100207 if ((s->flags & SRV_F_COOKIESET) ||
208 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
209 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200210 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100211 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100212
213 if (s->addr.ss_family != AF_INET &&
214 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200215 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100216 /*
217 * Buffer to calculate the cookie value.
218 * The buffer contains the secret key + the server IP address
219 * + the TCP port.
220 */
221 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
222 /*
223 * The TCP port should use only 2 bytes, but is stored in
224 * an unsigned int in struct server, so let's use 4, to be
225 * on the safe side.
226 */
227 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200228 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100229 memcpy(tmpbuf, p->dyncookie_key, key_len);
230 memcpy(&(tmpbuf[key_len]),
231 s->addr.ss_family == AF_INET ?
232 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
233 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
234 addr_len);
235 /*
236 * Make sure it's the same across all the load balancers,
237 * no matter their endianness.
238 */
239 port = htonl(s->svc_port);
240 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
241 hash_value = XXH64(tmpbuf, buffer_len, 0);
242 memprintf(&s->cookie, "%016llx", hash_value);
243 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200244 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100245 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100246
247 /* Don't bother checking if the dyncookie is duplicated if
248 * the server is marked as "disabled", maybe it doesn't have
249 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100250 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100251 if (!(s->next_admin & SRV_ADMF_FMAINT))
252 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200253 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200254 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100255}
256
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200257/* Returns true if it's possible to reuse an idle connection from server <srv>
258 * for a websocket stream. This is the case if server is configured to use the
259 * same protocol for both HTTP and websocket streams. This depends on the value
260 * of "proto", "alpn" and "ws" keywords.
261 */
262int srv_check_reuse_ws(struct server *srv)
263{
264 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
265 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
266 * for mux selection.
267 */
268 const struct ist srv_mux = srv->mux_proto ?
269 srv->mux_proto->token : IST_NULL;
270
271 switch (srv->ws) {
272 /* "auto" means use the same protocol : reuse is possible. */
273 case SRV_WS_AUTO:
274 return 1;
275
276 /* "h2" means use h2 for websocket : reuse is possible if
277 * server mux is h2.
278 */
279 case SRV_WS_H2:
280 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
281 return 1;
282 break;
283
284 /* "h1" means use h1 for websocket : reuse is possible if
285 * server mux is h1.
286 */
287 case SRV_WS_H1:
288 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
289 return 1;
290 break;
291 }
292 }
293 else {
294 /* ALPN selection.
295 * Based on the assumption that only "h2" and "http/1.1" token
296 * are used on server ALPN.
297 */
298 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
299 srv->ssl_ctx.alpn_len);
300
301 switch (srv->ws) {
302 case SRV_WS_AUTO:
303 /* for auto mode, consider reuse as possible if the
304 * server uses a single protocol ALPN
305 */
306 if (!istchr(alpn, ','))
307 return 1;
308 break;
309
310 case SRV_WS_H2:
311 return isteq(alpn, ist("\x02h2"));
312
313 case SRV_WS_H1:
314 return isteq(alpn, ist("\x08http/1.1"));
315 }
316 }
317
318 return 0;
319}
320
321/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
322 * is a valid value indicating to use the fallback mux.
323 */
324const struct mux_ops *srv_get_ws_proto(struct server *srv)
325{
326 const struct mux_proto_list *mux = NULL;
327
328 switch (srv->ws) {
329 case SRV_WS_AUTO:
330 mux = srv->mux_proto;
331 break;
332
333 case SRV_WS_H1:
334 mux = get_mux_proto(ist("h1"));
335 break;
336
337 case SRV_WS_H2:
338 mux = get_mux_proto(ist("h2"));
339 break;
340 }
341
342 return mux ? mux->mux : NULL;
343}
344
Willy Tarreau21faa912012-10-10 08:27:36 +0200345/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200346 * Must be called with the server lock held. The server is first removed from
347 * the proxy tree if it was already attached. If <reattach> is true, the server
348 * will then be attached in the proxy tree. The proxy lock is held to
349 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700350 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200351static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700352{
353 struct proxy *p = s->proxy;
354 char *key;
355
356 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
357
358 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700359 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700360 free(key);
361 return;
362 }
363
364 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
365 ebpt_delete(&s->addr_node);
366 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
367
368 free(s->addr_node.key);
369 }
370
371 s->addr_node.key = key;
372
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200373 if (reattach) {
374 if (s->addr_node.key) {
375 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
376 ebis_insert(&p->used_server_addr, &s->addr_node);
377 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
378 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700379 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700380}
381
382/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200383 * Registers the server keyword list <kwl> as a list of valid keywords for next
384 * parsing sessions.
385 */
386void srv_register_keywords(struct srv_kw_list *kwl)
387{
Willy Tarreau2b718102021-04-21 07:32:39 +0200388 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200389}
390
391/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
392 * keyword is found with a NULL ->parse() function, then an attempt is made to
393 * find one with a valid ->parse() function. This way it is possible to declare
394 * platform-dependant, known keywords as NULL, then only declare them as valid
395 * if some options are met. Note that if the requested keyword contains an
396 * opening parenthesis, everything from this point is ignored.
397 */
398struct srv_kw *srv_find_kw(const char *kw)
399{
400 int index;
401 const char *kwend;
402 struct srv_kw_list *kwl;
403 struct srv_kw *ret = NULL;
404
405 kwend = strchr(kw, '(');
406 if (!kwend)
407 kwend = kw + strlen(kw);
408
409 list_for_each_entry(kwl, &srv_keywords.list, list) {
410 for (index = 0; kwl->kw[index].kw != NULL; index++) {
411 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
412 kwl->kw[index].kw[kwend-kw] == 0) {
413 if (kwl->kw[index].parse)
414 return &kwl->kw[index]; /* found it !*/
415 else
416 ret = &kwl->kw[index]; /* may be OK */
417 }
418 }
419 }
420 return ret;
421}
422
423/* Dumps all registered "server" keywords to the <out> string pointer. The
424 * unsupported keywords are only dumped if their supported form was not
425 * found.
426 */
427void srv_dump_kws(char **out)
428{
429 struct srv_kw_list *kwl;
430 int index;
431
Christopher Faulet784063e2020-05-18 12:14:18 +0200432 if (!out)
433 return;
434
Willy Tarreau21faa912012-10-10 08:27:36 +0200435 *out = NULL;
436 list_for_each_entry(kwl, &srv_keywords.list, list) {
437 for (index = 0; kwl->kw[index].kw != NULL; index++) {
438 if (kwl->kw[index].parse ||
439 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
440 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
441 kwl->scope,
442 kwl->kw[index].kw,
443 kwl->kw[index].skip ? " <arg>" : "",
444 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
445 kwl->kw[index].parse ? "" : " (not supported)");
446 }
447 }
448 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100449}
450
451/* Try to find in srv_keyword the word that looks closest to <word> by counting
452 * transitions between letters, digits and other characters. Will return the
453 * best matching word if found, otherwise NULL. An optional array of extra
454 * words to compare may be passed in <extra>, but it must then be terminated
455 * by a NULL entry. If unused it may be NULL.
456 */
457static const char *srv_find_best_kw(const char *word)
458{
459 uint8_t word_sig[1024];
460 uint8_t list_sig[1024];
461 const struct srv_kw_list *kwl;
462 const char *best_ptr = NULL;
463 int dist, best_dist = INT_MAX;
464 const char **extra;
465 int index;
466
467 make_word_fingerprint(word_sig, word);
468 list_for_each_entry(kwl, &srv_keywords.list, list) {
469 for (index = 0; kwl->kw[index].kw != NULL; index++) {
470 make_word_fingerprint(list_sig, kwl->kw[index].kw);
471 dist = word_fingerprint_distance(word_sig, list_sig);
472 if (dist < best_dist) {
473 best_dist = dist;
474 best_ptr = kwl->kw[index].kw;
475 }
476 }
477 }
478
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100479 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100480 make_word_fingerprint(list_sig, *extra);
481 dist = word_fingerprint_distance(word_sig, list_sig);
482 if (dist < best_dist) {
483 best_dist = dist;
484 best_ptr = *extra;
485 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100486 }
487
488 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
489 best_ptr = NULL;
490
491 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200492}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100493
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100494/* Parse the "backup" server keyword */
495static int srv_parse_backup(char **args, int *cur_arg,
496 struct proxy *curproxy, struct server *newsrv, char **err)
497{
498 newsrv->flags |= SRV_F_BACKUP;
499 return 0;
500}
501
Alexander Liu2a54bb72019-05-22 19:44:48 +0800502
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100503/* Parse the "cookie" server keyword */
504static int srv_parse_cookie(char **args, int *cur_arg,
505 struct proxy *curproxy, struct server *newsrv, char **err)
506{
507 char *arg;
508
509 arg = args[*cur_arg + 1];
510 if (!*arg) {
511 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
512 return ERR_ALERT | ERR_FATAL;
513 }
514
515 free(newsrv->cookie);
516 newsrv->cookie = strdup(arg);
517 newsrv->cklen = strlen(arg);
518 newsrv->flags |= SRV_F_COOKIESET;
519 return 0;
520}
521
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100522/* Parse the "disabled" server keyword */
523static int srv_parse_disabled(char **args, int *cur_arg,
524 struct proxy *curproxy, struct server *newsrv, char **err)
525{
Emeric Brun52a91d32017-08-31 14:41:55 +0200526 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
527 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100528 newsrv->check.state |= CHK_ST_PAUSED;
529 newsrv->check.health = 0;
530 return 0;
531}
532
533/* Parse the "enabled" server keyword */
534static int srv_parse_enabled(char **args, int *cur_arg,
535 struct proxy *curproxy, struct server *newsrv, char **err)
536{
Emeric Brun52a91d32017-08-31 14:41:55 +0200537 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
538 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100539 newsrv->check.state &= ~CHK_ST_PAUSED;
540 newsrv->check.health = newsrv->check.rise;
541 return 0;
542}
543
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100544/* Parse the "error-limit" server keyword */
545static int srv_parse_error_limit(char **args, int *cur_arg,
546 struct proxy *curproxy, struct server *newsrv, char **err)
547{
548 if (!*args[*cur_arg + 1]) {
549 memprintf(err, "'%s' expects an integer argument.",
550 args[*cur_arg]);
551 return ERR_ALERT | ERR_FATAL;
552 }
553
554 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
555
556 if (newsrv->consecutive_errors_limit <= 0) {
557 memprintf(err, "%s has to be > 0.",
558 args[*cur_arg]);
559 return ERR_ALERT | ERR_FATAL;
560 }
561
562 return 0;
563}
564
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200565/* Parse the "ws" keyword */
566static int srv_parse_ws(char **args, int *cur_arg,
567 struct proxy *curproxy, struct server *newsrv, char **err)
568{
569 if (!args[*cur_arg + 1]) {
570 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
571 return ERR_ALERT | ERR_FATAL;
572 }
573
574 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
575 newsrv->ws = SRV_WS_H1;
576 }
577 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
578 newsrv->ws = SRV_WS_H2;
579 }
580 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
581 newsrv->ws = SRV_WS_AUTO;
582 }
583 else {
584 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
585 return ERR_ALERT | ERR_FATAL;
586 }
587
588
589 return 0;
590}
591
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100592/* Parse the "init-addr" server keyword */
593static int srv_parse_init_addr(char **args, int *cur_arg,
594 struct proxy *curproxy, struct server *newsrv, char **err)
595{
596 char *p, *end;
597 int done;
598 struct sockaddr_storage sa;
599
600 newsrv->init_addr_methods = 0;
601 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
602
603 for (p = args[*cur_arg + 1]; *p; p = end) {
604 /* cut on next comma */
605 for (end = p; *end && *end != ','; end++);
606 if (*end)
607 *(end++) = 0;
608
609 memset(&sa, 0, sizeof(sa));
610 if (strcmp(p, "libc") == 0) {
611 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
612 }
613 else if (strcmp(p, "last") == 0) {
614 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
615 }
616 else if (strcmp(p, "none") == 0) {
617 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
618 }
619 else if (str2ip2(p, &sa, 0)) {
620 if (is_addr(&newsrv->init_addr)) {
621 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
622 args[*cur_arg], p);
623 return ERR_ALERT | ERR_FATAL;
624 }
625 newsrv->init_addr = sa;
626 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
627 }
628 else {
629 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
630 args[*cur_arg], p);
631 return ERR_ALERT | ERR_FATAL;
632 }
633 if (!done) {
634 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
635 args[*cur_arg], p);
636 return ERR_ALERT | ERR_FATAL;
637 }
638 }
639
640 return 0;
641}
642
643/* Parse the "log-proto" server keyword */
644static int srv_parse_log_proto(char **args, int *cur_arg,
645 struct proxy *curproxy, struct server *newsrv, char **err)
646{
647 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
648 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
649 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
650 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
651 else {
652 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
653 args[*cur_arg], args[*cur_arg + 1]);
654 return ERR_ALERT | ERR_FATAL;
655 }
656
657 return 0;
658}
659
660/* Parse the "maxconn" server keyword */
661static int srv_parse_maxconn(char **args, int *cur_arg,
662 struct proxy *curproxy, struct server *newsrv, char **err)
663{
664 newsrv->maxconn = atol(args[*cur_arg + 1]);
665 return 0;
666}
667
668/* Parse the "maxqueue" server keyword */
669static int srv_parse_maxqueue(char **args, int *cur_arg,
670 struct proxy *curproxy, struct server *newsrv, char **err)
671{
672 newsrv->maxqueue = atol(args[*cur_arg + 1]);
673 return 0;
674}
675
676/* Parse the "minconn" server keyword */
677static int srv_parse_minconn(char **args, int *cur_arg,
678 struct proxy *curproxy, struct server *newsrv, char **err)
679{
680 newsrv->minconn = atol(args[*cur_arg + 1]);
681 return 0;
682}
683
Willy Tarreau9c538e02019-01-23 10:21:49 +0100684static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
685{
686 char *arg;
687
688 arg = args[*cur_arg + 1];
689 if (!*arg) {
690 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
691 return ERR_ALERT | ERR_FATAL;
692 }
693 newsrv->max_reuse = atoi(arg);
694
695 return 0;
696}
697
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100698static int srv_parse_pool_purge_delay(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100699{
700 const char *res;
701 char *arg;
702 unsigned int time;
703
704 arg = args[*cur_arg + 1];
705 if (!*arg) {
706 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
707 return ERR_ALERT | ERR_FATAL;
708 }
709 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200710 if (res == PARSE_TIME_OVER) {
711 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
712 args[*cur_arg+1], args[*cur_arg]);
713 return ERR_ALERT | ERR_FATAL;
714 }
715 else if (res == PARSE_TIME_UNDER) {
716 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
717 args[*cur_arg+1], args[*cur_arg]);
718 return ERR_ALERT | ERR_FATAL;
719 }
720 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100721 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
722 *res, args[*cur_arg]);
723 return ERR_ALERT | ERR_FATAL;
724 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100725 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100726
727 return 0;
728}
729
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200730static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
731{
732 char *arg;
733
734 arg = args[*cur_arg + 1];
735 if (!*arg) {
736 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
737 return ERR_ALERT | ERR_FATAL;
738 }
739
740 newsrv->low_idle_conns = atoi(arg);
741 return 0;
742}
743
Olivier Houchard006e3102018-12-10 18:30:32 +0100744static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
745{
746 char *arg;
747
748 arg = args[*cur_arg + 1];
749 if (!*arg) {
750 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
751 return ERR_ALERT | ERR_FATAL;
752 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100753
Olivier Houchard006e3102018-12-10 18:30:32 +0100754 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100755 if ((int)newsrv->max_idle_conns < -1) {
756 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
757 return ERR_ALERT | ERR_FATAL;
758 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100759
760 return 0;
761}
762
Willy Tarreaudff55432012-10-10 17:51:05 +0200763/* parse the "id" server keyword */
764static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
765{
766 struct eb32_node *node;
767
768 if (!*args[*cur_arg + 1]) {
769 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
770 return ERR_ALERT | ERR_FATAL;
771 }
772
773 newsrv->puid = atol(args[*cur_arg + 1]);
774 newsrv->conf.id.key = newsrv->puid;
775
776 if (newsrv->puid <= 0) {
777 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
778 return ERR_ALERT | ERR_FATAL;
779 }
780
781 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
782 if (node) {
783 struct server *target = container_of(node, struct server, conf.id);
784 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
785 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
786 target->id);
787 return ERR_ALERT | ERR_FATAL;
788 }
789
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200790 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200791 return 0;
792}
793
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100794/* Parse the "namespace" server keyword */
795static int srv_parse_namespace(char **args, int *cur_arg,
796 struct proxy *curproxy, struct server *newsrv, char **err)
797{
Willy Tarreaue5733232019-05-22 19:24:06 +0200798#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100799 char *arg;
800
801 arg = args[*cur_arg + 1];
802 if (!*arg) {
803 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
804 return ERR_ALERT | ERR_FATAL;
805 }
806
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100807 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100808 /* Use the namespace associated with the connection (if present). */
809 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
810 return 0;
811 }
812
813 /*
814 * As this parser may be called several times for the same 'default-server'
815 * object, or for a new 'server' instance deriving from a 'default-server'
816 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
817 */
818 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
819
820 newsrv->netns = netns_store_lookup(arg, strlen(arg));
821 if (!newsrv->netns)
822 newsrv->netns = netns_store_insert(arg);
823
824 if (!newsrv->netns) {
825 memprintf(err, "Cannot open namespace '%s'", arg);
826 return ERR_ALERT | ERR_FATAL;
827 }
828
829 return 0;
830#else
831 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
832 return ERR_ALERT | ERR_FATAL;
833#endif
834}
835
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100836/* Parse the "no-backup" server keyword */
837static int srv_parse_no_backup(char **args, int *cur_arg,
838 struct proxy *curproxy, struct server *newsrv, char **err)
839{
840 newsrv->flags &= ~SRV_F_BACKUP;
841 return 0;
842}
843
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100844
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100845/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200846static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100847{
848 srv->pp_opts &= ~flags;
849 return 0;
850}
851
852/* Parse the "no-send-proxy" server keyword */
853static int srv_parse_no_send_proxy(char **args, int *cur_arg,
854 struct proxy *curproxy, struct server *newsrv, char **err)
855{
856 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
857}
858
859/* Parse the "no-send-proxy-v2" server keyword */
860static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
861 struct proxy *curproxy, struct server *newsrv, char **err)
862{
863 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
864}
865
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200866/* Parse the "shard" server keyword */
867static int srv_parse_shard(char **args, int *cur_arg,
868 struct proxy *curproxy, struct server *newsrv, char **err)
869{
870 newsrv->shard = atol(args[*cur_arg + 1]);
871 return 0;
872}
873
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200874/* Parse the "no-tfo" server keyword */
875static int srv_parse_no_tfo(char **args, int *cur_arg,
876 struct proxy *curproxy, struct server *newsrv, char **err)
877{
878 newsrv->flags &= ~SRV_F_FASTOPEN;
879 return 0;
880}
881
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100882/* Parse the "non-stick" server keyword */
883static int srv_parse_non_stick(char **args, int *cur_arg,
884 struct proxy *curproxy, struct server *newsrv, char **err)
885{
886 newsrv->flags |= SRV_F_NON_STICK;
887 return 0;
888}
889
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100890/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200891static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100892{
893 srv->pp_opts |= flags;
894 return 0;
895}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200896/* parse the "proto" server keyword */
897static int srv_parse_proto(char **args, int *cur_arg,
898 struct proxy *px, struct server *newsrv, char **err)
899{
900 struct ist proto;
901
902 if (!*args[*cur_arg + 1]) {
903 memprintf(err, "'%s' : missing value", args[*cur_arg]);
904 return ERR_ALERT | ERR_FATAL;
905 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100906 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200907 newsrv->mux_proto = get_mux_proto(proto);
908 if (!newsrv->mux_proto) {
909 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
910 return ERR_ALERT | ERR_FATAL;
911 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200912 return 0;
913}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100914
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100915/* parse the "proxy-v2-options" */
916static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
917 struct proxy *px, struct server *newsrv, char **err)
918{
919 char *p, *n;
920 for (p = args[*cur_arg+1]; p; p = n) {
921 n = strchr(p, ',');
922 if (n)
923 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100924 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100925 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100926 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100927 newsrv->pp_opts |= SRV_PP_V2_SSL;
928 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100929 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100930 newsrv->pp_opts |= SRV_PP_V2_SSL;
931 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100932 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100933 newsrv->pp_opts |= SRV_PP_V2_SSL;
934 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100935 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100936 newsrv->pp_opts |= SRV_PP_V2_SSL;
937 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100938 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100939 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100940 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100941 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100942 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100943 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100944 } else
945 goto fail;
946 }
947 return 0;
948 fail:
949 if (err)
950 memprintf(err, "'%s' : proxy v2 option not implemented", p);
951 return ERR_ALERT | ERR_FATAL;
952}
953
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100954/* Parse the "observe" server keyword */
955static int srv_parse_observe(char **args, int *cur_arg,
956 struct proxy *curproxy, struct server *newsrv, char **err)
957{
958 char *arg;
959
960 arg = args[*cur_arg + 1];
961 if (!*arg) {
962 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
963 return ERR_ALERT | ERR_FATAL;
964 }
965
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100966 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100967 newsrv->observe = HANA_OBS_NONE;
968 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100969 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100970 newsrv->observe = HANA_OBS_LAYER4;
971 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100972 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100973 if (curproxy->mode != PR_MODE_HTTP) {
974 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
975 return ERR_ALERT;
976 }
977 newsrv->observe = HANA_OBS_LAYER7;
978 }
979 else {
980 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
981 "but got '%s'\n", args[*cur_arg], arg);
982 return ERR_ALERT | ERR_FATAL;
983 }
984
985 return 0;
986}
987
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100988/* Parse the "on-error" server keyword */
989static int srv_parse_on_error(char **args, int *cur_arg,
990 struct proxy *curproxy, struct server *newsrv, char **err)
991{
992 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
993 newsrv->onerror = HANA_ONERR_FASTINTER;
994 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
995 newsrv->onerror = HANA_ONERR_FAILCHK;
996 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
997 newsrv->onerror = HANA_ONERR_SUDDTH;
998 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
999 newsrv->onerror = HANA_ONERR_MARKDWN;
1000 else {
1001 memprintf(err, "'%s' expects one of 'fastinter', "
1002 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1003 args[*cur_arg], args[*cur_arg + 1]);
1004 return ERR_ALERT | ERR_FATAL;
1005 }
1006
1007 return 0;
1008}
1009
1010/* Parse the "on-marked-down" server keyword */
1011static int srv_parse_on_marked_down(char **args, int *cur_arg,
1012 struct proxy *curproxy, struct server *newsrv, char **err)
1013{
1014 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1015 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1016 else {
1017 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1018 args[*cur_arg], args[*cur_arg + 1]);
1019 return ERR_ALERT | ERR_FATAL;
1020 }
1021
1022 return 0;
1023}
1024
1025/* Parse the "on-marked-up" server keyword */
1026static int srv_parse_on_marked_up(char **args, int *cur_arg,
1027 struct proxy *curproxy, struct server *newsrv, char **err)
1028{
1029 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1030 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1031 else {
1032 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1033 args[*cur_arg], args[*cur_arg + 1]);
1034 return ERR_ALERT | ERR_FATAL;
1035 }
1036
1037 return 0;
1038}
1039
Frédéric Lécaille16186232017-03-14 16:42:49 +01001040/* Parse the "redir" server keyword */
1041static int srv_parse_redir(char **args, int *cur_arg,
1042 struct proxy *curproxy, struct server *newsrv, char **err)
1043{
1044 char *arg;
1045
1046 arg = args[*cur_arg + 1];
1047 if (!*arg) {
1048 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1049 return ERR_ALERT | ERR_FATAL;
1050 }
1051
1052 free(newsrv->rdr_pfx);
1053 newsrv->rdr_pfx = strdup(arg);
1054 newsrv->rdr_len = strlen(arg);
1055
1056 return 0;
1057}
1058
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001059/* Parse the "resolvers" server keyword */
1060static int srv_parse_resolvers(char **args, int *cur_arg,
1061 struct proxy *curproxy, struct server *newsrv, char **err)
1062{
1063 free(newsrv->resolvers_id);
1064 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1065 return 0;
1066}
1067
1068/* Parse the "resolve-net" server keyword */
1069static int srv_parse_resolve_net(char **args, int *cur_arg,
1070 struct proxy *curproxy, struct server *newsrv, char **err)
1071{
1072 char *p, *e;
1073 unsigned char mask;
1074 struct resolv_options *opt;
1075
1076 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1077 memprintf(err, "'%s' expects a list of networks.",
1078 args[*cur_arg]);
1079 return ERR_ALERT | ERR_FATAL;
1080 }
1081
1082 opt = &newsrv->resolv_opts;
1083
1084 /* Split arguments by comma, and convert it from ipv4 or ipv6
1085 * string network in in_addr or in6_addr.
1086 */
1087 p = args[*cur_arg + 1];
1088 e = p;
1089 while (*p != '\0') {
1090 /* If no room available, return error. */
1091 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1092 memprintf(err, "'%s' exceed %d networks.",
1093 args[*cur_arg], SRV_MAX_PREF_NET);
1094 return ERR_ALERT | ERR_FATAL;
1095 }
1096 /* look for end or comma. */
1097 while (*e != ',' && *e != '\0')
1098 e++;
1099 if (*e == ',') {
1100 *e = '\0';
1101 e++;
1102 }
1103 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1104 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1105 /* Try to convert input string from ipv4 or ipv6 network. */
1106 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1107 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1108 &mask)) {
1109 /* Try to convert input string from ipv6 network. */
1110 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1111 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1112 } else {
1113 /* All network conversions fail, return error. */
1114 memprintf(err, "'%s' invalid network '%s'.",
1115 args[*cur_arg], p);
1116 return ERR_ALERT | ERR_FATAL;
1117 }
1118 opt->pref_net_nb++;
1119 p = e;
1120 }
1121
1122 return 0;
1123}
1124
1125/* Parse the "resolve-opts" server keyword */
1126static int srv_parse_resolve_opts(char **args, int *cur_arg,
1127 struct proxy *curproxy, struct server *newsrv, char **err)
1128{
1129 char *p, *end;
1130
1131 for (p = args[*cur_arg + 1]; *p; p = end) {
1132 /* cut on next comma */
1133 for (end = p; *end && *end != ','; end++);
1134 if (*end)
1135 *(end++) = 0;
1136
1137 if (strcmp(p, "allow-dup-ip") == 0) {
1138 newsrv->resolv_opts.accept_duplicate_ip = 1;
1139 }
1140 else if (strcmp(p, "ignore-weight") == 0) {
1141 newsrv->resolv_opts.ignore_weight = 1;
1142 }
1143 else if (strcmp(p, "prevent-dup-ip") == 0) {
1144 newsrv->resolv_opts.accept_duplicate_ip = 0;
1145 }
1146 else {
1147 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1148 args[*cur_arg], p);
1149 return ERR_ALERT | ERR_FATAL;
1150 }
1151 }
1152
1153 return 0;
1154}
1155
1156/* Parse the "resolve-prefer" server keyword */
1157static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1158 struct proxy *curproxy, struct server *newsrv, char **err)
1159{
1160 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1161 newsrv->resolv_opts.family_prio = AF_INET;
1162 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1163 newsrv->resolv_opts.family_prio = AF_INET6;
1164 else {
1165 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1166 args[*cur_arg]);
1167 return ERR_ALERT | ERR_FATAL;
1168 }
1169
1170 return 0;
1171}
1172
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001173/* Parse the "send-proxy" server keyword */
1174static int srv_parse_send_proxy(char **args, int *cur_arg,
1175 struct proxy *curproxy, struct server *newsrv, char **err)
1176{
1177 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1178}
1179
1180/* Parse the "send-proxy-v2" server keyword */
1181static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1182 struct proxy *curproxy, struct server *newsrv, char **err)
1183{
1184 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1185}
1186
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001187/* Parse the "slowstart" server keyword */
1188static int srv_parse_slowstart(char **args, int *cur_arg,
1189 struct proxy *curproxy, struct server *newsrv, char **err)
1190{
1191 /* slowstart is stored in seconds */
1192 unsigned int val;
1193 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1194
1195 if (time_err == PARSE_TIME_OVER) {
1196 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1197 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1198 return ERR_ALERT | ERR_FATAL;
1199 }
1200 else if (time_err == PARSE_TIME_UNDER) {
1201 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1202 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1203 return ERR_ALERT | ERR_FATAL;
1204 }
1205 else if (time_err) {
1206 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1207 *time_err, newsrv->id);
1208 return ERR_ALERT | ERR_FATAL;
1209 }
1210 newsrv->slowstart = (val + 999) / 1000;
1211
1212 return 0;
1213}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001214
1215/* Parse the "source" server keyword */
1216static int srv_parse_source(char **args, int *cur_arg,
1217 struct proxy *curproxy, struct server *newsrv, char **err)
1218{
1219 char *errmsg;
1220 int port_low, port_high;
1221 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001222
1223 errmsg = NULL;
1224
1225 if (!*args[*cur_arg + 1]) {
1226 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1227 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1228 goto err;
1229 }
1230
1231 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001232 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1233 &errmsg, NULL, NULL,
1234 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_RANGE | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001235 if (!sk) {
1236 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1237 goto err;
1238 }
1239
Frédéric Lécailledba97072017-03-17 15:33:50 +01001240 newsrv->conn_src.opts |= CO_SRC_BIND;
1241 newsrv->conn_src.source_addr = *sk;
1242
1243 if (port_low != port_high) {
1244 int i;
1245
Frédéric Lécailledba97072017-03-17 15:33:50 +01001246 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001247 if (!newsrv->conn_src.sport_range) {
1248 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1249 goto err;
1250 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001251 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1252 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1253 }
1254
1255 *cur_arg += 2;
1256 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001257 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001258#if defined(CONFIG_HAP_TRANSPARENT)
1259 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001260 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1261 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001262 goto err;
1263 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001264 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001265 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1266 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1267 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001268 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001269 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1270 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1271 }
1272 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1273 char *name, *end;
1274
1275 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001276 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001277 name++;
1278
1279 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001280 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001281 end++;
1282
1283 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1284 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1285 free(newsrv->conn_src.bind_hdr_name);
1286 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001287 if (!newsrv->conn_src.bind_hdr_name) {
1288 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1289 goto err;
1290 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001291 newsrv->conn_src.bind_hdr_len = end - name;
1292 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1293 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1294 newsrv->conn_src.bind_hdr_occ = -1;
1295
1296 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001297 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001298 end++;
1299 if (*end == ',') {
1300 end++;
1301 name = end;
1302 if (*end == '-')
1303 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001304 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001305 end++;
1306 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1307 }
1308
1309 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001310 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1311 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001312 goto err;
1313 }
1314 }
1315 else {
1316 struct sockaddr_storage *sk;
1317 int port1, port2;
1318
1319 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001320 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1321 &errmsg, NULL, NULL,
1322 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001323 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001324 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001325 goto err;
1326 }
1327
Frédéric Lécailledba97072017-03-17 15:33:50 +01001328 newsrv->conn_src.tproxy_addr = *sk;
1329 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1330 }
1331 global.last_checks |= LSTCHK_NETADM;
1332 *cur_arg += 2;
1333 continue;
1334#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001335 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001336 goto err;
1337#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1338 } /* "usesrc" */
1339
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001340 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001341#ifdef SO_BINDTODEVICE
1342 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001343 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001344 goto err;
1345 }
1346 free(newsrv->conn_src.iface_name);
1347 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1348 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1349 global.last_checks |= LSTCHK_NETADM;
1350#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001351 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001352 goto err;
1353#endif
1354 *cur_arg += 2;
1355 continue;
1356 }
1357 /* this keyword in not an option of "source" */
1358 break;
1359 } /* while */
1360
1361 return 0;
1362
1363 err:
1364 free(errmsg);
1365 return ERR_ALERT | ERR_FATAL;
1366}
1367
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001368/* Parse the "stick" server keyword */
1369static int srv_parse_stick(char **args, int *cur_arg,
1370 struct proxy *curproxy, struct server *newsrv, char **err)
1371{
1372 newsrv->flags &= ~SRV_F_NON_STICK;
1373 return 0;
1374}
1375
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001376/* Parse the "track" server keyword */
1377static int srv_parse_track(char **args, int *cur_arg,
1378 struct proxy *curproxy, struct server *newsrv, char **err)
1379{
1380 char *arg;
1381
1382 arg = args[*cur_arg + 1];
1383 if (!*arg) {
1384 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1385 return ERR_ALERT | ERR_FATAL;
1386 }
1387
1388 free(newsrv->trackit);
1389 newsrv->trackit = strdup(arg);
1390
1391 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001392}
1393
1394/* Parse the "socks4" server keyword */
1395static int srv_parse_socks4(char **args, int *cur_arg,
1396 struct proxy *curproxy, struct server *newsrv, char **err)
1397{
1398 char *errmsg;
1399 int port_low, port_high;
1400 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001401
1402 errmsg = NULL;
1403
1404 if (!*args[*cur_arg + 1]) {
1405 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1406 goto err;
1407 }
1408
1409 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001410 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1411 &errmsg, NULL, NULL,
1412 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001413 if (!sk) {
1414 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1415 goto err;
1416 }
1417
Alexander Liu2a54bb72019-05-22 19:44:48 +08001418 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1419 newsrv->socks4_addr = *sk;
1420
Alexander Liu2a54bb72019-05-22 19:44:48 +08001421 return 0;
1422
1423 err:
1424 free(errmsg);
1425 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001426}
1427
Frédéric Lécailledba97072017-03-17 15:33:50 +01001428
Willy Tarreau034c88c2017-01-23 23:36:45 +01001429/* parse the "tfo" server keyword */
1430static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1431{
1432 newsrv->flags |= SRV_F_FASTOPEN;
1433 return 0;
1434}
1435
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001436/* parse the "usesrc" server keyword */
1437static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1438{
1439 memprintf(err, "'%s' only allowed after a '%s' statement.",
1440 "usesrc", "source");
1441 return ERR_ALERT | ERR_FATAL;
1442}
1443
1444/* parse the "weight" server keyword */
1445static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1446{
1447 int w;
1448
1449 w = atol(args[*cur_arg + 1]);
1450 if (w < 0 || w > SRV_UWGHT_MAX) {
1451 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1452 newsrv->id, SRV_UWGHT_MAX, w);
1453 return ERR_ALERT | ERR_FATAL;
1454 }
1455 newsrv->uweight = newsrv->iweight = w;
1456
1457 return 0;
1458}
1459
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001460/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001461 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001462 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001463 *
1464 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001465 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001466void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001467{
Willy Tarreau751153e2021-02-17 13:33:24 +01001468 struct stream *stream;
1469 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001470 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001471
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001472 for (thr = 0; thr < global.nbthread; thr++)
1473 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1474 if (stream->srv_conn == srv)
1475 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001476}
1477
1478/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001479 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001480 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001481 *
1482 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001483 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001484void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001485{
1486 struct server *srv;
1487
1488 for (srv = px->srv; srv != NULL; srv = srv->next)
1489 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001490 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001491}
1492
Willy Tarreaubda92272014-05-20 21:55:30 +02001493/* Appends some information to a message string related to a server going UP or
1494 * DOWN. If both <forced> and <reason> are null and the server tracks another
1495 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001496 * If <check> is non-null, an entire string describing the check result will be
1497 * appended after a comma and a space (eg: to report some information from the
1498 * check that changed the state). In the other case, the string will be built
1499 * using the check results stored into the struct server if present.
1500 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001501 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001502 *
1503 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001504 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001505void srv_append_status(struct buffer *msg, struct server *s,
1506 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001507{
Emeric Brun5a133512017-10-19 14:42:30 +02001508 short status = s->op_st_chg.status;
1509 short code = s->op_st_chg.code;
1510 long duration = s->op_st_chg.duration;
1511 char *desc = s->op_st_chg.reason;
1512
1513 if (check) {
1514 status = check->status;
1515 code = check->code;
1516 duration = check->duration;
1517 desc = check->desc;
1518 }
1519
1520 if (status != -1) {
1521 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1522
1523 if (status >= HCHK_STATUS_L57DATA)
1524 chunk_appendf(msg, ", code: %d", code);
1525
1526 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001527 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001528
1529 chunk_appendf(msg, ", info: \"");
1530
1531 chunk_initlen(&src, desc, 0, strlen(desc));
1532 chunk_asciiencode(msg, &src, '"');
1533
1534 chunk_appendf(msg, "\"");
1535 }
1536
1537 if (duration >= 0)
1538 chunk_appendf(msg, ", check duration: %ldms", duration);
1539 }
1540 else if (desc && *desc) {
1541 chunk_appendf(msg, ", %s", desc);
1542 }
1543 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001544 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001545 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001546
1547 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001548 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001549 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1550 " %d sessions active, %d requeued, %d remaining in queue",
1551 s->proxy->srv_act, s->proxy->srv_bck,
1552 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001553 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001554 else
1555 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1556 " %d sessions requeued, %d total in queue",
1557 s->proxy->srv_act, s->proxy->srv_bck,
1558 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001559 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001560 }
1561}
1562
Emeric Brun5a133512017-10-19 14:42:30 +02001563/* Marks server <s> down, regardless of its checks' statuses. The server is
1564 * registered in a list to postpone the counting of the remaining servers on
1565 * the proxy and transfers queued streams whenever possible to other servers at
1566 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1567 * non-null as the reason for going down or the available data from the check
1568 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001569 *
1570 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001571 */
Emeric Brun5a133512017-10-19 14:42:30 +02001572void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001573{
1574 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001575
Emeric Brun64cc49c2017-10-03 14:46:45 +02001576 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001577 return;
1578
Emeric Brun52a91d32017-08-31 14:41:55 +02001579 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001580 *s->op_st_chg.reason = 0;
1581 s->op_st_chg.status = -1;
1582 if (reason) {
1583 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1584 }
1585 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001586 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001587 s->op_st_chg.code = check->code;
1588 s->op_st_chg.status = check->status;
1589 s->op_st_chg.duration = check->duration;
1590 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001591
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001592 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001593 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001594
Emeric Brun9f0b4582017-10-23 14:39:51 +02001595 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001596 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001597 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001598 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001599 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001600}
1601
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001602/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001603 * in maintenance. The server is registered in a list to postpone the counting
1604 * of the remaining servers on the proxy and tries to grab requests from the
1605 * proxy at a sync point. Maintenance servers are ignored. It stores the
1606 * <reason> if non-null as the reason for going down or the available data
1607 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001608 *
1609 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001610 */
Emeric Brun5a133512017-10-19 14:42:30 +02001611void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001612{
1613 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001614
Emeric Brun64cc49c2017-10-03 14:46:45 +02001615 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001616 return;
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001619 return;
1620
Emeric Brun52a91d32017-08-31 14:41:55 +02001621 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001622 *s->op_st_chg.reason = 0;
1623 s->op_st_chg.status = -1;
1624 if (reason) {
1625 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1626 }
1627 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001628 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001629 s->op_st_chg.code = check->code;
1630 s->op_st_chg.status = check->status;
1631 s->op_st_chg.duration = check->duration;
1632 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001633
Emeric Brun64cc49c2017-10-03 14:46:45 +02001634 if (s->slowstart <= 0)
1635 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001636
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001637 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001638 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001639
Emeric Brun9f0b4582017-10-23 14:39:51 +02001640 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001641 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001642 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001643 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001644 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001645}
1646
Willy Tarreau8eb77842014-05-21 13:54:57 +02001647/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001648 * isn't in maintenance. The server is registered in a list to postpone the
1649 * counting of the remaining servers on the proxy and tries to grab requests
1650 * from the proxy. Maintenance servers are ignored. It stores the
1651 * <reason> if non-null as the reason for going down or the available data
1652 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001653 * up. Note that it makes use of the trash to build the log strings, so <reason>
1654 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001655 *
1656 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001657 */
Emeric Brun5a133512017-10-19 14:42:30 +02001658void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001659{
1660 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001661
Emeric Brun64cc49c2017-10-03 14:46:45 +02001662 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001663 return;
1664
Emeric Brun52a91d32017-08-31 14:41:55 +02001665 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001666 return;
1667
Emeric Brun52a91d32017-08-31 14:41:55 +02001668 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001669 *s->op_st_chg.reason = 0;
1670 s->op_st_chg.status = -1;
1671 if (reason) {
1672 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1673 }
1674 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001675 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001676 s->op_st_chg.code = check->code;
1677 s->op_st_chg.status = check->status;
1678 s->op_st_chg.duration = check->duration;
1679 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001680
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001681 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001682 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001683
Emeric Brun9f0b4582017-10-23 14:39:51 +02001684 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001685 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001686 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001687 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001688 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001689}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001690
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001691/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1692 * enforce either maint mode or drain mode. It is not allowed to set more than
1693 * one flag at once. The equivalent "inherited" flag is propagated to all
1694 * tracking servers. Maintenance mode disables health checks (but not agent
1695 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001696 * is done. If <cause> is non-null, it will be displayed at the end of the log
1697 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001698 *
1699 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001700 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001701void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001702{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001703 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001704
1705 if (!mode)
1706 return;
1707
1708 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001710 return;
1711
Emeric Brun52a91d32017-08-31 14:41:55 +02001712 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001713 if (cause)
1714 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1715
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001716 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001717 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001718
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001719 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001720 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1721 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001722 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001723
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001724 /* compute the inherited flag to propagate */
1725 if (mode & SRV_ADMF_MAINT)
1726 mode = SRV_ADMF_IMAINT;
1727 else if (mode & SRV_ADMF_DRAIN)
1728 mode = SRV_ADMF_IDRAIN;
1729
Emeric Brun9f0b4582017-10-23 14:39:51 +02001730 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001731 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001732 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001733 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001734 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001735}
1736
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001737/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1738 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1739 * than one flag at once. The equivalent "inherited" flag is propagated to all
1740 * tracking servers. Leaving maintenance mode re-enables health checks. When
1741 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001742 *
1743 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001744 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001745void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001746{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001747 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001748
1749 if (!mode)
1750 return;
1751
1752 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001753 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001754 return;
1755
Emeric Brun52a91d32017-08-31 14:41:55 +02001756 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001757
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001758 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001759 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001760
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001761 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001762 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1763 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001764 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001765
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001766 if (mode & SRV_ADMF_MAINT)
1767 mode = SRV_ADMF_IMAINT;
1768 else if (mode & SRV_ADMF_DRAIN)
1769 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001770
Emeric Brun9f0b4582017-10-23 14:39:51 +02001771 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001772 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001773 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001774 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001775 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001776}
1777
Willy Tarreau757478e2016-11-03 19:22:19 +01001778/* principle: propagate maint and drain to tracking servers. This is useful
1779 * upon startup so that inherited states are correct.
1780 */
1781static void srv_propagate_admin_state(struct server *srv)
1782{
1783 struct server *srv2;
1784
1785 if (!srv->trackers)
1786 return;
1787
1788 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001789 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001790 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001791 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001792
Emeric Brun52a91d32017-08-31 14:41:55 +02001793 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001794 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001795 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001796 }
1797}
1798
1799/* Compute and propagate the admin states for all servers in proxy <px>.
1800 * Only servers *not* tracking another one are considered, because other
1801 * ones will be handled when the server they track is visited.
1802 */
1803void srv_compute_all_admin_states(struct proxy *px)
1804{
1805 struct server *srv;
1806
1807 for (srv = px->srv; srv; srv = srv->next) {
1808 if (srv->track)
1809 continue;
1810 srv_propagate_admin_state(srv);
1811 }
1812}
1813
Willy Tarreaudff55432012-10-10 17:51:05 +02001814/* Note: must not be declared <const> as its list will be overwritten.
1815 * Please take care of keeping this list alphabetically sorted, doing so helps
1816 * all code contributors.
1817 * Optional keywords are also declared with a NULL ->parse() function so that
1818 * the config parser can report an appropriate error when a known keyword was
1819 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001820 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001821 */
1822static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001823 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001824 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001825 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1826 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001827 { "error-limit", srv_parse_error_limit, 1, 1, 1 }, /* Configure the consecutive count of check failures to consider a server on error */
Amaury Denoyellef9d59572021-10-18 14:40:29 +02001828 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001829 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001830 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1831 { "log-proto", srv_parse_log_proto, 1, 1, 0 }, /* Set the protocol for event messages, only relevant in a ring section */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001832 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1833 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001834 { "max-reuse", srv_parse_max_reuse, 1, 1, 0 }, /* Set the max number of requests on a connection, -1 means unlimited */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001835 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001836 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001837 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1838 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1839 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1840 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001841 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001842 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1843 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1844 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1845 { "on-marked-up", srv_parse_on_marked_up, 1, 1, 1 }, /* Configure the action when a server is marked up */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001846 { "pool-low-conn", srv_parse_pool_low_conn, 1, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
1847 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1848 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Amaury Denoyelle30467232021-03-12 18:03:27 +01001849 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001850 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001851 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001852 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001853 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001854 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001855 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001856 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1857 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécaille36d15652022-10-17 14:58:19 +02001858 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001859 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001860 { "source", srv_parse_source, -1, 1, 1 }, /* Set the source address to be used to connect to the server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001861 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001862 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001863 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001864 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001865 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1866 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001867 { NULL, NULL, 0 },
1868}};
1869
Willy Tarreau0108d902018-11-25 19:14:37 +01001870INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001871
Willy Tarreau004e0452013-11-21 11:22:01 +01001872/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001873 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001874 * state is automatically disabled if the time is elapsed. If <must_update> is
1875 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001876 *
1877 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001878 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001879void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001880{
1881 struct proxy *px = sv->proxy;
1882 unsigned w;
1883
1884 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1885 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001886 if (sv->next_state == SRV_ST_STARTING)
1887 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001888 }
1889
1890 /* We must take care of not pushing the server to full throttle during slow starts.
1891 * It must also start immediately, at least at the minimal step when leaving maintenance.
1892 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001893 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001894 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1895 else
1896 w = px->lbprm.wdiv;
1897
Emeric Brun52a91d32017-08-31 14:41:55 +02001898 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001899
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001900 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001901 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001902 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001903}
1904
Willy Tarreaubaaee002006-06-26 02:48:02 +02001905/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001906 * Parses weight_str and configures sv accordingly.
1907 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001908 *
1909 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001910 */
1911const char *server_parse_weight_change_request(struct server *sv,
1912 const char *weight_str)
1913{
1914 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001915 long int w;
1916 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001917
1918 px = sv->proxy;
1919
1920 /* if the weight is terminated with '%', it is set relative to
1921 * the initial weight, otherwise it is absolute.
1922 */
1923 if (!*weight_str)
1924 return "Require <weight> or <weight%>.\n";
1925
Simon Hormanb796afa2013-02-12 10:45:53 +09001926 w = strtol(weight_str, &end, 10);
1927 if (end == weight_str)
1928 return "Empty weight string empty or preceded by garbage";
1929 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001930 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001931 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001932 /* Avoid integer overflow */
1933 if (w > 25600)
1934 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001935 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001936 if (w > 256)
1937 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001938 }
1939 else if (w < 0 || w > 256)
1940 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001941 else if (end[0] != '\0')
1942 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001943
1944 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1945 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1946
1947 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001948 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001949
1950 return NULL;
1951}
1952
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001953/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001954 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1955 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001956 * Returns:
1957 * - error string on error
1958 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001959 *
1960 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001961 */
1962const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001963 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001964{
1965 unsigned char ip[INET6_ADDRSTRLEN];
1966
1967 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001968 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001969 return NULL;
1970 }
1971 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001972 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001973 return NULL;
1974 }
1975
1976 return "Could not understand IP address format.\n";
1977}
1978
Willy Tarreau46b7f532018-08-21 11:54:26 +02001979/*
1980 * Must be called with the server lock held.
1981 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001982const char *server_parse_maxconn_change_request(struct server *sv,
1983 const char *maxconn_str)
1984{
1985 long int v;
1986 char *end;
1987
1988 if (!*maxconn_str)
1989 return "Require <maxconn>.\n";
1990
1991 v = strtol(maxconn_str, &end, 10);
1992 if (end == maxconn_str)
1993 return "maxconn string empty or preceded by garbage";
1994 else if (end[0] != '\0')
1995 return "Trailing garbage in maxconn string";
1996
1997 if (sv->maxconn == sv->minconn) { // static maxconn
1998 sv->maxconn = sv->minconn = v;
1999 } else { // dynamic maxconn
2000 sv->maxconn = v;
2001 }
2002
2003 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002004 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002005
2006 return NULL;
2007}
2008
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002009static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
2010 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002011{
2012 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002013 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002014 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002015 NULL,
2016 };
2017
2018 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002019 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002020
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002021 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002022}
2023
William Lallemand0d058672022-03-16 15:44:42 +01002024int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002025{
2026 struct sample_expr *expr;
2027
2028 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002029 if (!expr) {
2030 memprintf(err, "error detected while parsing sni expression : %s", *err);
2031 return ERR_ALERT | ERR_FATAL;
2032 }
2033
2034 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2035 memprintf(err, "error detected while parsing sni expression : "
2036 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002037 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002038 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002039 return ERR_ALERT | ERR_FATAL;
2040 }
2041
2042 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2043 release_sample_expr(newsrv->ssl_ctx.sni);
2044 newsrv->ssl_ctx.sni = expr;
2045
2046 return 0;
2047}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002048
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002049static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, int err_code, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002050{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002051 char *msg = "error encountered while processing ";
2052 char *quote = "'";
2053 char *token = args[cur_arg];
2054
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002055 if (err && *err) {
2056 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002057 msg = *err;
2058 quote = "";
2059 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002060 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002061
2062 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002063 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002064 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002065 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002066}
2067
Christopher Faulet0b365e32022-08-03 11:21:14 +02002068static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002069{
2070 int range_sz;
2071
2072 range_sz = src->conn_src.sport_range->size;
2073 if (range_sz > 0) {
2074 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2075 if (srv->conn_src.sport_range != NULL) {
2076 int i;
2077
2078 for (i = 0; i < range_sz; i++) {
2079 srv->conn_src.sport_range->ports[i] =
2080 src->conn_src.sport_range->ports[i];
2081 }
2082 }
2083 }
2084}
2085
2086/*
2087 * Copy <src> server connection source settings to <srv> server everything needed.
2088 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002089static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002090{
2091 srv->conn_src.opts = src->conn_src.opts;
2092 srv->conn_src.source_addr = src->conn_src.source_addr;
2093
2094 /* Source port range copy. */
2095 if (src->conn_src.sport_range != NULL)
2096 srv_conn_src_sport_range_cpy(srv, src);
2097
2098#ifdef CONFIG_HAP_TRANSPARENT
2099 if (src->conn_src.bind_hdr_name != NULL) {
2100 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2101 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2102 }
2103 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2104 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2105#endif
2106 if (src->conn_src.iface_name != NULL)
2107 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2108}
2109
2110/*
2111 * Copy <src> server SSL settings to <srv> server allocating
2112 * everything needed.
2113 */
2114#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002115static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002116{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002117 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002118 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2119
Christopher Faulet4ab26792021-12-01 09:50:41 +01002120 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2121 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2122
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002123 if (src->ssl_ctx.ca_file != NULL)
2124 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2125 if (src->ssl_ctx.crl_file != NULL)
2126 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002127 if (src->ssl_ctx.client_crt != NULL)
2128 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002129
2130 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2131
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002132
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002133 if (src->ssl_ctx.verify_host != NULL)
2134 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2135 if (src->ssl_ctx.ciphers != NULL)
2136 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002137 if (src->ssl_ctx.options)
2138 srv->ssl_ctx.options = src->ssl_ctx.options;
2139 if (src->ssl_ctx.methods.flags)
2140 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2141 if (src->ssl_ctx.methods.min)
2142 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2143 if (src->ssl_ctx.methods.max)
2144 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2145
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002146 if (src->ssl_ctx.ciphersuites != NULL)
2147 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002148 if (src->sni_expr != NULL)
2149 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002150
Olivier Houchardc7566002018-11-20 23:33:50 +01002151 if (src->ssl_ctx.alpn_str) {
2152 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2153 if (srv->ssl_ctx.alpn_str) {
2154 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2155 src->ssl_ctx.alpn_len);
2156 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2157 }
2158 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002159
Olivier Houchardc7566002018-11-20 23:33:50 +01002160 if (src->ssl_ctx.npn_str) {
2161 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2162 if (srv->ssl_ctx.npn_str) {
2163 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2164 src->ssl_ctx.npn_len);
2165 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2166 }
2167 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002168}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002169
2170/* Activate ssl on server <s>.
2171 * do nothing if there is no change to apply
2172 *
2173 * Must be called with the server lock held.
2174 */
2175void srv_set_ssl(struct server *s, int use_ssl)
2176{
2177 if (s->use_ssl == use_ssl)
2178 return;
2179
2180 s->use_ssl = use_ssl;
2181 if (s->use_ssl)
2182 s->xprt = xprt_get(XPRT_SSL);
2183 else
William Dauchya087f872022-01-06 16:57:15 +01002184 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002185}
2186
2187#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002188
2189/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002190 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002191 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002192 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002193 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002194int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002195{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002196 char *hostname_dn;
2197 int hostname_len, hostname_dn_len;
2198
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002199 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002200 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002201
Christopher Faulet67957bd2017-09-27 11:00:59 +02002202 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002203 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002204 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002205 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002206 if (hostname_dn_len == -1)
2207 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002208
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002209
Christopher Faulet67957bd2017-09-27 11:00:59 +02002210 free(srv->hostname);
2211 free(srv->hostname_dn);
2212 srv->hostname = strdup(hostname);
2213 srv->hostname_dn = strdup(hostname_dn);
2214 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002215 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002216 goto err;
2217
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002218 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002219
2220 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002221 ha_free(&srv->hostname);
2222 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002223 return -1;
2224}
2225
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002226/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002227 * Copy <src> server settings to <srv> server allocating
2228 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002229 * This function is not supposed to be called at any time, but only
2230 * during server settings parsing or during server allocations from
2231 * a server template, and just after having calloc()'ed a new server.
2232 * So, <src> may only be a default server (when parsing server settings)
2233 * or a server template (during server allocations from a server template).
2234 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2235 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002236 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002237void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002238{
2239 /* Connection source settings copy */
2240 srv_conn_src_cpy(srv, src);
2241
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002242 if (srv_tmpl) {
2243 srv->addr = src->addr;
2244 srv->svc_port = src->svc_port;
2245 }
2246
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002247 srv->pp_opts = src->pp_opts;
2248 if (src->rdr_pfx != NULL) {
2249 srv->rdr_pfx = strdup(src->rdr_pfx);
2250 srv->rdr_len = src->rdr_len;
2251 }
2252 if (src->cookie != NULL) {
2253 srv->cookie = strdup(src->cookie);
2254 srv->cklen = src->cklen;
2255 }
2256 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002257 srv->check.addr = src->check.addr;
2258 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002259 srv->check.use_ssl = src->check.use_ssl;
2260 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002261 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002262 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002263 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002264 /* Note: 'flags' field has potentially been already initialized. */
2265 srv->flags |= src->flags;
2266 srv->do_check = src->do_check;
2267 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002268 srv->check.inter = src->check.inter;
2269 srv->check.fastinter = src->check.fastinter;
2270 srv->check.downinter = src->check.downinter;
2271 srv->agent.use_ssl = src->agent.use_ssl;
2272 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002273
2274 if (src->agent.tcpcheck_rules) {
2275 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2276 if (srv->agent.tcpcheck_rules) {
2277 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2278 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2279 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2280 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2281 &src->agent.tcpcheck_rules->preset_vars);
2282 }
2283 }
2284
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002285 srv->agent.inter = src->agent.inter;
2286 srv->agent.fastinter = src->agent.fastinter;
2287 srv->agent.downinter = src->agent.downinter;
2288 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002289 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002290 srv->minconn = src->minconn;
2291 srv->maxconn = src->maxconn;
2292 srv->slowstart = src->slowstart;
2293 srv->observe = src->observe;
2294 srv->onerror = src->onerror;
2295 srv->onmarkeddown = src->onmarkeddown;
2296 srv->onmarkedup = src->onmarkedup;
2297 if (src->trackit != NULL)
2298 srv->trackit = strdup(src->trackit);
2299 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2300 srv->uweight = srv->iweight = src->iweight;
2301
2302 srv->check.send_proxy = src->check.send_proxy;
2303 /* health: up, but will fall down at first failure */
2304 srv->check.rise = srv->check.health = src->check.rise;
2305 srv->check.fall = src->check.fall;
2306
2307 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002308 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2309 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2310 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002311 srv->check.state |= CHK_ST_PAUSED;
2312 srv->check.health = 0;
2313 }
2314
2315 /* health: up but will fall down at first failure */
2316 srv->agent.rise = srv->agent.health = src->agent.rise;
2317 srv->agent.fall = src->agent.fall;
2318
2319 if (src->resolvers_id != NULL)
2320 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002321 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2322 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2323 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2324 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2325 srv->resolv_opts.family_prio = AF_INET6;
2326 memcpy(srv->resolv_opts.pref_net,
2327 src->resolv_opts.pref_net,
2328 sizeof srv->resolv_opts.pref_net);
2329 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002330
2331 srv->init_addr_methods = src->init_addr_methods;
2332 srv->init_addr = src->init_addr;
2333#if defined(USE_OPENSSL)
2334 srv_ssl_settings_cpy(srv, src);
2335#endif
2336#ifdef TCP_USER_TIMEOUT
2337 srv->tcp_ut = src->tcp_ut;
2338#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002339 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002340 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002341 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002342 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002343 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002344
Olivier Houchard8da5f982017-08-04 18:35:36 +02002345 if (srv_tmpl)
2346 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002347
2348 srv->check.via_socks4 = src->check.via_socks4;
2349 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002350}
2351
Willy Tarreau198e92a2021-03-05 10:23:32 +01002352/* allocate a server and attach it to the global servers_list. Returns
2353 * the server on success, otherwise NULL.
2354 */
William Lallemand313bfd12018-10-26 14:47:32 +02002355struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002356{
2357 struct server *srv;
2358
2359 srv = calloc(1, sizeof *srv);
2360 if (!srv)
2361 return NULL;
2362
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002363 srv_take(srv);
2364
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002365 srv->obj_type = OBJ_TYPE_SERVER;
2366 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002367 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002368 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002369 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002370 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002371 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002372
Emeric Brun52a91d32017-08-31 14:41:55 +02002373 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002374 srv->last_change = now.tv_sec;
2375
Christopher Faulet38290462020-04-21 11:46:40 +02002376 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002377 srv->check.status = HCHK_STATUS_INI;
2378 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002379 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002380 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002381
Christopher Faulet38290462020-04-21 11:46:40 +02002382 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002383 srv->agent.status = HCHK_STATUS_INI;
2384 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002385 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002386 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002387#if defined(USE_QUIC)
2388 srv->cids = EB_ROOT_UNIQUE;
2389#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002390
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002391 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002392#ifdef USE_OPENSSL
2393 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2394#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002395
Willy Tarreau975b1552019-06-06 16:25:55 +02002396 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002397 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002398 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002399 return srv;
2400}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002401
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002402/* Increment the server refcount. */
2403void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002404{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002405 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002406}
2407
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002408/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2409 * dynamic servers, its refcount is decremented first. The free operations are
2410 * conducted only if the refcount is nul, unless the process is stopping.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002411 *
2412 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002413 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002414 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002415struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002416{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002417 struct server *next = NULL;
2418
William Lallemand4c395fc2021-08-20 10:10:15 +02002419 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002420 goto end;
2421
2422 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002423
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002424 /* For dynamic servers, decrement the reference counter. Only free the
2425 * server when reaching zero.
2426 */
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002427 if (likely(!(global.mode & MODE_STOPPING))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002428 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2429 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002430 }
2431
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002432 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002433 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002434
2435 free(srv->id);
2436 free(srv->cookie);
2437 free(srv->hostname);
2438 free(srv->hostname_dn);
2439 free((char*)srv->conf.file);
2440 free(srv->per_thr);
Willy Tarreauc21a1872022-11-21 14:14:06 +01002441 free(srv->per_tgrp);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002442 free(srv->curr_idle_thr);
2443 free(srv->resolvers_id);
2444 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002445 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002446
2447 if (srv->use_ssl == 1 || srv->check.use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL)) {
2448 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2449 xprt_get(XPRT_SSL)->destroy_srv(srv);
2450 }
2451 HA_SPIN_DESTROY(&srv->lock);
2452
Willy Tarreau2b718102021-04-21 07:32:39 +02002453 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002454 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002455
2456 EXTRA_COUNTERS_FREE(srv->extra_counters);
2457
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002458 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002459
2460 end:
2461 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002462}
2463
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002464/* Remove a server <srv> from a tracking list if <srv> is tracking another
2465 * server. No special care is taken if <srv> is tracked itself by another one :
2466 * this situation should be avoided by the caller.
2467 *
2468 * Not thread-safe.
2469 */
2470static void release_server_track(struct server *srv)
2471{
2472 struct server *strack = srv->track;
2473 struct server **base;
2474
2475 if (!strack)
2476 return;
2477
2478 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2479 if (*base == srv) {
2480 *base = srv->tracknext;
2481 return;
2482 }
2483 }
2484
2485 /* srv not found on the tracking list, this should never happen */
2486 BUG_ON(!*base);
2487}
2488
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002489/*
2490 * Parse as much as possible such a range string argument: low[-high]
2491 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2492 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2493 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2494 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002495 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002496static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2497 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002498{
2499 char *nb_high_arg;
2500
2501 *nb_high = 0;
2502 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002503 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002504
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002505 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002506 *nb_high_arg++ = '\0';
2507 *nb_high = atoi(nb_high_arg);
2508 }
2509 else {
2510 *nb_high += *nb_low;
2511 *nb_low = 1;
2512 }
2513
2514 if (*nb_low < 0 || *nb_high < *nb_low)
2515 return -1;
2516
2517 return 0;
2518}
2519
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002520/* Parse as much as possible such a range string argument: low[-high]
2521 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2522 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2523 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002524 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002525 * initialize a new server on startup.
2526 *
2527 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2528 * Returns 0 if succeeded, -1 if not.
2529 */
2530static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2531 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002532{
2533 chunk_printf(&trash, "%s%d", prefix, nb);
2534 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002535 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002536}
2537
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002538/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002539 * Note that a server template is a special server with
2540 * a few different parameters than a server which has
2541 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002542 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002543 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002544 * initialize a new server on startup.
2545 *
Joseph Herlant44466822018-11-15 08:57:51 -08002546 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002547 * 'srv' template included.
2548 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002549static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002550{
2551 int i;
2552 struct server *newsrv;
2553
2554 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 +02002555 newsrv = new_server(px);
2556 if (!newsrv)
2557 goto err;
2558
Christopher Faulet75bef002020-11-02 22:04:55 +01002559 newsrv->conf.file = strdup(srv->conf.file);
2560 newsrv->conf.line = srv->conf.line;
2561
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002562 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002563 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002564
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002565 if (newsrv->sni_expr) {
2566 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2567 if (!newsrv->ssl_ctx.sni)
2568 goto err;
2569 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002570
Emeric Brun34067662021-06-11 10:48:45 +02002571 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002572 if (newsrv->srvrq)
2573 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002574
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002575 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002576 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002577
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002578 /* Linked backwards first. This will be restablished after parsing. */
2579 newsrv->next = px->srv;
2580 px->srv = newsrv;
2581 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002582 _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 +02002583
2584 return i - srv->tmpl_info.nb_low;
2585
2586 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002587 _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 +02002588 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002589 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002590 free_check(&newsrv->agent);
2591 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002592 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002593 }
2594 free(newsrv);
2595 return i - srv->tmpl_info.nb_low;
2596}
2597
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002598/* Allocate a new server pointed by <srv> and try to parse the first arguments
2599 * in <args> as an address for a server or an address-range for a template or
2600 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2601 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002602 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002603 * initialize a new server on startup.
2604 *
2605 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2606 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2607 * <srv> will be set to NULL.
2608 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002609static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2610 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002611 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002612{
2613 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002614 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002615 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002616 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002617 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002618 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002619
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002620 *srv = NULL;
2621
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002622 /* There is no mandatory first arguments for default server. */
2623 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2624 if (parse_flags & SRV_PARSE_TEMPLATE) {
2625 if (!*args[3]) {
2626 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002627 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2628 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002629 err_code |= ERR_ALERT | ERR_FATAL;
2630 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002631 }
2632
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002633 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002634 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002635 else {
2636 if (!*args[2]) {
2637 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002638 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2639 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002640 err_code |= ERR_ALERT | ERR_FATAL;
2641 goto out;
2642 }
2643
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002644 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002645 }
2646
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002647 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002648 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2649 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002650 err_code |= ERR_ALERT | ERR_FATAL;
2651 goto out;
2652 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002653 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002654
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002655 *cur_arg = 2;
2656 if (parse_flags & SRV_PARSE_TEMPLATE) {
2657 /* Parse server-template <nb | range> arg. */
2658 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002659 ha_alert("Wrong %s number or range arg '%s'.\n",
2660 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002661 err_code |= ERR_ALERT | ERR_FATAL;
2662 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002663 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002664 (*cur_arg)++;
2665 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002666
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002667 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2668 struct sockaddr_storage *sk;
2669 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002670
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002671 *srv = newsrv = new_server(curproxy);
2672 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002673 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002674 err_code |= ERR_ALERT | ERR_ABORT;
2675 goto out;
2676 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002677 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002678
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002679 if (parse_flags & SRV_PARSE_TEMPLATE) {
2680 newsrv->tmpl_info.nb_low = tmpl_range_low;
2681 newsrv->tmpl_info.nb_high = tmpl_range_high;
2682 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002683
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002684 if (parse_flags & SRV_PARSE_DYNAMIC)
2685 newsrv->flags |= SRV_F_DYNAMIC;
2686
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002687 /* Note: for a server template, its id is its prefix.
2688 * This is a temporary id which will be used for server allocations to come
2689 * after parsing.
2690 */
2691 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2692 newsrv->id = strdup(args[1]);
2693 else
2694 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002695
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002696 /* revision defaults to 0 */
2697 newsrv->rid = 0;
2698
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002699 /* several ways to check the port component :
2700 * - IP => port=+0, relative (IPv4 only)
2701 * - IP: => port=+0, relative
2702 * - IP:N => port=N, absolute
2703 * - IP:+N => port=+N, relative
2704 * - IP:-N => port=-N, relative
2705 */
2706 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2707 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002708
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002709 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002710 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002711 (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);
2712 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002713 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002714 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002715 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002716 goto out;
2717 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002718
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002719 if (!port1 || !port2) {
2720 /* no port specified, +offset, -offset */
2721 newsrv->flags |= SRV_F_MAPPORTS;
2722 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002723
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002724 /* save hostname and create associated name resolution */
2725 if (fqdn) {
2726 if (fqdn[0] == '_') { /* SRV record */
2727 /* Check if a SRV request already exists, and if not, create it */
2728 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2729 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2730 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002731 err_code |= ERR_ALERT | ERR_FATAL;
2732 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002733 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002734 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002735 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002736 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002737 ha_alert("Can't create DNS resolution for server '%s'\n",
2738 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002739 err_code |= ERR_ALERT | ERR_FATAL;
2740 goto out;
2741 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002742 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002743
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002744 newsrv->addr = *sk;
2745 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002746 /*
2747 * we don't need to lock the server here, because
2748 * we are in the process of initializing.
2749 *
2750 * Note that the server is not attached into the proxy tree if
2751 * this is a dynamic server.
2752 */
2753 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002754
Willy Tarreau14e7f292021-10-27 17:41:07 +02002755 if (!newsrv->srvrq && !newsrv->hostname &&
2756 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002757 ha_alert("Unknown protocol family %d '%s'\n",
2758 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002759 err_code |= ERR_ALERT | ERR_FATAL;
2760 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002761 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002762
2763 (*cur_arg)++;
2764 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002765 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2766 /* Copy default server settings to new server */
2767 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2768 } else {
2769 /* Initialize dynamic server weight to 1 */
2770 newsrv->uweight = newsrv->iweight = 1;
2771
2772 /* A dynamic server is disabled on startup */
2773 newsrv->next_admin = SRV_ADMF_FMAINT;
2774 newsrv->next_state = SRV_ST_STOPPED;
2775 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002776
2777 /* Set default values for checks */
2778 newsrv->check.inter = DEF_CHKINTR;
2779 newsrv->check.rise = DEF_RISETIME;
2780 newsrv->check.fall = DEF_FALLTIME;
2781
2782 newsrv->agent.inter = DEF_CHKINTR;
2783 newsrv->agent.rise = DEF_AGENT_RISETIME;
2784 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002785 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002786 HA_SPIN_INIT(&newsrv->lock);
2787 }
2788 else {
2789 *srv = newsrv = &curproxy->defsrv;
2790 *cur_arg = 1;
2791 newsrv->resolv_opts.family_prio = AF_INET6;
2792 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002793 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002794
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002795 free(fqdn);
2796 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002797
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002798out:
2799 free(fqdn);
2800 return err_code;
2801}
Willy Tarreau272adea2014-03-31 10:39:59 +02002802
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002803/* Parse the server keyword in <args>.
2804 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2805 * might not be the case if an error is reported.
2806 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002807 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002808 * initialize a new server on startup.
2809 *
2810 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2811 * interrupted.
2812 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002813static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2814 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002815 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002816{
2817 int err_code = 0;
2818 struct srv_kw *kw;
2819 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002820 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002821
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002822 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002823 if (!kw) {
2824 best = srv_find_best_kw(args[*cur_arg]);
2825 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002826 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2827 args[*cur_arg], best,
2828 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2829 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002830 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002831 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2832 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2833 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002834
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002835 return ERR_ALERT | ERR_FATAL;
2836 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002837
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002838 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002839 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2840 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002841 err_code = ERR_ALERT | ERR_FATAL;
2842 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002843 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002844
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002845 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002846 ha_alert("'%s' option is not accepted in default-server sections\n",
2847 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002848 err_code = ERR_ALERT;
2849 goto out;
2850 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002851 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002852 ha_alert("'%s' option is not accepted for dynamic server\n",
2853 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002854 err_code |= ERR_ALERT;
2855 goto out;
2856 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002857
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002858 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2859 if (err_code) {
2860 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2861 free(errmsg);
2862 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002863
2864out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002865 if (kw->skip != -1)
2866 *cur_arg += 1 + kw->skip;
2867
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002868 return err_code;
2869}
2870
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002871/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002872 * initialize a new server on startup.
2873 */
2874static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2875 struct server *srv, struct proxy *proxy,
2876 char **errmsg)
2877{
2878 int ret;
2879
2880 if (!srv->sni_expr)
2881 return 0;
2882
2883 ret = server_parse_sni_expr(srv, proxy, errmsg);
2884 if (!ret)
2885 return 0;
2886
2887 return ret;
2888}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002889
2890/* Server initializations finalization.
2891 * Initialize health check, agent check and SNI expression if enabled.
2892 * Must not be called for a default server instance.
2893 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002894 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002895 * initialize a new server on startup.
2896 */
2897static int _srv_parse_finalize(char **args, int cur_arg,
2898 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002899 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002900{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002901 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002902 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002903
2904 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002905 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002906 return ERR_ALERT | ERR_FATAL;
2907 }
2908
2909 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002910 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2911 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002912 return ERR_ALERT | ERR_FATAL;
2913 }
2914
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002915 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2916 if (errmsg) {
2917 ha_alert("%s\n", errmsg);
2918 free(errmsg);
2919 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002920 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002921 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002922
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002923 /* A dynamic server is disabled on startup. It must not be counted as
2924 * an active backend entry.
2925 */
2926 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2927 if (srv->flags & SRV_F_BACKUP)
2928 px->srv_bck++;
2929 else
2930 px->srv_act++;
2931 }
2932
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002933 srv_lb_commit_status(srv);
2934
2935 return 0;
2936}
2937
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002938int parse_server(const char *file, int linenum, char **args,
2939 struct proxy *curproxy, const struct proxy *defproxy,
2940 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002941{
2942 struct server *newsrv = NULL;
2943 int err_code = 0;
2944
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002945 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002946
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002947 set_usermsgs_ctx(file, linenum, NULL);
2948
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002949 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002950 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002951 err_code |= ERR_ALERT | ERR_FATAL;
2952 goto out;
2953 }
2954 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2955 err_code |= ERR_ALERT | ERR_FATAL;
2956 goto out;
2957 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002958
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002959 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2960 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2961 if (!*args[2])
2962 return 0;
2963 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002964
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002965 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002966 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002967
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002968 /* the servers are linked backwards first */
2969 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2970 newsrv->next = curproxy->srv;
2971 curproxy->srv = newsrv;
2972 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002973
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002974 if (err_code & ERR_CODE)
2975 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002976
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002977 newsrv->conf.file = strdup(file);
2978 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002979
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002980 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002981 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002982 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002983 if (err_code & ERR_FATAL)
2984 goto out;
2985 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002986
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002987 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002988 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002989 if (err_code & ERR_FATAL)
2990 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002991 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002992
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002993 if (parse_flags & SRV_PARSE_TEMPLATE)
2994 _srv_parse_tmpl_init(newsrv, curproxy);
2995
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02002996 /* If the server id is fixed, insert it in the proxy used_id tree.
2997 * This is needed to detect a later duplicate id via srv_parse_id.
2998 *
2999 * If no is specified, a dynamic one is generated in
3000 * check_config_validity.
3001 */
3002 if (newsrv->flags & SRV_F_FORCED_ID)
3003 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3004
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02003005 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003006 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02003007
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003008 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003009 return 0;
3010
3011 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003012 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003013 return err_code;
3014}
3015
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003016/* Returns a pointer to the first server matching either id <id>.
3017 * NULL is returned if no match is found.
3018 * the lookup is performed in the backend <bk>
3019 */
3020struct server *server_find_by_id(struct proxy *bk, int id)
3021{
3022 struct eb32_node *eb32;
3023 struct server *curserver;
3024
3025 if (!bk || (id ==0))
3026 return NULL;
3027
3028 /* <bk> has no backend capabilities, so it can't have a server */
3029 if (!(bk->cap & PR_CAP_BE))
3030 return NULL;
3031
3032 curserver = NULL;
3033
3034 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3035 if (eb32)
3036 curserver = container_of(eb32, struct server, conf.id);
3037
3038 return curserver;
3039}
3040
3041/* Returns a pointer to the first server matching either name <name>, or id
3042 * if <name> starts with a '#'. NULL is returned if no match is found.
3043 * the lookup is performed in the backend <bk>
3044 */
3045struct server *server_find_by_name(struct proxy *bk, const char *name)
3046{
3047 struct server *curserver;
3048
3049 if (!bk || !name)
3050 return NULL;
3051
3052 /* <bk> has no backend capabilities, so it can't have a server */
3053 if (!(bk->cap & PR_CAP_BE))
3054 return NULL;
3055
3056 curserver = NULL;
3057 if (*name == '#') {
3058 curserver = server_find_by_id(bk, atoi(name + 1));
3059 if (curserver)
3060 return curserver;
3061 }
3062 else {
3063 curserver = bk->srv;
3064
3065 while (curserver && (strcmp(curserver->id, name) != 0))
3066 curserver = curserver->next;
3067
3068 if (curserver)
3069 return curserver;
3070 }
3071
3072 return NULL;
3073}
3074
3075struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3076{
3077 struct server *byname;
3078 struct server *byid;
3079
3080 if (!name && !id)
3081 return NULL;
3082
3083 if (diff)
3084 *diff = 0;
3085
3086 byname = byid = NULL;
3087
3088 if (name) {
3089 byname = server_find_by_name(bk, name);
3090 if (byname && (!id || byname->puid == id))
3091 return byname;
3092 }
3093
3094 /* remaining possibilities :
3095 * - name not set
3096 * - name set but not found
3097 * - name found but ID doesn't match
3098 */
3099 if (id) {
3100 byid = server_find_by_id(bk, id);
3101 if (byid) {
3102 if (byname) {
3103 /* use id only if forced by configuration */
3104 if (byid->flags & SRV_F_FORCED_ID) {
3105 if (diff)
3106 *diff |= 2;
3107 return byid;
3108 }
3109 else {
3110 if (diff)
3111 *diff |= 1;
3112 return byname;
3113 }
3114 }
3115
3116 /* remaining possibilities:
3117 * - name not set
3118 * - name set but not found
3119 */
3120 if (name && diff)
3121 *diff |= 2;
3122 return byid;
3123 }
3124
3125 /* id bot found */
3126 if (byname) {
3127 if (diff)
3128 *diff |= 1;
3129 return byname;
3130 }
3131 }
3132
3133 return NULL;
3134}
3135
Simon Horman7d09b9a2013-02-12 10:45:51 +09003136/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003137 * update a server's current IP address.
3138 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3139 * ip is in network format.
3140 * updater is a string which contains an information about the requester of the update.
3141 * updater is used if not NULL.
3142 *
3143 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003144 *
3145 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003146 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003147int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003148{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003149 /* save the new IP family & address if necessary */
3150 switch (ip_sin_family) {
3151 case AF_INET:
3152 if (s->addr.ss_family == ip_sin_family &&
3153 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3154 return 0;
3155 break;
3156 case AF_INET6:
3157 if (s->addr.ss_family == ip_sin_family &&
3158 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3159 return 0;
3160 break;
3161 };
3162
Baptiste Assmann14e40142015-04-14 01:13:07 +02003163 /* generates a log line and a warning on stderr */
3164 if (1) {
3165 /* book enough space for both IPv4 and IPv6 */
3166 char oldip[INET6_ADDRSTRLEN];
3167 char newip[INET6_ADDRSTRLEN];
3168
3169 memset(oldip, '\0', INET6_ADDRSTRLEN);
3170 memset(newip, '\0', INET6_ADDRSTRLEN);
3171
3172 /* copy old IP address in a string */
3173 switch (s->addr.ss_family) {
3174 case AF_INET:
3175 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3176 break;
3177 case AF_INET6:
3178 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3179 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003180 default:
3181 strcpy(oldip, "(none)");
3182 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003183 };
3184
3185 /* copy new IP address in a string */
3186 switch (ip_sin_family) {
3187 case AF_INET:
3188 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3189 break;
3190 case AF_INET6:
3191 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3192 break;
3193 };
3194
3195 /* save log line into a buffer */
3196 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3197 s->proxy->id, s->id, oldip, newip, updater);
3198
3199 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003200 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003201
3202 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003203 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003204 }
3205
3206 /* save the new IP family */
3207 s->addr.ss_family = ip_sin_family;
3208 /* save the new IP address */
3209 switch (ip_sin_family) {
3210 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003211 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003212 break;
3213 case AF_INET6:
3214 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3215 break;
3216 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003217 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003218 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003219
3220 return 0;
3221}
3222
William Dauchy7cabc062021-02-11 22:51:24 +01003223/* update agent health check address and port
3224 * addr can be ip4/ip6 or a hostname
3225 * if one error occurs, don't apply anything
3226 * must be called with the server lock held.
3227 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003228const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003229{
3230 struct sockaddr_storage sk;
3231 struct buffer *msg;
3232 int new_port;
3233
3234 msg = get_trash_chunk();
3235 chunk_reset(msg);
3236
3237 if (!(s->agent.state & CHK_ST_ENABLED)) {
3238 chunk_strcat(msg, "agent checks are not enabled on this server");
3239 goto out;
3240 }
3241 if (addr) {
3242 memset(&sk, 0, sizeof(struct sockaddr_storage));
3243 if (str2ip(addr, &sk) == NULL) {
3244 chunk_appendf(msg, "invalid addr '%s'", addr);
3245 goto out;
3246 }
3247 }
3248 if (port) {
3249 if (strl2irc(port, strlen(port), &new_port) != 0) {
3250 chunk_appendf(msg, "provided port is not an integer");
3251 goto out;
3252 }
3253 if (new_port < 0 || new_port > 65535) {
3254 chunk_appendf(msg, "provided port is invalid");
3255 goto out;
3256 }
3257 }
3258out:
3259 if (msg->data)
3260 return msg->area;
3261 else {
3262 if (addr)
3263 set_srv_agent_addr(s, &sk);
3264 if (port)
3265 set_srv_agent_port(s, new_port);
3266 }
3267 return NULL;
3268}
3269
William Dauchyb456e1f2021-02-11 22:51:23 +01003270/* update server health check address and port
3271 * addr must be ip4 or ip6, it won't be resolved
3272 * if one error occurs, don't apply anything
3273 * must be called with the server lock held.
3274 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003275const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003276{
3277 struct sockaddr_storage sk;
3278 struct buffer *msg;
3279 int new_port;
3280
3281 msg = get_trash_chunk();
3282 chunk_reset(msg);
3283
3284 if (!(s->check.state & CHK_ST_ENABLED)) {
3285 chunk_strcat(msg, "health checks are not enabled on this server");
3286 goto out;
3287 }
3288 if (addr) {
3289 memset(&sk, 0, sizeof(struct sockaddr_storage));
3290 if (str2ip2(addr, &sk, 0) == NULL) {
3291 chunk_appendf(msg, "invalid addr '%s'", addr);
3292 goto out;
3293 }
3294 }
3295 if (port) {
3296 if (strl2irc(port, strlen(port), &new_port) != 0) {
3297 chunk_appendf(msg, "provided port is not an integer");
3298 goto out;
3299 }
3300 if (new_port < 0 || new_port > 65535) {
3301 chunk_appendf(msg, "provided port is invalid");
3302 goto out;
3303 }
3304 /* prevent the update of port to 0 if MAPPORTS are in use */
3305 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3306 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3307 goto out;
3308 }
3309 }
3310out:
3311 if (msg->data)
3312 return msg->area;
3313 else {
3314 if (addr)
3315 s->check.addr = sk;
3316 if (port)
3317 s->check.port = new_port;
3318 }
3319 return NULL;
3320}
3321
Baptiste Assmann14e40142015-04-14 01:13:07 +02003322/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003323 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3324 *
3325 * Caller can pass its name through <updater> to get it integrated in the response message
3326 * returned by the function.
3327 *
3328 * The function first does the following, in that order:
3329 * - validates the new addr and/or port
3330 * - checks if an update is required (new IP or port is different than current ones)
3331 * - checks the update is allowed:
3332 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3333 * - allow all changes if no CHECKS are configured
3334 * - if CHECK is configured:
3335 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3336 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3337 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003338 *
3339 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003340 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003341const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003342{
3343 struct sockaddr_storage sa;
3344 int ret, port_change_required;
3345 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003346 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003347 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003348 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003349
3350 msg = get_trash_chunk();
3351 chunk_reset(msg);
3352
3353 if (addr) {
3354 memset(&sa, 0, sizeof(struct sockaddr_storage));
3355 if (str2ip2(addr, &sa, 0) == NULL) {
3356 chunk_printf(msg, "Invalid addr '%s'", addr);
3357 goto out;
3358 }
3359
3360 /* changes are allowed on AF_INET* families only */
3361 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3362 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3363 goto out;
3364 }
3365
3366 /* collecting data currently setup */
3367 memset(current_addr, '\0', sizeof(current_addr));
3368 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3369 /* changes are allowed on AF_INET* families only */
3370 if ((ret != AF_INET) && (ret != AF_INET6)) {
3371 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3372 goto out;
3373 }
3374
3375 /* applying ADDR changes if required and allowed
3376 * ipcmp returns 0 when both ADDR are the same
3377 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003378 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003379 chunk_appendf(msg, "no need to change the addr");
3380 goto port;
3381 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003382 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003383 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003384
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003385 /* update report for caller */
3386 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3387 }
3388
3389 port:
3390 if (port) {
3391 char sign = '\0';
3392 char *endptr;
3393
3394 if (addr)
3395 chunk_appendf(msg, ", ");
3396
3397 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003398 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003399
3400 /* check if PORT change is required */
3401 port_change_required = 0;
3402
3403 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003404 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003405 new_port = strtol(port, &endptr, 10);
3406 if ((errno != 0) || (port == endptr)) {
3407 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3408 goto out;
3409 }
3410
3411 /* check if caller triggers a port mapped or offset */
3412 if (sign == '-' || (sign == '+')) {
3413 /* check if server currently uses port map */
3414 if (!(s->flags & SRV_F_MAPPORTS)) {
3415 /* switch from fixed port to port map mandatorily triggers
3416 * a port change */
3417 port_change_required = 1;
3418 /* check is configured
3419 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3420 * prevent PORT change if check doesn't have it's dedicated port while switching
3421 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003422 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003423 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.");
3424 goto out;
3425 }
3426 }
3427 /* we're already using port maps */
3428 else {
3429 port_change_required = current_port != new_port;
3430 }
3431 }
3432 /* fixed port */
3433 else {
3434 port_change_required = current_port != new_port;
3435 }
3436
3437 /* applying PORT changes if required and update response message */
3438 if (port_change_required) {
3439 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003440 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003441 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003442
3443 /* prepare message */
3444 chunk_appendf(msg, "port changed from '");
3445 if (s->flags & SRV_F_MAPPORTS)
3446 chunk_appendf(msg, "+");
3447 chunk_appendf(msg, "%d' to '", current_port);
3448
3449 if (sign == '-') {
3450 s->flags |= SRV_F_MAPPORTS;
3451 chunk_appendf(msg, "%c", sign);
3452 /* just use for result output */
3453 new_port = -new_port;
3454 }
3455 else if (sign == '+') {
3456 s->flags |= SRV_F_MAPPORTS;
3457 chunk_appendf(msg, "%c", sign);
3458 }
3459 else {
3460 s->flags &= ~SRV_F_MAPPORTS;
3461 }
3462
3463 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003464 }
3465 else {
3466 chunk_appendf(msg, "no need to change the port");
3467 }
3468 }
3469
3470out:
William Dauchy6318d332020-05-02 21:52:36 +02003471 if (changed) {
3472 /* force connection cleanup on the given server */
3473 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003474 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003475 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003476 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003477 if (updater)
3478 chunk_appendf(msg, " by '%s'", updater);
3479 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003480 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003481}
3482
Christopher Faulet5efdef22021-03-11 18:03:21 +01003483/*
3484 * update server status based on result of SRV resolution
3485 * returns:
3486 * 0 if server status is updated
3487 * 1 if server status has not changed
3488 *
3489 * Must be called with the server lock held.
3490 */
3491int srvrq_update_srv_status(struct server *s, int has_no_ip)
3492{
3493 if (!s->srvrq)
3494 return 1;
3495
3496 /* since this server has an IP, it can go back in production */
3497 if (has_no_ip == 0) {
3498 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3499 return 1;
3500 }
3501
3502 if (s->next_admin & SRV_ADMF_RMAINT)
3503 return 1;
3504
3505 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3506 return 0;
3507}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003508
3509/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003510 * update server status based on result of name resolution
3511 * returns:
3512 * 0 if server status is updated
3513 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003514 *
3515 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003516 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003517int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003518{
Emeric Brun750fe792020-12-23 16:51:12 +01003519 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003520 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003521 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003522
Jerome Magnin012261a2020-07-28 13:38:22 +02003523 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003524 if (resolution == NULL)
3525 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003526
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003527 switch (resolution->status) {
3528 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003529 /* status when HAProxy has just (re)started.
3530 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003531 break;
3532
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003533 case RSLV_STATUS_VALID:
3534 /*
3535 * resume health checks
3536 * server will be turned back on if health check is safe
3537 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003538 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003539 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003540 return 1;
3541 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3542 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003543 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003544 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003545
Emeric Brun52a91d32017-08-31 14:41:55 +02003546 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003547 return 1;
3548 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3549 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3550 s->proxy->id, s->id);
3551
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003552 ha_warning("%s.\n", trash.area);
3553 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003554 return 0;
3555
3556 case RSLV_STATUS_NX:
3557 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003558 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3559 if (!tick_is_expired(exp, now_ms))
3560 break;
3561
3562 if (s->next_admin & SRV_ADMF_RMAINT)
3563 return 1;
3564 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3565 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003566
3567 case RSLV_STATUS_TIMEOUT:
3568 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003569 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3570 if (!tick_is_expired(exp, now_ms))
3571 break;
3572
3573 if (s->next_admin & SRV_ADMF_RMAINT)
3574 return 1;
3575 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3576 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003577
3578 case RSLV_STATUS_REFUSED:
3579 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003580 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3581 if (!tick_is_expired(exp, now_ms))
3582 break;
3583
3584 if (s->next_admin & SRV_ADMF_RMAINT)
3585 return 1;
3586 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3587 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003588
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003589 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003590 /* stop server if resolution failed for a long enough period */
3591 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3592 if (!tick_is_expired(exp, now_ms))
3593 break;
3594
3595 if (s->next_admin & SRV_ADMF_RMAINT)
3596 return 1;
3597 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3598 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003599 }
3600
3601 return 1;
3602}
3603
3604/*
3605 * Server Name Resolution valid response callback
3606 * It expects:
3607 * - <nameserver>: the name server which answered the valid response
3608 * - <response>: buffer containing a valid DNS response
3609 * - <response_len>: size of <response>
3610 * It performs the following actions:
3611 * - ignore response if current ip found and server family not met
3612 * - update with first new ip found if family is met and current IP is not found
3613 * returns:
3614 * 0 on error
3615 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003616 *
3617 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003618 */
Emeric Brun08622d32020-12-23 17:41:43 +01003619int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003620{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003621 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003622 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003623 void *serverip, *firstip;
3624 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003625 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003626 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003627 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003628
Christopher Faulet67957bd2017-09-27 11:00:59 +02003629 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003630 if (!s)
3631 return 1;
3632
Christopher Faulet49531e82021-03-10 15:07:27 +01003633 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003634 /* If DNS resolution is disabled ignore it.
3635 * This is the case if the server was associated to
3636 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003637 */
Emeric Brun34067662021-06-11 10:48:45 +02003638 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003639 return 1;
3640 }
3641
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003642 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003643 if (!resolution)
3644 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003645
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003646 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003647 firstip = NULL; /* pointer to the first valid response found */
3648 /* it will be used as the new IP if a change is required */
3649 firstip_sin_family = AF_UNSPEC;
3650 serverip = NULL; /* current server IP address */
3651
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003652 /* initializing server IP pointer */
3653 server_sin_family = s->addr.ss_family;
3654 switch (server_sin_family) {
3655 case AF_INET:
3656 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3657 break;
3658
3659 case AF_INET6:
3660 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3661 break;
3662
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003663 case AF_UNSPEC:
3664 break;
3665
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003666 default:
3667 goto invalid;
3668 }
3669
Emeric Brund30e9a12020-12-23 18:49:16 +01003670 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3671 serverip, server_sin_family, &firstip,
3672 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003673
3674 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003675 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003676 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003677
Emeric Brun456de772020-12-23 18:17:31 +01003678 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003679 goto save_ip;
3680
Emeric Brun456de772020-12-23 18:17:31 +01003681 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003682 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003683 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003684
Emeric Brun456de772020-12-23 18:17:31 +01003685 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003686 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003687 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003688 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003689 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003690
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003691 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003692 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003693 goto invalid;
3694
3695 }
3696
3697 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003698 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003699 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003700 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003701 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003702 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003703 else
3704 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003705 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003706
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003708 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3709 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003710 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003711
3712 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003713 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003714 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003715 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003716 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003717 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3718 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003719 return 0;
3720}
3721
3722/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003723 * SRV record error management callback
3724 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003725 * 0 if we can trash answser items.
3726 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003727 *
3728 * Grabs the server's lock.
3729 */
3730int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3731{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003732 struct resolv_srvrq *srvrq;
3733 struct resolv_resolution *res;
3734 struct resolvers *resolvers;
3735 int exp;
3736
3737 /* SRV records */
3738 srvrq = objt_resolv_srvrq(requester->owner);
3739 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003740 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003741
3742 resolvers = srvrq->resolvers;
3743 res = requester->resolution;
3744
3745 switch (res->status) {
3746
3747 case RSLV_STATUS_NX:
3748 /* stop server if resolution is NX for a long enough period */
3749 exp = tick_add(res->last_valid, resolvers->hold.nx);
3750 if (!tick_is_expired(exp, now_ms))
3751 return 1;
3752 break;
3753
3754 case RSLV_STATUS_TIMEOUT:
3755 /* stop server if resolution is TIMEOUT for a long enough period */
3756 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3757 if (!tick_is_expired(exp, now_ms))
3758 return 1;
3759 break;
3760
3761 case RSLV_STATUS_REFUSED:
3762 /* stop server if resolution is REFUSED for a long enough period */
3763 exp = tick_add(res->last_valid, resolvers->hold.refused);
3764 if (!tick_is_expired(exp, now_ms))
3765 return 1;
3766 break;
3767
3768 default:
3769 /* stop server if resolution failed for a long enough period */
3770 exp = tick_add(res->last_valid, resolvers->hold.other);
3771 if (!tick_is_expired(exp, now_ms))
3772 return 1;
3773 }
3774
Emeric Brun34067662021-06-11 10:48:45 +02003775 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003776 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003777
Emeric Brun12ca6582021-06-10 15:25:25 +02003778 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003779}
3780
3781/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003782 * Server Name Resolution error management callback
3783 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003784 * 0 if we can trash answser items.
3785 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003786 *
3787 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003788 */
Emeric Brun08622d32020-12-23 17:41:43 +01003789int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003790{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003791 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792
Christopher Faulet67957bd2017-09-27 11:00:59 +02003793 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003794 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003795 return 0;
3796
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003797 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003798 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003799 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003800 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003801 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003802 return 0;
3803 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003804 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003805
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003806 return 1;
3807}
3808
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003809/*
3810 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003811 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003812 * It returns a pointer to the first server found or NULL if <ip> is not yet
3813 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003814 *
3815 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003816 */
3817struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3818{
3819 struct server *tmpsrv;
3820 struct proxy *be;
3821
3822 if (!srv)
3823 return NULL;
3824
3825 be = srv->proxy;
3826 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003827 /* we found the current server is the same, ignore it */
3828 if (srv == tmpsrv)
3829 continue;
3830
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003831 /* We want to compare the IP in the record with the IP of the servers in the
3832 * same backend, only if:
3833 * * DNS resolution is enabled on the server
3834 * * the hostname used for the resolution by our server is the same than the
3835 * one used for the server found in the backend
3836 * * the server found in the backend is not our current server
3837 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003838 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003839 if ((tmpsrv->hostname_dn == NULL) ||
3840 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003841 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003842 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003843 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003844 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003845 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003846
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003847 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003848 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003849 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003850 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003851 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003852
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003853 /* At this point, we have 2 different servers using the same DNS hostname
3854 * for their respective resolution.
3855 */
3856 if (*ip_family == tmpsrv->addr.ss_family &&
3857 ((tmpsrv->addr.ss_family == AF_INET &&
3858 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3859 (tmpsrv->addr.ss_family == AF_INET6 &&
3860 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003861 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003862 return tmpsrv;
3863 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003864 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003865 }
3866
Emeric Brune9fd6b52017-11-02 17:20:39 +01003867
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003868 return NULL;
3869}
3870
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003871/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3872 * resolver. This is suited for initial address configuration. Returns 0 on
3873 * success otherwise a non-zero error code. In case of error, *err_code, if
3874 * not NULL, is filled up.
3875 */
3876int srv_set_addr_via_libc(struct server *srv, int *err_code)
3877{
3878 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003879 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003880 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003881 return 1;
3882 }
3883 return 0;
3884}
3885
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003886/* Set the server's FDQN (->hostname) from <hostname>.
3887 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003888 *
3889 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003890 */
Emeric Brun08622d32020-12-23 17:41:43 +01003891int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003892{
Emeric Brun08622d32020-12-23 17:41:43 +01003893 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003894 char *hostname_dn;
3895 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003896
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003897 /* Note that the server lock is already held. */
3898 if (!srv->resolvers)
3899 return -1;
3900
Emeric Brun08622d32020-12-23 17:41:43 +01003901 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003902 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003903 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003904 * and we can't enable it at run time for now.
3905 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003906 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003907 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003908
3909 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003910 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003911 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003912 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003913 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003914 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003915 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003916
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003917 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003918 if (resolution &&
3919 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003920 resolution->hostname_dn_len == hostname_dn_len &&
3921 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003922 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003923
Willy Tarreau6878f802021-10-20 14:07:31 +02003924 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003925
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003926 free(srv->hostname);
3927 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003928 srv->hostname = strdup(hostname);
3929 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003930 srv->hostname_dn_len = hostname_dn_len;
3931 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003932 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003933
Baptiste Assmann13a92322019-06-07 09:40:55 +02003934 if (srv->flags & SRV_F_NO_RESOLUTION)
3935 goto end;
3936
Emeric Brund30e9a12020-12-23 18:49:16 +01003937 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003938 goto err;
3939
3940 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003941 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003942 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003943 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003944
3945 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003946 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003947 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003948 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003949}
3950
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003951/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3952 * from the state file. This is suited for initial address configuration.
3953 * Returns 0 on success otherwise a non-zero error code. In case of error,
3954 * *err_code, if not NULL, is filled up.
3955 */
3956static int srv_apply_lastaddr(struct server *srv, int *err_code)
3957{
3958 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3959 if (err_code)
3960 *err_code |= ERR_WARN;
3961 return 1;
3962 }
3963 return 0;
3964}
3965
Willy Tarreau25e51522016-11-04 15:10:17 +01003966/* returns 0 if no error, otherwise a combination of ERR_* flags */
3967static int srv_iterate_initaddr(struct server *srv)
3968{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003969 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003970 int return_code = 0;
3971 int err_code;
3972 unsigned int methods;
3973
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003974 /* If no addr and no hostname set, get the name from the DNS SRV request */
3975 if (!name && srv->srvrq)
3976 name = srv->srvrq->name;
3977
Willy Tarreau25e51522016-11-04 15:10:17 +01003978 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003979 if (!methods) {
3980 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003981 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3982 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003983 if (srv->resolvers_id) {
3984 /* dns resolution is configured, add "none" to not fail on startup */
3985 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3986 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003987 }
3988
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003989 /* "-dr" : always append "none" so that server addresses resolution
3990 * failures are silently ignored, this is convenient to validate some
3991 * configs out of their environment.
3992 */
3993 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3994 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3995
Willy Tarreau25e51522016-11-04 15:10:17 +01003996 while (methods) {
3997 err_code = 0;
3998 switch (srv_get_next_initaddr(&methods)) {
3999 case SRV_IADDR_LAST:
4000 if (!srv->lastaddr)
4001 continue;
4002 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004003 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004004 return_code |= err_code;
4005 break;
4006
4007 case SRV_IADDR_LIBC:
4008 if (!srv->hostname)
4009 continue;
4010 if (srv_set_addr_via_libc(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
Willy Tarreau37ebe122016-11-04 15:17:58 +01004015 case SRV_IADDR_NONE:
4016 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004017 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004018 ha_warning("could not resolve address '%s', disabling server.\n",
4019 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004020 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004021 return return_code;
4022
Willy Tarreau4310d362016-11-02 15:05:56 +01004023 case SRV_IADDR_IP:
4024 ipcpy(&srv->init_addr, &srv->addr);
4025 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004026 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4027 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004028 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004029 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004030
Willy Tarreau25e51522016-11-04 15:10:17 +01004031 default: /* unhandled method */
4032 break;
4033 }
4034 }
4035
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004036 if (!return_code)
4037 ha_alert("no method found to resolve address '%s'.\n", name);
4038 else
4039 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004040
4041 return_code |= ERR_ALERT | ERR_FATAL;
4042 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004043out:
4044 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004045 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004046 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004047}
4048
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004049/*
4050 * This function parses all backends and all servers within each backend
4051 * and performs servers' addr resolution based on information provided by:
4052 * - configuration file
4053 * - server-state file (states provided by an 'old' haproxy process)
4054 *
4055 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4056 */
4057int srv_init_addr(void)
4058{
4059 struct proxy *curproxy;
4060 int return_code = 0;
4061
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004062 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004063 while (curproxy) {
4064 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004065
4066 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004067 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004068 goto srv_init_addr_next;
4069
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004070 for (srv = curproxy->srv; srv; srv = srv->next) {
4071 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004072 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004073 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004074 reset_usermsgs_ctx();
4075 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004076
4077 srv_init_addr_next:
4078 curproxy = curproxy->next;
4079 }
4080
4081 return return_code;
4082}
4083
Willy Tarreau46b7f532018-08-21 11:54:26 +02004084/*
4085 * Must be called with the server lock held.
4086 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004087const 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 +02004088{
4089
Willy Tarreau83061a82018-07-13 11:56:34 +02004090 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004091
4092 msg = get_trash_chunk();
4093 chunk_reset(msg);
4094
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004095 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004096 chunk_appendf(msg, "no need to change the FDQN");
4097 goto out;
4098 }
4099
4100 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4101 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4102 goto out;
4103 }
4104
4105 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4106 server->proxy->id, server->id, server->hostname, fqdn);
4107
Emeric Brun08622d32020-12-23 17:41:43 +01004108 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004109 chunk_reset(msg);
4110 chunk_appendf(msg, "could not update %s/%s FQDN",
4111 server->proxy->id, server->id);
4112 goto out;
4113 }
4114
4115 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004116 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004117
4118 out:
4119 if (updater)
4120 chunk_appendf(msg, " by '%s'", updater);
4121 chunk_appendf(msg, "\n");
4122
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004123 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004124}
4125
4126
Willy Tarreau21b069d2016-11-23 17:15:08 +01004127/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4128 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004129 * 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 +01004130 * used for CLI commands requiring a server name.
4131 * Important: the <arg> is modified to remove the '/'.
4132 */
4133struct server *cli_find_server(struct appctx *appctx, char *arg)
4134{
4135 struct proxy *px;
4136 struct server *sv;
4137 char *line;
4138
4139 /* split "backend/server" and make <line> point to server */
4140 for (line = arg; *line; line++)
4141 if (*line == '/') {
4142 *line++ = '\0';
4143 break;
4144 }
4145
4146 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004147 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004148 return NULL;
4149 }
4150
4151 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004152 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004153 return NULL;
4154 }
4155
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004156 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004157 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004158 return NULL;
4159 }
4160
4161 return sv;
4162}
4163
William Lallemand222baf22016-11-19 02:00:33 +01004164
Willy Tarreau46b7f532018-08-21 11:54:26 +02004165/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004166static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004167{
4168 struct server *sv;
4169 const char *warning;
4170
4171 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4172 return 1;
4173
4174 sv = cli_find_server(appctx, args[2]);
4175 if (!sv)
4176 return 1;
4177
4178 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004179 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004180 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004181 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004182 if (warning)
4183 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004184 }
4185 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004186 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004187 if (strcmp(args[4], "ready") == 0)
4188 srv_adm_set_ready(sv);
4189 else if (strcmp(args[4], "drain") == 0)
4190 srv_adm_set_drain(sv);
4191 else if (strcmp(args[4], "maint") == 0)
4192 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004193 else
4194 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004195 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004196 }
4197 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004198 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004199 if (sv->track)
4200 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004201 else if (strcmp(args[4], "up") == 0) {
4202 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004203 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004204 }
4205 else if (strcmp(args[4], "stopping") == 0) {
4206 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004207 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004208 }
4209 else if (strcmp(args[4], "down") == 0) {
4210 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004211 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004212 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004213 else
4214 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004215 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004216 }
4217 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004218 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004219 if (!(sv->agent.state & CHK_ST_ENABLED))
4220 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004221 else if (strcmp(args[4], "up") == 0) {
4222 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004223 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004224 }
4225 else if (strcmp(args[4], "down") == 0) {
4226 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004227 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004228 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004229 else
4230 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004231 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004232 }
Misiek2da082d2017-01-09 09:40:42 +01004233 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004234 char *addr = NULL;
4235 char *port = NULL;
4236 if (strlen(args[4]) == 0) {
4237 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4238 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004239 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004240 }
4241 addr = args[4];
4242 if (strcmp(args[5], "port") == 0)
4243 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004244 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004245 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004246 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004247 if (warning)
4248 cli_msg(appctx, LOG_WARNING, warning);
4249 }
4250 else if (strcmp(args[3], "agent-port") == 0) {
4251 char *port = NULL;
4252 if (strlen(args[4]) == 0) {
4253 cli_err(appctx, "set server <b>/<s> agent-port requires"
4254 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004255 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004256 }
4257 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004258 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004259 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004260 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004261 if (warning)
4262 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004263 }
4264 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004265 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004266 if (!(sv->agent.state & CHK_ST_ENABLED))
4267 cli_err(appctx, "agent checks are not enabled on this server.\n");
4268 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004269 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004270 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004271 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004272 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004273 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004274 else if (strcmp(args[3], "check-addr") == 0) {
4275 char *addr = NULL;
4276 char *port = NULL;
4277 if (strlen(args[4]) == 0) {
4278 cli_err(appctx, "set server <b>/<s> check-addr requires"
4279 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004280 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004281 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004282 addr = args[4];
4283 if (strcmp(args[5], "port") == 0)
4284 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004285 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004286 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004287 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004288 if (warning)
4289 cli_msg(appctx, LOG_WARNING, warning);
4290 }
4291 else if (strcmp(args[3], "check-port") == 0) {
4292 char *port = NULL;
4293 if (strlen(args[4]) == 0) {
4294 cli_err(appctx, "set server <b>/<s> check-port requires"
4295 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004296 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004297 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004298 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004299 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004300 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004301 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004302 if (warning)
4303 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004304 }
4305 else if (strcmp(args[3], "addr") == 0) {
4306 char *addr = NULL;
4307 char *port = NULL;
4308 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004309 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004310 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004311 }
4312 else {
4313 addr = args[4];
4314 }
4315 if (strcmp(args[5], "port") == 0) {
4316 port = args[6];
4317 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004318 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004319 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004320 if (warning)
4321 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004322 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004323 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004324 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004325 else if (strcmp(args[3], "fqdn") == 0) {
4326 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004327 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004328 goto out;
4329 }
4330 if (!sv->resolvers) {
4331 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4332 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004333 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004334 if (sv->srvrq) {
4335 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004336 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004337 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004338 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4339 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004340 /* ensure runtime resolver will process this new fqdn */
4341 if (sv->flags & SRV_F_NO_RESOLUTION) {
4342 sv->flags &= ~SRV_F_NO_RESOLUTION;
4343 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004344 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004345 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004346 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004347 if (warning)
4348 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004349 }
William Dauchyf6370442020-11-14 19:25:33 +01004350 else if (strcmp(args[3], "ssl") == 0) {
4351#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004352 if (sv->flags & SRV_F_DYNAMIC) {
4353 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4354 goto out;
4355 }
4356
William Dauchyf6370442020-11-14 19:25:33 +01004357 if (sv->ssl_ctx.ctx == NULL) {
4358 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4359 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004360 goto out;
4361 }
4362
4363 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4364 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004365 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004366 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004367 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004368 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004369 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004370 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004371 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004372 }
4373 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004374 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004375 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4376#else
4377 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4378#endif
4379 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004380 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004381 "usage: set server <backend>/<server> "
4382 "addr | agent | agent-addr | agent-port | agent-send | "
4383 "check-addr | check-port | fqdn | health | ssl | "
4384 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004385 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004386 out:
William Lallemand222baf22016-11-19 02:00:33 +01004387 return 1;
4388}
4389
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004390static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004391{
William Lallemand6b160942016-11-22 12:34:35 +01004392 struct proxy *px;
4393 struct server *sv;
4394 char *line;
4395
4396
4397 /* split "backend/server" and make <line> point to server */
4398 for (line = args[2]; *line; line++)
4399 if (*line == '/') {
4400 *line++ = '\0';
4401 break;
4402 }
4403
Willy Tarreau9d008692019-08-09 11:21:01 +02004404 if (!*line)
4405 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004406
Willy Tarreau9d008692019-08-09 11:21:01 +02004407 if (!get_backend_server(args[2], line, &px, &sv))
4408 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004409
4410 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004411 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4412 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004413 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004414 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004415 return 1;
4416}
4417
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004418/* Parse a "set weight" command.
4419 *
4420 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004421 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004422static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004423{
4424 struct server *sv;
4425 const char *warning;
4426
4427 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4428 return 1;
4429
4430 sv = cli_find_server(appctx, args[2]);
4431 if (!sv)
4432 return 1;
4433
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004434 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4435
William Lallemand6b160942016-11-22 12:34:35 +01004436 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004437 if (warning)
4438 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004439
4440 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4441
William Lallemand6b160942016-11-22 12:34:35 +01004442 return 1;
4443}
4444
Willy Tarreau46b7f532018-08-21 11:54:26 +02004445/* parse a "set maxconn server" command. It always returns 1.
4446 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004447 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004448 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004449static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004450{
4451 struct server *sv;
4452 const char *warning;
4453
4454 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4455 return 1;
4456
4457 sv = cli_find_server(appctx, args[3]);
4458 if (!sv)
4459 return 1;
4460
Amaury Denoyelle02742862021-06-18 11:11:36 +02004461 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4462
Willy Tarreaub8026272016-11-23 11:26:56 +01004463 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004464 if (warning)
4465 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004466
Amaury Denoyelle02742862021-06-18 11:11:36 +02004467 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4468
Willy Tarreaub8026272016-11-23 11:26:56 +01004469 return 1;
4470}
William Lallemand6b160942016-11-22 12:34:35 +01004471
Willy Tarreau46b7f532018-08-21 11:54:26 +02004472/* parse a "disable agent" command. It always returns 1.
4473 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004474 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004475 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004476static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004477{
4478 struct server *sv;
4479
4480 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4481 return 1;
4482
4483 sv = cli_find_server(appctx, args[2]);
4484 if (!sv)
4485 return 1;
4486
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004487 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004488 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004489 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004490 return 1;
4491}
4492
Willy Tarreau46b7f532018-08-21 11:54:26 +02004493/* parse a "disable health" command. It always returns 1.
4494 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004495 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004496 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004497static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004498{
4499 struct server *sv;
4500
4501 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4502 return 1;
4503
4504 sv = cli_find_server(appctx, args[2]);
4505 if (!sv)
4506 return 1;
4507
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004508 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004509 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004510 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004511 return 1;
4512}
4513
Willy Tarreau46b7f532018-08-21 11:54:26 +02004514/* parse a "disable server" command. It always returns 1.
4515 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004516 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004517 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004518static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004519{
4520 struct server *sv;
4521
4522 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4523 return 1;
4524
4525 sv = cli_find_server(appctx, args[2]);
4526 if (!sv)
4527 return 1;
4528
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004529 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004530 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004531 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004532 return 1;
4533}
4534
Willy Tarreau46b7f532018-08-21 11:54:26 +02004535/* parse a "enable agent" command. It always returns 1.
4536 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004537 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004538 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004539static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004540{
4541 struct server *sv;
4542
4543 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4544 return 1;
4545
4546 sv = cli_find_server(appctx, args[2]);
4547 if (!sv)
4548 return 1;
4549
Willy Tarreau9d008692019-08-09 11:21:01 +02004550 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4551 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004552
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004553 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004554 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004555 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004556 return 1;
4557}
4558
Willy Tarreau46b7f532018-08-21 11:54:26 +02004559/* parse a "enable health" command. It always returns 1.
4560 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004561 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004562 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004563static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004564{
4565 struct server *sv;
4566
4567 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4568 return 1;
4569
4570 sv = cli_find_server(appctx, args[2]);
4571 if (!sv)
4572 return 1;
4573
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004574 if (!(sv->check.state & CHK_ST_CONFIGURED))
4575 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4576
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004577 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004578 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004579 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004580 return 1;
4581}
4582
Willy Tarreau46b7f532018-08-21 11:54:26 +02004583/* parse a "enable server" command. It always returns 1.
4584 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004585 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004586 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004587static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004588{
4589 struct server *sv;
4590
4591 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4592 return 1;
4593
4594 sv = cli_find_server(appctx, args[2]);
4595 if (!sv)
4596 return 1;
4597
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004598 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004599 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004600 if (!(sv->flags & SRV_F_COOKIESET)
4601 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4602 sv->cookie)
4603 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004604 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004605 return 1;
4606}
4607
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004608/* Allocates data structure related to load balancing for the server <sv>. It
4609 * is only required for dynamic servers.
4610 *
4611 * At the moment, the server lock is not used as this function is only called
4612 * for a dynamic server not yet registered.
4613 *
4614 * Returns 1 on success, 0 on allocation failure.
4615 */
4616static int srv_alloc_lb(struct server *sv, struct proxy *be)
4617{
4618 int node;
4619
4620 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4621 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4622 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4623 sv->lb_nodes_now = 0;
4624
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004625 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4626 ((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 +01004627 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4628
4629 if (!sv->lb_nodes)
4630 return 0;
4631
4632 for (node = 0; node < sv->lb_nodes_tot; node++) {
4633 sv->lb_nodes[node].server = sv;
4634 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4635 }
4636 }
4637
4638 return 1;
4639}
4640
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004641/* updates the server's weight during a warmup stage. Once the final weight is
4642 * reached, the task automatically stops. Note that any server status change
4643 * must have updated s->last_change accordingly.
4644 */
4645static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4646{
4647 struct server *s = context;
4648
4649 /* by default, plan on stopping the task */
4650 t->expire = TICK_ETERNITY;
4651 if ((s->next_admin & SRV_ADMF_MAINT) ||
4652 (s->next_state != SRV_ST_STARTING))
4653 return t;
4654
4655 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4656
4657 /* recalculate the weights and update the state */
4658 server_recalc_eweight(s, 1);
4659
4660 /* probably that we can refill this server with a bit more connections */
4661 pendconn_grab_from_px(s);
4662
4663 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4664
4665 /* get back there in 1 second or 1/20th of the slowstart interval,
4666 * whichever is greater, resulting in small 5% steps.
4667 */
4668 if (s->next_state == SRV_ST_STARTING)
4669 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4670 return t;
4671}
4672
4673/* Allocate the slowstart task if the server is configured with a slowstart
4674 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4675 *
4676 * Returns 0 on success else non-zero.
4677 */
4678static int init_srv_slowstart(struct server *srv)
4679{
4680 struct task *t;
4681
4682 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004683 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004684 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4685 return ERR_ALERT | ERR_FATAL;
4686 }
4687
4688 /* We need a warmup task that will be called when the server
4689 * state switches from down to up.
4690 */
4691 srv->warmup = t;
4692 t->process = server_warmup;
4693 t->context = srv;
4694
4695 /* server can be in this state only because of */
4696 if (srv->next_state == SRV_ST_STARTING) {
4697 task_schedule(srv->warmup,
4698 tick_add(now_ms,
4699 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4700 }
4701 }
4702
4703 return ERR_NONE;
4704}
4705REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4706
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004707/* Memory allocation and initialization of the per_thr field.
4708 * Returns 0 if the field has been successfully initialized, -1 on failure.
4709 */
4710int srv_init_per_thr(struct server *srv)
4711{
4712 int i;
4713
4714 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004715 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4716 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004717 return -1;
4718
4719 for (i = 0; i < global.nbthread; i++) {
4720 srv->per_thr[i].idle_conns = EB_ROOT;
4721 srv->per_thr[i].safe_conns = EB_ROOT;
4722 srv->per_thr[i].avail_conns = EB_ROOT;
4723 MT_LIST_INIT(&srv->per_thr[i].streams);
4724 }
4725
4726 return 0;
4727}
4728
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004729/* Parse a "add server" command
4730 * Returns 0 if the server has been successfully initialized, 1 on failure.
4731 */
4732static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4733{
4734 struct proxy *be;
4735 struct server *srv;
4736 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004737 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004738 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004739 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4740
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004741 usermsgs_clr("CLI");
4742
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004743 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4744 return 1;
4745
4746 ++args;
4747
4748 sv_name = be_name = args[1];
4749 /* split backend/server arg */
4750 while (*sv_name && *(++sv_name)) {
4751 if (*sv_name == '/') {
4752 *sv_name = '\0';
4753 ++sv_name;
4754 break;
4755 }
4756 }
4757
4758 if (!*sv_name)
4759 return cli_err(appctx, "Require 'backend/server'.");
4760
Amaury Denoyellecece9182021-04-20 17:09:08 +02004761 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004762 if (!be)
4763 return cli_err(appctx, "No such backend.");
4764
4765 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004766 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004767 return 1;
4768 }
4769
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004770 /* At this point, some operations might not be thread-safe anymore. This
4771 * might be the case for parsing handlers which were designed to run
4772 * only at the starting stage on single-thread mode.
4773 *
4774 * Activate thread isolation to ensure thread-safety.
4775 */
4776 thread_isolate();
4777
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004778 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004779 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4780 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004781 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004782
4783 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004784 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004785
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004786 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004787 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004788 }
4789
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004790 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4791 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004792 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004793
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004794 /* A dynamic server does not currently support resolution.
4795 *
4796 * Initialize it explicitly to the "none" method to ensure no
4797 * resolution will ever be executed.
4798 */
4799 srv->init_addr_methods = SRV_IADDR_NONE;
4800
Amaury Denoyelle30467232021-03-12 18:03:27 +01004801 if (srv->mux_proto) {
4802 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004803 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004804 goto out;
4805 }
4806 }
4807
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004808 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004809 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004810 goto out;
4811 }
4812
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004813 if (srv->max_idle_conns != 0) {
4814 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4815 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004816 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004817 goto out;
4818 }
4819 }
4820
4821 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004822 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004823 goto out;
4824 }
4825
4826 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4827 COUNTERS_SV,
4828 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004829 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004830 goto out;
4831 }
4832
Aurelien DARRAGON86207e72023-02-08 11:55:08 +01004833 /* ensure minconn/maxconn consistency */
4834 srv_minmax_conn_apply(srv);
4835
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004836 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4837 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004838 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4839 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4840 goto out;
4841 }
4842 }
4843
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004844 if (srv->trackit) {
4845 if (srv_apply_track(srv, be))
4846 goto out;
4847 }
4848
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004849 /* Init check/agent if configured. The check is manually disabled
4850 * because a dynamic server is started in a disable state. It must be
4851 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004852 */
4853 if (srv->do_check) {
4854 if (init_srv_check(srv))
4855 goto out;
4856
4857 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004858 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004859
4860 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004861 if (init_srv_agent_check(srv))
4862 goto out;
4863
4864 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004865 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004866
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004867 /* Init slowstart if needed. */
4868 if (init_srv_slowstart(srv))
4869 goto out;
4870
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004871 /* Attach the server to the end of the proxy linked list. Note that this
4872 * operation is not thread-safe so this is executed under thread
4873 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004874 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004875 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004876 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004877
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004878 /* TODO use a double-linked list for px->srv */
4879 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004880 struct server *next = be->srv;
4881
4882 while (1) {
4883 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004884 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004885 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004886 goto out;
4887 }
4888
4889 if (!next->next)
4890 break;
4891
4892 next = next->next;
4893 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004894
4895 next->next = srv;
4896 }
4897 else {
4898 srv->next = be->srv;
4899 be->srv = srv;
4900 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004901
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004902 /* generate the server id if not manually specified */
4903 if (!srv->puid) {
4904 next_id = get_next_id(&be->conf.used_server_id, 1);
4905 if (!next_id) {
4906 ha_alert("Cannot attach server : no id left in proxy\n");
4907 goto out;
4908 }
4909
4910 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004911 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004912 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004913
4914 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004915 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4916 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004917 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4918 if (srv->addr_node.key)
4919 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004920
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004921 /* check if LSB bit (odd bit) is set for reuse_cnt */
4922 if (srv_id_reuse_cnt & 1) {
4923 /* cnt must be increased */
4924 srv_id_reuse_cnt++;
4925 }
4926 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4927 * save some space
4928 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
4929 */
4930 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
4931
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01004932 /* adding server cannot fail when we reach this:
4933 * publishing EVENT_HDL_SUB_SERVER_ADD
4934 */
4935 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
4936
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004937 thread_release();
4938
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004939 /* Start the check task. The server must be fully initialized.
4940 *
4941 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4942 * need to randomly spread the task interval for dynamic servers.
4943 */
4944 if (srv->check.state & CHK_ST_CONFIGURED) {
4945 if (!start_check_task(&srv->check, 0, 1, 1))
4946 ha_alert("System might be unstable, consider to execute a reload");
4947 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004948 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004949 if (!start_check_task(&srv->agent, 0, 1, 1))
4950 ha_alert("System might be unstable, consider to execute a reload");
4951 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004952
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004953 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004954 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004955
4956 return 0;
4957
4958out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004959 if (srv) {
4960 if (srv->track)
4961 release_server_track(srv);
4962
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004963 if (srv->check.state & CHK_ST_CONFIGURED)
4964 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004965 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004966 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004967
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004968 /* remove the server from the proxy linked list */
4969 if (be->srv == srv) {
4970 be->srv = srv->next;
4971 }
4972 else {
4973 struct server *prev;
4974 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4975 ;
4976 if (prev)
4977 prev->next = srv->next;
4978 }
4979
4980 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004981
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004982 thread_release();
4983
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004984 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004985 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004986
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004987 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004988 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004989
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004990 return 1;
4991}
4992
Amaury Denoyellee5580432021-04-15 14:41:20 +02004993/* Parse a "del server" command
4994 * Returns 0 if the server has been successfully initialized, 1 on failure.
4995 */
4996static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4997{
4998 struct proxy *be;
4999 struct server *srv;
5000 char *be_name, *sv_name;
5001
5002 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
5003 return 1;
5004
5005 ++args;
5006
5007 sv_name = be_name = args[1];
5008 /* split backend/server arg */
5009 while (*sv_name && *(++sv_name)) {
5010 if (*sv_name == '/') {
5011 *sv_name = '\0';
5012 ++sv_name;
5013 break;
5014 }
5015 }
5016
5017 if (!*sv_name)
5018 return cli_err(appctx, "Require 'backend/server'.");
5019
5020 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005021 * requires thread isolation. In addition, any place referencing the
5022 * server about to be deleted would be unsafe after our operation, so
5023 * we must be certain to be alone so that no other thread has even
5024 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005025 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005026 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005027
5028 get_backend_server(be_name, sv_name, &be, &srv);
5029 if (!be) {
5030 cli_err(appctx, "No such backend.");
5031 goto out;
5032 }
5033
5034 if (!srv) {
5035 cli_err(appctx, "No such server.");
5036 goto out;
5037 }
5038
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005039 if (srv->flags & SRV_F_NON_PURGEABLE) {
5040 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 +02005041 goto out;
5042 }
5043
5044 /* Only servers in maintenance can be deleted. This ensures that the
5045 * server is not present anymore in the lb structures (through
5046 * lbprm.set_server_status_down).
5047 */
5048 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5049 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5050 goto out;
5051 }
5052
5053 /* Ensure that there is no active/idle/pending connection on the server.
5054 *
5055 * TODO idle connections should not prevent server deletion. A proper
5056 * cleanup function should be implemented to be used here.
5057 */
5058 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005059 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005060 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5061 goto out;
5062 }
5063
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005064 /* removing cannot fail anymore when we reach this:
5065 * publishing EVENT_HDL_SUB_SERVER_DEL
5066 */
5067 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5068
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005069 /* remove srv from tracking list */
5070 if (srv->track)
5071 release_server_track(srv);
5072
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005073 /* stop the check task if running */
5074 if (srv->check.state & CHK_ST_CONFIGURED)
5075 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005076 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005077 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005078
5079 /* detach the server from the proxy linked list
5080 * The proxy servers list is currently not protected by a lock, so this
5081 * requires thread_isolate/release.
5082 */
5083
5084 /* be->srv cannot be empty since we have already found the server with
5085 * get_backend_server */
5086 BUG_ON(!be->srv);
5087 if (be->srv == srv) {
5088 be->srv = srv->next;
5089 }
5090 else {
5091 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005092 for (next = be->srv; srv != next->next; next = next->next) {
5093 /* srv cannot be not found since we have already found
5094 * it with get_backend_server */
5095 BUG_ON(!next);
5096 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005097
Amaury Denoyellee5580432021-04-15 14:41:20 +02005098 next->next = srv->next;
5099 }
5100
5101 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005102 eb32_delete(&srv->conf.id);
5103 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005104 if (srv->addr_node.key)
5105 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005106
5107 /* remove srv from idle_node tree for idle conn cleanup */
5108 eb32_delete(&srv->idle_node);
5109
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005110 /* set LSB bit (odd bit) for reuse_cnt */
5111 srv_id_reuse_cnt |= 1;
5112
Amaury Denoyellee5580432021-04-15 14:41:20 +02005113 thread_release();
5114
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005115 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005116 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005117
5118 cli_msg(appctx, LOG_INFO, "Server deleted.");
5119
5120 return 0;
5121
5122out:
5123 thread_release();
5124
5125 return 1;
5126}
5127
William Lallemand222baf22016-11-19 02:00:33 +01005128/* register cli keywords */
5129static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005130 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5131 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005132 { { "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 +02005133 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5134 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005135 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5136 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5137 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5138 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5139 { { "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 +01005140 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5141 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005142 {{},}
5143}};
5144
Willy Tarreau0108d902018-11-25 19:14:37 +01005145INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005146
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005147/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5148 * field is used to retrieve the identifier of the tracked server, either with
5149 * the format "proxy/server" or just "server". <curproxy> must point to the
5150 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5151 * to find the tracked server.
5152 *
5153 * Returns 0 if the server track has been activated else non-zero.
5154 *
5155 * Not thread-safe.
5156 */
5157int srv_apply_track(struct server *srv, struct proxy *curproxy)
5158{
5159 struct proxy *px;
5160 struct server *strack, *loop;
5161 char *pname, *sname;
5162
5163 if (!srv->trackit)
5164 return 1;
5165
5166 pname = srv->trackit;
5167 sname = strrchr(pname, '/');
5168
5169 if (sname) {
5170 *sname++ = '\0';
5171 }
5172 else {
5173 sname = pname;
5174 pname = NULL;
5175 }
5176
5177 if (pname) {
5178 px = proxy_be_by_name(pname);
5179 if (!px) {
5180 ha_alert("unable to find required proxy '%s' for tracking.\n",
5181 pname);
5182 return 1;
5183 }
5184 }
5185 else {
5186 px = curproxy;
5187 }
5188
5189 strack = findserver(px, sname);
5190 if (!strack) {
5191 ha_alert("unable to find required server '%s' for tracking.\n",
5192 sname);
5193 return 1;
5194 }
5195
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005196 if (strack->flags & SRV_F_DYNAMIC) {
5197 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5198 px->id, strack->id);
5199 return 1;
5200 }
5201
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005202 if (!strack->do_check && !strack->do_agent && !strack->track &&
5203 !strack->trackit) {
5204 ha_alert("unable to use %s/%s for "
5205 "tracking as it does not have any check nor agent enabled.\n",
5206 px->id, strack->id);
5207 return 1;
5208 }
5209
5210 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5211 ;
5212
5213 if (srv == strack || loop) {
5214 ha_alert("unable to track %s/%s as it "
5215 "belongs to a tracking chain looping back to %s/%s.\n",
5216 px->id, strack->id, px->id,
5217 srv == strack ? strack->id : loop->id);
5218 return 1;
5219 }
5220
5221 if (curproxy != px &&
5222 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5223 ha_alert("unable to use %s/%s for"
5224 "tracking: disable-on-404 option inconsistency.\n",
5225 px->id, strack->id);
5226 return 1;
5227 }
5228
5229 srv->track = strack;
5230 srv->tracknext = strack->trackers;
5231 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005232 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005233
5234 ha_free(&srv->trackit);
5235
5236 return 0;
5237}
5238
Emeric Brun64cc49c2017-10-03 14:46:45 +02005239/*
5240 * This function applies server's status changes, it is
5241 * is designed to be called asynchronously.
5242 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005243 * Must be called with the server lock held. This may also be called at init
5244 * time as the result of parsing the state file, in which case no lock will be
5245 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005246 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005247static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005248{
5249 struct check *check = &s->check;
5250 int xferred;
5251 struct proxy *px = s->proxy;
5252 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5253 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5254 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005255 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005256
Emeric Brun64cc49c2017-10-03 14:46:45 +02005257 /* If currently main is not set we try to apply pending state changes */
5258 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5259 int next_admin;
5260
5261 /* Backup next admin */
5262 next_admin = s->next_admin;
5263
5264 /* restore current admin state */
5265 s->next_admin = s->cur_admin;
5266
5267 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005268 /* no maintenance + server DOWN: publish event SERVER DOWN */
5269 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5270
Emeric Brun64cc49c2017-10-03 14:46:45 +02005271 s->last_change = now.tv_sec;
5272 if (s->proxy->lbprm.set_server_status_down)
5273 s->proxy->lbprm.set_server_status_down(s);
5274
5275 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5276 srv_shutdown_streams(s, SF_ERR_DOWN);
5277
5278 /* we might have streams queued on this server and waiting for
5279 * a connection. Those which are redispatchable will be queued
5280 * to another server or to the proxy itself.
5281 */
5282 xferred = pendconn_redistribute(s);
5283
5284 tmptrash = alloc_trash_chunk();
5285 if (tmptrash) {
5286 chunk_printf(tmptrash,
5287 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5288 s->proxy->id, s->id);
5289
Emeric Brun5a133512017-10-19 14:42:30 +02005290 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005291 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005292
5293 /* we don't send an alert if the server was previously paused */
5294 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005295 send_log(s->proxy, log_level, "%s.\n",
5296 tmptrash->area);
5297 send_email_alert(s, log_level, "%s",
5298 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005299 free_trash_chunk(tmptrash);
5300 tmptrash = NULL;
5301 }
5302 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5303 set_backend_down(s->proxy);
5304
5305 s->counters.down_trans++;
5306 }
5307 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5308 s->last_change = now.tv_sec;
5309 if (s->proxy->lbprm.set_server_status_down)
5310 s->proxy->lbprm.set_server_status_down(s);
5311
5312 /* we might have streams queued on this server and waiting for
5313 * a connection. Those which are redispatchable will be queued
5314 * to another server or to the proxy itself.
5315 */
5316 xferred = pendconn_redistribute(s);
5317
5318 tmptrash = alloc_trash_chunk();
5319 if (tmptrash) {
5320 chunk_printf(tmptrash,
5321 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5322 s->proxy->id, s->id);
5323
Emeric Brun5a133512017-10-19 14:42:30 +02005324 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005325
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005326 ha_warning("%s.\n", tmptrash->area);
5327 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5328 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005329 free_trash_chunk(tmptrash);
5330 tmptrash = NULL;
5331 }
5332
5333 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5334 set_backend_down(s->proxy);
5335 }
5336 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5337 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005338 /* no maintenance + server going UP: publish event SERVER UP */
5339 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5340
Emeric Brun64cc49c2017-10-03 14:46:45 +02005341 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5342 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5343 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5344 s->proxy->last_change = now.tv_sec;
5345 }
5346
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01005347 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02005348 s->down_time += now.tv_sec - s->last_change;
5349
5350 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005351 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005352 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5353
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005354 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005355 /* now propagate the status change to any LB algorithms */
5356 if (px->lbprm.update_server_eweight)
5357 px->lbprm.update_server_eweight(s);
5358 else if (srv_willbe_usable(s)) {
5359 if (px->lbprm.set_server_status_up)
5360 px->lbprm.set_server_status_up(s);
5361 }
5362 else {
5363 if (px->lbprm.set_server_status_down)
5364 px->lbprm.set_server_status_down(s);
5365 }
5366
5367 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5368 * and it's not a backup server and its effective weight is > 0,
5369 * then it can accept new connections, so we shut down all streams
5370 * on all backup servers.
5371 */
5372 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5373 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5374 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5375
5376 /* check if we can handle some connections queued at the proxy. We
5377 * will take as many as we can handle.
5378 */
5379 xferred = pendconn_grab_from_px(s);
5380
5381 tmptrash = alloc_trash_chunk();
5382 if (tmptrash) {
5383 chunk_printf(tmptrash,
5384 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5385 s->proxy->id, s->id);
5386
Emeric Brun5a133512017-10-19 14:42:30 +02005387 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005388 ha_warning("%s.\n", tmptrash->area);
5389 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5390 tmptrash->area);
5391 send_email_alert(s, LOG_NOTICE, "%s",
5392 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005393 free_trash_chunk(tmptrash);
5394 tmptrash = NULL;
5395 }
5396
5397 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5398 set_backend_down(s->proxy);
5399 }
5400 else if (s->cur_eweight != s->next_eweight) {
5401 /* now propagate the status change to any LB algorithms */
5402 if (px->lbprm.update_server_eweight)
5403 px->lbprm.update_server_eweight(s);
5404 else if (srv_willbe_usable(s)) {
5405 if (px->lbprm.set_server_status_up)
5406 px->lbprm.set_server_status_up(s);
5407 }
5408 else {
5409 if (px->lbprm.set_server_status_down)
5410 px->lbprm.set_server_status_down(s);
5411 }
5412
5413 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5414 set_backend_down(s->proxy);
5415 }
5416
5417 s->next_admin = next_admin;
5418 }
5419
Emeric Brun5a133512017-10-19 14:42:30 +02005420 /* reset operational state change */
5421 *s->op_st_chg.reason = 0;
5422 s->op_st_chg.status = s->op_st_chg.code = -1;
5423 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005424
5425 /* Now we try to apply pending admin changes */
5426
5427 /* Maintenance must also disable health checks */
5428 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5429 if (s->check.state & CHK_ST_ENABLED) {
5430 s->check.state |= CHK_ST_PAUSED;
5431 check->health = 0;
5432 }
5433
5434 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005435 tmptrash = alloc_trash_chunk();
5436 if (tmptrash) {
5437 chunk_printf(tmptrash,
5438 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5439 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5440 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005441
Olivier Houchard796a2b32017-10-24 17:42:47 +02005442 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005443
Olivier Houchard796a2b32017-10-24 17:42:47 +02005444 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005445 ha_warning("%s.\n", tmptrash->area);
5446 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5447 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005448 }
5449 free_trash_chunk(tmptrash);
5450 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005451 }
Emeric Brun8f298292017-12-06 16:47:17 +01005452 /* commit new admin status */
5453
5454 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005455 }
5456 else { /* server was still running */
5457 check->health = 0; /* failure */
5458 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005459
5460 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005461 if (s->proxy->lbprm.set_server_status_down)
5462 s->proxy->lbprm.set_server_status_down(s);
5463
Emeric Brun64cc49c2017-10-03 14:46:45 +02005464 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5465 srv_shutdown_streams(s, SF_ERR_DOWN);
5466
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005467 /* maintenance on previously running server: publish event SERVER DOWN */
5468 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DOWN, s, 0);
5469
William Dauchy6318d332020-05-02 21:52:36 +02005470 /* force connection cleanup on the given server */
5471 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005472 /* we might have streams queued on this server and waiting for
5473 * a connection. Those which are redispatchable will be queued
5474 * to another server or to the proxy itself.
5475 */
5476 xferred = pendconn_redistribute(s);
5477
5478 tmptrash = alloc_trash_chunk();
5479 if (tmptrash) {
5480 chunk_printf(tmptrash,
5481 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5482 s->flags & SRV_F_BACKUP ? "Backup " : "",
5483 s->proxy->id, s->id,
5484 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5485
5486 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5487
5488 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005489 ha_warning("%s.\n", tmptrash->area);
5490 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5491 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005492 }
5493 free_trash_chunk(tmptrash);
5494 tmptrash = NULL;
5495 }
5496 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5497 set_backend_down(s->proxy);
5498
5499 s->counters.down_trans++;
5500 }
5501 }
5502 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5503 /* OK here we're leaving maintenance, we have many things to check,
5504 * because the server might possibly be coming back up depending on
5505 * its state. In practice, leaving maintenance means that we should
5506 * immediately turn to UP (more or less the slowstart) under the
5507 * following conditions :
5508 * - server is neither checked nor tracked
5509 * - server tracks another server which is not checked
5510 * - server tracks another server which is already up
5511 * Which sums up as something simpler :
5512 * "either the tracking server is up or the server's checks are disabled
5513 * or up". Otherwise we only re-enable health checks. There's a special
5514 * case associated to the stopping state which can be inherited. Note
5515 * that the server might still be in drain mode, which is naturally dealt
5516 * with by the lower level functions.
5517 */
5518
5519 if (s->check.state & CHK_ST_ENABLED) {
5520 s->check.state &= ~CHK_ST_PAUSED;
5521 check->health = check->rise; /* start OK but check immediately */
5522 }
5523
5524 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5525 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5526 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5527 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5528 s->next_state = SRV_ST_STOPPING;
5529 }
5530 else {
Willy Tarreaud332f132021-08-04 19:35:13 +02005531 s->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005532 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005533 if (s->slowstart > 0) {
5534 if (s->warmup)
5535 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5536 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005537 else
5538 s->next_state = SRV_ST_RUNNING;
5539 }
5540
5541 }
5542
Aurelien DARRAGON22f82f82022-11-25 18:07:49 +01005543 /* ignore if server stays down when leaving maintenance mode */
5544 if (s->next_state != SRV_ST_STOPPED) {
5545 /* leaving maintenance + server UP: publish event SERVER UP */
5546 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_UP, s, 0);
5547 }
5548
Emeric Brun64cc49c2017-10-03 14:46:45 +02005549 tmptrash = alloc_trash_chunk();
5550 if (tmptrash) {
5551 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5552 chunk_printf(tmptrash,
5553 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5554 s->flags & SRV_F_BACKUP ? "Backup " : "",
5555 s->proxy->id, s->id,
5556 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5557 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5558 }
5559 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5560 chunk_printf(tmptrash,
5561 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5562 s->flags & SRV_F_BACKUP ? "Backup " : "",
5563 s->proxy->id, s->id, s->hostname,
5564 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5565 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5566 }
5567 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5568 chunk_printf(tmptrash,
5569 "%sServer %s/%s is %s/%s (leaving maintenance)",
5570 s->flags & SRV_F_BACKUP ? "Backup " : "",
5571 s->proxy->id, s->id,
5572 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5573 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5574 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005575 ha_warning("%s.\n", tmptrash->area);
5576 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5577 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005578 free_trash_chunk(tmptrash);
5579 tmptrash = NULL;
5580 }
5581
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005582 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005583 /* now propagate the status change to any LB algorithms */
5584 if (px->lbprm.update_server_eweight)
5585 px->lbprm.update_server_eweight(s);
5586 else if (srv_willbe_usable(s)) {
5587 if (px->lbprm.set_server_status_up)
5588 px->lbprm.set_server_status_up(s);
5589 }
5590 else {
5591 if (px->lbprm.set_server_status_down)
5592 px->lbprm.set_server_status_down(s);
5593 }
5594
5595 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5596 set_backend_down(s->proxy);
Willy Tarreaud332f132021-08-04 19:35:13 +02005597 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act))
5598 s->proxy->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005599
Willy Tarreau6a78e612018-08-07 10:14:53 +02005600 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5601 * and it's not a backup server and its effective weight is > 0,
5602 * then it can accept new connections, so we shut down all streams
5603 * on all backup servers.
5604 */
5605 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5606 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5607 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5608
5609 /* check if we can handle some connections queued at the proxy. We
5610 * will take as many as we can handle.
5611 */
5612 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005613 }
5614 else if (s->next_admin & SRV_ADMF_MAINT) {
5615 /* remaining in maintenance mode, let's inform precisely about the
5616 * situation.
5617 */
5618 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5619 tmptrash = alloc_trash_chunk();
5620 if (tmptrash) {
5621 chunk_printf(tmptrash,
5622 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5623 s->flags & SRV_F_BACKUP ? "Backup " : "",
5624 s->proxy->id, s->id);
5625
5626 if (s->track) /* normally it's mandatory here */
5627 chunk_appendf(tmptrash, " via %s/%s",
5628 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005629 ha_warning("%s.\n", tmptrash->area);
5630 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5631 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005632 free_trash_chunk(tmptrash);
5633 tmptrash = NULL;
5634 }
5635 }
5636 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5637 tmptrash = alloc_trash_chunk();
5638 if (tmptrash) {
5639 chunk_printf(tmptrash,
5640 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5641 s->flags & SRV_F_BACKUP ? "Backup " : "",
5642 s->proxy->id, s->id, s->hostname);
5643
5644 if (s->track) /* normally it's mandatory here */
5645 chunk_appendf(tmptrash, " via %s/%s",
5646 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005647 ha_warning("%s.\n", tmptrash->area);
5648 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5649 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005650 free_trash_chunk(tmptrash);
5651 tmptrash = NULL;
5652 }
5653 }
5654 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5655 tmptrash = alloc_trash_chunk();
5656 if (tmptrash) {
5657 chunk_printf(tmptrash,
5658 "%sServer %s/%s remains in forced maintenance",
5659 s->flags & SRV_F_BACKUP ? "Backup " : "",
5660 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005661 ha_warning("%s.\n", tmptrash->area);
5662 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5663 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005664 free_trash_chunk(tmptrash);
5665 tmptrash = NULL;
5666 }
5667 }
5668 /* don't report anything when leaving drain mode and remaining in maintenance */
5669
5670 s->cur_admin = s->next_admin;
5671 }
5672
5673 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5674 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5675 /* drain state is applied only if not yet in maint */
5676
5677 s->last_change = now.tv_sec;
5678 if (px->lbprm.set_server_status_down)
5679 px->lbprm.set_server_status_down(s);
5680
5681 /* we might have streams queued on this server and waiting for
5682 * a connection. Those which are redispatchable will be queued
5683 * to another server or to the proxy itself.
5684 */
5685 xferred = pendconn_redistribute(s);
5686
5687 tmptrash = alloc_trash_chunk();
5688 if (tmptrash) {
5689 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5690 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5691 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5692
5693 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5694
5695 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005696 ha_warning("%s.\n", tmptrash->area);
5697 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5698 tmptrash->area);
5699 send_email_alert(s, LOG_NOTICE, "%s",
5700 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005701 }
5702 free_trash_chunk(tmptrash);
5703 tmptrash = NULL;
5704 }
5705
5706 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5707 set_backend_down(s->proxy);
5708 }
5709 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5710 /* OK completely leaving drain mode */
5711 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5712 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5713 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5714 s->proxy->last_change = now.tv_sec;
5715 }
5716
5717 if (s->last_change < now.tv_sec) // ignore negative times
5718 s->down_time += now.tv_sec - s->last_change;
5719 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005720 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005721
5722 tmptrash = alloc_trash_chunk();
5723 if (tmptrash) {
5724 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5725 chunk_printf(tmptrash,
5726 "%sServer %s/%s is %s (leaving forced drain)",
5727 s->flags & SRV_F_BACKUP ? "Backup " : "",
5728 s->proxy->id, s->id,
5729 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5730 }
5731 else {
5732 chunk_printf(tmptrash,
5733 "%sServer %s/%s is %s (leaving drain)",
5734 s->flags & SRV_F_BACKUP ? "Backup " : "",
5735 s->proxy->id, s->id,
5736 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5737 if (s->track) /* normally it's mandatory here */
5738 chunk_appendf(tmptrash, " via %s/%s",
5739 s->track->proxy->id, s->track->id);
5740 }
5741
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005742 ha_warning("%s.\n", tmptrash->area);
5743 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5744 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005745 free_trash_chunk(tmptrash);
5746 tmptrash = NULL;
5747 }
5748
5749 /* now propagate the status change to any LB algorithms */
5750 if (px->lbprm.update_server_eweight)
5751 px->lbprm.update_server_eweight(s);
5752 else if (srv_willbe_usable(s)) {
5753 if (px->lbprm.set_server_status_up)
5754 px->lbprm.set_server_status_up(s);
5755 }
5756 else {
5757 if (px->lbprm.set_server_status_down)
5758 px->lbprm.set_server_status_down(s);
5759 }
5760 }
5761 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5762 /* remaining in drain mode after removing one of its flags */
5763
5764 tmptrash = alloc_trash_chunk();
5765 if (tmptrash) {
5766 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5767 chunk_printf(tmptrash,
5768 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5769 s->flags & SRV_F_BACKUP ? "Backup " : "",
5770 s->proxy->id, s->id);
5771
5772 if (s->track) /* normally it's mandatory here */
5773 chunk_appendf(tmptrash, " via %s/%s",
5774 s->track->proxy->id, s->track->id);
5775 }
5776 else {
5777 chunk_printf(tmptrash,
5778 "%sServer %s/%s remains in forced drain mode",
5779 s->flags & SRV_F_BACKUP ? "Backup " : "",
5780 s->proxy->id, s->id);
5781 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005782 ha_warning("%s.\n", tmptrash->area);
5783 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5784 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005785 free_trash_chunk(tmptrash);
5786 tmptrash = NULL;
5787 }
5788
5789 /* commit new admin status */
5790
5791 s->cur_admin = s->next_admin;
5792 }
5793 }
5794
5795 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005796 *s->adm_st_chg_cause = 0;
5797}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005798
Willy Tarreau144f84a2021-03-02 16:09:26 +01005799struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005800{
5801 struct connection *conn;
5802
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005803 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005804 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005805 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005806 }
5807
5808 return task;
5809}
5810
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005811/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005812 * moving them all.
5813 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005814 *
5815 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005816 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005817static 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 +02005818{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005819 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005820 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005821 int i = 0;
5822
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005823 node = eb_first(idle_tree);
5824 while (node) {
5825 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005826 if (toremove_nb != -1 && i >= toremove_nb)
5827 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005828
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005829 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005830 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005831 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005832 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005833
5834 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005835 }
5836 return i;
5837}
William Dauchy6318d332020-05-02 21:52:36 +02005838/* cleanup connections for a given server
5839 * might be useful when going on forced maintenance or live changing ip/port
5840 */
William Dauchy707ad322020-05-04 13:52:40 +02005841static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005842{
William Dauchy6318d332020-05-02 21:52:36 +02005843 int did_remove;
5844 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005845
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005846 /* nothing to do if pool-max-conn is null */
5847 if (!srv->max_idle_conns)
5848 return;
5849
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005850 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005851 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005852 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005853 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005854 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 +02005855 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005856 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 +02005857 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005858 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005859 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005860 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005861
5862 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5863 break;
William Dauchy6318d332020-05-02 21:52:36 +02005864 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005865}
5866
5867/* removes an idle conn after updating the server idle conns counters */
5868void srv_release_conn(struct server *srv, struct connection *conn)
5869{
5870 if (conn->flags & CO_FL_LIST_MASK) {
5871 /* The connection is currently in the server's idle list, so tell it
5872 * there's one less connection available in that list.
5873 */
5874 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5875 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5876 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5877 }
5878 else {
5879 /* The connection is not private and not in any server's idle
5880 * list, so decrement the current number of used connections
5881 */
5882 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5883 }
5884
5885 /* Remove the connection from any tree (safe, idle or available) */
5886 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5887 conn_delete_from_tree(&conn->hash_node->node);
5888 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5889}
5890
5891/* retrieve a connection from its <hash> in <tree>
5892 * returns NULL if no connection found
5893 */
5894struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5895{
Willy Tarreau85223482022-09-29 20:32:43 +02005896 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005897 struct connection *conn = NULL;
5898 struct conn_hash_node *hash_node = NULL;
5899
Willy Tarreau85223482022-09-29 20:32:43 +02005900 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005901 if (node) {
5902 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5903 conn = hash_node->conn;
5904 }
5905
5906 return conn;
5907}
5908
5909/* retrieve the next connection sharing the same hash as <conn>
5910 * returns NULL if no connection found
5911 */
5912struct connection *srv_lookup_conn_next(struct connection *conn)
5913{
Willy Tarreau85223482022-09-29 20:32:43 +02005914 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005915 struct connection *next_conn = NULL;
5916 struct conn_hash_node *hash_node = NULL;
5917
Willy Tarreau85223482022-09-29 20:32:43 +02005918 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005919 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005920 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005921 next_conn = hash_node->conn;
5922 }
5923
5924 return next_conn;
5925}
5926
5927/* This adds an idle connection to the server's list if the connection is
5928 * reusable, not held by any owner anymore, but still has available streams.
5929 */
5930int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5931{
5932 /* we try to keep the connection in the server's idle list
5933 * if we don't have too many FD in use, and if the number of
5934 * idle+current conns is lower than what was observed before
5935 * last purge, or if we already don't have idle conns for the
5936 * current thread and we don't exceed last count by global.nbthread.
5937 */
5938 if (!(conn->flags & CO_FL_PRIVATE) &&
5939 srv && srv->pool_purge_delay > 0 &&
5940 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
5941 ha_used_fds < global.tune.pool_high_count &&
5942 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
5943 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
5944 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
5945 (ha_used_fds < global.tune.pool_low_count &&
5946 (srv->curr_used_conns + srv->curr_idle_conns <=
5947 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
5948 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
5949 int retadd;
5950
5951 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
5952 if (retadd > srv->max_idle_conns) {
5953 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5954 return 0;
5955 }
5956 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5957
5958 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5959 conn_delete_from_tree(&conn->hash_node->node);
5960
5961 if (is_safe) {
5962 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005963 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005964 _HA_ATOMIC_INC(&srv->curr_safe_nb);
5965 } else {
5966 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005967 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005968 _HA_ATOMIC_INC(&srv->curr_idle_nb);
5969 }
5970 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5971 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
5972
5973 __ha_barrier_full();
5974 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5975 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5976 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5977 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5978 now_ms);
5979 eb32_insert(&idle_conn_srv, &srv->idle_node);
5980 if (!task_in_wq(idle_conn_task) && !
5981 task_in_rq(idle_conn_task)) {
5982 task_schedule(idle_conn_task,
5983 srv->idle_node.key);
5984 }
5985
5986 }
5987 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5988 }
5989 return 1;
5990 }
5991 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02005992}
5993
Willy Tarreau144f84a2021-03-02 16:09:26 +01005994struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005995{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005996 struct server *srv;
5997 struct eb32_node *eb;
5998 int i;
5999 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006000
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006001 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006002 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
6003 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006004 int exceed_conns;
6005 int to_kill;
6006 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01006007
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006008 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
6009 if (!eb) {
6010 /* we might have reached the end of the tree, typically because
6011 * <now_ms> is in the first half and we're first scanning the last
6012 * half. Let's loop back to the beginning of the tree now.
6013 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006014
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006015 eb = eb32_first(&idle_conn_srv);
6016 if (likely(!eb))
6017 break;
6018 }
6019 if (tick_is_lt(now_ms, eb->key)) {
6020 /* timer not expired yet, revisit it later */
6021 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006022 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006023 }
6024 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006025
6026 /* Calculate how many idle connections we want to kill :
6027 * we want to remove half the difference between the total
6028 * of established connections (used or idle) and the max
6029 * number of used connections.
6030 */
6031 curr_idle = srv->curr_idle_conns;
6032 if (curr_idle == 0)
6033 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006034 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006035 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006036
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006037 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006038 if (srv->est_need_conns < srv->max_used_conns)
6039 srv->est_need_conns = srv->max_used_conns;
6040
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006041 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006042
Willy Tarreau18ed7892020-07-02 19:05:30 +02006043 if (exceed_conns <= 0)
6044 goto remove;
6045
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006046 /* check all threads starting with ours */
6047 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006048 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006049 int j;
6050 int did_remove = 0;
6051
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006052 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6053 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006054
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006055 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006056 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 +02006057 if (j > 0)
6058 did_remove = 1;
6059 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006060 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 +01006061 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006062 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006063
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006064 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006065 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006066
6067 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6068 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006069 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006070remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006071 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006072
6073 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006074 /* There are still more idle connections, add the
6075 * server back in the tree.
6076 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006077 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006078 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006079 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006080 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006081 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006082 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6083
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006084 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006085 return task;
6086}
Olivier Houchard88698d92019-04-16 19:07:22 +02006087
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006088/* Close remaining idle connections. This functions is designed to be run on
6089 * process shutdown. This guarantees a proper socket shutdown to avoid
6090 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6091 * bypassed.
6092 *
6093 * This function is not thread-safe so it must only be called via a global
6094 * deinit function.
6095 */
6096static void srv_close_idle_conns(struct server *srv)
6097{
6098 struct eb_root **cleaned_tree;
6099 int i;
6100
6101 for (i = 0; i < global.nbthread; ++i) {
6102 struct eb_root *conn_trees[] = {
6103 &srv->per_thr[i].idle_conns,
6104 &srv->per_thr[i].safe_conns,
6105 &srv->per_thr[i].avail_conns,
6106 NULL
6107 };
6108
6109 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6110 while (!eb_is_empty(*cleaned_tree)) {
6111 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6112 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6113 struct connection *conn = conn_hash_node->conn;
6114
6115 if (conn->ctrl->ctrl_close)
6116 conn->ctrl->ctrl_close(conn);
6117 ebmb_delete(node);
6118 }
6119 }
6120 }
6121}
6122
6123REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6124
Willy Tarreau76cc6992020-07-01 18:49:24 +02006125/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6126static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006127 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006128 char **err)
6129{
6130 if (too_many_args(1, args, err, NULL))
6131 return -1;
6132
6133 if (strcmp(args[1], "on") == 0)
6134 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6135 else if (strcmp(args[1], "off") == 0)
6136 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6137 else {
6138 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6139 return -1;
6140 }
6141 return 0;
6142}
6143
Olivier Houchard88698d92019-04-16 19:07:22 +02006144/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6145static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006146 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006147 char **err)
6148{
6149 int arg = -1;
6150
6151 if (too_many_args(1, args, err, NULL))
6152 return -1;
6153
6154 if (*(args[1]) != 0)
6155 arg = atoi(args[1]);
6156
6157 if (arg < 0 || arg > 100) {
6158 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6159 return -1;
6160 }
6161
6162 if (args[0][10] == 'h')
6163 global.tune.pool_high_ratio = arg;
6164 else
6165 global.tune.pool_low_ratio = arg;
6166 return 0;
6167}
6168
6169/* config keyword parsers */
6170static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006171 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006172 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6173 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6174 { 0, NULL, NULL }
6175}};
6176
6177INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6178
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006179/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006180 * Local variables:
6181 * c-indent-level: 8
6182 * c-basic-offset: 8
6183 * End:
6184 */