blob: eddaf9107bb20db7a97e1ef862c0e46c2476e326 [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
Willy Tarreauff5ae352013-12-11 20:36:34 +0100127 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900128 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100129
Emeric Brun52a91d32017-08-31 14:41:55 +0200130 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900131 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100132
Simon Horman4a741432013-02-23 15:35:38 +0900133 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100134}
135
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100136/*
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +0100137 * Use this to publish EVENT_HDL_SUB_SERVER family type event
138 * from srv facility
139 * Event will be published in both global subscription list and
140 * server dedicated subscription list
141 * server ptr must be valid
142 */
143static inline void srv_event_hdl_publish(struct event_hdl_sub_type event, struct server *srv, uint8_t thread_isolate)
144{
145 struct event_hdl_cb_data_server cb_data;
146
147 /* safe data assignments */
148 cb_data.safe.puid = srv->puid;
149 cb_data.safe.rid = srv->rid;
150 cb_data.safe.flags = srv->flags;
151 snprintf(cb_data.safe.name, sizeof(cb_data.safe.name), "%s", srv->id);
152 if (srv->proxy)
153 snprintf(cb_data.safe.proxy_name, sizeof(cb_data.safe.proxy_name), "%s", srv->proxy->id);
154 /* unsafe data assignments */
155 cb_data.unsafe.ptr = srv;
156 cb_data.unsafe.thread_isolate = thread_isolate;
157 /* publish in server dedicated sub list */
158 event_hdl_publish(&srv->e_subs, event, EVENT_HDL_CB_DATA(&cb_data));
159 /* publish in global subscription list */
160 event_hdl_publish(NULL, event, EVENT_HDL_CB_DATA(&cb_data));
161}
162
163/*
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100164 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200165 * Unlikely, but it can happen. The server's proxy must be at least
166 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100167 */
168static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100169{
170 struct proxy *p = s->proxy;
171 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100172
173 for (tmpserv = p->srv; tmpserv != NULL;
174 tmpserv = tmpserv->next) {
175 if (tmpserv == s)
176 continue;
177 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
178 continue;
179 if (tmpserv->cookie &&
180 strcmp(tmpserv->cookie, s->cookie) == 0) {
181 ha_warning("We generated two equal cookies for two different servers.\n"
182 "Please change the secret key for '%s'.\n",
183 s->proxy->id);
184 }
185 }
186
187}
188
Willy Tarreau46b7f532018-08-21 11:54:26 +0200189/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200190 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200191 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100192void srv_set_dyncookie(struct server *s)
193{
194 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100195 char *tmpbuf;
196 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100197 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100198 size_t buffer_len;
199 int addr_len;
200 int port;
201
Willy Tarreau595e7672020-10-20 17:30:08 +0200202 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200203
Olivier Houchard4e694042017-03-14 20:01:29 +0100204 if ((s->flags & SRV_F_COOKIESET) ||
205 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
206 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200207 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100208 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100209
210 if (s->addr.ss_family != AF_INET &&
211 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200212 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100213 /*
214 * Buffer to calculate the cookie value.
215 * The buffer contains the secret key + the server IP address
216 * + the TCP port.
217 */
218 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
219 /*
220 * The TCP port should use only 2 bytes, but is stored in
221 * an unsigned int in struct server, so let's use 4, to be
222 * on the safe side.
223 */
224 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200225 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100226 memcpy(tmpbuf, p->dyncookie_key, key_len);
227 memcpy(&(tmpbuf[key_len]),
228 s->addr.ss_family == AF_INET ?
229 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
230 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
231 addr_len);
232 /*
233 * Make sure it's the same across all the load balancers,
234 * no matter their endianness.
235 */
236 port = htonl(s->svc_port);
237 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
238 hash_value = XXH64(tmpbuf, buffer_len, 0);
239 memprintf(&s->cookie, "%016llx", hash_value);
240 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200241 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100242 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100243
244 /* Don't bother checking if the dyncookie is duplicated if
245 * the server is marked as "disabled", maybe it doesn't have
246 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100247 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100248 if (!(s->next_admin & SRV_ADMF_FMAINT))
249 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200250 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200251 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100252}
253
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +0200254/* Returns true if it's possible to reuse an idle connection from server <srv>
255 * for a websocket stream. This is the case if server is configured to use the
256 * same protocol for both HTTP and websocket streams. This depends on the value
257 * of "proto", "alpn" and "ws" keywords.
258 */
259int srv_check_reuse_ws(struct server *srv)
260{
261 if (srv->mux_proto || srv->use_ssl != 1 || !srv->ssl_ctx.alpn_str) {
262 /* explicit srv.mux_proto or no ALPN : srv.mux_proto is used
263 * for mux selection.
264 */
265 const struct ist srv_mux = srv->mux_proto ?
266 srv->mux_proto->token : IST_NULL;
267
268 switch (srv->ws) {
269 /* "auto" means use the same protocol : reuse is possible. */
270 case SRV_WS_AUTO:
271 return 1;
272
273 /* "h2" means use h2 for websocket : reuse is possible if
274 * server mux is h2.
275 */
276 case SRV_WS_H2:
277 if (srv->mux_proto && isteq(srv_mux, ist("h2")))
278 return 1;
279 break;
280
281 /* "h1" means use h1 for websocket : reuse is possible if
282 * server mux is h1.
283 */
284 case SRV_WS_H1:
285 if (!srv->mux_proto || isteq(srv_mux, ist("h1")))
286 return 1;
287 break;
288 }
289 }
290 else {
291 /* ALPN selection.
292 * Based on the assumption that only "h2" and "http/1.1" token
293 * are used on server ALPN.
294 */
295 const struct ist alpn = ist2(srv->ssl_ctx.alpn_str,
296 srv->ssl_ctx.alpn_len);
297
298 switch (srv->ws) {
299 case SRV_WS_AUTO:
300 /* for auto mode, consider reuse as possible if the
301 * server uses a single protocol ALPN
302 */
303 if (!istchr(alpn, ','))
304 return 1;
305 break;
306
307 case SRV_WS_H2:
308 return isteq(alpn, ist("\x02h2"));
309
310 case SRV_WS_H1:
311 return isteq(alpn, ist("\x08http/1.1"));
312 }
313 }
314
315 return 0;
316}
317
318/* Return the proto to used for a websocket stream on <srv> without ALPN. NULL
319 * is a valid value indicating to use the fallback mux.
320 */
321const struct mux_ops *srv_get_ws_proto(struct server *srv)
322{
323 const struct mux_proto_list *mux = NULL;
324
325 switch (srv->ws) {
326 case SRV_WS_AUTO:
327 mux = srv->mux_proto;
328 break;
329
330 case SRV_WS_H1:
331 mux = get_mux_proto(ist("h1"));
332 break;
333
334 case SRV_WS_H2:
335 mux = get_mux_proto(ist("h2"));
336 break;
337 }
338
339 return mux ? mux->mux : NULL;
340}
341
Willy Tarreau21faa912012-10-10 08:27:36 +0200342/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200343 * Must be called with the server lock held. The server is first removed from
344 * the proxy tree if it was already attached. If <reattach> is true, the server
345 * will then be attached in the proxy tree. The proxy lock is held to
346 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700347 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200348static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700349{
350 struct proxy *p = s->proxy;
351 char *key;
352
353 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
354
355 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700356 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700357 free(key);
358 return;
359 }
360
361 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
362 ebpt_delete(&s->addr_node);
363 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
364
365 free(s->addr_node.key);
366 }
367
368 s->addr_node.key = key;
369
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200370 if (reattach) {
371 if (s->addr_node.key) {
372 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
373 ebis_insert(&p->used_server_addr, &s->addr_node);
374 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
375 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700376 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700377}
378
379/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200380 * Registers the server keyword list <kwl> as a list of valid keywords for next
381 * parsing sessions.
382 */
383void srv_register_keywords(struct srv_kw_list *kwl)
384{
Willy Tarreau2b718102021-04-21 07:32:39 +0200385 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200386}
387
388/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
389 * keyword is found with a NULL ->parse() function, then an attempt is made to
390 * find one with a valid ->parse() function. This way it is possible to declare
391 * platform-dependant, known keywords as NULL, then only declare them as valid
392 * if some options are met. Note that if the requested keyword contains an
393 * opening parenthesis, everything from this point is ignored.
394 */
395struct srv_kw *srv_find_kw(const char *kw)
396{
397 int index;
398 const char *kwend;
399 struct srv_kw_list *kwl;
400 struct srv_kw *ret = NULL;
401
402 kwend = strchr(kw, '(');
403 if (!kwend)
404 kwend = kw + strlen(kw);
405
406 list_for_each_entry(kwl, &srv_keywords.list, list) {
407 for (index = 0; kwl->kw[index].kw != NULL; index++) {
408 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
409 kwl->kw[index].kw[kwend-kw] == 0) {
410 if (kwl->kw[index].parse)
411 return &kwl->kw[index]; /* found it !*/
412 else
413 ret = &kwl->kw[index]; /* may be OK */
414 }
415 }
416 }
417 return ret;
418}
419
420/* Dumps all registered "server" keywords to the <out> string pointer. The
421 * unsupported keywords are only dumped if their supported form was not
422 * found.
423 */
424void srv_dump_kws(char **out)
425{
426 struct srv_kw_list *kwl;
427 int index;
428
Christopher Faulet784063e2020-05-18 12:14:18 +0200429 if (!out)
430 return;
431
Willy Tarreau21faa912012-10-10 08:27:36 +0200432 *out = NULL;
433 list_for_each_entry(kwl, &srv_keywords.list, list) {
434 for (index = 0; kwl->kw[index].kw != NULL; index++) {
435 if (kwl->kw[index].parse ||
436 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
437 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
438 kwl->scope,
439 kwl->kw[index].kw,
440 kwl->kw[index].skip ? " <arg>" : "",
441 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
442 kwl->kw[index].parse ? "" : " (not supported)");
443 }
444 }
445 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100446}
447
448/* Try to find in srv_keyword the word that looks closest to <word> by counting
449 * transitions between letters, digits and other characters. Will return the
450 * best matching word if found, otherwise NULL. An optional array of extra
451 * words to compare may be passed in <extra>, but it must then be terminated
452 * by a NULL entry. If unused it may be NULL.
453 */
454static const char *srv_find_best_kw(const char *word)
455{
456 uint8_t word_sig[1024];
457 uint8_t list_sig[1024];
458 const struct srv_kw_list *kwl;
459 const char *best_ptr = NULL;
460 int dist, best_dist = INT_MAX;
461 const char **extra;
462 int index;
463
464 make_word_fingerprint(word_sig, word);
465 list_for_each_entry(kwl, &srv_keywords.list, list) {
466 for (index = 0; kwl->kw[index].kw != NULL; index++) {
467 make_word_fingerprint(list_sig, kwl->kw[index].kw);
468 dist = word_fingerprint_distance(word_sig, list_sig);
469 if (dist < best_dist) {
470 best_dist = dist;
471 best_ptr = kwl->kw[index].kw;
472 }
473 }
474 }
475
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100476 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100477 make_word_fingerprint(list_sig, *extra);
478 dist = word_fingerprint_distance(word_sig, list_sig);
479 if (dist < best_dist) {
480 best_dist = dist;
481 best_ptr = *extra;
482 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100483 }
484
485 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
486 best_ptr = NULL;
487
488 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200489}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100490
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100491/* Parse the "backup" server keyword */
492static int srv_parse_backup(char **args, int *cur_arg,
493 struct proxy *curproxy, struct server *newsrv, char **err)
494{
495 newsrv->flags |= SRV_F_BACKUP;
496 return 0;
497}
498
Alexander Liu2a54bb72019-05-22 19:44:48 +0800499
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100500/* Parse the "cookie" server keyword */
501static int srv_parse_cookie(char **args, int *cur_arg,
502 struct proxy *curproxy, struct server *newsrv, char **err)
503{
504 char *arg;
505
506 arg = args[*cur_arg + 1];
507 if (!*arg) {
508 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
509 return ERR_ALERT | ERR_FATAL;
510 }
511
512 free(newsrv->cookie);
513 newsrv->cookie = strdup(arg);
514 newsrv->cklen = strlen(arg);
515 newsrv->flags |= SRV_F_COOKIESET;
516 return 0;
517}
518
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100519/* Parse the "disabled" server keyword */
520static int srv_parse_disabled(char **args, int *cur_arg,
521 struct proxy *curproxy, struct server *newsrv, char **err)
522{
Emeric Brun52a91d32017-08-31 14:41:55 +0200523 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
524 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100525 newsrv->check.state |= CHK_ST_PAUSED;
526 newsrv->check.health = 0;
527 return 0;
528}
529
530/* Parse the "enabled" server keyword */
531static int srv_parse_enabled(char **args, int *cur_arg,
532 struct proxy *curproxy, struct server *newsrv, char **err)
533{
Emeric Brun52a91d32017-08-31 14:41:55 +0200534 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
535 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100536 newsrv->check.state &= ~CHK_ST_PAUSED;
537 newsrv->check.health = newsrv->check.rise;
538 return 0;
539}
540
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100541/* Parse the "error-limit" server keyword */
542static int srv_parse_error_limit(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 if (!*args[*cur_arg + 1]) {
546 memprintf(err, "'%s' expects an integer argument.",
547 args[*cur_arg]);
548 return ERR_ALERT | ERR_FATAL;
549 }
550
551 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
552
553 if (newsrv->consecutive_errors_limit <= 0) {
554 memprintf(err, "%s has to be > 0.",
555 args[*cur_arg]);
556 return ERR_ALERT | ERR_FATAL;
557 }
558
559 return 0;
560}
561
Amaury Denoyellef9d59572021-10-18 14:40:29 +0200562/* Parse the "ws" keyword */
563static int srv_parse_ws(char **args, int *cur_arg,
564 struct proxy *curproxy, struct server *newsrv, char **err)
565{
566 if (!args[*cur_arg + 1]) {
567 memprintf(err, "'%s' expects 'auto', 'h1' or 'h2' value", args[*cur_arg]);
568 return ERR_ALERT | ERR_FATAL;
569 }
570
571 if (strcmp(args[*cur_arg + 1], "h1") == 0) {
572 newsrv->ws = SRV_WS_H1;
573 }
574 else if (strcmp(args[*cur_arg + 1], "h2") == 0) {
575 newsrv->ws = SRV_WS_H2;
576 }
577 else if (strcmp(args[*cur_arg + 1], "auto") == 0) {
578 newsrv->ws = SRV_WS_AUTO;
579 }
580 else {
581 memprintf(err, "'%s' has to be 'auto', 'h1' or 'h2'", args[*cur_arg]);
582 return ERR_ALERT | ERR_FATAL;
583 }
584
585
586 return 0;
587}
588
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100589/* Parse the "init-addr" server keyword */
590static int srv_parse_init_addr(char **args, int *cur_arg,
591 struct proxy *curproxy, struct server *newsrv, char **err)
592{
593 char *p, *end;
594 int done;
595 struct sockaddr_storage sa;
596
597 newsrv->init_addr_methods = 0;
598 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
599
600 for (p = args[*cur_arg + 1]; *p; p = end) {
601 /* cut on next comma */
602 for (end = p; *end && *end != ','; end++);
603 if (*end)
604 *(end++) = 0;
605
606 memset(&sa, 0, sizeof(sa));
607 if (strcmp(p, "libc") == 0) {
608 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
609 }
610 else if (strcmp(p, "last") == 0) {
611 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
612 }
613 else if (strcmp(p, "none") == 0) {
614 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
615 }
616 else if (str2ip2(p, &sa, 0)) {
617 if (is_addr(&newsrv->init_addr)) {
618 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
619 args[*cur_arg], p);
620 return ERR_ALERT | ERR_FATAL;
621 }
622 newsrv->init_addr = sa;
623 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
624 }
625 else {
626 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
627 args[*cur_arg], p);
628 return ERR_ALERT | ERR_FATAL;
629 }
630 if (!done) {
631 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
632 args[*cur_arg], p);
633 return ERR_ALERT | ERR_FATAL;
634 }
635 }
636
637 return 0;
638}
639
640/* Parse the "log-proto" server keyword */
641static int srv_parse_log_proto(char **args, int *cur_arg,
642 struct proxy *curproxy, struct server *newsrv, char **err)
643{
644 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
645 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
646 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
647 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
648 else {
649 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
650 args[*cur_arg], args[*cur_arg + 1]);
651 return ERR_ALERT | ERR_FATAL;
652 }
653
654 return 0;
655}
656
657/* Parse the "maxconn" server keyword */
658static int srv_parse_maxconn(char **args, int *cur_arg,
659 struct proxy *curproxy, struct server *newsrv, char **err)
660{
661 newsrv->maxconn = atol(args[*cur_arg + 1]);
662 return 0;
663}
664
665/* Parse the "maxqueue" server keyword */
666static int srv_parse_maxqueue(char **args, int *cur_arg,
667 struct proxy *curproxy, struct server *newsrv, char **err)
668{
669 newsrv->maxqueue = atol(args[*cur_arg + 1]);
670 return 0;
671}
672
673/* Parse the "minconn" server keyword */
674static int srv_parse_minconn(char **args, int *cur_arg,
675 struct proxy *curproxy, struct server *newsrv, char **err)
676{
677 newsrv->minconn = atol(args[*cur_arg + 1]);
678 return 0;
679}
680
Willy Tarreau9c538e02019-01-23 10:21:49 +0100681static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
682{
683 char *arg;
684
685 arg = args[*cur_arg + 1];
686 if (!*arg) {
687 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
688 return ERR_ALERT | ERR_FATAL;
689 }
690 newsrv->max_reuse = atoi(arg);
691
692 return 0;
693}
694
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100695static 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 +0100696{
697 const char *res;
698 char *arg;
699 unsigned int time;
700
701 arg = args[*cur_arg + 1];
702 if (!*arg) {
703 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
704 return ERR_ALERT | ERR_FATAL;
705 }
706 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200707 if (res == PARSE_TIME_OVER) {
708 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
709 args[*cur_arg+1], args[*cur_arg]);
710 return ERR_ALERT | ERR_FATAL;
711 }
712 else if (res == PARSE_TIME_UNDER) {
713 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
714 args[*cur_arg+1], args[*cur_arg]);
715 return ERR_ALERT | ERR_FATAL;
716 }
717 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100718 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
719 *res, args[*cur_arg]);
720 return ERR_ALERT | ERR_FATAL;
721 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100722 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100723
724 return 0;
725}
726
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200727static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
728{
729 char *arg;
730
731 arg = args[*cur_arg + 1];
732 if (!*arg) {
733 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
734 return ERR_ALERT | ERR_FATAL;
735 }
736
737 newsrv->low_idle_conns = atoi(arg);
738 return 0;
739}
740
Olivier Houchard006e3102018-12-10 18:30:32 +0100741static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
742{
743 char *arg;
744
745 arg = args[*cur_arg + 1];
746 if (!*arg) {
747 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
748 return ERR_ALERT | ERR_FATAL;
749 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100750
Olivier Houchard006e3102018-12-10 18:30:32 +0100751 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100752 if ((int)newsrv->max_idle_conns < -1) {
753 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
754 return ERR_ALERT | ERR_FATAL;
755 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100756
757 return 0;
758}
759
Willy Tarreaudff55432012-10-10 17:51:05 +0200760/* parse the "id" server keyword */
761static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
762{
763 struct eb32_node *node;
764
765 if (!*args[*cur_arg + 1]) {
766 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
767 return ERR_ALERT | ERR_FATAL;
768 }
769
770 newsrv->puid = atol(args[*cur_arg + 1]);
771 newsrv->conf.id.key = newsrv->puid;
772
773 if (newsrv->puid <= 0) {
774 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
775 return ERR_ALERT | ERR_FATAL;
776 }
777
778 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
779 if (node) {
780 struct server *target = container_of(node, struct server, conf.id);
781 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
782 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
783 target->id);
784 return ERR_ALERT | ERR_FATAL;
785 }
786
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200787 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200788 return 0;
789}
790
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100791/* Parse the "namespace" server keyword */
792static int srv_parse_namespace(char **args, int *cur_arg,
793 struct proxy *curproxy, struct server *newsrv, char **err)
794{
Willy Tarreaue5733232019-05-22 19:24:06 +0200795#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100796 char *arg;
797
798 arg = args[*cur_arg + 1];
799 if (!*arg) {
800 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
801 return ERR_ALERT | ERR_FATAL;
802 }
803
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100804 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100805 /* Use the namespace associated with the connection (if present). */
806 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
807 return 0;
808 }
809
810 /*
811 * As this parser may be called several times for the same 'default-server'
812 * object, or for a new 'server' instance deriving from a 'default-server'
813 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
814 */
815 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
816
817 newsrv->netns = netns_store_lookup(arg, strlen(arg));
818 if (!newsrv->netns)
819 newsrv->netns = netns_store_insert(arg);
820
821 if (!newsrv->netns) {
822 memprintf(err, "Cannot open namespace '%s'", arg);
823 return ERR_ALERT | ERR_FATAL;
824 }
825
826 return 0;
827#else
828 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
829 return ERR_ALERT | ERR_FATAL;
830#endif
831}
832
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100833/* Parse the "no-backup" server keyword */
834static int srv_parse_no_backup(char **args, int *cur_arg,
835 struct proxy *curproxy, struct server *newsrv, char **err)
836{
837 newsrv->flags &= ~SRV_F_BACKUP;
838 return 0;
839}
840
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100841
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100842/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200843static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100844{
845 srv->pp_opts &= ~flags;
846 return 0;
847}
848
849/* Parse the "no-send-proxy" server keyword */
850static int srv_parse_no_send_proxy(char **args, int *cur_arg,
851 struct proxy *curproxy, struct server *newsrv, char **err)
852{
853 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
854}
855
856/* Parse the "no-send-proxy-v2" server keyword */
857static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
858 struct proxy *curproxy, struct server *newsrv, char **err)
859{
860 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
861}
862
Frédéric Lécaille36d15652022-10-17 14:58:19 +0200863/* Parse the "shard" server keyword */
864static int srv_parse_shard(char **args, int *cur_arg,
865 struct proxy *curproxy, struct server *newsrv, char **err)
866{
867 newsrv->shard = atol(args[*cur_arg + 1]);
868 return 0;
869}
870
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200871/* Parse the "no-tfo" server keyword */
872static int srv_parse_no_tfo(char **args, int *cur_arg,
873 struct proxy *curproxy, struct server *newsrv, char **err)
874{
875 newsrv->flags &= ~SRV_F_FASTOPEN;
876 return 0;
877}
878
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100879/* Parse the "non-stick" server keyword */
880static int srv_parse_non_stick(char **args, int *cur_arg,
881 struct proxy *curproxy, struct server *newsrv, char **err)
882{
883 newsrv->flags |= SRV_F_NON_STICK;
884 return 0;
885}
886
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100887/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200888static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100889{
890 srv->pp_opts |= flags;
891 return 0;
892}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200893/* parse the "proto" server keyword */
894static int srv_parse_proto(char **args, int *cur_arg,
895 struct proxy *px, struct server *newsrv, char **err)
896{
897 struct ist proto;
898
899 if (!*args[*cur_arg + 1]) {
900 memprintf(err, "'%s' : missing value", args[*cur_arg]);
901 return ERR_ALERT | ERR_FATAL;
902 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100903 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200904 newsrv->mux_proto = get_mux_proto(proto);
905 if (!newsrv->mux_proto) {
906 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
907 return ERR_ALERT | ERR_FATAL;
908 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200909 return 0;
910}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100911
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100912/* parse the "proxy-v2-options" */
913static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
914 struct proxy *px, struct server *newsrv, char **err)
915{
916 char *p, *n;
917 for (p = args[*cur_arg+1]; p; p = n) {
918 n = strchr(p, ',');
919 if (n)
920 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100921 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100922 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100923 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100924 newsrv->pp_opts |= SRV_PP_V2_SSL;
925 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100926 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100927 newsrv->pp_opts |= SRV_PP_V2_SSL;
928 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100929 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100930 newsrv->pp_opts |= SRV_PP_V2_SSL;
931 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100932 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100933 newsrv->pp_opts |= SRV_PP_V2_SSL;
934 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100935 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100936 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100937 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100938 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100939 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100940 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100941 } else
942 goto fail;
943 }
944 return 0;
945 fail:
946 if (err)
947 memprintf(err, "'%s' : proxy v2 option not implemented", p);
948 return ERR_ALERT | ERR_FATAL;
949}
950
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100951/* Parse the "observe" server keyword */
952static int srv_parse_observe(char **args, int *cur_arg,
953 struct proxy *curproxy, struct server *newsrv, char **err)
954{
955 char *arg;
956
957 arg = args[*cur_arg + 1];
958 if (!*arg) {
959 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
960 return ERR_ALERT | ERR_FATAL;
961 }
962
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100963 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100964 newsrv->observe = HANA_OBS_NONE;
965 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100966 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100967 newsrv->observe = HANA_OBS_LAYER4;
968 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100969 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100970 if (curproxy->mode != PR_MODE_HTTP) {
971 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
972 return ERR_ALERT;
973 }
974 newsrv->observe = HANA_OBS_LAYER7;
975 }
976 else {
977 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
978 "but got '%s'\n", args[*cur_arg], arg);
979 return ERR_ALERT | ERR_FATAL;
980 }
981
982 return 0;
983}
984
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100985/* Parse the "on-error" server keyword */
986static int srv_parse_on_error(char **args, int *cur_arg,
987 struct proxy *curproxy, struct server *newsrv, char **err)
988{
989 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
990 newsrv->onerror = HANA_ONERR_FASTINTER;
991 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
992 newsrv->onerror = HANA_ONERR_FAILCHK;
993 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
994 newsrv->onerror = HANA_ONERR_SUDDTH;
995 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
996 newsrv->onerror = HANA_ONERR_MARKDWN;
997 else {
998 memprintf(err, "'%s' expects one of 'fastinter', "
999 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
1000 args[*cur_arg], args[*cur_arg + 1]);
1001 return ERR_ALERT | ERR_FATAL;
1002 }
1003
1004 return 0;
1005}
1006
1007/* Parse the "on-marked-down" server keyword */
1008static int srv_parse_on_marked_down(char **args, int *cur_arg,
1009 struct proxy *curproxy, struct server *newsrv, char **err)
1010{
1011 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
1012 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
1013 else {
1014 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
1015 args[*cur_arg], args[*cur_arg + 1]);
1016 return ERR_ALERT | ERR_FATAL;
1017 }
1018
1019 return 0;
1020}
1021
1022/* Parse the "on-marked-up" server keyword */
1023static int srv_parse_on_marked_up(char **args, int *cur_arg,
1024 struct proxy *curproxy, struct server *newsrv, char **err)
1025{
1026 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
1027 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
1028 else {
1029 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
1030 args[*cur_arg], args[*cur_arg + 1]);
1031 return ERR_ALERT | ERR_FATAL;
1032 }
1033
1034 return 0;
1035}
1036
Frédéric Lécaille16186232017-03-14 16:42:49 +01001037/* Parse the "redir" server keyword */
1038static int srv_parse_redir(char **args, int *cur_arg,
1039 struct proxy *curproxy, struct server *newsrv, char **err)
1040{
1041 char *arg;
1042
1043 arg = args[*cur_arg + 1];
1044 if (!*arg) {
1045 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
1046 return ERR_ALERT | ERR_FATAL;
1047 }
1048
1049 free(newsrv->rdr_pfx);
1050 newsrv->rdr_pfx = strdup(arg);
1051 newsrv->rdr_len = strlen(arg);
1052
1053 return 0;
1054}
1055
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001056/* Parse the "resolvers" server keyword */
1057static int srv_parse_resolvers(char **args, int *cur_arg,
1058 struct proxy *curproxy, struct server *newsrv, char **err)
1059{
1060 free(newsrv->resolvers_id);
1061 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
1062 return 0;
1063}
1064
1065/* Parse the "resolve-net" server keyword */
1066static int srv_parse_resolve_net(char **args, int *cur_arg,
1067 struct proxy *curproxy, struct server *newsrv, char **err)
1068{
1069 char *p, *e;
1070 unsigned char mask;
1071 struct resolv_options *opt;
1072
1073 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
1074 memprintf(err, "'%s' expects a list of networks.",
1075 args[*cur_arg]);
1076 return ERR_ALERT | ERR_FATAL;
1077 }
1078
1079 opt = &newsrv->resolv_opts;
1080
1081 /* Split arguments by comma, and convert it from ipv4 or ipv6
1082 * string network in in_addr or in6_addr.
1083 */
1084 p = args[*cur_arg + 1];
1085 e = p;
1086 while (*p != '\0') {
1087 /* If no room available, return error. */
1088 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
1089 memprintf(err, "'%s' exceed %d networks.",
1090 args[*cur_arg], SRV_MAX_PREF_NET);
1091 return ERR_ALERT | ERR_FATAL;
1092 }
1093 /* look for end or comma. */
1094 while (*e != ',' && *e != '\0')
1095 e++;
1096 if (*e == ',') {
1097 *e = '\0';
1098 e++;
1099 }
1100 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
1101 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
1102 /* Try to convert input string from ipv4 or ipv6 network. */
1103 opt->pref_net[opt->pref_net_nb].family = AF_INET;
1104 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
1105 &mask)) {
1106 /* Try to convert input string from ipv6 network. */
1107 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
1108 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
1109 } else {
1110 /* All network conversions fail, return error. */
1111 memprintf(err, "'%s' invalid network '%s'.",
1112 args[*cur_arg], p);
1113 return ERR_ALERT | ERR_FATAL;
1114 }
1115 opt->pref_net_nb++;
1116 p = e;
1117 }
1118
1119 return 0;
1120}
1121
1122/* Parse the "resolve-opts" server keyword */
1123static int srv_parse_resolve_opts(char **args, int *cur_arg,
1124 struct proxy *curproxy, struct server *newsrv, char **err)
1125{
1126 char *p, *end;
1127
1128 for (p = args[*cur_arg + 1]; *p; p = end) {
1129 /* cut on next comma */
1130 for (end = p; *end && *end != ','; end++);
1131 if (*end)
1132 *(end++) = 0;
1133
1134 if (strcmp(p, "allow-dup-ip") == 0) {
1135 newsrv->resolv_opts.accept_duplicate_ip = 1;
1136 }
1137 else if (strcmp(p, "ignore-weight") == 0) {
1138 newsrv->resolv_opts.ignore_weight = 1;
1139 }
1140 else if (strcmp(p, "prevent-dup-ip") == 0) {
1141 newsrv->resolv_opts.accept_duplicate_ip = 0;
1142 }
1143 else {
1144 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
1145 args[*cur_arg], p);
1146 return ERR_ALERT | ERR_FATAL;
1147 }
1148 }
1149
1150 return 0;
1151}
1152
1153/* Parse the "resolve-prefer" server keyword */
1154static int srv_parse_resolve_prefer(char **args, int *cur_arg,
1155 struct proxy *curproxy, struct server *newsrv, char **err)
1156{
1157 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
1158 newsrv->resolv_opts.family_prio = AF_INET;
1159 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
1160 newsrv->resolv_opts.family_prio = AF_INET6;
1161 else {
1162 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
1163 args[*cur_arg]);
1164 return ERR_ALERT | ERR_FATAL;
1165 }
1166
1167 return 0;
1168}
1169
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001170/* Parse the "send-proxy" server keyword */
1171static int srv_parse_send_proxy(char **args, int *cur_arg,
1172 struct proxy *curproxy, struct server *newsrv, char **err)
1173{
1174 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
1175}
1176
1177/* Parse the "send-proxy-v2" server keyword */
1178static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1179 struct proxy *curproxy, struct server *newsrv, char **err)
1180{
1181 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1182}
1183
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001184/* Parse the "slowstart" server keyword */
1185static int srv_parse_slowstart(char **args, int *cur_arg,
1186 struct proxy *curproxy, struct server *newsrv, char **err)
1187{
1188 /* slowstart is stored in seconds */
1189 unsigned int val;
1190 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1191
1192 if (time_err == PARSE_TIME_OVER) {
1193 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1194 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1195 return ERR_ALERT | ERR_FATAL;
1196 }
1197 else if (time_err == PARSE_TIME_UNDER) {
1198 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1199 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1200 return ERR_ALERT | ERR_FATAL;
1201 }
1202 else if (time_err) {
1203 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1204 *time_err, newsrv->id);
1205 return ERR_ALERT | ERR_FATAL;
1206 }
1207 newsrv->slowstart = (val + 999) / 1000;
1208
1209 return 0;
1210}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001211
1212/* Parse the "source" server keyword */
1213static int srv_parse_source(char **args, int *cur_arg,
1214 struct proxy *curproxy, struct server *newsrv, char **err)
1215{
1216 char *errmsg;
1217 int port_low, port_high;
1218 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001219
1220 errmsg = NULL;
1221
1222 if (!*args[*cur_arg + 1]) {
1223 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1224 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1225 goto err;
1226 }
1227
1228 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001229 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1230 &errmsg, NULL, NULL,
1231 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 +01001232 if (!sk) {
1233 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1234 goto err;
1235 }
1236
Frédéric Lécailledba97072017-03-17 15:33:50 +01001237 newsrv->conn_src.opts |= CO_SRC_BIND;
1238 newsrv->conn_src.source_addr = *sk;
1239
1240 if (port_low != port_high) {
1241 int i;
1242
Frédéric Lécailledba97072017-03-17 15:33:50 +01001243 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001244 if (!newsrv->conn_src.sport_range) {
1245 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1246 goto err;
1247 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001248 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1249 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1250 }
1251
1252 *cur_arg += 2;
1253 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001254 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001255#if defined(CONFIG_HAP_TRANSPARENT)
1256 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001257 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1258 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001259 goto err;
1260 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001261 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001262 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1263 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1264 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001265 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001266 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1267 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1268 }
1269 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1270 char *name, *end;
1271
1272 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001273 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001274 name++;
1275
1276 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001277 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001278 end++;
1279
1280 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1281 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1282 free(newsrv->conn_src.bind_hdr_name);
1283 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001284 if (!newsrv->conn_src.bind_hdr_name) {
1285 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1286 goto err;
1287 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001288 newsrv->conn_src.bind_hdr_len = end - name;
1289 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1290 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1291 newsrv->conn_src.bind_hdr_occ = -1;
1292
1293 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001294 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001295 end++;
1296 if (*end == ',') {
1297 end++;
1298 name = end;
1299 if (*end == '-')
1300 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001301 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001302 end++;
1303 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1304 }
1305
1306 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001307 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1308 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001309 goto err;
1310 }
1311 }
1312 else {
1313 struct sockaddr_storage *sk;
1314 int port1, port2;
1315
1316 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001317 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1318 &errmsg, NULL, NULL,
1319 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001320 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001321 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001322 goto err;
1323 }
1324
Frédéric Lécailledba97072017-03-17 15:33:50 +01001325 newsrv->conn_src.tproxy_addr = *sk;
1326 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1327 }
1328 global.last_checks |= LSTCHK_NETADM;
1329 *cur_arg += 2;
1330 continue;
1331#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001332 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 +01001333 goto err;
1334#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1335 } /* "usesrc" */
1336
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001337 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001338#ifdef SO_BINDTODEVICE
1339 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001340 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001341 goto err;
1342 }
1343 free(newsrv->conn_src.iface_name);
1344 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1345 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1346 global.last_checks |= LSTCHK_NETADM;
1347#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001348 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001349 goto err;
1350#endif
1351 *cur_arg += 2;
1352 continue;
1353 }
1354 /* this keyword in not an option of "source" */
1355 break;
1356 } /* while */
1357
1358 return 0;
1359
1360 err:
1361 free(errmsg);
1362 return ERR_ALERT | ERR_FATAL;
1363}
1364
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001365/* Parse the "stick" server keyword */
1366static int srv_parse_stick(char **args, int *cur_arg,
1367 struct proxy *curproxy, struct server *newsrv, char **err)
1368{
1369 newsrv->flags &= ~SRV_F_NON_STICK;
1370 return 0;
1371}
1372
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001373/* Parse the "track" server keyword */
1374static int srv_parse_track(char **args, int *cur_arg,
1375 struct proxy *curproxy, struct server *newsrv, char **err)
1376{
1377 char *arg;
1378
1379 arg = args[*cur_arg + 1];
1380 if (!*arg) {
1381 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1382 return ERR_ALERT | ERR_FATAL;
1383 }
1384
1385 free(newsrv->trackit);
1386 newsrv->trackit = strdup(arg);
1387
1388 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001389}
1390
1391/* Parse the "socks4" server keyword */
1392static int srv_parse_socks4(char **args, int *cur_arg,
1393 struct proxy *curproxy, struct server *newsrv, char **err)
1394{
1395 char *errmsg;
1396 int port_low, port_high;
1397 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001398
1399 errmsg = NULL;
1400
1401 if (!*args[*cur_arg + 1]) {
1402 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
1403 goto err;
1404 }
1405
1406 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001407 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1408 &errmsg, NULL, NULL,
1409 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001410 if (!sk) {
1411 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1412 goto err;
1413 }
1414
Alexander Liu2a54bb72019-05-22 19:44:48 +08001415 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1416 newsrv->socks4_addr = *sk;
1417
Alexander Liu2a54bb72019-05-22 19:44:48 +08001418 return 0;
1419
1420 err:
1421 free(errmsg);
1422 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001423}
1424
Frédéric Lécailledba97072017-03-17 15:33:50 +01001425
Willy Tarreau034c88c2017-01-23 23:36:45 +01001426/* parse the "tfo" server keyword */
1427static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1428{
1429 newsrv->flags |= SRV_F_FASTOPEN;
1430 return 0;
1431}
1432
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001433/* parse the "usesrc" server keyword */
1434static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1435{
1436 memprintf(err, "'%s' only allowed after a '%s' statement.",
1437 "usesrc", "source");
1438 return ERR_ALERT | ERR_FATAL;
1439}
1440
1441/* parse the "weight" server keyword */
1442static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1443{
1444 int w;
1445
1446 w = atol(args[*cur_arg + 1]);
1447 if (w < 0 || w > SRV_UWGHT_MAX) {
1448 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1449 newsrv->id, SRV_UWGHT_MAX, w);
1450 return ERR_ALERT | ERR_FATAL;
1451 }
1452 newsrv->uweight = newsrv->iweight = w;
1453
1454 return 0;
1455}
1456
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001457/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001458 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001459 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001460 *
1461 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001462 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001463void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001464{
Willy Tarreau751153e2021-02-17 13:33:24 +01001465 struct stream *stream;
1466 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001467 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001468
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001469 for (thr = 0; thr < global.nbthread; thr++)
1470 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1471 if (stream->srv_conn == srv)
1472 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001473}
1474
1475/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001476 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001477 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001478 *
1479 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001480 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001481void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001482{
1483 struct server *srv;
1484
1485 for (srv = px->srv; srv != NULL; srv = srv->next)
1486 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001487 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001488}
1489
Willy Tarreaubda92272014-05-20 21:55:30 +02001490/* Appends some information to a message string related to a server going UP or
1491 * DOWN. If both <forced> and <reason> are null and the server tracks another
1492 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001493 * If <check> is non-null, an entire string describing the check result will be
1494 * appended after a comma and a space (eg: to report some information from the
1495 * check that changed the state). In the other case, the string will be built
1496 * using the check results stored into the struct server if present.
1497 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001498 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001499 *
1500 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001501 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001502void srv_append_status(struct buffer *msg, struct server *s,
1503 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001504{
Emeric Brun5a133512017-10-19 14:42:30 +02001505 short status = s->op_st_chg.status;
1506 short code = s->op_st_chg.code;
1507 long duration = s->op_st_chg.duration;
1508 char *desc = s->op_st_chg.reason;
1509
1510 if (check) {
1511 status = check->status;
1512 code = check->code;
1513 duration = check->duration;
1514 desc = check->desc;
1515 }
1516
1517 if (status != -1) {
1518 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1519
1520 if (status >= HCHK_STATUS_L57DATA)
1521 chunk_appendf(msg, ", code: %d", code);
1522
1523 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001524 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001525
1526 chunk_appendf(msg, ", info: \"");
1527
1528 chunk_initlen(&src, desc, 0, strlen(desc));
1529 chunk_asciiencode(msg, &src, '"');
1530
1531 chunk_appendf(msg, "\"");
1532 }
1533
1534 if (duration >= 0)
1535 chunk_appendf(msg, ", check duration: %ldms", duration);
1536 }
1537 else if (desc && *desc) {
1538 chunk_appendf(msg, ", %s", desc);
1539 }
1540 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001541 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001542 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001543
1544 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001545 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001546 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1547 " %d sessions active, %d requeued, %d remaining in queue",
1548 s->proxy->srv_act, s->proxy->srv_bck,
1549 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001550 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001551 else
1552 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1553 " %d sessions requeued, %d total in queue",
1554 s->proxy->srv_act, s->proxy->srv_bck,
1555 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001556 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001557 }
1558}
1559
Emeric Brun5a133512017-10-19 14:42:30 +02001560/* Marks server <s> down, regardless of its checks' statuses. The server is
1561 * registered in a list to postpone the counting of the remaining servers on
1562 * the proxy and transfers queued streams whenever possible to other servers at
1563 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1564 * non-null as the reason for going down or the available data from the check
1565 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001566 *
1567 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001568 */
Emeric Brun5a133512017-10-19 14:42:30 +02001569void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001570{
1571 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001572
Emeric Brun64cc49c2017-10-03 14:46:45 +02001573 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001574 return;
1575
Emeric Brun52a91d32017-08-31 14:41:55 +02001576 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001577 *s->op_st_chg.reason = 0;
1578 s->op_st_chg.status = -1;
1579 if (reason) {
1580 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1581 }
1582 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001583 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001584 s->op_st_chg.code = check->code;
1585 s->op_st_chg.status = check->status;
1586 s->op_st_chg.duration = check->duration;
1587 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001588
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001589 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001590 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001591
Emeric Brun9f0b4582017-10-23 14:39:51 +02001592 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001593 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001594 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001595 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001596 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001597}
1598
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001599/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001600 * in maintenance. The server is registered in a list to postpone the counting
1601 * of the remaining servers on the proxy and tries to grab requests from the
1602 * proxy at a sync point. Maintenance servers are ignored. It stores the
1603 * <reason> if non-null as the reason for going down or the available data
1604 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001605 *
1606 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001607 */
Emeric Brun5a133512017-10-19 14:42:30 +02001608void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001609{
1610 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001611
Emeric Brun64cc49c2017-10-03 14:46:45 +02001612 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001613 return;
1614
Emeric Brun52a91d32017-08-31 14:41:55 +02001615 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001616 return;
1617
Emeric Brun52a91d32017-08-31 14:41:55 +02001618 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001619 *s->op_st_chg.reason = 0;
1620 s->op_st_chg.status = -1;
1621 if (reason) {
1622 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1623 }
1624 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001625 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001626 s->op_st_chg.code = check->code;
1627 s->op_st_chg.status = check->status;
1628 s->op_st_chg.duration = check->duration;
1629 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001630
Emeric Brun64cc49c2017-10-03 14:46:45 +02001631 if (s->slowstart <= 0)
1632 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001633
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001634 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001635 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001636
Emeric Brun9f0b4582017-10-23 14:39:51 +02001637 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001638 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001639 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001640 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001641 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001642}
1643
Willy Tarreau8eb77842014-05-21 13:54:57 +02001644/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001645 * isn't in maintenance. The server is registered in a list to postpone the
1646 * counting of the remaining servers on the proxy and tries to grab requests
1647 * from the proxy. Maintenance servers are ignored. It stores the
1648 * <reason> if non-null as the reason for going down or the available data
1649 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001650 * up. Note that it makes use of the trash to build the log strings, so <reason>
1651 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001652 *
1653 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001654 */
Emeric Brun5a133512017-10-19 14:42:30 +02001655void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001656{
1657 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001658
Emeric Brun64cc49c2017-10-03 14:46:45 +02001659 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001660 return;
1661
Emeric Brun52a91d32017-08-31 14:41:55 +02001662 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001663 return;
1664
Emeric Brun52a91d32017-08-31 14:41:55 +02001665 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001666 *s->op_st_chg.reason = 0;
1667 s->op_st_chg.status = -1;
1668 if (reason) {
1669 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1670 }
1671 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001672 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001673 s->op_st_chg.code = check->code;
1674 s->op_st_chg.status = check->status;
1675 s->op_st_chg.duration = check->duration;
1676 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001677
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001678 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001679 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001680
Emeric Brun9f0b4582017-10-23 14:39:51 +02001681 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001682 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001683 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001684 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001685 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001686}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001687
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001688/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1689 * enforce either maint mode or drain mode. It is not allowed to set more than
1690 * one flag at once. The equivalent "inherited" flag is propagated to all
1691 * tracking servers. Maintenance mode disables health checks (but not agent
1692 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001693 * is done. If <cause> is non-null, it will be displayed at the end of the log
1694 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001695 *
1696 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001697 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001698void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001699{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001700 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001701
1702 if (!mode)
1703 return;
1704
1705 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001706 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001707 return;
1708
Emeric Brun52a91d32017-08-31 14:41:55 +02001709 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001710 if (cause)
1711 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1712
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001713 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001714 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001715
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001716 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001717 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1718 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001719 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001720
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001721 /* compute the inherited flag to propagate */
1722 if (mode & SRV_ADMF_MAINT)
1723 mode = SRV_ADMF_IMAINT;
1724 else if (mode & SRV_ADMF_DRAIN)
1725 mode = SRV_ADMF_IDRAIN;
1726
Emeric Brun9f0b4582017-10-23 14:39:51 +02001727 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001728 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001729 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001730 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001731 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001732}
1733
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001734/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1735 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1736 * than one flag at once. The equivalent "inherited" flag is propagated to all
1737 * tracking servers. Leaving maintenance mode re-enables health checks. When
1738 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001739 *
1740 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001741 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001742void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001743{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001744 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001745
1746 if (!mode)
1747 return;
1748
1749 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001750 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001751 return;
1752
Emeric Brun52a91d32017-08-31 14:41:55 +02001753 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001754
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001755 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001756 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001757
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001758 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001759 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1760 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001761 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001762
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001763 if (mode & SRV_ADMF_MAINT)
1764 mode = SRV_ADMF_IMAINT;
1765 else if (mode & SRV_ADMF_DRAIN)
1766 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001767
Emeric Brun9f0b4582017-10-23 14:39:51 +02001768 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001769 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001770 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001771 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001772 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001773}
1774
Willy Tarreau757478e2016-11-03 19:22:19 +01001775/* principle: propagate maint and drain to tracking servers. This is useful
1776 * upon startup so that inherited states are correct.
1777 */
1778static void srv_propagate_admin_state(struct server *srv)
1779{
1780 struct server *srv2;
1781
1782 if (!srv->trackers)
1783 return;
1784
1785 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001786 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001787 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001788 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001789
Emeric Brun52a91d32017-08-31 14:41:55 +02001790 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001791 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001792 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001793 }
1794}
1795
1796/* Compute and propagate the admin states for all servers in proxy <px>.
1797 * Only servers *not* tracking another one are considered, because other
1798 * ones will be handled when the server they track is visited.
1799 */
1800void srv_compute_all_admin_states(struct proxy *px)
1801{
1802 struct server *srv;
1803
1804 for (srv = px->srv; srv; srv = srv->next) {
1805 if (srv->track)
1806 continue;
1807 srv_propagate_admin_state(srv);
1808 }
1809}
1810
Willy Tarreaudff55432012-10-10 17:51:05 +02001811/* Note: must not be declared <const> as its list will be overwritten.
1812 * Please take care of keeping this list alphabetically sorted, doing so helps
1813 * all code contributors.
1814 * Optional keywords are also declared with a NULL ->parse() function so that
1815 * the config parser can report an appropriate error when a known keyword was
1816 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001817 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001818 */
1819static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001820 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001821 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001822 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1823 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001824 { "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 +02001825 { "ws", srv_parse_ws, 1, 1, 1 }, /* websocket protocol */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001826 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001827 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1828 { "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 +01001829 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1830 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001831 { "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 +01001832 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001833 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001834 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1835 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1836 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1837 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001838 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001839 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1840 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1841 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1842 { "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 +01001843 { "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 */
1844 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1845 { "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 +01001846 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001847 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001848 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001849 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001850 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001851 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001852 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001853 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1854 { "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 +02001855 { "shard", srv_parse_shard, 1, 1, 1 }, /* Server shard (only in peers protocol context) */
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02001856 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001857 { "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 +01001858 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001859 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001860 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001861 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001862 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1863 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001864 { NULL, NULL, 0 },
1865}};
1866
Willy Tarreau0108d902018-11-25 19:14:37 +01001867INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001868
Willy Tarreau004e0452013-11-21 11:22:01 +01001869/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001870 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001871 * state is automatically disabled if the time is elapsed. If <must_update> is
1872 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001873 *
1874 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001875 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001876void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001877{
1878 struct proxy *px = sv->proxy;
1879 unsigned w;
1880
1881 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1882 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001883 if (sv->next_state == SRV_ST_STARTING)
1884 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001885 }
1886
1887 /* We must take care of not pushing the server to full throttle during slow starts.
1888 * It must also start immediately, at least at the minimal step when leaving maintenance.
1889 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001890 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001891 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1892 else
1893 w = px->lbprm.wdiv;
1894
Emeric Brun52a91d32017-08-31 14:41:55 +02001895 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001896
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001897 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001898 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001899 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001900}
1901
Willy Tarreaubaaee002006-06-26 02:48:02 +02001902/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001903 * Parses weight_str and configures sv accordingly.
1904 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001905 *
1906 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001907 */
1908const char *server_parse_weight_change_request(struct server *sv,
1909 const char *weight_str)
1910{
1911 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001912 long int w;
1913 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001914
1915 px = sv->proxy;
1916
1917 /* if the weight is terminated with '%', it is set relative to
1918 * the initial weight, otherwise it is absolute.
1919 */
1920 if (!*weight_str)
1921 return "Require <weight> or <weight%>.\n";
1922
Simon Hormanb796afa2013-02-12 10:45:53 +09001923 w = strtol(weight_str, &end, 10);
1924 if (end == weight_str)
1925 return "Empty weight string empty or preceded by garbage";
1926 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001927 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001928 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001929 /* Avoid integer overflow */
1930 if (w > 25600)
1931 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001932 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001933 if (w > 256)
1934 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001935 }
1936 else if (w < 0 || w > 256)
1937 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001938 else if (end[0] != '\0')
1939 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001940
1941 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1942 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1943
1944 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001945 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001946
1947 return NULL;
1948}
1949
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001950/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001951 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1952 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001953 * Returns:
1954 * - error string on error
1955 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001956 *
1957 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001958 */
1959const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001960 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001961{
1962 unsigned char ip[INET6_ADDRSTRLEN];
1963
1964 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001965 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001966 return NULL;
1967 }
1968 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001969 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001970 return NULL;
1971 }
1972
1973 return "Could not understand IP address format.\n";
1974}
1975
Willy Tarreau46b7f532018-08-21 11:54:26 +02001976/*
1977 * Must be called with the server lock held.
1978 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001979const char *server_parse_maxconn_change_request(struct server *sv,
1980 const char *maxconn_str)
1981{
1982 long int v;
1983 char *end;
1984
1985 if (!*maxconn_str)
1986 return "Require <maxconn>.\n";
1987
1988 v = strtol(maxconn_str, &end, 10);
1989 if (end == maxconn_str)
1990 return "maxconn string empty or preceded by garbage";
1991 else if (end[0] != '\0')
1992 return "Trailing garbage in maxconn string";
1993
1994 if (sv->maxconn == sv->minconn) { // static maxconn
1995 sv->maxconn = sv->minconn = v;
1996 } else { // dynamic maxconn
1997 sv->maxconn = v;
1998 }
1999
2000 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02002001 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02002002
2003 return NULL;
2004}
2005
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002006static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
2007 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002008{
2009 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002010 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002011 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002012 NULL,
2013 };
2014
2015 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02002016 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002017
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01002018 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002019}
2020
William Lallemand0d058672022-03-16 15:44:42 +01002021int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002022{
2023 struct sample_expr *expr;
2024
2025 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 +01002026 if (!expr) {
2027 memprintf(err, "error detected while parsing sni expression : %s", *err);
2028 return ERR_ALERT | ERR_FATAL;
2029 }
2030
2031 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
2032 memprintf(err, "error detected while parsing sni expression : "
2033 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02002034 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002035 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002036 return ERR_ALERT | ERR_FATAL;
2037 }
2038
2039 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
2040 release_sample_expr(newsrv->ssl_ctx.sni);
2041 newsrv->ssl_ctx.sni = expr;
2042
2043 return 0;
2044}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002045
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002046static 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 +01002047{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002048 char *msg = "error encountered while processing ";
2049 char *quote = "'";
2050 char *token = args[cur_arg];
2051
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002052 if (err && *err) {
2053 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002054 msg = *err;
2055 quote = "";
2056 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002057 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002058
2059 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002060 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002061 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002062 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002063}
2064
Christopher Faulet0b365e32022-08-03 11:21:14 +02002065static void srv_conn_src_sport_range_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002066{
2067 int range_sz;
2068
2069 range_sz = src->conn_src.sport_range->size;
2070 if (range_sz > 0) {
2071 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
2072 if (srv->conn_src.sport_range != NULL) {
2073 int i;
2074
2075 for (i = 0; i < range_sz; i++) {
2076 srv->conn_src.sport_range->ports[i] =
2077 src->conn_src.sport_range->ports[i];
2078 }
2079 }
2080 }
2081}
2082
2083/*
2084 * Copy <src> server connection source settings to <srv> server everything needed.
2085 */
Christopher Faulet0b365e32022-08-03 11:21:14 +02002086static void srv_conn_src_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002087{
2088 srv->conn_src.opts = src->conn_src.opts;
2089 srv->conn_src.source_addr = src->conn_src.source_addr;
2090
2091 /* Source port range copy. */
2092 if (src->conn_src.sport_range != NULL)
2093 srv_conn_src_sport_range_cpy(srv, src);
2094
2095#ifdef CONFIG_HAP_TRANSPARENT
2096 if (src->conn_src.bind_hdr_name != NULL) {
2097 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
2098 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
2099 }
2100 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
2101 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
2102#endif
2103 if (src->conn_src.iface_name != NULL)
2104 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
2105}
2106
2107/*
2108 * Copy <src> server SSL settings to <srv> server allocating
2109 * everything needed.
2110 */
2111#if defined(USE_OPENSSL)
Christopher Faulet0b365e32022-08-03 11:21:14 +02002112static void srv_ssl_settings_cpy(struct server *srv, const struct server *src)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002113{
Christopher Faulet4ab26792021-12-01 09:50:41 +01002114 /* <src> is the current proxy's default server and SSL is enabled */
William Lallemand2c776f12021-12-28 18:47:17 +01002115 BUG_ON(src->ssl_ctx.ctx != NULL); /* the SSL_CTX must never be initialized in a default-server */
2116
Christopher Faulet4ab26792021-12-01 09:50:41 +01002117 if (src == &srv->proxy->defsrv && src->use_ssl == 1)
2118 srv->flags |= SRV_F_DEFSRV_USE_SSL;
2119
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002120 if (src->ssl_ctx.ca_file != NULL)
2121 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
2122 if (src->ssl_ctx.crl_file != NULL)
2123 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
William Lallemand2c776f12021-12-28 18:47:17 +01002124 if (src->ssl_ctx.client_crt != NULL)
2125 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002126
2127 srv->ssl_ctx.verify = src->ssl_ctx.verify;
2128
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02002129
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002130 if (src->ssl_ctx.verify_host != NULL)
2131 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
2132 if (src->ssl_ctx.ciphers != NULL)
2133 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02002134 if (src->ssl_ctx.options)
2135 srv->ssl_ctx.options = src->ssl_ctx.options;
2136 if (src->ssl_ctx.methods.flags)
2137 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
2138 if (src->ssl_ctx.methods.min)
2139 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
2140 if (src->ssl_ctx.methods.max)
2141 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
2142
Dirkjan Bussink415150f2018-09-14 11:14:21 +02002143 if (src->ssl_ctx.ciphersuites != NULL)
2144 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002145 if (src->sni_expr != NULL)
2146 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01002147
Olivier Houchardc7566002018-11-20 23:33:50 +01002148 if (src->ssl_ctx.alpn_str) {
2149 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
2150 if (srv->ssl_ctx.alpn_str) {
2151 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
2152 src->ssl_ctx.alpn_len);
2153 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
2154 }
2155 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002156
Olivier Houchardc7566002018-11-20 23:33:50 +01002157 if (src->ssl_ctx.npn_str) {
2158 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
2159 if (srv->ssl_ctx.npn_str) {
2160 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
2161 src->ssl_ctx.npn_len);
2162 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
2163 }
2164 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002165}
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002166
2167/* Activate ssl on server <s>.
2168 * do nothing if there is no change to apply
2169 *
2170 * Must be called with the server lock held.
2171 */
2172void srv_set_ssl(struct server *s, int use_ssl)
2173{
2174 if (s->use_ssl == use_ssl)
2175 return;
2176
2177 s->use_ssl = use_ssl;
2178 if (s->use_ssl)
2179 s->xprt = xprt_get(XPRT_SSL);
2180 else
William Dauchya087f872022-01-06 16:57:15 +01002181 s->xprt = xprt_get(XPRT_RAW);
Willy Tarreaua8a72c62021-10-06 11:48:34 +02002182}
2183
2184#endif /* USE_OPENSSL */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002185
2186/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002187 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002188 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002189 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002190 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002191int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002192{
Christopher Faulet67957bd2017-09-27 11:00:59 +02002193 char *hostname_dn;
2194 int hostname_len, hostname_dn_len;
2195
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002196 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002197 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002198
Christopher Faulet67957bd2017-09-27 11:00:59 +02002199 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002200 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02002201 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01002202 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002203 if (hostname_dn_len == -1)
2204 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002205
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002206
Christopher Faulet67957bd2017-09-27 11:00:59 +02002207 free(srv->hostname);
2208 free(srv->hostname_dn);
2209 srv->hostname = strdup(hostname);
2210 srv->hostname_dn = strdup(hostname_dn);
2211 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002212 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002213 goto err;
2214
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002215 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002216
2217 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002218 ha_free(&srv->hostname);
2219 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002220 return -1;
2221}
2222
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002223/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002224 * Copy <src> server settings to <srv> server allocating
2225 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002226 * This function is not supposed to be called at any time, but only
2227 * during server settings parsing or during server allocations from
2228 * a server template, and just after having calloc()'ed a new server.
2229 * So, <src> may only be a default server (when parsing server settings)
2230 * or a server template (during server allocations from a server template).
2231 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2232 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002233 */
Christopher Fauletb32cb9b2022-08-03 11:28:08 +02002234void srv_settings_cpy(struct server *srv, const struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002235{
2236 /* Connection source settings copy */
2237 srv_conn_src_cpy(srv, src);
2238
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002239 if (srv_tmpl) {
2240 srv->addr = src->addr;
2241 srv->svc_port = src->svc_port;
2242 }
2243
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002244 srv->pp_opts = src->pp_opts;
2245 if (src->rdr_pfx != NULL) {
2246 srv->rdr_pfx = strdup(src->rdr_pfx);
2247 srv->rdr_len = src->rdr_len;
2248 }
2249 if (src->cookie != NULL) {
2250 srv->cookie = strdup(src->cookie);
2251 srv->cklen = src->cklen;
2252 }
2253 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002254 srv->check.addr = src->check.addr;
2255 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002256 srv->check.use_ssl = src->check.use_ssl;
2257 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002258 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002259 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002260 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002261 /* Note: 'flags' field has potentially been already initialized. */
2262 srv->flags |= src->flags;
2263 srv->do_check = src->do_check;
2264 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002265 srv->check.inter = src->check.inter;
2266 srv->check.fastinter = src->check.fastinter;
2267 srv->check.downinter = src->check.downinter;
2268 srv->agent.use_ssl = src->agent.use_ssl;
2269 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002270
2271 if (src->agent.tcpcheck_rules) {
2272 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2273 if (srv->agent.tcpcheck_rules) {
2274 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2275 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2276 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2277 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2278 &src->agent.tcpcheck_rules->preset_vars);
2279 }
2280 }
2281
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002282 srv->agent.inter = src->agent.inter;
2283 srv->agent.fastinter = src->agent.fastinter;
2284 srv->agent.downinter = src->agent.downinter;
2285 srv->maxqueue = src->maxqueue;
Amaury Denoyelle9c3251d2021-10-18 14:39:57 +02002286 srv->ws = src->ws;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002287 srv->minconn = src->minconn;
2288 srv->maxconn = src->maxconn;
2289 srv->slowstart = src->slowstart;
2290 srv->observe = src->observe;
2291 srv->onerror = src->onerror;
2292 srv->onmarkeddown = src->onmarkeddown;
2293 srv->onmarkedup = src->onmarkedup;
2294 if (src->trackit != NULL)
2295 srv->trackit = strdup(src->trackit);
2296 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2297 srv->uweight = srv->iweight = src->iweight;
2298
2299 srv->check.send_proxy = src->check.send_proxy;
2300 /* health: up, but will fall down at first failure */
2301 srv->check.rise = srv->check.health = src->check.rise;
2302 srv->check.fall = src->check.fall;
2303
2304 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002305 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2306 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2307 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002308 srv->check.state |= CHK_ST_PAUSED;
2309 srv->check.health = 0;
2310 }
2311
2312 /* health: up but will fall down at first failure */
2313 srv->agent.rise = srv->agent.health = src->agent.rise;
2314 srv->agent.fall = src->agent.fall;
2315
2316 if (src->resolvers_id != NULL)
2317 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002318 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2319 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2320 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2321 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2322 srv->resolv_opts.family_prio = AF_INET6;
2323 memcpy(srv->resolv_opts.pref_net,
2324 src->resolv_opts.pref_net,
2325 sizeof srv->resolv_opts.pref_net);
2326 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002327
2328 srv->init_addr_methods = src->init_addr_methods;
2329 srv->init_addr = src->init_addr;
2330#if defined(USE_OPENSSL)
2331 srv_ssl_settings_cpy(srv, src);
2332#endif
2333#ifdef TCP_USER_TIMEOUT
2334 srv->tcp_ut = src->tcp_ut;
2335#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002336 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002337 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002338 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002339 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002340 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002341
Olivier Houchard8da5f982017-08-04 18:35:36 +02002342 if (srv_tmpl)
2343 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002344
2345 srv->check.via_socks4 = src->check.via_socks4;
2346 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002347}
2348
Willy Tarreau198e92a2021-03-05 10:23:32 +01002349/* allocate a server and attach it to the global servers_list. Returns
2350 * the server on success, otherwise NULL.
2351 */
William Lallemand313bfd12018-10-26 14:47:32 +02002352struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002353{
2354 struct server *srv;
2355
2356 srv = calloc(1, sizeof *srv);
2357 if (!srv)
2358 return NULL;
2359
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002360 srv_take(srv);
2361
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002362 srv->obj_type = OBJ_TYPE_SERVER;
2363 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002364 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002365 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002366 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002367 LIST_INIT(&srv->ip_rec_item);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002368 event_hdl_sub_list_init(&srv->e_subs);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002369
Emeric Brun52a91d32017-08-31 14:41:55 +02002370 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002371 srv->last_change = now.tv_sec;
2372
Christopher Faulet38290462020-04-21 11:46:40 +02002373 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002374 srv->check.status = HCHK_STATUS_INI;
2375 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002376 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002377 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002378
Christopher Faulet38290462020-04-21 11:46:40 +02002379 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002380 srv->agent.status = HCHK_STATUS_INI;
2381 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002382 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002383 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002384#if defined(USE_QUIC)
2385 srv->cids = EB_ROOT_UNIQUE;
2386#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002387
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002388 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002389#ifdef USE_OPENSSL
2390 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2391#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002392
Willy Tarreau975b1552019-06-06 16:25:55 +02002393 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002394 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002395 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002396 return srv;
2397}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002398
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002399/* Increment the server refcount. */
2400void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002401{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002402 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002403}
2404
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002405/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2406 * dynamic servers, its refcount is decremented first. The free operations are
2407 * conducted only if the refcount is nul, unless the process is stopping.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002408 *
2409 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002410 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002411 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002412struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002413{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002414 struct server *next = NULL;
2415
William Lallemand4c395fc2021-08-20 10:10:15 +02002416 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002417 goto end;
2418
2419 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002420
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002421 /* For dynamic servers, decrement the reference counter. Only free the
2422 * server when reaching zero.
2423 */
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002424 if (likely(!(global.mode & MODE_STOPPING))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002425 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2426 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002427 }
2428
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002429 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002430 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002431
2432 free(srv->id);
2433 free(srv->cookie);
2434 free(srv->hostname);
2435 free(srv->hostname_dn);
2436 free((char*)srv->conf.file);
2437 free(srv->per_thr);
Willy Tarreauc21a1872022-11-21 14:14:06 +01002438 free(srv->per_tgrp);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002439 free(srv->curr_idle_thr);
2440 free(srv->resolvers_id);
2441 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002442 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002443
2444 if (srv->use_ssl == 1 || srv->check.use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL)) {
2445 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2446 xprt_get(XPRT_SSL)->destroy_srv(srv);
2447 }
2448 HA_SPIN_DESTROY(&srv->lock);
2449
Willy Tarreau2b718102021-04-21 07:32:39 +02002450 LIST_DELETE(&srv->global_list);
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01002451 event_hdl_sub_list_destroy(&srv->e_subs);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002452
2453 EXTRA_COUNTERS_FREE(srv->extra_counters);
2454
Tim Duesterhus025b93e2021-11-04 21:03:52 +01002455 ha_free(&srv);
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002456
2457 end:
2458 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002459}
2460
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002461/* Remove a server <srv> from a tracking list if <srv> is tracking another
2462 * server. No special care is taken if <srv> is tracked itself by another one :
2463 * this situation should be avoided by the caller.
2464 *
2465 * Not thread-safe.
2466 */
2467static void release_server_track(struct server *srv)
2468{
2469 struct server *strack = srv->track;
2470 struct server **base;
2471
2472 if (!strack)
2473 return;
2474
2475 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2476 if (*base == srv) {
2477 *base = srv->tracknext;
2478 return;
2479 }
2480 }
2481
2482 /* srv not found on the tracking list, this should never happen */
2483 BUG_ON(!*base);
2484}
2485
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002486/*
2487 * Parse as much as possible such a range string argument: low[-high]
2488 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2489 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2490 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2491 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002492 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002493static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2494 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002495{
2496 char *nb_high_arg;
2497
2498 *nb_high = 0;
2499 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002500 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002501
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002502 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002503 *nb_high_arg++ = '\0';
2504 *nb_high = atoi(nb_high_arg);
2505 }
2506 else {
2507 *nb_high += *nb_low;
2508 *nb_low = 1;
2509 }
2510
2511 if (*nb_low < 0 || *nb_high < *nb_low)
2512 return -1;
2513
2514 return 0;
2515}
2516
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002517/* Parse as much as possible such a range string argument: low[-high]
2518 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2519 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2520 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002521 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002522 * initialize a new server on startup.
2523 *
2524 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2525 * Returns 0 if succeeded, -1 if not.
2526 */
2527static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2528 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002529{
2530 chunk_printf(&trash, "%s%d", prefix, nb);
2531 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002532 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002533}
2534
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002535/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002536 * Note that a server template is a special server with
2537 * a few different parameters than a server which has
2538 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002539 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002540 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002541 * initialize a new server on startup.
2542 *
Joseph Herlant44466822018-11-15 08:57:51 -08002543 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002544 * 'srv' template included.
2545 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002546static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002547{
2548 int i;
2549 struct server *newsrv;
2550
2551 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 +02002552 newsrv = new_server(px);
2553 if (!newsrv)
2554 goto err;
2555
Christopher Faulet75bef002020-11-02 22:04:55 +01002556 newsrv->conf.file = strdup(srv->conf.file);
2557 newsrv->conf.line = srv->conf.line;
2558
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002559 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002560 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002561
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002562 if (newsrv->sni_expr) {
2563 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2564 if (!newsrv->ssl_ctx.sni)
2565 goto err;
2566 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002567
Emeric Brun34067662021-06-11 10:48:45 +02002568 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002569 if (newsrv->srvrq)
2570 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002571
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002572 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002573 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002574
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002575 /* Linked backwards first. This will be restablished after parsing. */
2576 newsrv->next = px->srv;
2577 px->srv = newsrv;
2578 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002579 _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 +02002580
2581 return i - srv->tmpl_info.nb_low;
2582
2583 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002584 _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 +02002585 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002586 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002587 free_check(&newsrv->agent);
2588 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002589 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002590 }
2591 free(newsrv);
2592 return i - srv->tmpl_info.nb_low;
2593}
2594
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002595/* Allocate a new server pointed by <srv> and try to parse the first arguments
2596 * in <args> as an address for a server or an address-range for a template or
2597 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2598 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002599 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002600 * initialize a new server on startup.
2601 *
2602 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2603 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2604 * <srv> will be set to NULL.
2605 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002606static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2607 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002608 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002609{
2610 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002611 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002612 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002613 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002614 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002615 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002616
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002617 *srv = NULL;
2618
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002619 /* There is no mandatory first arguments for default server. */
2620 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2621 if (parse_flags & SRV_PARSE_TEMPLATE) {
2622 if (!*args[3]) {
2623 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002624 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2625 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002626 err_code |= ERR_ALERT | ERR_FATAL;
2627 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002628 }
2629
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002630 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002631 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002632 else {
2633 if (!*args[2]) {
2634 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002635 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2636 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002637 err_code |= ERR_ALERT | ERR_FATAL;
2638 goto out;
2639 }
2640
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002641 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002642 }
2643
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002644 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002645 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2646 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002647 err_code |= ERR_ALERT | ERR_FATAL;
2648 goto out;
2649 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002650 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002651
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002652 *cur_arg = 2;
2653 if (parse_flags & SRV_PARSE_TEMPLATE) {
2654 /* Parse server-template <nb | range> arg. */
2655 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002656 ha_alert("Wrong %s number or range arg '%s'.\n",
2657 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002658 err_code |= ERR_ALERT | ERR_FATAL;
2659 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002660 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002661 (*cur_arg)++;
2662 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002663
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002664 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2665 struct sockaddr_storage *sk;
2666 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002667
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002668 *srv = newsrv = new_server(curproxy);
2669 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002670 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002671 err_code |= ERR_ALERT | ERR_ABORT;
2672 goto out;
2673 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002674 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002675
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002676 if (parse_flags & SRV_PARSE_TEMPLATE) {
2677 newsrv->tmpl_info.nb_low = tmpl_range_low;
2678 newsrv->tmpl_info.nb_high = tmpl_range_high;
2679 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002680
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002681 if (parse_flags & SRV_PARSE_DYNAMIC)
2682 newsrv->flags |= SRV_F_DYNAMIC;
2683
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002684 /* Note: for a server template, its id is its prefix.
2685 * This is a temporary id which will be used for server allocations to come
2686 * after parsing.
2687 */
2688 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2689 newsrv->id = strdup(args[1]);
2690 else
2691 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002692
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01002693 /* revision defaults to 0 */
2694 newsrv->rid = 0;
2695
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002696 /* several ways to check the port component :
2697 * - IP => port=+0, relative (IPv4 only)
2698 * - IP: => port=+0, relative
2699 * - IP:N => port=N, absolute
2700 * - IP:+N => port=+N, relative
2701 * - IP:-N => port=-N, relative
2702 */
2703 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2704 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002705
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002706 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002707 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002708 (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);
2709 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002710 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002711 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002712 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002713 goto out;
2714 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002715
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002716 if (!port1 || !port2) {
2717 /* no port specified, +offset, -offset */
2718 newsrv->flags |= SRV_F_MAPPORTS;
2719 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002720
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002721 /* save hostname and create associated name resolution */
2722 if (fqdn) {
2723 if (fqdn[0] == '_') { /* SRV record */
2724 /* Check if a SRV request already exists, and if not, create it */
2725 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2726 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2727 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002728 err_code |= ERR_ALERT | ERR_FATAL;
2729 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002730 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002731 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002732 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002733 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002734 ha_alert("Can't create DNS resolution for server '%s'\n",
2735 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002736 err_code |= ERR_ALERT | ERR_FATAL;
2737 goto out;
2738 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002739 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002740
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002741 newsrv->addr = *sk;
2742 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002743 /*
2744 * we don't need to lock the server here, because
2745 * we are in the process of initializing.
2746 *
2747 * Note that the server is not attached into the proxy tree if
2748 * this is a dynamic server.
2749 */
2750 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002751
Willy Tarreau14e7f292021-10-27 17:41:07 +02002752 if (!newsrv->srvrq && !newsrv->hostname &&
2753 !protocol_lookup(newsrv->addr.ss_family, PROTO_TYPE_STREAM, 0)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002754 ha_alert("Unknown protocol family %d '%s'\n",
2755 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002756 err_code |= ERR_ALERT | ERR_FATAL;
2757 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002758 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002759
2760 (*cur_arg)++;
2761 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002762 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2763 /* Copy default server settings to new server */
2764 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2765 } else {
2766 /* Initialize dynamic server weight to 1 */
2767 newsrv->uweight = newsrv->iweight = 1;
2768
2769 /* A dynamic server is disabled on startup */
2770 newsrv->next_admin = SRV_ADMF_FMAINT;
2771 newsrv->next_state = SRV_ST_STOPPED;
2772 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002773
2774 /* Set default values for checks */
2775 newsrv->check.inter = DEF_CHKINTR;
2776 newsrv->check.rise = DEF_RISETIME;
2777 newsrv->check.fall = DEF_FALLTIME;
2778
2779 newsrv->agent.inter = DEF_CHKINTR;
2780 newsrv->agent.rise = DEF_AGENT_RISETIME;
2781 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002782 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002783 HA_SPIN_INIT(&newsrv->lock);
2784 }
2785 else {
2786 *srv = newsrv = &curproxy->defsrv;
2787 *cur_arg = 1;
2788 newsrv->resolv_opts.family_prio = AF_INET6;
2789 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002790 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002791
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002792 free(fqdn);
2793 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002794
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002795out:
2796 free(fqdn);
2797 return err_code;
2798}
Willy Tarreau272adea2014-03-31 10:39:59 +02002799
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002800/* Parse the server keyword in <args>.
2801 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2802 * might not be the case if an error is reported.
2803 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002804 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002805 * initialize a new server on startup.
2806 *
2807 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2808 * interrupted.
2809 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002810static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2811 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002812 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002813{
2814 int err_code = 0;
2815 struct srv_kw *kw;
2816 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002817 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002818
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002819 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002820 if (!kw) {
2821 best = srv_find_best_kw(args[*cur_arg]);
2822 if (best)
Willy Tarreau245721b2022-05-31 09:25:34 +02002823 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?%s\n",
2824 args[*cur_arg], best,
2825 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2826 " Hint: no address was expected for this server.");
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002827 else
Willy Tarreau245721b2022-05-31 09:25:34 +02002828 ha_alert("unknown keyword '%s'.%s\n", args[*cur_arg],
2829 (parse_flags & SRV_PARSE_PARSE_ADDR) ? "" :
2830 " Hint: no address was expected for this server.");
Willy Tarreau272adea2014-03-31 10:39:59 +02002831
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002832 return ERR_ALERT | ERR_FATAL;
2833 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002834
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002835 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002836 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2837 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002838 err_code = ERR_ALERT | ERR_FATAL;
2839 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002840 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002841
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002842 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002843 ha_alert("'%s' option is not accepted in default-server sections\n",
2844 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002845 err_code = ERR_ALERT;
2846 goto out;
2847 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002848 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002849 ha_alert("'%s' option is not accepted for dynamic server\n",
2850 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002851 err_code |= ERR_ALERT;
2852 goto out;
2853 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002854
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002855 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2856 if (err_code) {
2857 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2858 free(errmsg);
2859 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002860
2861out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002862 if (kw->skip != -1)
2863 *cur_arg += 1 + kw->skip;
2864
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002865 return err_code;
2866}
2867
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002868/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002869 * initialize a new server on startup.
2870 */
2871static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2872 struct server *srv, struct proxy *proxy,
2873 char **errmsg)
2874{
2875 int ret;
2876
2877 if (!srv->sni_expr)
2878 return 0;
2879
2880 ret = server_parse_sni_expr(srv, proxy, errmsg);
2881 if (!ret)
2882 return 0;
2883
2884 return ret;
2885}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002886
2887/* Server initializations finalization.
2888 * Initialize health check, agent check and SNI expression if enabled.
2889 * Must not be called for a default server instance.
2890 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002891 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002892 * initialize a new server on startup.
2893 */
2894static int _srv_parse_finalize(char **args, int cur_arg,
2895 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002896 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002897{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002898 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002899 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002900
2901 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002902 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002903 return ERR_ALERT | ERR_FATAL;
2904 }
2905
2906 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002907 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2908 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002909 return ERR_ALERT | ERR_FATAL;
2910 }
2911
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002912 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2913 if (errmsg) {
2914 ha_alert("%s\n", errmsg);
2915 free(errmsg);
2916 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002917 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002918 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002919
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002920 /* A dynamic server is disabled on startup. It must not be counted as
2921 * an active backend entry.
2922 */
2923 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2924 if (srv->flags & SRV_F_BACKUP)
2925 px->srv_bck++;
2926 else
2927 px->srv_act++;
2928 }
2929
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002930 srv_lb_commit_status(srv);
2931
2932 return 0;
2933}
2934
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002935int parse_server(const char *file, int linenum, char **args,
2936 struct proxy *curproxy, const struct proxy *defproxy,
2937 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002938{
2939 struct server *newsrv = NULL;
2940 int err_code = 0;
2941
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002942 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002943
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002944 set_usermsgs_ctx(file, linenum, NULL);
2945
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002946 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002947 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002948 err_code |= ERR_ALERT | ERR_FATAL;
2949 goto out;
2950 }
2951 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2952 err_code |= ERR_ALERT | ERR_FATAL;
2953 goto out;
2954 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002955
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002956 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2957 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2958 if (!*args[2])
2959 return 0;
2960 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002961
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002962 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002963 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002964
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002965 /* the servers are linked backwards first */
2966 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2967 newsrv->next = curproxy->srv;
2968 curproxy->srv = newsrv;
2969 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002970
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002971 if (err_code & ERR_CODE)
2972 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002973
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002974 newsrv->conf.file = strdup(file);
2975 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002976
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002977 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002978 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002979 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002980 if (err_code & ERR_FATAL)
2981 goto out;
2982 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002983
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002984 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002985 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002986 if (err_code & ERR_FATAL)
2987 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002988 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002989
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002990 if (parse_flags & SRV_PARSE_TEMPLATE)
2991 _srv_parse_tmpl_init(newsrv, curproxy);
2992
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02002993 /* If the server id is fixed, insert it in the proxy used_id tree.
2994 * This is needed to detect a later duplicate id via srv_parse_id.
2995 *
2996 * If no is specified, a dynamic one is generated in
2997 * check_config_validity.
2998 */
2999 if (newsrv->flags & SRV_F_FORCED_ID)
3000 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
3001
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02003002 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003003 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02003004
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003005 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003006 return 0;
3007
3008 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02003009 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02003010 return err_code;
3011}
3012
Baptiste Assmann19a106d2015-07-08 22:03:56 +02003013/* Returns a pointer to the first server matching either id <id>.
3014 * NULL is returned if no match is found.
3015 * the lookup is performed in the backend <bk>
3016 */
3017struct server *server_find_by_id(struct proxy *bk, int id)
3018{
3019 struct eb32_node *eb32;
3020 struct server *curserver;
3021
3022 if (!bk || (id ==0))
3023 return NULL;
3024
3025 /* <bk> has no backend capabilities, so it can't have a server */
3026 if (!(bk->cap & PR_CAP_BE))
3027 return NULL;
3028
3029 curserver = NULL;
3030
3031 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
3032 if (eb32)
3033 curserver = container_of(eb32, struct server, conf.id);
3034
3035 return curserver;
3036}
3037
3038/* Returns a pointer to the first server matching either name <name>, or id
3039 * if <name> starts with a '#'. NULL is returned if no match is found.
3040 * the lookup is performed in the backend <bk>
3041 */
3042struct server *server_find_by_name(struct proxy *bk, const char *name)
3043{
3044 struct server *curserver;
3045
3046 if (!bk || !name)
3047 return NULL;
3048
3049 /* <bk> has no backend capabilities, so it can't have a server */
3050 if (!(bk->cap & PR_CAP_BE))
3051 return NULL;
3052
3053 curserver = NULL;
3054 if (*name == '#') {
3055 curserver = server_find_by_id(bk, atoi(name + 1));
3056 if (curserver)
3057 return curserver;
3058 }
3059 else {
3060 curserver = bk->srv;
3061
3062 while (curserver && (strcmp(curserver->id, name) != 0))
3063 curserver = curserver->next;
3064
3065 if (curserver)
3066 return curserver;
3067 }
3068
3069 return NULL;
3070}
3071
3072struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
3073{
3074 struct server *byname;
3075 struct server *byid;
3076
3077 if (!name && !id)
3078 return NULL;
3079
3080 if (diff)
3081 *diff = 0;
3082
3083 byname = byid = NULL;
3084
3085 if (name) {
3086 byname = server_find_by_name(bk, name);
3087 if (byname && (!id || byname->puid == id))
3088 return byname;
3089 }
3090
3091 /* remaining possibilities :
3092 * - name not set
3093 * - name set but not found
3094 * - name found but ID doesn't match
3095 */
3096 if (id) {
3097 byid = server_find_by_id(bk, id);
3098 if (byid) {
3099 if (byname) {
3100 /* use id only if forced by configuration */
3101 if (byid->flags & SRV_F_FORCED_ID) {
3102 if (diff)
3103 *diff |= 2;
3104 return byid;
3105 }
3106 else {
3107 if (diff)
3108 *diff |= 1;
3109 return byname;
3110 }
3111 }
3112
3113 /* remaining possibilities:
3114 * - name not set
3115 * - name set but not found
3116 */
3117 if (name && diff)
3118 *diff |= 2;
3119 return byid;
3120 }
3121
3122 /* id bot found */
3123 if (byname) {
3124 if (diff)
3125 *diff |= 1;
3126 return byname;
3127 }
3128 }
3129
3130 return NULL;
3131}
3132
Simon Horman7d09b9a2013-02-12 10:45:51 +09003133/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003134 * update a server's current IP address.
3135 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3136 * ip is in network format.
3137 * updater is a string which contains an information about the requester of the update.
3138 * updater is used if not NULL.
3139 *
3140 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003141 *
3142 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003143 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003144int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003145{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003146 /* save the new IP family & address if necessary */
3147 switch (ip_sin_family) {
3148 case AF_INET:
3149 if (s->addr.ss_family == ip_sin_family &&
3150 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3151 return 0;
3152 break;
3153 case AF_INET6:
3154 if (s->addr.ss_family == ip_sin_family &&
3155 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3156 return 0;
3157 break;
3158 };
3159
Baptiste Assmann14e40142015-04-14 01:13:07 +02003160 /* generates a log line and a warning on stderr */
3161 if (1) {
3162 /* book enough space for both IPv4 and IPv6 */
3163 char oldip[INET6_ADDRSTRLEN];
3164 char newip[INET6_ADDRSTRLEN];
3165
3166 memset(oldip, '\0', INET6_ADDRSTRLEN);
3167 memset(newip, '\0', INET6_ADDRSTRLEN);
3168
3169 /* copy old IP address in a string */
3170 switch (s->addr.ss_family) {
3171 case AF_INET:
3172 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3173 break;
3174 case AF_INET6:
3175 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3176 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003177 default:
3178 strcpy(oldip, "(none)");
3179 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003180 };
3181
3182 /* copy new IP address in a string */
3183 switch (ip_sin_family) {
3184 case AF_INET:
3185 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3186 break;
3187 case AF_INET6:
3188 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3189 break;
3190 };
3191
3192 /* save log line into a buffer */
3193 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3194 s->proxy->id, s->id, oldip, newip, updater);
3195
3196 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003197 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003198
3199 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003200 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003201 }
3202
3203 /* save the new IP family */
3204 s->addr.ss_family = ip_sin_family;
3205 /* save the new IP address */
3206 switch (ip_sin_family) {
3207 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003208 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003209 break;
3210 case AF_INET6:
3211 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3212 break;
3213 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003214 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003215 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003216
3217 return 0;
3218}
3219
William Dauchy7cabc062021-02-11 22:51:24 +01003220/* update agent health check address and port
3221 * addr can be ip4/ip6 or a hostname
3222 * if one error occurs, don't apply anything
3223 * must be called with the server lock held.
3224 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003225const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003226{
3227 struct sockaddr_storage sk;
3228 struct buffer *msg;
3229 int new_port;
3230
3231 msg = get_trash_chunk();
3232 chunk_reset(msg);
3233
3234 if (!(s->agent.state & CHK_ST_ENABLED)) {
3235 chunk_strcat(msg, "agent checks are not enabled on this server");
3236 goto out;
3237 }
3238 if (addr) {
3239 memset(&sk, 0, sizeof(struct sockaddr_storage));
3240 if (str2ip(addr, &sk) == NULL) {
3241 chunk_appendf(msg, "invalid addr '%s'", addr);
3242 goto out;
3243 }
3244 }
3245 if (port) {
3246 if (strl2irc(port, strlen(port), &new_port) != 0) {
3247 chunk_appendf(msg, "provided port is not an integer");
3248 goto out;
3249 }
3250 if (new_port < 0 || new_port > 65535) {
3251 chunk_appendf(msg, "provided port is invalid");
3252 goto out;
3253 }
3254 }
3255out:
3256 if (msg->data)
3257 return msg->area;
3258 else {
3259 if (addr)
3260 set_srv_agent_addr(s, &sk);
3261 if (port)
3262 set_srv_agent_port(s, new_port);
3263 }
3264 return NULL;
3265}
3266
William Dauchyb456e1f2021-02-11 22:51:23 +01003267/* update server health check address and port
3268 * addr must be ip4 or ip6, it won't be resolved
3269 * if one error occurs, don't apply anything
3270 * must be called with the server lock held.
3271 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003272const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003273{
3274 struct sockaddr_storage sk;
3275 struct buffer *msg;
3276 int new_port;
3277
3278 msg = get_trash_chunk();
3279 chunk_reset(msg);
3280
3281 if (!(s->check.state & CHK_ST_ENABLED)) {
3282 chunk_strcat(msg, "health checks are not enabled on this server");
3283 goto out;
3284 }
3285 if (addr) {
3286 memset(&sk, 0, sizeof(struct sockaddr_storage));
3287 if (str2ip2(addr, &sk, 0) == NULL) {
3288 chunk_appendf(msg, "invalid addr '%s'", addr);
3289 goto out;
3290 }
3291 }
3292 if (port) {
3293 if (strl2irc(port, strlen(port), &new_port) != 0) {
3294 chunk_appendf(msg, "provided port is not an integer");
3295 goto out;
3296 }
3297 if (new_port < 0 || new_port > 65535) {
3298 chunk_appendf(msg, "provided port is invalid");
3299 goto out;
3300 }
3301 /* prevent the update of port to 0 if MAPPORTS are in use */
3302 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3303 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3304 goto out;
3305 }
3306 }
3307out:
3308 if (msg->data)
3309 return msg->area;
3310 else {
3311 if (addr)
3312 s->check.addr = sk;
3313 if (port)
3314 s->check.port = new_port;
3315 }
3316 return NULL;
3317}
3318
Baptiste Assmann14e40142015-04-14 01:13:07 +02003319/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003320 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3321 *
3322 * Caller can pass its name through <updater> to get it integrated in the response message
3323 * returned by the function.
3324 *
3325 * The function first does the following, in that order:
3326 * - validates the new addr and/or port
3327 * - checks if an update is required (new IP or port is different than current ones)
3328 * - checks the update is allowed:
3329 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3330 * - allow all changes if no CHECKS are configured
3331 * - if CHECK is configured:
3332 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3333 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3334 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003335 *
3336 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003337 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003338const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003339{
3340 struct sockaddr_storage sa;
3341 int ret, port_change_required;
3342 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003343 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003344 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003345 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003346
3347 msg = get_trash_chunk();
3348 chunk_reset(msg);
3349
3350 if (addr) {
3351 memset(&sa, 0, sizeof(struct sockaddr_storage));
3352 if (str2ip2(addr, &sa, 0) == NULL) {
3353 chunk_printf(msg, "Invalid addr '%s'", addr);
3354 goto out;
3355 }
3356
3357 /* changes are allowed on AF_INET* families only */
3358 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3359 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3360 goto out;
3361 }
3362
3363 /* collecting data currently setup */
3364 memset(current_addr, '\0', sizeof(current_addr));
3365 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3366 /* changes are allowed on AF_INET* families only */
3367 if ((ret != AF_INET) && (ret != AF_INET6)) {
3368 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3369 goto out;
3370 }
3371
3372 /* applying ADDR changes if required and allowed
3373 * ipcmp returns 0 when both ADDR are the same
3374 */
Amaury Denoyelle21e611d2022-12-01 17:46:45 +01003375 if (ipcmp(&s->addr, &sa, 0) == 0) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003376 chunk_appendf(msg, "no need to change the addr");
3377 goto port;
3378 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003379 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003380 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003381
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003382 /* update report for caller */
3383 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3384 }
3385
3386 port:
3387 if (port) {
3388 char sign = '\0';
3389 char *endptr;
3390
3391 if (addr)
3392 chunk_appendf(msg, ", ");
3393
3394 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003395 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003396
3397 /* check if PORT change is required */
3398 port_change_required = 0;
3399
3400 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003401 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003402 new_port = strtol(port, &endptr, 10);
3403 if ((errno != 0) || (port == endptr)) {
3404 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3405 goto out;
3406 }
3407
3408 /* check if caller triggers a port mapped or offset */
3409 if (sign == '-' || (sign == '+')) {
3410 /* check if server currently uses port map */
3411 if (!(s->flags & SRV_F_MAPPORTS)) {
3412 /* switch from fixed port to port map mandatorily triggers
3413 * a port change */
3414 port_change_required = 1;
3415 /* check is configured
3416 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3417 * prevent PORT change if check doesn't have it's dedicated port while switching
3418 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003419 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003420 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.");
3421 goto out;
3422 }
3423 }
3424 /* we're already using port maps */
3425 else {
3426 port_change_required = current_port != new_port;
3427 }
3428 }
3429 /* fixed port */
3430 else {
3431 port_change_required = current_port != new_port;
3432 }
3433
3434 /* applying PORT changes if required and update response message */
3435 if (port_change_required) {
3436 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003437 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003438 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003439
3440 /* prepare message */
3441 chunk_appendf(msg, "port changed from '");
3442 if (s->flags & SRV_F_MAPPORTS)
3443 chunk_appendf(msg, "+");
3444 chunk_appendf(msg, "%d' to '", current_port);
3445
3446 if (sign == '-') {
3447 s->flags |= SRV_F_MAPPORTS;
3448 chunk_appendf(msg, "%c", sign);
3449 /* just use for result output */
3450 new_port = -new_port;
3451 }
3452 else if (sign == '+') {
3453 s->flags |= SRV_F_MAPPORTS;
3454 chunk_appendf(msg, "%c", sign);
3455 }
3456 else {
3457 s->flags &= ~SRV_F_MAPPORTS;
3458 }
3459
3460 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003461 }
3462 else {
3463 chunk_appendf(msg, "no need to change the port");
3464 }
3465 }
3466
3467out:
William Dauchy6318d332020-05-02 21:52:36 +02003468 if (changed) {
3469 /* force connection cleanup on the given server */
3470 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003471 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003472 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003473 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003474 if (updater)
3475 chunk_appendf(msg, " by '%s'", updater);
3476 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003477 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003478}
3479
Christopher Faulet5efdef22021-03-11 18:03:21 +01003480/*
3481 * update server status based on result of SRV resolution
3482 * returns:
3483 * 0 if server status is updated
3484 * 1 if server status has not changed
3485 *
3486 * Must be called with the server lock held.
3487 */
3488int srvrq_update_srv_status(struct server *s, int has_no_ip)
3489{
3490 if (!s->srvrq)
3491 return 1;
3492
3493 /* since this server has an IP, it can go back in production */
3494 if (has_no_ip == 0) {
3495 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3496 return 1;
3497 }
3498
3499 if (s->next_admin & SRV_ADMF_RMAINT)
3500 return 1;
3501
3502 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3503 return 0;
3504}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003505
3506/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003507 * update server status based on result of name resolution
3508 * returns:
3509 * 0 if server status is updated
3510 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003511 *
3512 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003513 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003514int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003515{
Emeric Brun750fe792020-12-23 16:51:12 +01003516 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003517 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003518 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003519
Jerome Magnin012261a2020-07-28 13:38:22 +02003520 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003521 if (resolution == NULL)
3522 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003523
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003524 switch (resolution->status) {
3525 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003526 /* status when HAProxy has just (re)started.
3527 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003528 break;
3529
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003530 case RSLV_STATUS_VALID:
3531 /*
3532 * resume health checks
3533 * server will be turned back on if health check is safe
3534 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003535 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003536 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003537 return 1;
3538 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3539 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003540 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003541 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003542
Emeric Brun52a91d32017-08-31 14:41:55 +02003543 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003544 return 1;
3545 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3546 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3547 s->proxy->id, s->id);
3548
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003549 ha_warning("%s.\n", trash.area);
3550 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003551 return 0;
3552
3553 case RSLV_STATUS_NX:
3554 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003555 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3556 if (!tick_is_expired(exp, now_ms))
3557 break;
3558
3559 if (s->next_admin & SRV_ADMF_RMAINT)
3560 return 1;
3561 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3562 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003563
3564 case RSLV_STATUS_TIMEOUT:
3565 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003566 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3567 if (!tick_is_expired(exp, now_ms))
3568 break;
3569
3570 if (s->next_admin & SRV_ADMF_RMAINT)
3571 return 1;
3572 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3573 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003574
3575 case RSLV_STATUS_REFUSED:
3576 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003577 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3578 if (!tick_is_expired(exp, now_ms))
3579 break;
3580
3581 if (s->next_admin & SRV_ADMF_RMAINT)
3582 return 1;
3583 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3584 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003585
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003586 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003587 /* stop server if resolution failed for a long enough period */
3588 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3589 if (!tick_is_expired(exp, now_ms))
3590 break;
3591
3592 if (s->next_admin & SRV_ADMF_RMAINT)
3593 return 1;
3594 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3595 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003596 }
3597
3598 return 1;
3599}
3600
3601/*
3602 * Server Name Resolution valid response callback
3603 * It expects:
3604 * - <nameserver>: the name server which answered the valid response
3605 * - <response>: buffer containing a valid DNS response
3606 * - <response_len>: size of <response>
3607 * It performs the following actions:
3608 * - ignore response if current ip found and server family not met
3609 * - update with first new ip found if family is met and current IP is not found
3610 * returns:
3611 * 0 on error
3612 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003613 *
3614 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003615 */
Emeric Brun08622d32020-12-23 17:41:43 +01003616int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003617{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003618 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003619 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003620 void *serverip, *firstip;
3621 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003622 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003623 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003624 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003625
Christopher Faulet67957bd2017-09-27 11:00:59 +02003626 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003627 if (!s)
3628 return 1;
3629
Christopher Faulet49531e82021-03-10 15:07:27 +01003630 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003631 /* If DNS resolution is disabled ignore it.
3632 * This is the case if the server was associated to
3633 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003634 */
Emeric Brun34067662021-06-11 10:48:45 +02003635 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003636 return 1;
3637 }
3638
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003639 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003640 if (!resolution)
3641 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003642
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003643 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003644 firstip = NULL; /* pointer to the first valid response found */
3645 /* it will be used as the new IP if a change is required */
3646 firstip_sin_family = AF_UNSPEC;
3647 serverip = NULL; /* current server IP address */
3648
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003649 /* initializing server IP pointer */
3650 server_sin_family = s->addr.ss_family;
3651 switch (server_sin_family) {
3652 case AF_INET:
3653 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3654 break;
3655
3656 case AF_INET6:
3657 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3658 break;
3659
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003660 case AF_UNSPEC:
3661 break;
3662
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003663 default:
3664 goto invalid;
3665 }
3666
Emeric Brund30e9a12020-12-23 18:49:16 +01003667 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3668 serverip, server_sin_family, &firstip,
3669 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003670
3671 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003672 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003673 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003674
Emeric Brun456de772020-12-23 18:17:31 +01003675 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003676 goto save_ip;
3677
Emeric Brun456de772020-12-23 18:17:31 +01003678 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003679 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003680 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003681
Emeric Brun456de772020-12-23 18:17:31 +01003682 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003683 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003684 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003685 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003686 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003687
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003688 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003689 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003690 goto invalid;
3691
3692 }
3693
3694 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003695 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003696 counters->app.resolver.update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003697 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003698 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003699 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003700 else
3701 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003702 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003703
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003704 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003705 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3706 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708
3709 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003710 if (counters) {
Emeric Brund174f0e2021-10-29 17:30:41 +02003711 counters->app.resolver.invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003712 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003713 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003714 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3715 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003716 return 0;
3717}
3718
3719/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003720 * SRV record error management callback
3721 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003722 * 0 if we can trash answser items.
3723 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003724 *
3725 * Grabs the server's lock.
3726 */
3727int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3728{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003729 struct resolv_srvrq *srvrq;
3730 struct resolv_resolution *res;
3731 struct resolvers *resolvers;
3732 int exp;
3733
3734 /* SRV records */
3735 srvrq = objt_resolv_srvrq(requester->owner);
3736 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003737 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003738
3739 resolvers = srvrq->resolvers;
3740 res = requester->resolution;
3741
3742 switch (res->status) {
3743
3744 case RSLV_STATUS_NX:
3745 /* stop server if resolution is NX for a long enough period */
3746 exp = tick_add(res->last_valid, resolvers->hold.nx);
3747 if (!tick_is_expired(exp, now_ms))
3748 return 1;
3749 break;
3750
3751 case RSLV_STATUS_TIMEOUT:
3752 /* stop server if resolution is TIMEOUT for a long enough period */
3753 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3754 if (!tick_is_expired(exp, now_ms))
3755 return 1;
3756 break;
3757
3758 case RSLV_STATUS_REFUSED:
3759 /* stop server if resolution is REFUSED for a long enough period */
3760 exp = tick_add(res->last_valid, resolvers->hold.refused);
3761 if (!tick_is_expired(exp, now_ms))
3762 return 1;
3763 break;
3764
3765 default:
3766 /* stop server if resolution failed for a long enough period */
3767 exp = tick_add(res->last_valid, resolvers->hold.other);
3768 if (!tick_is_expired(exp, now_ms))
3769 return 1;
3770 }
3771
Emeric Brun34067662021-06-11 10:48:45 +02003772 /* Remove any associated server ref */
Willy Tarreau6878f802021-10-20 14:07:31 +02003773 resolv_detach_from_resolution_answer_items(res, requester);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003774
Emeric Brun12ca6582021-06-10 15:25:25 +02003775 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003776}
3777
3778/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003779 * Server Name Resolution error management callback
3780 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003781 * 0 if we can trash answser items.
3782 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003783 *
3784 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003785 */
Emeric Brun08622d32020-12-23 17:41:43 +01003786int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003788 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003789
Christopher Faulet67957bd2017-09-27 11:00:59 +02003790 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003791 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003792 return 0;
3793
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003794 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003795 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003796 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003797 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Willy Tarreau6878f802021-10-20 14:07:31 +02003798 resolv_detach_from_resolution_answer_items(requester->resolution, requester);
Emeric Brun12ca6582021-06-10 15:25:25 +02003799 return 0;
3800 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003801 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003802
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003803 return 1;
3804}
3805
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003806/*
3807 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003808 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003809 * It returns a pointer to the first server found or NULL if <ip> is not yet
3810 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003811 *
3812 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003813 */
3814struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3815{
3816 struct server *tmpsrv;
3817 struct proxy *be;
3818
3819 if (!srv)
3820 return NULL;
3821
3822 be = srv->proxy;
3823 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003824 /* we found the current server is the same, ignore it */
3825 if (srv == tmpsrv)
3826 continue;
3827
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003828 /* We want to compare the IP in the record with the IP of the servers in the
3829 * same backend, only if:
3830 * * DNS resolution is enabled on the server
3831 * * the hostname used for the resolution by our server is the same than the
3832 * one used for the server found in the backend
3833 * * the server found in the backend is not our current server
3834 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003835 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003836 if ((tmpsrv->hostname_dn == NULL) ||
3837 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003838 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003839 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003840 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003841 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003842 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003843
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003844 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003845 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003846 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003847 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003848 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003849
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003850 /* At this point, we have 2 different servers using the same DNS hostname
3851 * for their respective resolution.
3852 */
3853 if (*ip_family == tmpsrv->addr.ss_family &&
3854 ((tmpsrv->addr.ss_family == AF_INET &&
3855 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3856 (tmpsrv->addr.ss_family == AF_INET6 &&
3857 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003858 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003859 return tmpsrv;
3860 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003861 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003862 }
3863
Emeric Brune9fd6b52017-11-02 17:20:39 +01003864
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003865 return NULL;
3866}
3867
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003868/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3869 * resolver. This is suited for initial address configuration. Returns 0 on
3870 * success otherwise a non-zero error code. In case of error, *err_code, if
3871 * not NULL, is filled up.
3872 */
3873int srv_set_addr_via_libc(struct server *srv, int *err_code)
3874{
3875 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003876 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003877 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003878 return 1;
3879 }
3880 return 0;
3881}
3882
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003883/* Set the server's FDQN (->hostname) from <hostname>.
3884 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003885 *
3886 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003887 */
Emeric Brun08622d32020-12-23 17:41:43 +01003888int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003889{
Emeric Brun08622d32020-12-23 17:41:43 +01003890 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003891 char *hostname_dn;
3892 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003893
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003894 /* Note that the server lock is already held. */
3895 if (!srv->resolvers)
3896 return -1;
3897
Emeric Brun08622d32020-12-23 17:41:43 +01003898 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003899 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003900 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003901 * and we can't enable it at run time for now.
3902 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003903 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003904 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003905
3906 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003907 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003908 hostname_dn = trash.area;
Willy Tarreaubf9498a2021-10-14 07:49:49 +02003909 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len,
Emeric Brund30e9a12020-12-23 18:49:16 +01003910 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003911 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003912 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003913
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003914 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003915 if (resolution &&
3916 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003917 resolution->hostname_dn_len == hostname_dn_len &&
3918 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003919 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003920
Willy Tarreau6878f802021-10-20 14:07:31 +02003921 resolv_unlink_resolution(srv->resolv_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003922
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003923 free(srv->hostname);
3924 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003925 srv->hostname = strdup(hostname);
3926 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003927 srv->hostname_dn_len = hostname_dn_len;
3928 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003929 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003930
Baptiste Assmann13a92322019-06-07 09:40:55 +02003931 if (srv->flags & SRV_F_NO_RESOLUTION)
3932 goto end;
3933
Emeric Brund30e9a12020-12-23 18:49:16 +01003934 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003935 goto err;
3936
3937 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003938 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003939 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003940 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003941
3942 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003943 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003944 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003945 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003946}
3947
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003948/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3949 * from the state file. This is suited for initial address configuration.
3950 * Returns 0 on success otherwise a non-zero error code. In case of error,
3951 * *err_code, if not NULL, is filled up.
3952 */
3953static int srv_apply_lastaddr(struct server *srv, int *err_code)
3954{
3955 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3956 if (err_code)
3957 *err_code |= ERR_WARN;
3958 return 1;
3959 }
3960 return 0;
3961}
3962
Willy Tarreau25e51522016-11-04 15:10:17 +01003963/* returns 0 if no error, otherwise a combination of ERR_* flags */
3964static int srv_iterate_initaddr(struct server *srv)
3965{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003966 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003967 int return_code = 0;
3968 int err_code;
3969 unsigned int methods;
3970
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003971 /* If no addr and no hostname set, get the name from the DNS SRV request */
3972 if (!name && srv->srvrq)
3973 name = srv->srvrq->name;
3974
Willy Tarreau25e51522016-11-04 15:10:17 +01003975 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003976 if (!methods) {
3977 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003978 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3979 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003980 if (srv->resolvers_id) {
3981 /* dns resolution is configured, add "none" to not fail on startup */
3982 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3983 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003984 }
3985
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003986 /* "-dr" : always append "none" so that server addresses resolution
3987 * failures are silently ignored, this is convenient to validate some
3988 * configs out of their environment.
3989 */
3990 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3991 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3992
Willy Tarreau25e51522016-11-04 15:10:17 +01003993 while (methods) {
3994 err_code = 0;
3995 switch (srv_get_next_initaddr(&methods)) {
3996 case SRV_IADDR_LAST:
3997 if (!srv->lastaddr)
3998 continue;
3999 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004000 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004001 return_code |= err_code;
4002 break;
4003
4004 case SRV_IADDR_LIBC:
4005 if (!srv->hostname)
4006 continue;
4007 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004008 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004009 return_code |= err_code;
4010 break;
4011
Willy Tarreau37ebe122016-11-04 15:17:58 +01004012 case SRV_IADDR_NONE:
4013 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004014 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004015 ha_warning("could not resolve address '%s', disabling server.\n",
4016 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004017 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004018 return return_code;
4019
Willy Tarreau4310d362016-11-02 15:05:56 +01004020 case SRV_IADDR_IP:
4021 ipcpy(&srv->init_addr, &srv->addr);
4022 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004023 ha_warning("could not resolve address '%s', falling back to configured address.\n",
4024 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004025 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004026 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004027
Willy Tarreau25e51522016-11-04 15:10:17 +01004028 default: /* unhandled method */
4029 break;
4030 }
4031 }
4032
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004033 if (!return_code)
4034 ha_alert("no method found to resolve address '%s'.\n", name);
4035 else
4036 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004037
4038 return_code |= ERR_ALERT | ERR_FATAL;
4039 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004040out:
4041 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004042 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01004043 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004044}
4045
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004046/*
4047 * This function parses all backends and all servers within each backend
4048 * and performs servers' addr resolution based on information provided by:
4049 * - configuration file
4050 * - server-state file (states provided by an 'old' haproxy process)
4051 *
4052 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4053 */
4054int srv_init_addr(void)
4055{
4056 struct proxy *curproxy;
4057 int return_code = 0;
4058
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004059 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004060 while (curproxy) {
4061 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004062
4063 /* servers are in backend only */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004064 if (!(curproxy->cap & PR_CAP_BE) || (curproxy->flags & (PR_FL_DISABLED|PR_FL_STOPPED)))
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004065 goto srv_init_addr_next;
4066
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004067 for (srv = curproxy->srv; srv; srv = srv->next) {
4068 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004069 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004070 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02004071 reset_usermsgs_ctx();
4072 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004073
4074 srv_init_addr_next:
4075 curproxy = curproxy->next;
4076 }
4077
4078 return return_code;
4079}
4080
Willy Tarreau46b7f532018-08-21 11:54:26 +02004081/*
4082 * Must be called with the server lock held.
4083 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01004084const 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 +02004085{
4086
Willy Tarreau83061a82018-07-13 11:56:34 +02004087 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004088
4089 msg = get_trash_chunk();
4090 chunk_reset(msg);
4091
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004092 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004093 chunk_appendf(msg, "no need to change the FDQN");
4094 goto out;
4095 }
4096
4097 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4098 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4099 goto out;
4100 }
4101
4102 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4103 server->proxy->id, server->id, server->hostname, fqdn);
4104
Emeric Brun08622d32020-12-23 17:41:43 +01004105 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004106 chunk_reset(msg);
4107 chunk_appendf(msg, "could not update %s/%s FQDN",
4108 server->proxy->id, server->id);
4109 goto out;
4110 }
4111
4112 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004113 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004114
4115 out:
4116 if (updater)
4117 chunk_appendf(msg, " by '%s'", updater);
4118 chunk_appendf(msg, "\n");
4119
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004120 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004121}
4122
4123
Willy Tarreau21b069d2016-11-23 17:15:08 +01004124/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4125 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004126 * 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 +01004127 * used for CLI commands requiring a server name.
4128 * Important: the <arg> is modified to remove the '/'.
4129 */
4130struct server *cli_find_server(struct appctx *appctx, char *arg)
4131{
4132 struct proxy *px;
4133 struct server *sv;
4134 char *line;
4135
4136 /* split "backend/server" and make <line> point to server */
4137 for (line = arg; *line; line++)
4138 if (*line == '/') {
4139 *line++ = '\0';
4140 break;
4141 }
4142
4143 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004144 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004145 return NULL;
4146 }
4147
4148 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004149 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004150 return NULL;
4151 }
4152
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02004153 if (px->flags & (PR_FL_DISABLED|PR_FL_STOPPED)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004154 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004155 return NULL;
4156 }
4157
4158 return sv;
4159}
4160
William Lallemand222baf22016-11-19 02:00:33 +01004161
Willy Tarreau46b7f532018-08-21 11:54:26 +02004162/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004163static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004164{
4165 struct server *sv;
4166 const char *warning;
4167
4168 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4169 return 1;
4170
4171 sv = cli_find_server(appctx, args[2]);
4172 if (!sv)
4173 return 1;
4174
4175 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004176 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004177 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004178 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004179 if (warning)
4180 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004181 }
4182 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004183 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004184 if (strcmp(args[4], "ready") == 0)
4185 srv_adm_set_ready(sv);
4186 else if (strcmp(args[4], "drain") == 0)
4187 srv_adm_set_drain(sv);
4188 else if (strcmp(args[4], "maint") == 0)
4189 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004190 else
4191 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004192 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004193 }
4194 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004195 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004196 if (sv->track)
4197 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004198 else if (strcmp(args[4], "up") == 0) {
4199 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004200 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004201 }
4202 else if (strcmp(args[4], "stopping") == 0) {
4203 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004204 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004205 }
4206 else if (strcmp(args[4], "down") == 0) {
4207 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004208 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004209 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004210 else
4211 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004212 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004213 }
4214 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004215 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004216 if (!(sv->agent.state & CHK_ST_ENABLED))
4217 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004218 else if (strcmp(args[4], "up") == 0) {
4219 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004220 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004221 }
4222 else if (strcmp(args[4], "down") == 0) {
4223 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004224 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004225 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004226 else
4227 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004228 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004229 }
Misiek2da082d2017-01-09 09:40:42 +01004230 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004231 char *addr = NULL;
4232 char *port = NULL;
4233 if (strlen(args[4]) == 0) {
4234 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4235 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004236 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004237 }
4238 addr = args[4];
4239 if (strcmp(args[5], "port") == 0)
4240 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004241 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004242 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004243 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004244 if (warning)
4245 cli_msg(appctx, LOG_WARNING, warning);
4246 }
4247 else if (strcmp(args[3], "agent-port") == 0) {
4248 char *port = NULL;
4249 if (strlen(args[4]) == 0) {
4250 cli_err(appctx, "set server <b>/<s> agent-port requires"
4251 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004252 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004253 }
4254 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004255 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004256 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004257 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004258 if (warning)
4259 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004260 }
4261 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004262 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004263 if (!(sv->agent.state & CHK_ST_ENABLED))
4264 cli_err(appctx, "agent checks are not enabled on this server.\n");
4265 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004266 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004267 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004268 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004269 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004270 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004271 else if (strcmp(args[3], "check-addr") == 0) {
4272 char *addr = NULL;
4273 char *port = NULL;
4274 if (strlen(args[4]) == 0) {
4275 cli_err(appctx, "set server <b>/<s> check-addr requires"
4276 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004277 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004278 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004279 addr = args[4];
4280 if (strcmp(args[5], "port") == 0)
4281 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004282 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004283 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004284 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004285 if (warning)
4286 cli_msg(appctx, LOG_WARNING, warning);
4287 }
4288 else if (strcmp(args[3], "check-port") == 0) {
4289 char *port = NULL;
4290 if (strlen(args[4]) == 0) {
4291 cli_err(appctx, "set server <b>/<s> check-port requires"
4292 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004293 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004294 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004295 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004296 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004297 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004298 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004299 if (warning)
4300 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004301 }
4302 else if (strcmp(args[3], "addr") == 0) {
4303 char *addr = NULL;
4304 char *port = NULL;
4305 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004306 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004307 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004308 }
4309 else {
4310 addr = args[4];
4311 }
4312 if (strcmp(args[5], "port") == 0) {
4313 port = args[6];
4314 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004315 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004316 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004317 if (warning)
4318 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004319 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004320 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004321 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004322 else if (strcmp(args[3], "fqdn") == 0) {
4323 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004324 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004325 goto out;
4326 }
4327 if (!sv->resolvers) {
4328 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4329 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004330 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004331 if (sv->srvrq) {
4332 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004333 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004334 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004335 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4336 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004337 /* ensure runtime resolver will process this new fqdn */
4338 if (sv->flags & SRV_F_NO_RESOLUTION) {
4339 sv->flags &= ~SRV_F_NO_RESOLUTION;
4340 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004341 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004342 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004343 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004344 if (warning)
4345 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004346 }
William Dauchyf6370442020-11-14 19:25:33 +01004347 else if (strcmp(args[3], "ssl") == 0) {
4348#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004349 if (sv->flags & SRV_F_DYNAMIC) {
4350 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4351 goto out;
4352 }
4353
William Dauchyf6370442020-11-14 19:25:33 +01004354 if (sv->ssl_ctx.ctx == NULL) {
4355 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4356 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004357 goto out;
4358 }
4359
4360 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4361 if (strcmp(args[4], "on") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004362 srv_set_ssl(sv, 1);
William Dauchyf6370442020-11-14 19:25:33 +01004363 } else if (strcmp(args[4], "off") == 0) {
Willy Tarreaua8a72c62021-10-06 11:48:34 +02004364 srv_set_ssl(sv, 0);
William Dauchyf6370442020-11-14 19:25:33 +01004365 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004366 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004367 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004368 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004369 }
4370 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004371 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004372 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4373#else
4374 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4375#endif
4376 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004377 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004378 "usage: set server <backend>/<server> "
4379 "addr | agent | agent-addr | agent-port | agent-send | "
4380 "check-addr | check-port | fqdn | health | ssl | "
4381 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004382 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004383 out:
William Lallemand222baf22016-11-19 02:00:33 +01004384 return 1;
4385}
4386
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004387static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004388{
William Lallemand6b160942016-11-22 12:34:35 +01004389 struct proxy *px;
4390 struct server *sv;
4391 char *line;
4392
4393
4394 /* split "backend/server" and make <line> point to server */
4395 for (line = args[2]; *line; line++)
4396 if (*line == '/') {
4397 *line++ = '\0';
4398 break;
4399 }
4400
Willy Tarreau9d008692019-08-09 11:21:01 +02004401 if (!*line)
4402 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004403
Willy Tarreau9d008692019-08-09 11:21:01 +02004404 if (!get_backend_server(args[2], line, &px, &sv))
4405 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004406
4407 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004408 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4409 sv->iweight);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02004410 if (applet_putstr(appctx, trash.area) == -1)
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004411 return 0;
William Lallemand6b160942016-11-22 12:34:35 +01004412 return 1;
4413}
4414
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004415/* Parse a "set weight" command.
4416 *
4417 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004418 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004419static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004420{
4421 struct server *sv;
4422 const char *warning;
4423
4424 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4425 return 1;
4426
4427 sv = cli_find_server(appctx, args[2]);
4428 if (!sv)
4429 return 1;
4430
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004431 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4432
William Lallemand6b160942016-11-22 12:34:35 +01004433 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004434 if (warning)
4435 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004436
4437 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4438
William Lallemand6b160942016-11-22 12:34:35 +01004439 return 1;
4440}
4441
Willy Tarreau46b7f532018-08-21 11:54:26 +02004442/* parse a "set maxconn server" command. It always returns 1.
4443 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004444 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004445 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004446static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004447{
4448 struct server *sv;
4449 const char *warning;
4450
4451 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4452 return 1;
4453
4454 sv = cli_find_server(appctx, args[3]);
4455 if (!sv)
4456 return 1;
4457
Amaury Denoyelle02742862021-06-18 11:11:36 +02004458 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4459
Willy Tarreaub8026272016-11-23 11:26:56 +01004460 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004461 if (warning)
4462 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004463
Amaury Denoyelle02742862021-06-18 11:11:36 +02004464 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4465
Willy Tarreaub8026272016-11-23 11:26:56 +01004466 return 1;
4467}
William Lallemand6b160942016-11-22 12:34:35 +01004468
Willy Tarreau46b7f532018-08-21 11:54:26 +02004469/* parse a "disable agent" command. It always returns 1.
4470 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004471 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004472 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004473static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004474{
4475 struct server *sv;
4476
4477 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4478 return 1;
4479
4480 sv = cli_find_server(appctx, args[2]);
4481 if (!sv)
4482 return 1;
4483
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004484 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004485 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004486 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004487 return 1;
4488}
4489
Willy Tarreau46b7f532018-08-21 11:54:26 +02004490/* parse a "disable health" command. It always returns 1.
4491 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004492 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004493 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004494static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004495{
4496 struct server *sv;
4497
4498 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4499 return 1;
4500
4501 sv = cli_find_server(appctx, args[2]);
4502 if (!sv)
4503 return 1;
4504
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004505 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004506 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004507 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004508 return 1;
4509}
4510
Willy Tarreau46b7f532018-08-21 11:54:26 +02004511/* parse a "disable server" command. It always returns 1.
4512 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004513 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004514 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004515static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004516{
4517 struct server *sv;
4518
4519 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4520 return 1;
4521
4522 sv = cli_find_server(appctx, args[2]);
4523 if (!sv)
4524 return 1;
4525
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004526 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004527 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004528 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004529 return 1;
4530}
4531
Willy Tarreau46b7f532018-08-21 11:54:26 +02004532/* parse a "enable agent" command. It always returns 1.
4533 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004534 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004535 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004536static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004537{
4538 struct server *sv;
4539
4540 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4541 return 1;
4542
4543 sv = cli_find_server(appctx, args[2]);
4544 if (!sv)
4545 return 1;
4546
Willy Tarreau9d008692019-08-09 11:21:01 +02004547 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4548 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004549
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004550 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004551 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004552 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004553 return 1;
4554}
4555
Willy Tarreau46b7f532018-08-21 11:54:26 +02004556/* parse a "enable health" command. It always returns 1.
4557 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004558 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004559 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004560static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004561{
4562 struct server *sv;
4563
4564 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4565 return 1;
4566
4567 sv = cli_find_server(appctx, args[2]);
4568 if (!sv)
4569 return 1;
4570
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004571 if (!(sv->check.state & CHK_ST_CONFIGURED))
4572 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4573
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004574 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004575 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004576 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004577 return 1;
4578}
4579
Willy Tarreau46b7f532018-08-21 11:54:26 +02004580/* parse a "enable server" command. It always returns 1.
4581 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004582 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004583 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004584static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004585{
4586 struct server *sv;
4587
4588 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4589 return 1;
4590
4591 sv = cli_find_server(appctx, args[2]);
4592 if (!sv)
4593 return 1;
4594
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004595 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004596 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004597 if (!(sv->flags & SRV_F_COOKIESET)
4598 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4599 sv->cookie)
4600 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004601 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004602 return 1;
4603}
4604
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004605/* Allocates data structure related to load balancing for the server <sv>. It
4606 * is only required for dynamic servers.
4607 *
4608 * At the moment, the server lock is not used as this function is only called
4609 * for a dynamic server not yet registered.
4610 *
4611 * Returns 1 on success, 0 on allocation failure.
4612 */
4613static int srv_alloc_lb(struct server *sv, struct proxy *be)
4614{
4615 int node;
4616
4617 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4618 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4619 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4620 sv->lb_nodes_now = 0;
4621
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004622 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4623 ((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 +01004624 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4625
4626 if (!sv->lb_nodes)
4627 return 0;
4628
4629 for (node = 0; node < sv->lb_nodes_tot; node++) {
4630 sv->lb_nodes[node].server = sv;
4631 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4632 }
4633 }
4634
4635 return 1;
4636}
4637
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004638/* updates the server's weight during a warmup stage. Once the final weight is
4639 * reached, the task automatically stops. Note that any server status change
4640 * must have updated s->last_change accordingly.
4641 */
4642static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4643{
4644 struct server *s = context;
4645
4646 /* by default, plan on stopping the task */
4647 t->expire = TICK_ETERNITY;
4648 if ((s->next_admin & SRV_ADMF_MAINT) ||
4649 (s->next_state != SRV_ST_STARTING))
4650 return t;
4651
4652 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4653
4654 /* recalculate the weights and update the state */
4655 server_recalc_eweight(s, 1);
4656
4657 /* probably that we can refill this server with a bit more connections */
4658 pendconn_grab_from_px(s);
4659
4660 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4661
4662 /* get back there in 1 second or 1/20th of the slowstart interval,
4663 * whichever is greater, resulting in small 5% steps.
4664 */
4665 if (s->next_state == SRV_ST_STARTING)
4666 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4667 return t;
4668}
4669
4670/* Allocate the slowstart task if the server is configured with a slowstart
4671 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4672 *
4673 * Returns 0 on success else non-zero.
4674 */
4675static int init_srv_slowstart(struct server *srv)
4676{
4677 struct task *t;
4678
4679 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004680 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004681 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4682 return ERR_ALERT | ERR_FATAL;
4683 }
4684
4685 /* We need a warmup task that will be called when the server
4686 * state switches from down to up.
4687 */
4688 srv->warmup = t;
4689 t->process = server_warmup;
4690 t->context = srv;
4691
4692 /* server can be in this state only because of */
4693 if (srv->next_state == SRV_ST_STARTING) {
4694 task_schedule(srv->warmup,
4695 tick_add(now_ms,
4696 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4697 }
4698 }
4699
4700 return ERR_NONE;
4701}
4702REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4703
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004704/* Memory allocation and initialization of the per_thr field.
4705 * Returns 0 if the field has been successfully initialized, -1 on failure.
4706 */
4707int srv_init_per_thr(struct server *srv)
4708{
4709 int i;
4710
4711 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
Willy Tarreauc21a1872022-11-21 14:14:06 +01004712 srv->per_tgrp = calloc(global.nbtgroups, sizeof(*srv->per_tgrp));
4713 if (!srv->per_thr || !srv->per_tgrp)
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004714 return -1;
4715
4716 for (i = 0; i < global.nbthread; i++) {
4717 srv->per_thr[i].idle_conns = EB_ROOT;
4718 srv->per_thr[i].safe_conns = EB_ROOT;
4719 srv->per_thr[i].avail_conns = EB_ROOT;
4720 MT_LIST_INIT(&srv->per_thr[i].streams);
4721 }
4722
4723 return 0;
4724}
4725
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004726/* Parse a "add server" command
4727 * Returns 0 if the server has been successfully initialized, 1 on failure.
4728 */
4729static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4730{
4731 struct proxy *be;
4732 struct server *srv;
4733 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004734 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004735 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004736 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4737
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004738 usermsgs_clr("CLI");
4739
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004740 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4741 return 1;
4742
4743 ++args;
4744
4745 sv_name = be_name = args[1];
4746 /* split backend/server arg */
4747 while (*sv_name && *(++sv_name)) {
4748 if (*sv_name == '/') {
4749 *sv_name = '\0';
4750 ++sv_name;
4751 break;
4752 }
4753 }
4754
4755 if (!*sv_name)
4756 return cli_err(appctx, "Require 'backend/server'.");
4757
Amaury Denoyellecece9182021-04-20 17:09:08 +02004758 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004759 if (!be)
4760 return cli_err(appctx, "No such backend.");
4761
4762 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004763 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004764 return 1;
4765 }
4766
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004767 /* At this point, some operations might not be thread-safe anymore. This
4768 * might be the case for parsing handlers which were designed to run
4769 * only at the starting stage on single-thread mode.
4770 *
4771 * Activate thread isolation to ensure thread-safety.
4772 */
4773 thread_isolate();
4774
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004775 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004776 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4777 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004778 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004779
4780 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004781 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004782
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004783 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004784 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004785 }
4786
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004787 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4788 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004789 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004790
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004791 /* A dynamic server does not currently support resolution.
4792 *
4793 * Initialize it explicitly to the "none" method to ensure no
4794 * resolution will ever be executed.
4795 */
4796 srv->init_addr_methods = SRV_IADDR_NONE;
4797
Amaury Denoyelle30467232021-03-12 18:03:27 +01004798 if (srv->mux_proto) {
4799 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004800 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004801 goto out;
4802 }
4803 }
4804
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004805 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004806 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004807 goto out;
4808 }
4809
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004810 if (srv->max_idle_conns != 0) {
4811 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4812 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004813 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004814 goto out;
4815 }
4816 }
4817
4818 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004819 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004820 goto out;
4821 }
4822
4823 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4824 COUNTERS_SV,
4825 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004826 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004827 goto out;
4828 }
4829
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004830 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4831 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004832 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4833 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4834 goto out;
4835 }
4836 }
4837
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004838 if (srv->trackit) {
4839 if (srv_apply_track(srv, be))
4840 goto out;
4841 }
4842
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004843 /* Init check/agent if configured. The check is manually disabled
4844 * because a dynamic server is started in a disable state. It must be
4845 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004846 */
4847 if (srv->do_check) {
4848 if (init_srv_check(srv))
4849 goto out;
4850
4851 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004852 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004853
4854 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004855 if (init_srv_agent_check(srv))
4856 goto out;
4857
4858 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004859 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004860
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004861 /* Init slowstart if needed. */
4862 if (init_srv_slowstart(srv))
4863 goto out;
4864
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004865 /* Attach the server to the end of the proxy linked list. Note that this
4866 * operation is not thread-safe so this is executed under thread
4867 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004868 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004869 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004870 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004871
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004872 /* TODO use a double-linked list for px->srv */
4873 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004874 struct server *next = be->srv;
4875
4876 while (1) {
4877 /* check for duplicate server */
Tim Duesterhusc5aa1132021-10-16 17:48:15 +02004878 if (strcmp(srv->id, next->id) == 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004879 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004880 goto out;
4881 }
4882
4883 if (!next->next)
4884 break;
4885
4886 next = next->next;
4887 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004888
4889 next->next = srv;
4890 }
4891 else {
4892 srv->next = be->srv;
4893 be->srv = srv;
4894 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004895
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004896 /* generate the server id if not manually specified */
4897 if (!srv->puid) {
4898 next_id = get_next_id(&be->conf.used_server_id, 1);
4899 if (!next_id) {
4900 ha_alert("Cannot attach server : no id left in proxy\n");
4901 goto out;
4902 }
4903
4904 srv->conf.id.key = srv->puid = next_id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004905 }
Christopher Faulet70f89482021-12-07 18:49:44 +01004906 srv->conf.name.key = srv->id;
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004907
4908 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004909 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4910 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02004911 /* addr_node.key could be NULL if FQDN resolution is postponed (ie: add server from cli) */
4912 if (srv->addr_node.key)
4913 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004914
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01004915 /* check if LSB bit (odd bit) is set for reuse_cnt */
4916 if (srv_id_reuse_cnt & 1) {
4917 /* cnt must be increased */
4918 srv_id_reuse_cnt++;
4919 }
4920 /* srv_id_reuse_cnt is always even at this stage, divide by 2 to
4921 * save some space
4922 * (sizeof(srv->rid) is half of sizeof(srv_id_reuse_cnt))
4923 */
4924 srv->rid = (srv_id_reuse_cnt) ? (srv_id_reuse_cnt / 2) : 0;
4925
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01004926 /* adding server cannot fail when we reach this:
4927 * publishing EVENT_HDL_SUB_SERVER_ADD
4928 */
4929 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_ADD, srv, 1);
4930
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004931 thread_release();
4932
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004933 /* Start the check task. The server must be fully initialized.
4934 *
4935 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4936 * need to randomly spread the task interval for dynamic servers.
4937 */
4938 if (srv->check.state & CHK_ST_CONFIGURED) {
4939 if (!start_check_task(&srv->check, 0, 1, 1))
4940 ha_alert("System might be unstable, consider to execute a reload");
4941 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004942 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004943 if (!start_check_task(&srv->agent, 0, 1, 1))
4944 ha_alert("System might be unstable, consider to execute a reload");
4945 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004946
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004947 ha_notice("New server registered.\n");
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004948 cli_umsg(appctx, LOG_INFO);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004949
4950 return 0;
4951
4952out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004953 if (srv) {
4954 if (srv->track)
4955 release_server_track(srv);
4956
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004957 if (srv->check.state & CHK_ST_CONFIGURED)
4958 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004959 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004960 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004961
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004962 /* remove the server from the proxy linked list */
4963 if (be->srv == srv) {
4964 be->srv = srv->next;
4965 }
4966 else {
4967 struct server *prev;
4968 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4969 ;
4970 if (prev)
4971 prev->next = srv->next;
4972 }
4973
4974 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004975
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004976 thread_release();
4977
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004978 if (!usermsgs_empty())
Amaury Denoyelle30fc6da2022-11-10 15:16:49 +01004979 cli_umsgerr(appctx);
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004980
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004981 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004982 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004983
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004984 return 1;
4985}
4986
Amaury Denoyellee5580432021-04-15 14:41:20 +02004987/* Parse a "del server" command
4988 * Returns 0 if the server has been successfully initialized, 1 on failure.
4989 */
4990static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4991{
4992 struct proxy *be;
4993 struct server *srv;
4994 char *be_name, *sv_name;
4995
4996 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4997 return 1;
4998
4999 ++args;
5000
5001 sv_name = be_name = args[1];
5002 /* split backend/server arg */
5003 while (*sv_name && *(++sv_name)) {
5004 if (*sv_name == '/') {
5005 *sv_name = '\0';
5006 ++sv_name;
5007 break;
5008 }
5009 }
5010
5011 if (!*sv_name)
5012 return cli_err(appctx, "Require 'backend/server'.");
5013
5014 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02005015 * requires thread isolation. In addition, any place referencing the
5016 * server about to be deleted would be unsafe after our operation, so
5017 * we must be certain to be alone so that no other thread has even
5018 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02005019 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02005020 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02005021
5022 get_backend_server(be_name, sv_name, &be, &srv);
5023 if (!be) {
5024 cli_err(appctx, "No such backend.");
5025 goto out;
5026 }
5027
5028 if (!srv) {
5029 cli_err(appctx, "No such server.");
5030 goto out;
5031 }
5032
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02005033 if (srv->flags & SRV_F_NON_PURGEABLE) {
5034 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 +02005035 goto out;
5036 }
5037
5038 /* Only servers in maintenance can be deleted. This ensures that the
5039 * server is not present anymore in the lb structures (through
5040 * lbprm.set_server_status_down).
5041 */
5042 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
5043 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
5044 goto out;
5045 }
5046
5047 /* Ensure that there is no active/idle/pending connection on the server.
5048 *
5049 * TODO idle connections should not prevent server deletion. A proper
5050 * cleanup function should be implemented to be used here.
5051 */
5052 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02005053 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02005054 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
5055 goto out;
5056 }
5057
Aurelien DARRAGON129ecf42022-11-17 10:37:58 +01005058 /* removing cannot fail anymore when we reach this:
5059 * publishing EVENT_HDL_SUB_SERVER_DEL
5060 */
5061 srv_event_hdl_publish(EVENT_HDL_SUB_SERVER_DEL, srv, 1);
5062
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02005063 /* remove srv from tracking list */
5064 if (srv->track)
5065 release_server_track(srv);
5066
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02005067 /* stop the check task if running */
5068 if (srv->check.state & CHK_ST_CONFIGURED)
5069 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02005070 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02005071 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005072
5073 /* detach the server from the proxy linked list
5074 * The proxy servers list is currently not protected by a lock, so this
5075 * requires thread_isolate/release.
5076 */
5077
5078 /* be->srv cannot be empty since we have already found the server with
5079 * get_backend_server */
5080 BUG_ON(!be->srv);
5081 if (be->srv == srv) {
5082 be->srv = srv->next;
5083 }
5084 else {
5085 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02005086 for (next = be->srv; srv != next->next; next = next->next) {
5087 /* srv cannot be not found since we have already found
5088 * it with get_backend_server */
5089 BUG_ON(!next);
5090 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02005091
Amaury Denoyellee5580432021-04-15 14:41:20 +02005092 next->next = srv->next;
5093 }
5094
5095 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02005096 eb32_delete(&srv->conf.id);
5097 ebpt_delete(&srv->conf.name);
Aurelien DARRAGON8d0ff282022-09-16 14:27:53 +02005098 if (srv->addr_node.key)
5099 ebpt_delete(&srv->addr_node);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005100
5101 /* remove srv from idle_node tree for idle conn cleanup */
5102 eb32_delete(&srv->idle_node);
5103
Aurelien DARRAGON61e38942022-11-17 16:10:35 +01005104 /* set LSB bit (odd bit) for reuse_cnt */
5105 srv_id_reuse_cnt |= 1;
5106
Amaury Denoyellee5580432021-04-15 14:41:20 +02005107 thread_release();
5108
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02005109 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02005110 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02005111
5112 cli_msg(appctx, LOG_INFO, "Server deleted.");
5113
5114 return 0;
5115
5116out:
5117 thread_release();
5118
5119 return 1;
5120}
5121
William Lallemand222baf22016-11-19 02:00:33 +01005122/* register cli keywords */
5123static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02005124 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
5125 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005126 { { "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 +02005127 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
5128 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02005129 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
5130 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
5131 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
5132 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
5133 { { "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 +01005134 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server", cli_parse_add_server, NULL },
5135 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server", cli_parse_delete_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01005136 {{},}
5137}};
5138
Willy Tarreau0108d902018-11-25 19:14:37 +01005139INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01005140
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005141/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
5142 * field is used to retrieve the identifier of the tracked server, either with
5143 * the format "proxy/server" or just "server". <curproxy> must point to the
5144 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
5145 * to find the tracked server.
5146 *
5147 * Returns 0 if the server track has been activated else non-zero.
5148 *
5149 * Not thread-safe.
5150 */
5151int srv_apply_track(struct server *srv, struct proxy *curproxy)
5152{
5153 struct proxy *px;
5154 struct server *strack, *loop;
5155 char *pname, *sname;
5156
5157 if (!srv->trackit)
5158 return 1;
5159
5160 pname = srv->trackit;
5161 sname = strrchr(pname, '/');
5162
5163 if (sname) {
5164 *sname++ = '\0';
5165 }
5166 else {
5167 sname = pname;
5168 pname = NULL;
5169 }
5170
5171 if (pname) {
5172 px = proxy_be_by_name(pname);
5173 if (!px) {
5174 ha_alert("unable to find required proxy '%s' for tracking.\n",
5175 pname);
5176 return 1;
5177 }
5178 }
5179 else {
5180 px = curproxy;
5181 }
5182
5183 strack = findserver(px, sname);
5184 if (!strack) {
5185 ha_alert("unable to find required server '%s' for tracking.\n",
5186 sname);
5187 return 1;
5188 }
5189
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02005190 if (strack->flags & SRV_F_DYNAMIC) {
5191 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
5192 px->id, strack->id);
5193 return 1;
5194 }
5195
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005196 if (!strack->do_check && !strack->do_agent && !strack->track &&
5197 !strack->trackit) {
5198 ha_alert("unable to use %s/%s for "
5199 "tracking as it does not have any check nor agent enabled.\n",
5200 px->id, strack->id);
5201 return 1;
5202 }
5203
5204 for (loop = strack->track; loop && loop != srv; loop = loop->track)
5205 ;
5206
5207 if (srv == strack || loop) {
5208 ha_alert("unable to track %s/%s as it "
5209 "belongs to a tracking chain looping back to %s/%s.\n",
5210 px->id, strack->id, px->id,
5211 srv == strack ? strack->id : loop->id);
5212 return 1;
5213 }
5214
5215 if (curproxy != px &&
5216 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
5217 ha_alert("unable to use %s/%s for"
5218 "tracking: disable-on-404 option inconsistency.\n",
5219 px->id, strack->id);
5220 return 1;
5221 }
5222
5223 srv->track = strack;
5224 srv->tracknext = strack->trackers;
5225 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02005226 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02005227
5228 ha_free(&srv->trackit);
5229
5230 return 0;
5231}
5232
Emeric Brun64cc49c2017-10-03 14:46:45 +02005233/*
5234 * This function applies server's status changes, it is
5235 * is designed to be called asynchronously.
5236 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005237 * Must be called with the server lock held. This may also be called at init
5238 * time as the result of parsing the state file, in which case no lock will be
5239 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005240 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005241static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005242{
5243 struct check *check = &s->check;
5244 int xferred;
5245 struct proxy *px = s->proxy;
5246 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5247 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5248 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005249 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005250
Emeric Brun64cc49c2017-10-03 14:46:45 +02005251 /* If currently main is not set we try to apply pending state changes */
5252 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5253 int next_admin;
5254
5255 /* Backup next admin */
5256 next_admin = s->next_admin;
5257
5258 /* restore current admin state */
5259 s->next_admin = s->cur_admin;
5260
5261 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5262 s->last_change = now.tv_sec;
5263 if (s->proxy->lbprm.set_server_status_down)
5264 s->proxy->lbprm.set_server_status_down(s);
5265
5266 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5267 srv_shutdown_streams(s, SF_ERR_DOWN);
5268
5269 /* we might have streams queued on this server and waiting for
5270 * a connection. Those which are redispatchable will be queued
5271 * to another server or to the proxy itself.
5272 */
5273 xferred = pendconn_redistribute(s);
5274
5275 tmptrash = alloc_trash_chunk();
5276 if (tmptrash) {
5277 chunk_printf(tmptrash,
5278 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5279 s->proxy->id, s->id);
5280
Emeric Brun5a133512017-10-19 14:42:30 +02005281 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005282 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005283
5284 /* we don't send an alert if the server was previously paused */
5285 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005286 send_log(s->proxy, log_level, "%s.\n",
5287 tmptrash->area);
5288 send_email_alert(s, log_level, "%s",
5289 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005290 free_trash_chunk(tmptrash);
5291 tmptrash = NULL;
5292 }
5293 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5294 set_backend_down(s->proxy);
5295
5296 s->counters.down_trans++;
5297 }
5298 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5299 s->last_change = now.tv_sec;
5300 if (s->proxy->lbprm.set_server_status_down)
5301 s->proxy->lbprm.set_server_status_down(s);
5302
5303 /* we might have streams queued on this server and waiting for
5304 * a connection. Those which are redispatchable will be queued
5305 * to another server or to the proxy itself.
5306 */
5307 xferred = pendconn_redistribute(s);
5308
5309 tmptrash = alloc_trash_chunk();
5310 if (tmptrash) {
5311 chunk_printf(tmptrash,
5312 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5313 s->proxy->id, s->id);
5314
Emeric Brun5a133512017-10-19 14:42:30 +02005315 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005316
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005317 ha_warning("%s.\n", tmptrash->area);
5318 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5319 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005320 free_trash_chunk(tmptrash);
5321 tmptrash = NULL;
5322 }
5323
5324 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5325 set_backend_down(s->proxy);
5326 }
5327 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5328 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5329 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5330 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5331 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5332 s->proxy->last_change = now.tv_sec;
5333 }
5334
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01005335 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02005336 s->down_time += now.tv_sec - s->last_change;
5337
5338 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005339 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005340 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5341
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005342 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005343 /* now propagate the status change to any LB algorithms */
5344 if (px->lbprm.update_server_eweight)
5345 px->lbprm.update_server_eweight(s);
5346 else if (srv_willbe_usable(s)) {
5347 if (px->lbprm.set_server_status_up)
5348 px->lbprm.set_server_status_up(s);
5349 }
5350 else {
5351 if (px->lbprm.set_server_status_down)
5352 px->lbprm.set_server_status_down(s);
5353 }
5354
5355 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5356 * and it's not a backup server and its effective weight is > 0,
5357 * then it can accept new connections, so we shut down all streams
5358 * on all backup servers.
5359 */
5360 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5361 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5362 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5363
5364 /* check if we can handle some connections queued at the proxy. We
5365 * will take as many as we can handle.
5366 */
5367 xferred = pendconn_grab_from_px(s);
5368
5369 tmptrash = alloc_trash_chunk();
5370 if (tmptrash) {
5371 chunk_printf(tmptrash,
5372 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5373 s->proxy->id, s->id);
5374
Emeric Brun5a133512017-10-19 14:42:30 +02005375 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005376 ha_warning("%s.\n", tmptrash->area);
5377 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5378 tmptrash->area);
5379 send_email_alert(s, LOG_NOTICE, "%s",
5380 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005381 free_trash_chunk(tmptrash);
5382 tmptrash = NULL;
5383 }
5384
5385 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5386 set_backend_down(s->proxy);
5387 }
5388 else if (s->cur_eweight != s->next_eweight) {
5389 /* now propagate the status change to any LB algorithms */
5390 if (px->lbprm.update_server_eweight)
5391 px->lbprm.update_server_eweight(s);
5392 else if (srv_willbe_usable(s)) {
5393 if (px->lbprm.set_server_status_up)
5394 px->lbprm.set_server_status_up(s);
5395 }
5396 else {
5397 if (px->lbprm.set_server_status_down)
5398 px->lbprm.set_server_status_down(s);
5399 }
5400
5401 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5402 set_backend_down(s->proxy);
5403 }
5404
5405 s->next_admin = next_admin;
5406 }
5407
Emeric Brun5a133512017-10-19 14:42:30 +02005408 /* reset operational state change */
5409 *s->op_st_chg.reason = 0;
5410 s->op_st_chg.status = s->op_st_chg.code = -1;
5411 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005412
5413 /* Now we try to apply pending admin changes */
5414
5415 /* Maintenance must also disable health checks */
5416 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5417 if (s->check.state & CHK_ST_ENABLED) {
5418 s->check.state |= CHK_ST_PAUSED;
5419 check->health = 0;
5420 }
5421
5422 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005423 tmptrash = alloc_trash_chunk();
5424 if (tmptrash) {
5425 chunk_printf(tmptrash,
5426 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5427 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5428 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005429
Olivier Houchard796a2b32017-10-24 17:42:47 +02005430 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005431
Olivier Houchard796a2b32017-10-24 17:42:47 +02005432 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005433 ha_warning("%s.\n", tmptrash->area);
5434 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5435 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005436 }
5437 free_trash_chunk(tmptrash);
5438 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005439 }
Emeric Brun8f298292017-12-06 16:47:17 +01005440 /* commit new admin status */
5441
5442 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005443 }
5444 else { /* server was still running */
5445 check->health = 0; /* failure */
5446 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005447
5448 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005449 if (s->proxy->lbprm.set_server_status_down)
5450 s->proxy->lbprm.set_server_status_down(s);
5451
Emeric Brun64cc49c2017-10-03 14:46:45 +02005452 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5453 srv_shutdown_streams(s, SF_ERR_DOWN);
5454
William Dauchy6318d332020-05-02 21:52:36 +02005455 /* force connection cleanup on the given server */
5456 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005457 /* we might have streams queued on this server and waiting for
5458 * a connection. Those which are redispatchable will be queued
5459 * to another server or to the proxy itself.
5460 */
5461 xferred = pendconn_redistribute(s);
5462
5463 tmptrash = alloc_trash_chunk();
5464 if (tmptrash) {
5465 chunk_printf(tmptrash,
5466 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5467 s->flags & SRV_F_BACKUP ? "Backup " : "",
5468 s->proxy->id, s->id,
5469 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5470
5471 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5472
5473 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005474 ha_warning("%s.\n", tmptrash->area);
5475 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5476 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005477 }
5478 free_trash_chunk(tmptrash);
5479 tmptrash = NULL;
5480 }
5481 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5482 set_backend_down(s->proxy);
5483
5484 s->counters.down_trans++;
5485 }
5486 }
5487 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5488 /* OK here we're leaving maintenance, we have many things to check,
5489 * because the server might possibly be coming back up depending on
5490 * its state. In practice, leaving maintenance means that we should
5491 * immediately turn to UP (more or less the slowstart) under the
5492 * following conditions :
5493 * - server is neither checked nor tracked
5494 * - server tracks another server which is not checked
5495 * - server tracks another server which is already up
5496 * Which sums up as something simpler :
5497 * "either the tracking server is up or the server's checks are disabled
5498 * or up". Otherwise we only re-enable health checks. There's a special
5499 * case associated to the stopping state which can be inherited. Note
5500 * that the server might still be in drain mode, which is naturally dealt
5501 * with by the lower level functions.
5502 */
5503
5504 if (s->check.state & CHK_ST_ENABLED) {
5505 s->check.state &= ~CHK_ST_PAUSED;
5506 check->health = check->rise; /* start OK but check immediately */
5507 }
5508
5509 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5510 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5511 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5512 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5513 s->next_state = SRV_ST_STOPPING;
5514 }
5515 else {
Willy Tarreaud332f132021-08-04 19:35:13 +02005516 s->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005517 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005518 if (s->slowstart > 0) {
5519 if (s->warmup)
5520 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5521 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005522 else
5523 s->next_state = SRV_ST_RUNNING;
5524 }
5525
5526 }
5527
5528 tmptrash = alloc_trash_chunk();
5529 if (tmptrash) {
5530 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5531 chunk_printf(tmptrash,
5532 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5533 s->flags & SRV_F_BACKUP ? "Backup " : "",
5534 s->proxy->id, s->id,
5535 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5536 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5537 }
5538 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5539 chunk_printf(tmptrash,
5540 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5541 s->flags & SRV_F_BACKUP ? "Backup " : "",
5542 s->proxy->id, s->id, s->hostname,
5543 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5544 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5545 }
5546 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5547 chunk_printf(tmptrash,
5548 "%sServer %s/%s is %s/%s (leaving maintenance)",
5549 s->flags & SRV_F_BACKUP ? "Backup " : "",
5550 s->proxy->id, s->id,
5551 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5552 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5553 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005554 ha_warning("%s.\n", tmptrash->area);
5555 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5556 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005557 free_trash_chunk(tmptrash);
5558 tmptrash = NULL;
5559 }
5560
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005561 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005562 /* now propagate the status change to any LB algorithms */
5563 if (px->lbprm.update_server_eweight)
5564 px->lbprm.update_server_eweight(s);
5565 else if (srv_willbe_usable(s)) {
5566 if (px->lbprm.set_server_status_up)
5567 px->lbprm.set_server_status_up(s);
5568 }
5569 else {
5570 if (px->lbprm.set_server_status_down)
5571 px->lbprm.set_server_status_down(s);
5572 }
5573
5574 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5575 set_backend_down(s->proxy);
Willy Tarreaud332f132021-08-04 19:35:13 +02005576 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act))
5577 s->proxy->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005578
Willy Tarreau6a78e612018-08-07 10:14:53 +02005579 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5580 * and it's not a backup server and its effective weight is > 0,
5581 * then it can accept new connections, so we shut down all streams
5582 * on all backup servers.
5583 */
5584 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5585 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5586 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5587
5588 /* check if we can handle some connections queued at the proxy. We
5589 * will take as many as we can handle.
5590 */
5591 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005592 }
5593 else if (s->next_admin & SRV_ADMF_MAINT) {
5594 /* remaining in maintenance mode, let's inform precisely about the
5595 * situation.
5596 */
5597 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5598 tmptrash = alloc_trash_chunk();
5599 if (tmptrash) {
5600 chunk_printf(tmptrash,
5601 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5602 s->flags & SRV_F_BACKUP ? "Backup " : "",
5603 s->proxy->id, s->id);
5604
5605 if (s->track) /* normally it's mandatory here */
5606 chunk_appendf(tmptrash, " via %s/%s",
5607 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005608 ha_warning("%s.\n", tmptrash->area);
5609 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5610 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005611 free_trash_chunk(tmptrash);
5612 tmptrash = NULL;
5613 }
5614 }
5615 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5616 tmptrash = alloc_trash_chunk();
5617 if (tmptrash) {
5618 chunk_printf(tmptrash,
5619 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5620 s->flags & SRV_F_BACKUP ? "Backup " : "",
5621 s->proxy->id, s->id, s->hostname);
5622
5623 if (s->track) /* normally it's mandatory here */
5624 chunk_appendf(tmptrash, " via %s/%s",
5625 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005626 ha_warning("%s.\n", tmptrash->area);
5627 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5628 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005629 free_trash_chunk(tmptrash);
5630 tmptrash = NULL;
5631 }
5632 }
5633 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5634 tmptrash = alloc_trash_chunk();
5635 if (tmptrash) {
5636 chunk_printf(tmptrash,
5637 "%sServer %s/%s remains in forced maintenance",
5638 s->flags & SRV_F_BACKUP ? "Backup " : "",
5639 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005640 ha_warning("%s.\n", tmptrash->area);
5641 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5642 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005643 free_trash_chunk(tmptrash);
5644 tmptrash = NULL;
5645 }
5646 }
5647 /* don't report anything when leaving drain mode and remaining in maintenance */
5648
5649 s->cur_admin = s->next_admin;
5650 }
5651
5652 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5653 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5654 /* drain state is applied only if not yet in maint */
5655
5656 s->last_change = now.tv_sec;
5657 if (px->lbprm.set_server_status_down)
5658 px->lbprm.set_server_status_down(s);
5659
5660 /* we might have streams queued on this server and waiting for
5661 * a connection. Those which are redispatchable will be queued
5662 * to another server or to the proxy itself.
5663 */
5664 xferred = pendconn_redistribute(s);
5665
5666 tmptrash = alloc_trash_chunk();
5667 if (tmptrash) {
5668 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5669 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5670 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5671
5672 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5673
5674 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005675 ha_warning("%s.\n", tmptrash->area);
5676 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5677 tmptrash->area);
5678 send_email_alert(s, LOG_NOTICE, "%s",
5679 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005680 }
5681 free_trash_chunk(tmptrash);
5682 tmptrash = NULL;
5683 }
5684
5685 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5686 set_backend_down(s->proxy);
5687 }
5688 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5689 /* OK completely leaving drain mode */
5690 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5691 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5692 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5693 s->proxy->last_change = now.tv_sec;
5694 }
5695
5696 if (s->last_change < now.tv_sec) // ignore negative times
5697 s->down_time += now.tv_sec - s->last_change;
5698 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005699 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005700
5701 tmptrash = alloc_trash_chunk();
5702 if (tmptrash) {
5703 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5704 chunk_printf(tmptrash,
5705 "%sServer %s/%s is %s (leaving forced drain)",
5706 s->flags & SRV_F_BACKUP ? "Backup " : "",
5707 s->proxy->id, s->id,
5708 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5709 }
5710 else {
5711 chunk_printf(tmptrash,
5712 "%sServer %s/%s is %s (leaving drain)",
5713 s->flags & SRV_F_BACKUP ? "Backup " : "",
5714 s->proxy->id, s->id,
5715 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5716 if (s->track) /* normally it's mandatory here */
5717 chunk_appendf(tmptrash, " via %s/%s",
5718 s->track->proxy->id, s->track->id);
5719 }
5720
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005721 ha_warning("%s.\n", tmptrash->area);
5722 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5723 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005724 free_trash_chunk(tmptrash);
5725 tmptrash = NULL;
5726 }
5727
5728 /* now propagate the status change to any LB algorithms */
5729 if (px->lbprm.update_server_eweight)
5730 px->lbprm.update_server_eweight(s);
5731 else if (srv_willbe_usable(s)) {
5732 if (px->lbprm.set_server_status_up)
5733 px->lbprm.set_server_status_up(s);
5734 }
5735 else {
5736 if (px->lbprm.set_server_status_down)
5737 px->lbprm.set_server_status_down(s);
5738 }
5739 }
5740 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5741 /* remaining in drain mode after removing one of its flags */
5742
5743 tmptrash = alloc_trash_chunk();
5744 if (tmptrash) {
5745 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5746 chunk_printf(tmptrash,
5747 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5748 s->flags & SRV_F_BACKUP ? "Backup " : "",
5749 s->proxy->id, s->id);
5750
5751 if (s->track) /* normally it's mandatory here */
5752 chunk_appendf(tmptrash, " via %s/%s",
5753 s->track->proxy->id, s->track->id);
5754 }
5755 else {
5756 chunk_printf(tmptrash,
5757 "%sServer %s/%s remains in forced drain mode",
5758 s->flags & SRV_F_BACKUP ? "Backup " : "",
5759 s->proxy->id, s->id);
5760 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005761 ha_warning("%s.\n", tmptrash->area);
5762 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5763 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005764 free_trash_chunk(tmptrash);
5765 tmptrash = NULL;
5766 }
5767
5768 /* commit new admin status */
5769
5770 s->cur_admin = s->next_admin;
5771 }
5772 }
5773
5774 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005775 *s->adm_st_chg_cause = 0;
5776}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005777
Willy Tarreau144f84a2021-03-02 16:09:26 +01005778struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005779{
5780 struct connection *conn;
5781
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005782 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005783 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005784 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005785 }
5786
5787 return task;
5788}
5789
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005790/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005791 * moving them all.
5792 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005793 *
5794 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005795 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005796static int srv_migrate_conns_to_remove(struct eb_root *idle_tree, struct mt_list *toremove_list, int toremove_nb)
Olivier Houchardff1d0922020-06-29 20:15:59 +02005797{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005798 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005799 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005800 int i = 0;
5801
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005802 node = eb_first(idle_tree);
5803 while (node) {
5804 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005805 if (toremove_nb != -1 && i >= toremove_nb)
5806 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005807
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005808 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005809 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005810 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005811 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005812
5813 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005814 }
5815 return i;
5816}
William Dauchy6318d332020-05-02 21:52:36 +02005817/* cleanup connections for a given server
5818 * might be useful when going on forced maintenance or live changing ip/port
5819 */
William Dauchy707ad322020-05-04 13:52:40 +02005820static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005821{
William Dauchy6318d332020-05-02 21:52:36 +02005822 int did_remove;
5823 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005824
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005825 /* nothing to do if pool-max-conn is null */
5826 if (!srv->max_idle_conns)
5827 return;
5828
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005829 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005830 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005831 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005832 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005833 if (srv_migrate_conns_to_remove(&srv->per_thr[i].idle_conns, &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02005834 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005835 if (srv_migrate_conns_to_remove(&srv->per_thr[i].safe_conns, &idle_conns[i].toremove_conns, -1) > 0)
Olivier Houchardff1d0922020-06-29 20:15:59 +02005836 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005837 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005838 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005839 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005840
5841 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5842 break;
William Dauchy6318d332020-05-02 21:52:36 +02005843 }
Willy Tarreau260f3242021-10-06 18:30:04 +02005844}
5845
5846/* removes an idle conn after updating the server idle conns counters */
5847void srv_release_conn(struct server *srv, struct connection *conn)
5848{
5849 if (conn->flags & CO_FL_LIST_MASK) {
5850 /* The connection is currently in the server's idle list, so tell it
5851 * there's one less connection available in that list.
5852 */
5853 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5854 _HA_ATOMIC_DEC(conn->flags & CO_FL_SAFE_LIST ? &srv->curr_safe_nb : &srv->curr_idle_nb);
5855 _HA_ATOMIC_DEC(&srv->curr_idle_thr[tid]);
5856 }
5857 else {
5858 /* The connection is not private and not in any server's idle
5859 * list, so decrement the current number of used connections
5860 */
5861 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5862 }
5863
5864 /* Remove the connection from any tree (safe, idle or available) */
5865 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5866 conn_delete_from_tree(&conn->hash_node->node);
5867 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5868}
5869
5870/* retrieve a connection from its <hash> in <tree>
5871 * returns NULL if no connection found
5872 */
5873struct connection *srv_lookup_conn(struct eb_root *tree, uint64_t hash)
5874{
Willy Tarreau85223482022-09-29 20:32:43 +02005875 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005876 struct connection *conn = NULL;
5877 struct conn_hash_node *hash_node = NULL;
5878
Willy Tarreau85223482022-09-29 20:32:43 +02005879 node = eb64_lookup(tree, hash);
Willy Tarreau260f3242021-10-06 18:30:04 +02005880 if (node) {
5881 hash_node = ebmb_entry(node, struct conn_hash_node, node);
5882 conn = hash_node->conn;
5883 }
5884
5885 return conn;
5886}
5887
5888/* retrieve the next connection sharing the same hash as <conn>
5889 * returns NULL if no connection found
5890 */
5891struct connection *srv_lookup_conn_next(struct connection *conn)
5892{
Willy Tarreau85223482022-09-29 20:32:43 +02005893 struct eb64_node *node = NULL;
Willy Tarreau260f3242021-10-06 18:30:04 +02005894 struct connection *next_conn = NULL;
5895 struct conn_hash_node *hash_node = NULL;
5896
Willy Tarreau85223482022-09-29 20:32:43 +02005897 node = eb64_next_dup(&conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005898 if (node) {
Willy Tarreau85223482022-09-29 20:32:43 +02005899 hash_node = eb64_entry(node, struct conn_hash_node, node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005900 next_conn = hash_node->conn;
5901 }
5902
5903 return next_conn;
5904}
5905
5906/* This adds an idle connection to the server's list if the connection is
5907 * reusable, not held by any owner anymore, but still has available streams.
5908 */
5909int srv_add_to_idle_list(struct server *srv, struct connection *conn, int is_safe)
5910{
5911 /* we try to keep the connection in the server's idle list
5912 * if we don't have too many FD in use, and if the number of
5913 * idle+current conns is lower than what was observed before
5914 * last purge, or if we already don't have idle conns for the
5915 * current thread and we don't exceed last count by global.nbthread.
5916 */
5917 if (!(conn->flags & CO_FL_PRIVATE) &&
5918 srv && srv->pool_purge_delay > 0 &&
5919 ((srv->proxy->options & PR_O_REUSE_MASK) != PR_O_REUSE_NEVR) &&
5920 ha_used_fds < global.tune.pool_high_count &&
5921 (srv->max_idle_conns == -1 || srv->max_idle_conns > srv->curr_idle_conns) &&
5922 ((eb_is_empty(&srv->per_thr[tid].safe_conns) &&
5923 (is_safe || eb_is_empty(&srv->per_thr[tid].idle_conns))) ||
5924 (ha_used_fds < global.tune.pool_low_count &&
5925 (srv->curr_used_conns + srv->curr_idle_conns <=
5926 MAX(srv->curr_used_conns, srv->est_need_conns) + srv->low_idle_conns))) &&
5927 !conn->mux->used_streams(conn) && conn->mux->avail_streams(conn)) {
5928 int retadd;
5929
5930 retadd = _HA_ATOMIC_ADD_FETCH(&srv->curr_idle_conns, 1);
5931 if (retadd > srv->max_idle_conns) {
5932 _HA_ATOMIC_DEC(&srv->curr_idle_conns);
5933 return 0;
5934 }
5935 _HA_ATOMIC_DEC(&srv->curr_used_conns);
5936
5937 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5938 conn_delete_from_tree(&conn->hash_node->node);
5939
5940 if (is_safe) {
5941 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_SAFE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005942 eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005943 _HA_ATOMIC_INC(&srv->curr_safe_nb);
5944 } else {
5945 conn->flags = (conn->flags & ~CO_FL_LIST_MASK) | CO_FL_IDLE_LIST;
Willy Tarreau85223482022-09-29 20:32:43 +02005946 eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node);
Willy Tarreau260f3242021-10-06 18:30:04 +02005947 _HA_ATOMIC_INC(&srv->curr_idle_nb);
5948 }
5949 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock);
5950 _HA_ATOMIC_INC(&srv->curr_idle_thr[tid]);
5951
5952 __ha_barrier_full();
5953 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5954 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5955 if ((volatile void *)srv->idle_node.node.leaf_p == NULL) {
5956 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5957 now_ms);
5958 eb32_insert(&idle_conn_srv, &srv->idle_node);
5959 if (!task_in_wq(idle_conn_task) && !
5960 task_in_rq(idle_conn_task)) {
5961 task_schedule(idle_conn_task,
5962 srv->idle_node.key);
5963 }
5964
5965 }
5966 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5967 }
5968 return 1;
5969 }
5970 return 0;
William Dauchy6318d332020-05-02 21:52:36 +02005971}
5972
Willy Tarreau144f84a2021-03-02 16:09:26 +01005973struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005974{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005975 struct server *srv;
5976 struct eb32_node *eb;
5977 int i;
5978 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005979
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005980 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005981 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5982 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005983 int exceed_conns;
5984 int to_kill;
5985 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005986
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005987 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5988 if (!eb) {
5989 /* we might have reached the end of the tree, typically because
5990 * <now_ms> is in the first half and we're first scanning the last
5991 * half. Let's loop back to the beginning of the tree now.
5992 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005993
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005994 eb = eb32_first(&idle_conn_srv);
5995 if (likely(!eb))
5996 break;
5997 }
5998 if (tick_is_lt(now_ms, eb->key)) {
5999 /* timer not expired yet, revisit it later */
6000 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006001 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006002 }
6003 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006004
6005 /* Calculate how many idle connections we want to kill :
6006 * we want to remove half the difference between the total
6007 * of established connections (used or idle) and the max
6008 * number of used connections.
6009 */
6010 curr_idle = srv->curr_idle_conns;
6011 if (curr_idle == 0)
6012 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006013 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006014 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006015
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006016 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02006017 if (srv->est_need_conns < srv->max_used_conns)
6018 srv->est_need_conns = srv->max_used_conns;
6019
Willy Tarreau9dc231a2022-11-21 14:32:33 +01006020 HA_ATOMIC_STORE(&srv->max_used_conns, srv->curr_used_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006021
Willy Tarreau18ed7892020-07-02 19:05:30 +02006022 if (exceed_conns <= 0)
6023 goto remove;
6024
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006025 /* check all threads starting with ours */
6026 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006027 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006028 int j;
6029 int did_remove = 0;
6030
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006031 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
6032 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02006033
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006034 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006035 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 +02006036 if (j > 0)
6037 did_remove = 1;
6038 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01006039 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 +01006040 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01006041 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02006042
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006043 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02006044 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02006045
6046 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
6047 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006048 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02006049remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006050 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006051
6052 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006053 /* There are still more idle connections, add the
6054 * server back in the tree.
6055 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006056 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006057 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006058 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006059 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006060 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01006061 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
6062
Willy Tarreau21b9ff52020-11-05 09:12:20 +01006063 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01006064 return task;
6065}
Olivier Houchard88698d92019-04-16 19:07:22 +02006066
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02006067/* Close remaining idle connections. This functions is designed to be run on
6068 * process shutdown. This guarantees a proper socket shutdown to avoid
6069 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
6070 * bypassed.
6071 *
6072 * This function is not thread-safe so it must only be called via a global
6073 * deinit function.
6074 */
6075static void srv_close_idle_conns(struct server *srv)
6076{
6077 struct eb_root **cleaned_tree;
6078 int i;
6079
6080 for (i = 0; i < global.nbthread; ++i) {
6081 struct eb_root *conn_trees[] = {
6082 &srv->per_thr[i].idle_conns,
6083 &srv->per_thr[i].safe_conns,
6084 &srv->per_thr[i].avail_conns,
6085 NULL
6086 };
6087
6088 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
6089 while (!eb_is_empty(*cleaned_tree)) {
6090 struct ebmb_node *node = ebmb_first(*cleaned_tree);
6091 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
6092 struct connection *conn = conn_hash_node->conn;
6093
6094 if (conn->ctrl->ctrl_close)
6095 conn->ctrl->ctrl_close(conn);
6096 ebmb_delete(node);
6097 }
6098 }
6099 }
6100}
6101
6102REGISTER_SERVER_DEINIT(srv_close_idle_conns);
6103
Willy Tarreau76cc6992020-07-01 18:49:24 +02006104/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
6105static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006106 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02006107 char **err)
6108{
6109 if (too_many_args(1, args, err, NULL))
6110 return -1;
6111
6112 if (strcmp(args[1], "on") == 0)
6113 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
6114 else if (strcmp(args[1], "off") == 0)
6115 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
6116 else {
6117 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
6118 return -1;
6119 }
6120 return 0;
6121}
6122
Olivier Houchard88698d92019-04-16 19:07:22 +02006123/* config parser for global "tune.pool-{low,high}-fd-ratio" */
6124static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01006125 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02006126 char **err)
6127{
6128 int arg = -1;
6129
6130 if (too_many_args(1, args, err, NULL))
6131 return -1;
6132
6133 if (*(args[1]) != 0)
6134 arg = atoi(args[1]);
6135
6136 if (arg < 0 || arg > 100) {
6137 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
6138 return -1;
6139 }
6140
6141 if (args[0][10] == 'h')
6142 global.tune.pool_high_ratio = arg;
6143 else
6144 global.tune.pool_low_ratio = arg;
6145 return 0;
6146}
6147
6148/* config keyword parsers */
6149static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02006150 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02006151 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
6152 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
6153 { 0, NULL, NULL }
6154}};
6155
6156INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
6157
Baptiste Assmanna68ca962015-04-14 01:15:08 +02006158/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02006159 * Local variables:
6160 * c-indent-level: 8
6161 * c-basic-offset: 8
6162 * End:
6163 */