blob: d770bdd3b04bc2797b61ae7e612dced1dda8e438 [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 Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020020#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020021#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020022#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020023#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020024#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020025#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020026#include <haproxy/dict-t.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020027#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020028#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020030#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020031#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/port_range.h>
33#include <haproxy/protocol.h>
Willy Tarreaub00a8e32021-05-08 20:18:59 +020034#include <haproxy/proxy.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020035#include <haproxy/queue.h>
Emeric Brunc9437992021-02-12 19:42:55 +010036#include <haproxy/resolvers.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020037#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020038#include <haproxy/server.h>
William Dauchyf6370442020-11-14 19:25:33 +010039#include <haproxy/ssl_sock.h>
Amaury Denoyellef99f77a2021-03-08 17:13:32 +010040#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020041#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020042#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020043#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020044#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020045#include <haproxy/time.h>
Willy Tarreauba6300e2021-05-08 14:09:40 +020046#include <haproxy/tools.h>
Tim Duesterhusd5fc8fc2021-09-11 17:51:13 +020047#include <haproxy/xxhash.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020048
Baptiste Assmannda29fe22019-06-13 13:24:29 +020049
Willy Tarreau3ff577e2018-08-02 11:48:52 +020050static void srv_update_status(struct server *s);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
William Dauchy6318d332020-05-02 21:52:36 +020052static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010054/* extra keywords used as value for other arguments. They are used as
55 * suggestions for mistyped words.
Willy Tarreau49c2b452021-03-12 09:58:04 +010056 */
Amaury Denoyelle587b71e2021-03-10 16:36:02 +010057static const char *extra_kw_list[] = {
58 "ipv4", "ipv6", "legacy", "octet-count",
Amaury Denoyelle3b89c112021-03-12 16:04:00 +010059 "fail-check", "sudden-death", "mark-down",
Willy Tarreau49c2b452021-03-12 09:58:04 +010060 NULL /* must be last */
61};
62
Willy Tarreau21faa912012-10-10 08:27:36 +020063/* List head of all known server keywords */
64static struct srv_kw_list srv_keywords = {
65 .list = LIST_HEAD_INIT(srv_keywords.list)
66};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020067
Willy Tarreauaf613e82020-06-05 08:40:51 +020068__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010069struct eb_root idle_conn_srv = EB_ROOT;
Willy Tarreau14015b82021-04-10 17:33:15 +020070struct task *idle_conn_task __read_mostly = NULL;
Willy Tarreau198e92a2021-03-05 10:23:32 +010071struct list servers_list = LIST_HEAD_INIT(servers_list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010072
Frédéric Lécaille7da71292019-05-20 09:47:07 +020073/* The server names dictionary */
Thayne McCombs92149f92020-11-20 01:28:26 -070074struct dict server_key_dict = {
75 .name = "server keys",
Frédéric Lécaille7da71292019-05-20 09:47:07 +020076 .values = EB_ROOT_UNIQUE,
77};
78
Simon Hormana3608442013-11-01 16:46:15 +090079int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020080{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +010081 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020082 return s->down_time;
83
84 return now.tv_sec - s->last_change + s->down_time;
85}
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050087int srv_lastsession(const struct server *s)
88{
89 if (s->counters.last_sess)
90 return now.tv_sec - s->counters.last_sess;
91
92 return -1;
93}
94
Simon Horman4a741432013-02-23 15:35:38 +090095int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020096{
Simon Horman4a741432013-02-23 15:35:38 +090097 const struct server *s = check->server;
98
Willy Tarreauff5ae352013-12-11 20:36:34 +010099 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900100 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100101
Emeric Brun52a91d32017-08-31 14:41:55 +0200102 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900103 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100104
Simon Horman4a741432013-02-23 15:35:38 +0900105 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100106}
107
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100108/*
109 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200110 * Unlikely, but it can happen. The server's proxy must be at least
111 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100112 */
113static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100114{
115 struct proxy *p = s->proxy;
116 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100117
118 for (tmpserv = p->srv; tmpserv != NULL;
119 tmpserv = tmpserv->next) {
120 if (tmpserv == s)
121 continue;
122 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
123 continue;
124 if (tmpserv->cookie &&
125 strcmp(tmpserv->cookie, s->cookie) == 0) {
126 ha_warning("We generated two equal cookies for two different servers.\n"
127 "Please change the secret key for '%s'.\n",
128 s->proxy->id);
129 }
130 }
131
132}
133
Willy Tarreau46b7f532018-08-21 11:54:26 +0200134/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200135 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200136 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100137void srv_set_dyncookie(struct server *s)
138{
139 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100140 char *tmpbuf;
141 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100142 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100143 size_t buffer_len;
144 int addr_len;
145 int port;
146
Willy Tarreau595e7672020-10-20 17:30:08 +0200147 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200148
Olivier Houchard4e694042017-03-14 20:01:29 +0100149 if ((s->flags & SRV_F_COOKIESET) ||
150 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
151 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200152 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100153 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100154
155 if (s->addr.ss_family != AF_INET &&
156 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200157 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100158 /*
159 * Buffer to calculate the cookie value.
160 * The buffer contains the secret key + the server IP address
161 * + the TCP port.
162 */
163 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
164 /*
165 * The TCP port should use only 2 bytes, but is stored in
166 * an unsigned int in struct server, so let's use 4, to be
167 * on the safe side.
168 */
169 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200170 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100171 memcpy(tmpbuf, p->dyncookie_key, key_len);
172 memcpy(&(tmpbuf[key_len]),
173 s->addr.ss_family == AF_INET ?
174 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
175 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
176 addr_len);
177 /*
178 * Make sure it's the same across all the load balancers,
179 * no matter their endianness.
180 */
181 port = htonl(s->svc_port);
182 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
183 hash_value = XXH64(tmpbuf, buffer_len, 0);
184 memprintf(&s->cookie, "%016llx", hash_value);
185 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200186 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100187 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100188
189 /* Don't bother checking if the dyncookie is duplicated if
190 * the server is marked as "disabled", maybe it doesn't have
191 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100192 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100193 if (!(s->next_admin & SRV_ADMF_FMAINT))
194 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200195 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200196 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100197}
198
Willy Tarreau21faa912012-10-10 08:27:36 +0200199/*
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200200 * Must be called with the server lock held. The server is first removed from
201 * the proxy tree if it was already attached. If <reattach> is true, the server
202 * will then be attached in the proxy tree. The proxy lock is held to
203 * manipulate the tree.
Thayne McCombs92149f92020-11-20 01:28:26 -0700204 */
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200205static void srv_set_addr_desc(struct server *s, int reattach)
Thayne McCombs92149f92020-11-20 01:28:26 -0700206{
207 struct proxy *p = s->proxy;
208 char *key;
209
210 key = sa2str(&s->addr, s->svc_port, s->flags & SRV_F_MAPPORTS);
211
212 if (s->addr_node.key) {
Thayne McCombs24da7e12021-01-05 23:10:09 -0700213 if (key && strcmp(key, s->addr_node.key) == 0) {
Thayne McCombs92149f92020-11-20 01:28:26 -0700214 free(key);
215 return;
216 }
217
218 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
219 ebpt_delete(&s->addr_node);
220 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
221
222 free(s->addr_node.key);
223 }
224
225 s->addr_node.key = key;
226
Amaury Denoyelle8ff04342021-06-08 15:19:51 +0200227 if (reattach) {
228 if (s->addr_node.key) {
229 HA_RWLOCK_WRLOCK(PROXY_LOCK, &p->lock);
230 ebis_insert(&p->used_server_addr, &s->addr_node);
231 HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &p->lock);
232 }
Thayne McCombs24da7e12021-01-05 23:10:09 -0700233 }
Thayne McCombs92149f92020-11-20 01:28:26 -0700234}
235
236/*
Willy Tarreau21faa912012-10-10 08:27:36 +0200237 * Registers the server keyword list <kwl> as a list of valid keywords for next
238 * parsing sessions.
239 */
240void srv_register_keywords(struct srv_kw_list *kwl)
241{
Willy Tarreau2b718102021-04-21 07:32:39 +0200242 LIST_APPEND(&srv_keywords.list, &kwl->list);
Willy Tarreau21faa912012-10-10 08:27:36 +0200243}
244
245/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
246 * keyword is found with a NULL ->parse() function, then an attempt is made to
247 * find one with a valid ->parse() function. This way it is possible to declare
248 * platform-dependant, known keywords as NULL, then only declare them as valid
249 * if some options are met. Note that if the requested keyword contains an
250 * opening parenthesis, everything from this point is ignored.
251 */
252struct srv_kw *srv_find_kw(const char *kw)
253{
254 int index;
255 const char *kwend;
256 struct srv_kw_list *kwl;
257 struct srv_kw *ret = NULL;
258
259 kwend = strchr(kw, '(');
260 if (!kwend)
261 kwend = kw + strlen(kw);
262
263 list_for_each_entry(kwl, &srv_keywords.list, list) {
264 for (index = 0; kwl->kw[index].kw != NULL; index++) {
265 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
266 kwl->kw[index].kw[kwend-kw] == 0) {
267 if (kwl->kw[index].parse)
268 return &kwl->kw[index]; /* found it !*/
269 else
270 ret = &kwl->kw[index]; /* may be OK */
271 }
272 }
273 }
274 return ret;
275}
276
277/* Dumps all registered "server" keywords to the <out> string pointer. The
278 * unsupported keywords are only dumped if their supported form was not
279 * found.
280 */
281void srv_dump_kws(char **out)
282{
283 struct srv_kw_list *kwl;
284 int index;
285
Christopher Faulet784063e2020-05-18 12:14:18 +0200286 if (!out)
287 return;
288
Willy Tarreau21faa912012-10-10 08:27:36 +0200289 *out = NULL;
290 list_for_each_entry(kwl, &srv_keywords.list, list) {
291 for (index = 0; kwl->kw[index].kw != NULL; index++) {
292 if (kwl->kw[index].parse ||
293 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
294 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
295 kwl->scope,
296 kwl->kw[index].kw,
297 kwl->kw[index].skip ? " <arg>" : "",
298 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
299 kwl->kw[index].parse ? "" : " (not supported)");
300 }
301 }
302 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100303}
304
305/* Try to find in srv_keyword the word that looks closest to <word> by counting
306 * transitions between letters, digits and other characters. Will return the
307 * best matching word if found, otherwise NULL. An optional array of extra
308 * words to compare may be passed in <extra>, but it must then be terminated
309 * by a NULL entry. If unused it may be NULL.
310 */
311static const char *srv_find_best_kw(const char *word)
312{
313 uint8_t word_sig[1024];
314 uint8_t list_sig[1024];
315 const struct srv_kw_list *kwl;
316 const char *best_ptr = NULL;
317 int dist, best_dist = INT_MAX;
318 const char **extra;
319 int index;
320
321 make_word_fingerprint(word_sig, word);
322 list_for_each_entry(kwl, &srv_keywords.list, list) {
323 for (index = 0; kwl->kw[index].kw != NULL; index++) {
324 make_word_fingerprint(list_sig, kwl->kw[index].kw);
325 dist = word_fingerprint_distance(word_sig, list_sig);
326 if (dist < best_dist) {
327 best_dist = dist;
328 best_ptr = kwl->kw[index].kw;
329 }
330 }
331 }
332
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100333 for (extra = extra_kw_list; *extra; extra++) {
Willy Tarreau49c2b452021-03-12 09:58:04 +0100334 make_word_fingerprint(list_sig, *extra);
335 dist = word_fingerprint_distance(word_sig, list_sig);
336 if (dist < best_dist) {
337 best_dist = dist;
338 best_ptr = *extra;
339 }
Willy Tarreau49c2b452021-03-12 09:58:04 +0100340 }
341
342 if (best_dist > 2 * strlen(word) || (best_ptr && best_dist > 2 * strlen(best_ptr)))
343 best_ptr = NULL;
344
345 return best_ptr;
Willy Tarreau21faa912012-10-10 08:27:36 +0200346}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100347
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100348/* Parse the "backup" server keyword */
349static int srv_parse_backup(char **args, int *cur_arg,
350 struct proxy *curproxy, struct server *newsrv, char **err)
351{
352 newsrv->flags |= SRV_F_BACKUP;
353 return 0;
354}
355
Alexander Liu2a54bb72019-05-22 19:44:48 +0800356
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100357/* Parse the "cookie" server keyword */
358static int srv_parse_cookie(char **args, int *cur_arg,
359 struct proxy *curproxy, struct server *newsrv, char **err)
360{
361 char *arg;
362
363 arg = args[*cur_arg + 1];
364 if (!*arg) {
365 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
366 return ERR_ALERT | ERR_FATAL;
367 }
368
369 free(newsrv->cookie);
370 newsrv->cookie = strdup(arg);
371 newsrv->cklen = strlen(arg);
372 newsrv->flags |= SRV_F_COOKIESET;
373 return 0;
374}
375
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100376/* Parse the "disabled" server keyword */
377static int srv_parse_disabled(char **args, int *cur_arg,
378 struct proxy *curproxy, struct server *newsrv, char **err)
379{
Emeric Brun52a91d32017-08-31 14:41:55 +0200380 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
381 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100382 newsrv->check.state |= CHK_ST_PAUSED;
383 newsrv->check.health = 0;
384 return 0;
385}
386
387/* Parse the "enabled" server keyword */
388static int srv_parse_enabled(char **args, int *cur_arg,
389 struct proxy *curproxy, struct server *newsrv, char **err)
390{
Emeric Brun52a91d32017-08-31 14:41:55 +0200391 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
392 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100393 newsrv->check.state &= ~CHK_ST_PAUSED;
394 newsrv->check.health = newsrv->check.rise;
395 return 0;
396}
397
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100398/* Parse the "error-limit" server keyword */
399static int srv_parse_error_limit(char **args, int *cur_arg,
400 struct proxy *curproxy, struct server *newsrv, char **err)
401{
402 if (!*args[*cur_arg + 1]) {
403 memprintf(err, "'%s' expects an integer argument.",
404 args[*cur_arg]);
405 return ERR_ALERT | ERR_FATAL;
406 }
407
408 newsrv->consecutive_errors_limit = atoi(args[*cur_arg + 1]);
409
410 if (newsrv->consecutive_errors_limit <= 0) {
411 memprintf(err, "%s has to be > 0.",
412 args[*cur_arg]);
413 return ERR_ALERT | ERR_FATAL;
414 }
415
416 return 0;
417}
418
419/* Parse the "init-addr" server keyword */
420static int srv_parse_init_addr(char **args, int *cur_arg,
421 struct proxy *curproxy, struct server *newsrv, char **err)
422{
423 char *p, *end;
424 int done;
425 struct sockaddr_storage sa;
426
427 newsrv->init_addr_methods = 0;
428 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
429
430 for (p = args[*cur_arg + 1]; *p; p = end) {
431 /* cut on next comma */
432 for (end = p; *end && *end != ','; end++);
433 if (*end)
434 *(end++) = 0;
435
436 memset(&sa, 0, sizeof(sa));
437 if (strcmp(p, "libc") == 0) {
438 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
439 }
440 else if (strcmp(p, "last") == 0) {
441 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
442 }
443 else if (strcmp(p, "none") == 0) {
444 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
445 }
446 else if (str2ip2(p, &sa, 0)) {
447 if (is_addr(&newsrv->init_addr)) {
448 memprintf(err, "'%s' : initial address already specified, cannot add '%s'.",
449 args[*cur_arg], p);
450 return ERR_ALERT | ERR_FATAL;
451 }
452 newsrv->init_addr = sa;
453 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
454 }
455 else {
456 memprintf(err, "'%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.",
457 args[*cur_arg], p);
458 return ERR_ALERT | ERR_FATAL;
459 }
460 if (!done) {
461 memprintf(err, "'%s' : too many init-addr methods when trying to add '%s'",
462 args[*cur_arg], p);
463 return ERR_ALERT | ERR_FATAL;
464 }
465 }
466
467 return 0;
468}
469
470/* Parse the "log-proto" server keyword */
471static int srv_parse_log_proto(char **args, int *cur_arg,
472 struct proxy *curproxy, struct server *newsrv, char **err)
473{
474 if (strcmp(args[*cur_arg + 1], "legacy") == 0)
475 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
476 else if (strcmp(args[*cur_arg + 1], "octet-count") == 0)
477 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
478 else {
479 memprintf(err, "'%s' expects one of 'legacy' or 'octet-count' but got '%s'",
480 args[*cur_arg], args[*cur_arg + 1]);
481 return ERR_ALERT | ERR_FATAL;
482 }
483
484 return 0;
485}
486
487/* Parse the "maxconn" server keyword */
488static int srv_parse_maxconn(char **args, int *cur_arg,
489 struct proxy *curproxy, struct server *newsrv, char **err)
490{
491 newsrv->maxconn = atol(args[*cur_arg + 1]);
492 return 0;
493}
494
495/* Parse the "maxqueue" server keyword */
496static int srv_parse_maxqueue(char **args, int *cur_arg,
497 struct proxy *curproxy, struct server *newsrv, char **err)
498{
499 newsrv->maxqueue = atol(args[*cur_arg + 1]);
500 return 0;
501}
502
503/* Parse the "minconn" server keyword */
504static int srv_parse_minconn(char **args, int *cur_arg,
505 struct proxy *curproxy, struct server *newsrv, char **err)
506{
507 newsrv->minconn = atol(args[*cur_arg + 1]);
508 return 0;
509}
510
Willy Tarreau9c538e02019-01-23 10:21:49 +0100511static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
512{
513 char *arg;
514
515 arg = args[*cur_arg + 1];
516 if (!*arg) {
517 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
518 return ERR_ALERT | ERR_FATAL;
519 }
520 newsrv->max_reuse = atoi(arg);
521
522 return 0;
523}
524
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100525static 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 +0100526{
527 const char *res;
528 char *arg;
529 unsigned int time;
530
531 arg = args[*cur_arg + 1];
532 if (!*arg) {
533 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
534 return ERR_ALERT | ERR_FATAL;
535 }
536 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200537 if (res == PARSE_TIME_OVER) {
538 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
539 args[*cur_arg+1], args[*cur_arg]);
540 return ERR_ALERT | ERR_FATAL;
541 }
542 else if (res == PARSE_TIME_UNDER) {
543 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
544 args[*cur_arg+1], args[*cur_arg]);
545 return ERR_ALERT | ERR_FATAL;
546 }
547 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100548 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
549 *res, args[*cur_arg]);
550 return ERR_ALERT | ERR_FATAL;
551 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100552 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100553
554 return 0;
555}
556
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200557static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
558{
559 char *arg;
560
561 arg = args[*cur_arg + 1];
562 if (!*arg) {
563 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
564 return ERR_ALERT | ERR_FATAL;
565 }
566
567 newsrv->low_idle_conns = atoi(arg);
568 return 0;
569}
570
Olivier Houchard006e3102018-12-10 18:30:32 +0100571static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
572{
573 char *arg;
574
575 arg = args[*cur_arg + 1];
576 if (!*arg) {
577 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
578 return ERR_ALERT | ERR_FATAL;
579 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100580
Olivier Houchard006e3102018-12-10 18:30:32 +0100581 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100582 if ((int)newsrv->max_idle_conns < -1) {
583 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
584 return ERR_ALERT | ERR_FATAL;
585 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100586
587 return 0;
588}
589
Willy Tarreaudff55432012-10-10 17:51:05 +0200590/* parse the "id" server keyword */
591static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
592{
593 struct eb32_node *node;
594
595 if (!*args[*cur_arg + 1]) {
596 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
597 return ERR_ALERT | ERR_FATAL;
598 }
599
600 newsrv->puid = atol(args[*cur_arg + 1]);
601 newsrv->conf.id.key = newsrv->puid;
602
603 if (newsrv->puid <= 0) {
604 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
605 return ERR_ALERT | ERR_FATAL;
606 }
607
608 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
609 if (node) {
610 struct server *target = container_of(node, struct server, conf.id);
611 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
612 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
613 target->id);
614 return ERR_ALERT | ERR_FATAL;
615 }
616
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200617 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200618 return 0;
619}
620
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100621/* Parse the "namespace" server keyword */
622static int srv_parse_namespace(char **args, int *cur_arg,
623 struct proxy *curproxy, struct server *newsrv, char **err)
624{
Willy Tarreaue5733232019-05-22 19:24:06 +0200625#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100626 char *arg;
627
628 arg = args[*cur_arg + 1];
629 if (!*arg) {
630 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
631 return ERR_ALERT | ERR_FATAL;
632 }
633
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100634 if (strcmp(arg, "*") == 0) {
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100635 /* Use the namespace associated with the connection (if present). */
636 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
637 return 0;
638 }
639
640 /*
641 * As this parser may be called several times for the same 'default-server'
642 * object, or for a new 'server' instance deriving from a 'default-server'
643 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
644 */
645 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
646
647 newsrv->netns = netns_store_lookup(arg, strlen(arg));
648 if (!newsrv->netns)
649 newsrv->netns = netns_store_insert(arg);
650
651 if (!newsrv->netns) {
652 memprintf(err, "Cannot open namespace '%s'", arg);
653 return ERR_ALERT | ERR_FATAL;
654 }
655
656 return 0;
657#else
658 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
659 return ERR_ALERT | ERR_FATAL;
660#endif
661}
662
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100663/* Parse the "no-backup" server keyword */
664static int srv_parse_no_backup(char **args, int *cur_arg,
665 struct proxy *curproxy, struct server *newsrv, char **err)
666{
667 newsrv->flags &= ~SRV_F_BACKUP;
668 return 0;
669}
670
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100671
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100672/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200673static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100674{
675 srv->pp_opts &= ~flags;
676 return 0;
677}
678
679/* Parse the "no-send-proxy" server keyword */
680static int srv_parse_no_send_proxy(char **args, int *cur_arg,
681 struct proxy *curproxy, struct server *newsrv, char **err)
682{
683 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
684}
685
686/* Parse the "no-send-proxy-v2" server keyword */
687static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
688 struct proxy *curproxy, struct server *newsrv, char **err)
689{
690 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
691}
692
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200693/* Parse the "no-tfo" server keyword */
694static int srv_parse_no_tfo(char **args, int *cur_arg,
695 struct proxy *curproxy, struct server *newsrv, char **err)
696{
697 newsrv->flags &= ~SRV_F_FASTOPEN;
698 return 0;
699}
700
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100701/* Parse the "non-stick" server keyword */
702static int srv_parse_non_stick(char **args, int *cur_arg,
703 struct proxy *curproxy, struct server *newsrv, char **err)
704{
705 newsrv->flags |= SRV_F_NON_STICK;
706 return 0;
707}
708
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100709/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200710static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100711{
712 srv->pp_opts |= flags;
713 return 0;
714}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200715/* parse the "proto" server keyword */
716static int srv_parse_proto(char **args, int *cur_arg,
717 struct proxy *px, struct server *newsrv, char **err)
718{
719 struct ist proto;
720
721 if (!*args[*cur_arg + 1]) {
722 memprintf(err, "'%s' : missing value", args[*cur_arg]);
723 return ERR_ALERT | ERR_FATAL;
724 }
Tim Duesterhusdcf753a2021-03-04 17:31:47 +0100725 proto = ist(args[*cur_arg + 1]);
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200726 newsrv->mux_proto = get_mux_proto(proto);
727 if (!newsrv->mux_proto) {
728 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
729 return ERR_ALERT | ERR_FATAL;
730 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200731 return 0;
732}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100733
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100734/* parse the "proxy-v2-options" */
735static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
736 struct proxy *px, struct server *newsrv, char **err)
737{
738 char *p, *n;
739 for (p = args[*cur_arg+1]; p; p = n) {
740 n = strchr(p, ',');
741 if (n)
742 *n++ = '\0';
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100743 if (strcmp(p, "ssl") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100744 newsrv->pp_opts |= SRV_PP_V2_SSL;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100745 } else if (strcmp(p, "cert-cn") == 0) {
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100746 newsrv->pp_opts |= SRV_PP_V2_SSL;
747 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100748 } else if (strcmp(p, "cert-key") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100749 newsrv->pp_opts |= SRV_PP_V2_SSL;
750 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100751 } else if (strcmp(p, "cert-sig") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100752 newsrv->pp_opts |= SRV_PP_V2_SSL;
753 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100754 } else if (strcmp(p, "ssl-cipher") == 0) {
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100755 newsrv->pp_opts |= SRV_PP_V2_SSL;
756 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100757 } else if (strcmp(p, "authority") == 0) {
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100758 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100759 } else if (strcmp(p, "crc32c") == 0) {
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100760 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100761 } else if (strcmp(p, "unique-id") == 0) {
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100762 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100763 } else
764 goto fail;
765 }
766 return 0;
767 fail:
768 if (err)
769 memprintf(err, "'%s' : proxy v2 option not implemented", p);
770 return ERR_ALERT | ERR_FATAL;
771}
772
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100773/* Parse the "observe" server keyword */
774static int srv_parse_observe(char **args, int *cur_arg,
775 struct proxy *curproxy, struct server *newsrv, char **err)
776{
777 char *arg;
778
779 arg = args[*cur_arg + 1];
780 if (!*arg) {
781 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
782 return ERR_ALERT | ERR_FATAL;
783 }
784
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100785 if (strcmp(arg, "none") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100786 newsrv->observe = HANA_OBS_NONE;
787 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100788 else if (strcmp(arg, "layer4") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100789 newsrv->observe = HANA_OBS_LAYER4;
790 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100791 else if (strcmp(arg, "layer7") == 0) {
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100792 if (curproxy->mode != PR_MODE_HTTP) {
793 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
794 return ERR_ALERT;
795 }
796 newsrv->observe = HANA_OBS_LAYER7;
797 }
798 else {
799 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
800 "but got '%s'\n", args[*cur_arg], arg);
801 return ERR_ALERT | ERR_FATAL;
802 }
803
804 return 0;
805}
806
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100807/* Parse the "on-error" server keyword */
808static int srv_parse_on_error(char **args, int *cur_arg,
809 struct proxy *curproxy, struct server *newsrv, char **err)
810{
811 if (strcmp(args[*cur_arg + 1], "fastinter") == 0)
812 newsrv->onerror = HANA_ONERR_FASTINTER;
813 else if (strcmp(args[*cur_arg + 1], "fail-check") == 0)
814 newsrv->onerror = HANA_ONERR_FAILCHK;
815 else if (strcmp(args[*cur_arg + 1], "sudden-death") == 0)
816 newsrv->onerror = HANA_ONERR_SUDDTH;
817 else if (strcmp(args[*cur_arg + 1], "mark-down") == 0)
818 newsrv->onerror = HANA_ONERR_MARKDWN;
819 else {
820 memprintf(err, "'%s' expects one of 'fastinter', "
821 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'",
822 args[*cur_arg], args[*cur_arg + 1]);
823 return ERR_ALERT | ERR_FATAL;
824 }
825
826 return 0;
827}
828
829/* Parse the "on-marked-down" server keyword */
830static int srv_parse_on_marked_down(char **args, int *cur_arg,
831 struct proxy *curproxy, struct server *newsrv, char **err)
832{
833 if (strcmp(args[*cur_arg + 1], "shutdown-sessions") == 0)
834 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
835 else {
836 memprintf(err, "'%s' expects 'shutdown-sessions' but got '%s'",
837 args[*cur_arg], args[*cur_arg + 1]);
838 return ERR_ALERT | ERR_FATAL;
839 }
840
841 return 0;
842}
843
844/* Parse the "on-marked-up" server keyword */
845static int srv_parse_on_marked_up(char **args, int *cur_arg,
846 struct proxy *curproxy, struct server *newsrv, char **err)
847{
848 if (strcmp(args[*cur_arg + 1], "shutdown-backup-sessions") == 0)
849 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
850 else {
851 memprintf(err, "'%s' expects 'shutdown-backup-sessions' but got '%s'",
852 args[*cur_arg], args[*cur_arg + 1]);
853 return ERR_ALERT | ERR_FATAL;
854 }
855
856 return 0;
857}
858
Frédéric Lécaille16186232017-03-14 16:42:49 +0100859/* Parse the "redir" server keyword */
860static int srv_parse_redir(char **args, int *cur_arg,
861 struct proxy *curproxy, struct server *newsrv, char **err)
862{
863 char *arg;
864
865 arg = args[*cur_arg + 1];
866 if (!*arg) {
867 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
868 return ERR_ALERT | ERR_FATAL;
869 }
870
871 free(newsrv->rdr_pfx);
872 newsrv->rdr_pfx = strdup(arg);
873 newsrv->rdr_len = strlen(arg);
874
875 return 0;
876}
877
Amaury Denoyelle587b71e2021-03-10 16:36:02 +0100878/* Parse the "resolvers" server keyword */
879static int srv_parse_resolvers(char **args, int *cur_arg,
880 struct proxy *curproxy, struct server *newsrv, char **err)
881{
882 free(newsrv->resolvers_id);
883 newsrv->resolvers_id = strdup(args[*cur_arg + 1]);
884 return 0;
885}
886
887/* Parse the "resolve-net" server keyword */
888static int srv_parse_resolve_net(char **args, int *cur_arg,
889 struct proxy *curproxy, struct server *newsrv, char **err)
890{
891 char *p, *e;
892 unsigned char mask;
893 struct resolv_options *opt;
894
895 if (!args[*cur_arg + 1] || args[*cur_arg + 1][0] == '\0') {
896 memprintf(err, "'%s' expects a list of networks.",
897 args[*cur_arg]);
898 return ERR_ALERT | ERR_FATAL;
899 }
900
901 opt = &newsrv->resolv_opts;
902
903 /* Split arguments by comma, and convert it from ipv4 or ipv6
904 * string network in in_addr or in6_addr.
905 */
906 p = args[*cur_arg + 1];
907 e = p;
908 while (*p != '\0') {
909 /* If no room available, return error. */
910 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
911 memprintf(err, "'%s' exceed %d networks.",
912 args[*cur_arg], SRV_MAX_PREF_NET);
913 return ERR_ALERT | ERR_FATAL;
914 }
915 /* look for end or comma. */
916 while (*e != ',' && *e != '\0')
917 e++;
918 if (*e == ',') {
919 *e = '\0';
920 e++;
921 }
922 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
923 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
924 /* Try to convert input string from ipv4 or ipv6 network. */
925 opt->pref_net[opt->pref_net_nb].family = AF_INET;
926 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
927 &mask)) {
928 /* Try to convert input string from ipv6 network. */
929 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
930 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
931 } else {
932 /* All network conversions fail, return error. */
933 memprintf(err, "'%s' invalid network '%s'.",
934 args[*cur_arg], p);
935 return ERR_ALERT | ERR_FATAL;
936 }
937 opt->pref_net_nb++;
938 p = e;
939 }
940
941 return 0;
942}
943
944/* Parse the "resolve-opts" server keyword */
945static int srv_parse_resolve_opts(char **args, int *cur_arg,
946 struct proxy *curproxy, struct server *newsrv, char **err)
947{
948 char *p, *end;
949
950 for (p = args[*cur_arg + 1]; *p; p = end) {
951 /* cut on next comma */
952 for (end = p; *end && *end != ','; end++);
953 if (*end)
954 *(end++) = 0;
955
956 if (strcmp(p, "allow-dup-ip") == 0) {
957 newsrv->resolv_opts.accept_duplicate_ip = 1;
958 }
959 else if (strcmp(p, "ignore-weight") == 0) {
960 newsrv->resolv_opts.ignore_weight = 1;
961 }
962 else if (strcmp(p, "prevent-dup-ip") == 0) {
963 newsrv->resolv_opts.accept_duplicate_ip = 0;
964 }
965 else {
966 memprintf(err, "'%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.",
967 args[*cur_arg], p);
968 return ERR_ALERT | ERR_FATAL;
969 }
970 }
971
972 return 0;
973}
974
975/* Parse the "resolve-prefer" server keyword */
976static int srv_parse_resolve_prefer(char **args, int *cur_arg,
977 struct proxy *curproxy, struct server *newsrv, char **err)
978{
979 if (strcmp(args[*cur_arg + 1], "ipv4") == 0)
980 newsrv->resolv_opts.family_prio = AF_INET;
981 else if (strcmp(args[*cur_arg + 1], "ipv6") == 0)
982 newsrv->resolv_opts.family_prio = AF_INET6;
983 else {
984 memprintf(err, "'%s' expects either ipv4 or ipv6 as argument.",
985 args[*cur_arg]);
986 return ERR_ALERT | ERR_FATAL;
987 }
988
989 return 0;
990}
991
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100992/* Parse the "send-proxy" server keyword */
993static int srv_parse_send_proxy(char **args, int *cur_arg,
994 struct proxy *curproxy, struct server *newsrv, char **err)
995{
996 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
997}
998
999/* Parse the "send-proxy-v2" server keyword */
1000static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
1001 struct proxy *curproxy, struct server *newsrv, char **err)
1002{
1003 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
1004}
1005
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001006/* Parse the "slowstart" server keyword */
1007static int srv_parse_slowstart(char **args, int *cur_arg,
1008 struct proxy *curproxy, struct server *newsrv, char **err)
1009{
1010 /* slowstart is stored in seconds */
1011 unsigned int val;
1012 const char *time_err = parse_time_err(args[*cur_arg + 1], &val, TIME_UNIT_MS);
1013
1014 if (time_err == PARSE_TIME_OVER) {
1015 memprintf(err, "overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).",
1016 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1017 return ERR_ALERT | ERR_FATAL;
1018 }
1019 else if (time_err == PARSE_TIME_UNDER) {
1020 memprintf(err, "underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.",
1021 args[*cur_arg+1], args[*cur_arg], newsrv->id);
1022 return ERR_ALERT | ERR_FATAL;
1023 }
1024 else if (time_err) {
1025 memprintf(err, "unexpected character '%c' in 'slowstart' argument of server %s.",
1026 *time_err, newsrv->id);
1027 return ERR_ALERT | ERR_FATAL;
1028 }
1029 newsrv->slowstart = (val + 999) / 1000;
1030
1031 return 0;
1032}
Frédéric Lécailledba97072017-03-17 15:33:50 +01001033
1034/* Parse the "source" server keyword */
1035static int srv_parse_source(char **args, int *cur_arg,
1036 struct proxy *curproxy, struct server *newsrv, char **err)
1037{
1038 char *errmsg;
1039 int port_low, port_high;
1040 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +01001041
1042 errmsg = NULL;
1043
1044 if (!*args[*cur_arg + 1]) {
1045 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
1046 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
1047 goto err;
1048 }
1049
1050 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001051 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
1052 &errmsg, NULL, NULL,
1053 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 +01001054 if (!sk) {
1055 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1056 goto err;
1057 }
1058
Frédéric Lécailledba97072017-03-17 15:33:50 +01001059 newsrv->conn_src.opts |= CO_SRC_BIND;
1060 newsrv->conn_src.source_addr = *sk;
1061
1062 if (port_low != port_high) {
1063 int i;
1064
Frédéric Lécailledba97072017-03-17 15:33:50 +01001065 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001066 if (!newsrv->conn_src.sport_range) {
1067 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
1068 goto err;
1069 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001070 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
1071 newsrv->conn_src.sport_range->ports[i] = port_low + i;
1072 }
1073
1074 *cur_arg += 2;
1075 while (*(args[*cur_arg])) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001076 if (strcmp(args[*cur_arg], "usesrc") == 0) { /* address to use outside */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001077#if defined(CONFIG_HAP_TRANSPARENT)
1078 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001079 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
1080 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +01001081 goto err;
1082 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001083 if (strcmp(args[*cur_arg + 1], "client") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001084 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1085 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
1086 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001087 else if (strcmp(args[*cur_arg + 1], "clientip") == 0) {
Frédéric Lécailledba97072017-03-17 15:33:50 +01001088 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1089 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
1090 }
1091 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
1092 char *name, *end;
1093
1094 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +01001095 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001096 name++;
1097
1098 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +01001099 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +01001100 end++;
1101
1102 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
1103 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
1104 free(newsrv->conn_src.bind_hdr_name);
1105 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonf1800e62021-05-12 09:44:06 +02001106 if (!newsrv->conn_src.bind_hdr_name) {
1107 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
1108 goto err;
1109 }
Frédéric Lécailledba97072017-03-17 15:33:50 +01001110 newsrv->conn_src.bind_hdr_len = end - name;
1111 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
1112 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
1113 newsrv->conn_src.bind_hdr_occ = -1;
1114
1115 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +01001116 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001117 end++;
1118 if (*end == ',') {
1119 end++;
1120 name = end;
1121 if (*end == '-')
1122 end++;
Willy Tarreau90807112020-02-25 08:16:33 +01001123 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +01001124 end++;
1125 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
1126 }
1127
1128 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001129 ha_alert("usesrc hdr_ip(name,num) does not support negative"
1130 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001131 goto err;
1132 }
1133 }
1134 else {
1135 struct sockaddr_storage *sk;
1136 int port1, port2;
1137
1138 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +02001139 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
1140 &errmsg, NULL, NULL,
1141 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001142 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001143 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001144 goto err;
1145 }
1146
Frédéric Lécailledba97072017-03-17 15:33:50 +01001147 newsrv->conn_src.tproxy_addr = *sk;
1148 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
1149 }
1150 global.last_checks |= LSTCHK_NETADM;
1151 *cur_arg += 2;
1152 continue;
1153#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001154 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 +01001155 goto err;
1156#endif /* defined(CONFIG_HAP_TRANSPARENT) */
1157 } /* "usesrc" */
1158
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01001159 if (strcmp(args[*cur_arg], "interface") == 0) { /* specifically bind to this interface */
Frédéric Lécailledba97072017-03-17 15:33:50 +01001160#ifdef SO_BINDTODEVICE
1161 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001162 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001163 goto err;
1164 }
1165 free(newsrv->conn_src.iface_name);
1166 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
1167 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
1168 global.last_checks |= LSTCHK_NETADM;
1169#else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001170 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +01001171 goto err;
1172#endif
1173 *cur_arg += 2;
1174 continue;
1175 }
1176 /* this keyword in not an option of "source" */
1177 break;
1178 } /* while */
1179
1180 return 0;
1181
1182 err:
1183 free(errmsg);
1184 return ERR_ALERT | ERR_FATAL;
1185}
1186
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001187/* Parse the "stick" server keyword */
1188static int srv_parse_stick(char **args, int *cur_arg,
1189 struct proxy *curproxy, struct server *newsrv, char **err)
1190{
1191 newsrv->flags &= ~SRV_F_NON_STICK;
1192 return 0;
1193}
1194
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001195/* Parse the "track" server keyword */
1196static int srv_parse_track(char **args, int *cur_arg,
1197 struct proxy *curproxy, struct server *newsrv, char **err)
1198{
1199 char *arg;
1200
1201 arg = args[*cur_arg + 1];
1202 if (!*arg) {
1203 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
1204 return ERR_ALERT | ERR_FATAL;
1205 }
1206
1207 free(newsrv->trackit);
1208 newsrv->trackit = strdup(arg);
1209
1210 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001211}
1212
1213/* Parse the "socks4" server keyword */
1214static int srv_parse_socks4(char **args, int *cur_arg,
1215 struct proxy *curproxy, struct server *newsrv, char **err)
1216{
1217 char *errmsg;
1218 int port_low, port_high;
1219 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001220
1221 errmsg = NULL;
1222
1223 if (!*args[*cur_arg + 1]) {
1224 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
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_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +08001232 if (!sk) {
1233 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
1234 goto err;
1235 }
1236
Alexander Liu2a54bb72019-05-22 19:44:48 +08001237 newsrv->flags |= SRV_F_SOCKS4_PROXY;
1238 newsrv->socks4_addr = *sk;
1239
Alexander Liu2a54bb72019-05-22 19:44:48 +08001240 return 0;
1241
1242 err:
1243 free(errmsg);
1244 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001245}
1246
Frédéric Lécailledba97072017-03-17 15:33:50 +01001247
Willy Tarreau034c88c2017-01-23 23:36:45 +01001248/* parse the "tfo" server keyword */
1249static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1250{
1251 newsrv->flags |= SRV_F_FASTOPEN;
1252 return 0;
1253}
1254
Amaury Denoyelle587b71e2021-03-10 16:36:02 +01001255/* parse the "usesrc" server keyword */
1256static int srv_parse_usesrc(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1257{
1258 memprintf(err, "'%s' only allowed after a '%s' statement.",
1259 "usesrc", "source");
1260 return ERR_ALERT | ERR_FATAL;
1261}
1262
1263/* parse the "weight" server keyword */
1264static int srv_parse_weight(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
1265{
1266 int w;
1267
1268 w = atol(args[*cur_arg + 1]);
1269 if (w < 0 || w > SRV_UWGHT_MAX) {
1270 memprintf(err, "weight of server %s is not within 0 and %d (%d).",
1271 newsrv->id, SRV_UWGHT_MAX, w);
1272 return ERR_ALERT | ERR_FATAL;
1273 }
1274 newsrv->uweight = newsrv->iweight = w;
1275
1276 return 0;
1277}
1278
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001279/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +02001280 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001281 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001282 *
1283 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001284 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001285void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001286{
Willy Tarreau751153e2021-02-17 13:33:24 +01001287 struct stream *stream;
1288 struct mt_list *elt1, elt2;
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001289 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001290
Willy Tarreaud4e78d82021-03-04 10:47:54 +01001291 for (thr = 0; thr < global.nbthread; thr++)
1292 mt_list_for_each_entry_safe(stream, &srv->per_thr[thr].streams, by_srv, elt1, elt2)
1293 if (stream->srv_conn == srv)
1294 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001295}
1296
1297/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +02001298 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001299 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001300 *
1301 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001302 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001303void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001304{
1305 struct server *srv;
1306
1307 for (srv = px->srv; srv != NULL; srv = srv->next)
1308 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +02001309 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +02001310}
1311
Willy Tarreaubda92272014-05-20 21:55:30 +02001312/* Appends some information to a message string related to a server going UP or
1313 * DOWN. If both <forced> and <reason> are null and the server tracks another
1314 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +02001315 * If <check> is non-null, an entire string describing the check result will be
1316 * appended after a comma and a space (eg: to report some information from the
1317 * check that changed the state). In the other case, the string will be built
1318 * using the check results stored into the struct server if present.
1319 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +02001320 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001321 *
1322 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001323 */
Willy Tarreau83061a82018-07-13 11:56:34 +02001324void srv_append_status(struct buffer *msg, struct server *s,
1325 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001326{
Emeric Brun5a133512017-10-19 14:42:30 +02001327 short status = s->op_st_chg.status;
1328 short code = s->op_st_chg.code;
1329 long duration = s->op_st_chg.duration;
1330 char *desc = s->op_st_chg.reason;
1331
1332 if (check) {
1333 status = check->status;
1334 code = check->code;
1335 duration = check->duration;
1336 desc = check->desc;
1337 }
1338
1339 if (status != -1) {
1340 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
1341
1342 if (status >= HCHK_STATUS_L57DATA)
1343 chunk_appendf(msg, ", code: %d", code);
1344
1345 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001346 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +02001347
1348 chunk_appendf(msg, ", info: \"");
1349
1350 chunk_initlen(&src, desc, 0, strlen(desc));
1351 chunk_asciiencode(msg, &src, '"');
1352
1353 chunk_appendf(msg, "\"");
1354 }
1355
1356 if (duration >= 0)
1357 chunk_appendf(msg, ", check duration: %ldms", duration);
1358 }
1359 else if (desc && *desc) {
1360 chunk_appendf(msg, ", %s", desc);
1361 }
1362 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +02001363 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +02001364 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001365
1366 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001367 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001368 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
1369 " %d sessions active, %d requeued, %d remaining in queue",
1370 s->proxy->srv_act, s->proxy->srv_bck,
1371 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001372 s->cur_sess, xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001373 else
1374 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
1375 " %d sessions requeued, %d total in queue",
1376 s->proxy->srv_act, s->proxy->srv_bck,
1377 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
Willy Tarreaua0570452021-06-18 09:30:30 +02001378 xferred, s->queue.length);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001379 }
1380}
1381
Emeric Brun5a133512017-10-19 14:42:30 +02001382/* Marks server <s> down, regardless of its checks' statuses. The server is
1383 * registered in a list to postpone the counting of the remaining servers on
1384 * the proxy and transfers queued streams whenever possible to other servers at
1385 * a sync point. Maintenance servers are ignored. It stores the <reason> if
1386 * non-null as the reason for going down or the available data from the check
1387 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001388 *
1389 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001390 */
Emeric Brun5a133512017-10-19 14:42:30 +02001391void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001392{
1393 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001394
Emeric Brun64cc49c2017-10-03 14:46:45 +02001395 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001396 return;
1397
Emeric Brun52a91d32017-08-31 14:41:55 +02001398 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001399 *s->op_st_chg.reason = 0;
1400 s->op_st_chg.status = -1;
1401 if (reason) {
1402 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1403 }
1404 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001405 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001406 s->op_st_chg.code = check->code;
1407 s->op_st_chg.status = check->status;
1408 s->op_st_chg.duration = check->duration;
1409 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001410
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001411 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001412 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001413
Emeric Brun9f0b4582017-10-23 14:39:51 +02001414 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001415 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001416 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001417 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001418 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001419}
1420
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001421/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001422 * in maintenance. The server is registered in a list to postpone the counting
1423 * of the remaining servers on the proxy and tries to grab requests from the
1424 * proxy at a sync point. Maintenance servers are ignored. It stores the
1425 * <reason> if non-null as the reason for going down or the available data
1426 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001427 *
1428 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001429 */
Emeric Brun5a133512017-10-19 14:42:30 +02001430void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001431{
1432 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001433
Emeric Brun64cc49c2017-10-03 14:46:45 +02001434 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001435 return;
1436
Emeric Brun52a91d32017-08-31 14:41:55 +02001437 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001438 return;
1439
Emeric Brun52a91d32017-08-31 14:41:55 +02001440 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001441 *s->op_st_chg.reason = 0;
1442 s->op_st_chg.status = -1;
1443 if (reason) {
1444 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1445 }
1446 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001447 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001448 s->op_st_chg.code = check->code;
1449 s->op_st_chg.status = check->status;
1450 s->op_st_chg.duration = check->duration;
1451 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001452
Emeric Brun64cc49c2017-10-03 14:46:45 +02001453 if (s->slowstart <= 0)
1454 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001455
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001456 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001457 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001458
Emeric Brun9f0b4582017-10-23 14:39:51 +02001459 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001460 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001461 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001462 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001463 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001464}
1465
Willy Tarreau8eb77842014-05-21 13:54:57 +02001466/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001467 * isn't in maintenance. The server is registered in a list to postpone the
1468 * counting of the remaining servers on the proxy and tries to grab requests
1469 * from the proxy. Maintenance servers are ignored. It stores the
1470 * <reason> if non-null as the reason for going down or the available data
1471 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001472 * up. Note that it makes use of the trash to build the log strings, so <reason>
1473 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001474 *
1475 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001476 */
Emeric Brun5a133512017-10-19 14:42:30 +02001477void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001478{
1479 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001480
Emeric Brun64cc49c2017-10-03 14:46:45 +02001481 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001482 return;
1483
Emeric Brun52a91d32017-08-31 14:41:55 +02001484 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001485 return;
1486
Emeric Brun52a91d32017-08-31 14:41:55 +02001487 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001488 *s->op_st_chg.reason = 0;
1489 s->op_st_chg.status = -1;
1490 if (reason) {
1491 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1492 }
1493 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001494 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001495 s->op_st_chg.code = check->code;
1496 s->op_st_chg.status = check->status;
1497 s->op_st_chg.duration = check->duration;
1498 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001499
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001500 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001501 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001502
Emeric Brun9f0b4582017-10-23 14:39:51 +02001503 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001504 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001505 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001506 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001507 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001508}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001509
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001510/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1511 * enforce either maint mode or drain mode. It is not allowed to set more than
1512 * one flag at once. The equivalent "inherited" flag is propagated to all
1513 * tracking servers. Maintenance mode disables health checks (but not agent
1514 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001515 * is done. If <cause> is non-null, it will be displayed at the end of the log
1516 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001517 *
1518 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001519 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001520void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001521{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001522 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001523
1524 if (!mode)
1525 return;
1526
1527 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001528 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001529 return;
1530
Emeric Brun52a91d32017-08-31 14:41:55 +02001531 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001532 if (cause)
1533 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1534
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001535 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001536 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001537
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001538 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001539 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1540 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001541 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001542
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001543 /* compute the inherited flag to propagate */
1544 if (mode & SRV_ADMF_MAINT)
1545 mode = SRV_ADMF_IMAINT;
1546 else if (mode & SRV_ADMF_DRAIN)
1547 mode = SRV_ADMF_IDRAIN;
1548
Emeric Brun9f0b4582017-10-23 14:39:51 +02001549 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001550 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001551 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001552 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001553 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001554}
1555
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001556/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1557 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1558 * than one flag at once. The equivalent "inherited" flag is propagated to all
1559 * tracking servers. Leaving maintenance mode re-enables health checks. When
1560 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001561 *
1562 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001563 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001564void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001565{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001566 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001567
1568 if (!mode)
1569 return;
1570
1571 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001572 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001573 return;
1574
Emeric Brun52a91d32017-08-31 14:41:55 +02001575 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001576
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001577 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001578 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001579
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001580 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001581 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1582 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001583 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001584
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001585 if (mode & SRV_ADMF_MAINT)
1586 mode = SRV_ADMF_IMAINT;
1587 else if (mode & SRV_ADMF_DRAIN)
1588 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001589
Emeric Brun9f0b4582017-10-23 14:39:51 +02001590 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001591 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001592 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001593 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001594 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001595}
1596
Willy Tarreau757478e2016-11-03 19:22:19 +01001597/* principle: propagate maint and drain to tracking servers. This is useful
1598 * upon startup so that inherited states are correct.
1599 */
1600static void srv_propagate_admin_state(struct server *srv)
1601{
1602 struct server *srv2;
1603
1604 if (!srv->trackers)
1605 return;
1606
1607 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001608 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001609 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001610 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001611
Emeric Brun52a91d32017-08-31 14:41:55 +02001612 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001613 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001614 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001615 }
1616}
1617
1618/* Compute and propagate the admin states for all servers in proxy <px>.
1619 * Only servers *not* tracking another one are considered, because other
1620 * ones will be handled when the server they track is visited.
1621 */
1622void srv_compute_all_admin_states(struct proxy *px)
1623{
1624 struct server *srv;
1625
1626 for (srv = px->srv; srv; srv = srv->next) {
1627 if (srv->track)
1628 continue;
1629 srv_propagate_admin_state(srv);
1630 }
1631}
1632
Willy Tarreaudff55432012-10-10 17:51:05 +02001633/* Note: must not be declared <const> as its list will be overwritten.
1634 * Please take care of keeping this list alphabetically sorted, doing so helps
1635 * all code contributors.
1636 * Optional keywords are also declared with a NULL ->parse() function so that
1637 * the config parser can report an appropriate error when a known keyword was
1638 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001639 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001640 */
1641static struct srv_kw_list srv_kws = { "ALL", { }, {
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001642 { "backup", srv_parse_backup, 0, 1, 1 }, /* Flag as backup server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001643 { "cookie", srv_parse_cookie, 1, 1, 0 }, /* Assign a cookie to the server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001644 { "disabled", srv_parse_disabled, 0, 1, 1 }, /* Start the server in 'disabled' state */
1645 { "enabled", srv_parse_enabled, 0, 1, 1 }, /* Start the server in 'enabled' state */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001646 { "error-limit", srv_parse_error_limit, 1, 1, 1 }, /* Configure the consecutive count of check failures to consider a server on error */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001647 { "id", srv_parse_id, 1, 0, 1 }, /* set id# of server */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001648 { "init-addr", srv_parse_init_addr, 1, 1, 0 }, /* */
1649 { "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 +01001650 { "maxconn", srv_parse_maxconn, 1, 1, 1 }, /* Set the max number of concurrent connection */
1651 { "maxqueue", srv_parse_maxqueue, 1, 1, 1 }, /* Set the max number of connection to put in queue */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001652 { "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 +01001653 { "minconn", srv_parse_minconn, 1, 1, 1 }, /* Enable a dynamic maxconn limit */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001654 { "namespace", srv_parse_namespace, 1, 1, 0 }, /* Namespace the server socket belongs to (if supported) */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001655 { "no-backup", srv_parse_no_backup, 0, 1, 1 }, /* Flag as non-backup server */
1656 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1, 1 }, /* Disable use of PROXY V1 protocol */
1657 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1, 1 }, /* Disable use of PROXY V2 protocol */
1658 { "no-tfo", srv_parse_no_tfo, 0, 1, 1 }, /* Disable use of TCP Fast Open */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001659 { "non-stick", srv_parse_non_stick, 0, 1, 0 }, /* Disable stick-table persistence */
Amaury Denoyelle725f8d22021-09-20 15:16:12 +02001660 { "observe", srv_parse_observe, 1, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
1661 { "on-error", srv_parse_on_error, 1, 1, 1 }, /* Configure the action on check failure */
1662 { "on-marked-down", srv_parse_on_marked_down, 1, 1, 1 }, /* Configure the action when a server is marked down */
1663 { "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 +01001664 { "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 */
1665 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1, 1 }, /* Set the max number of orphan idle connections, -1 means unlimited */
1666 { "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 +01001667 { "proto", srv_parse_proto, 1, 1, 1 }, /* Set the proto to use for all outgoing connections */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001668 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1, 1 }, /* options for send-proxy-v2 */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001669 { "redir", srv_parse_redir, 1, 1, 0 }, /* Enable redirection mode */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001670 { "resolve-net", srv_parse_resolve_net, 1, 1, 0 }, /* Set the preferred network range for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001671 { "resolve-opts", srv_parse_resolve_opts, 1, 1, 0 }, /* Set options for name resolution */
Ilya Shipitsinba13f162021-03-19 22:21:44 +05001672 { "resolve-prefer", srv_parse_resolve_prefer, 1, 1, 0 }, /* Set the preferred family for name resolution */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001673 { "resolvers", srv_parse_resolvers, 1, 1, 0 }, /* Configure the resolver to use for name resolution */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001674 { "send-proxy", srv_parse_send_proxy, 0, 1, 1 }, /* Enforce use of PROXY V1 protocol */
1675 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1, 1 }, /* Enforce use of PROXY V2 protocol */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001676 { "slowstart", srv_parse_slowstart, 1, 1, 0 }, /* Set the warm-up timer for a previously failed server */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001677 { "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 +01001678 { "stick", srv_parse_stick, 0, 1, 0 }, /* Enable stick-table persistence */
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001679 { "tfo", srv_parse_tfo, 0, 1, 1 }, /* enable TCP Fast Open of server */
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02001680 { "track", srv_parse_track, 1, 1, 1 }, /* Set the current state of the server, tracking another one */
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01001681 { "socks4", srv_parse_socks4, 1, 1, 0 }, /* Set the socks4 proxy of the server*/
Amaury Denoyellefc465a52021-03-09 17:36:23 +01001682 { "usesrc", srv_parse_usesrc, 0, 1, 1 }, /* safe-guard against usesrc without preceding <source> keyword */
1683 { "weight", srv_parse_weight, 1, 1, 1 }, /* Set the load-balancing weight */
Willy Tarreaudff55432012-10-10 17:51:05 +02001684 { NULL, NULL, 0 },
1685}};
1686
Willy Tarreau0108d902018-11-25 19:14:37 +01001687INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001688
Willy Tarreau004e0452013-11-21 11:22:01 +01001689/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001690 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001691 * state is automatically disabled if the time is elapsed. If <must_update> is
1692 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001693 *
1694 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001695 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001696void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001697{
1698 struct proxy *px = sv->proxy;
1699 unsigned w;
1700
1701 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1702 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 if (sv->next_state == SRV_ST_STARTING)
1704 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001705 }
1706
1707 /* We must take care of not pushing the server to full throttle during slow starts.
1708 * It must also start immediately, at least at the minimal step when leaving maintenance.
1709 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001710 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001711 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1712 else
1713 w = px->lbprm.wdiv;
1714
Emeric Brun52a91d32017-08-31 14:41:55 +02001715 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001716
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001717 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001718 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001719 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001720}
1721
Willy Tarreaubaaee002006-06-26 02:48:02 +02001722/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001723 * Parses weight_str and configures sv accordingly.
1724 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001725 *
1726 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001727 */
1728const char *server_parse_weight_change_request(struct server *sv,
1729 const char *weight_str)
1730{
1731 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001732 long int w;
1733 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001734
1735 px = sv->proxy;
1736
1737 /* if the weight is terminated with '%', it is set relative to
1738 * the initial weight, otherwise it is absolute.
1739 */
1740 if (!*weight_str)
1741 return "Require <weight> or <weight%>.\n";
1742
Simon Hormanb796afa2013-02-12 10:45:53 +09001743 w = strtol(weight_str, &end, 10);
1744 if (end == weight_str)
1745 return "Empty weight string empty or preceded by garbage";
1746 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001747 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001748 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001749 /* Avoid integer overflow */
1750 if (w > 25600)
1751 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001752 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001753 if (w > 256)
1754 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001755 }
1756 else if (w < 0 || w > 256)
1757 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001758 else if (end[0] != '\0')
1759 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001760
1761 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1762 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1763
1764 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001765 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001766
1767 return NULL;
1768}
1769
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001770/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001771 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1772 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001773 * Returns:
1774 * - error string on error
1775 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001776 *
1777 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001778 */
1779const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001780 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001781{
1782 unsigned char ip[INET6_ADDRSTRLEN];
1783
1784 if (inet_pton(AF_INET6, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001785 srv_update_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001786 return NULL;
1787 }
1788 if (inet_pton(AF_INET, addr_str, ip)) {
Christopher Faulet69beaa92021-02-16 12:07:47 +01001789 srv_update_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001790 return NULL;
1791 }
1792
1793 return "Could not understand IP address format.\n";
1794}
1795
Willy Tarreau46b7f532018-08-21 11:54:26 +02001796/*
1797 * Must be called with the server lock held.
1798 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001799const char *server_parse_maxconn_change_request(struct server *sv,
1800 const char *maxconn_str)
1801{
1802 long int v;
1803 char *end;
1804
1805 if (!*maxconn_str)
1806 return "Require <maxconn>.\n";
1807
1808 v = strtol(maxconn_str, &end, 10);
1809 if (end == maxconn_str)
1810 return "maxconn string empty or preceded by garbage";
1811 else if (end[0] != '\0')
1812 return "Trailing garbage in maxconn string";
1813
1814 if (sv->maxconn == sv->minconn) { // static maxconn
1815 sv->maxconn = sv->minconn = v;
1816 } else { // dynamic maxconn
1817 sv->maxconn = v;
1818 }
1819
1820 if (may_dequeue_tasks(sv, sv->proxy))
Willy Tarreau9ab78292021-06-22 18:47:51 +02001821 process_srv_queue(sv);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001822
1823 return NULL;
1824}
1825
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001826#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001827static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1828 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001829{
1830 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001831 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001832 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001833 NULL,
1834 };
1835
1836 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001837 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001838
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001839 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001840}
1841
1842static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1843{
1844 struct sample_expr *expr;
1845
1846 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 +01001847 if (!expr) {
1848 memprintf(err, "error detected while parsing sni expression : %s", *err);
1849 return ERR_ALERT | ERR_FATAL;
1850 }
1851
1852 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1853 memprintf(err, "error detected while parsing sni expression : "
1854 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02001855 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001856 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001857 return ERR_ALERT | ERR_FATAL;
1858 }
1859
1860 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1861 release_sample_expr(newsrv->ssl_ctx.sni);
1862 newsrv->ssl_ctx.sni = expr;
1863
1864 return 0;
1865}
1866#endif
1867
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001868static 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 +01001869{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001870 char *msg = "error encountered while processing ";
1871 char *quote = "'";
1872 char *token = args[cur_arg];
1873
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001874 if (err && *err) {
1875 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001876 msg = *err;
1877 quote = "";
1878 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001879 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001880
1881 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02001882 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001883 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02001884 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001885}
1886
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001887static void srv_conn_src_sport_range_cpy(struct server *srv,
1888 struct server *src)
1889{
1890 int range_sz;
1891
1892 range_sz = src->conn_src.sport_range->size;
1893 if (range_sz > 0) {
1894 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1895 if (srv->conn_src.sport_range != NULL) {
1896 int i;
1897
1898 for (i = 0; i < range_sz; i++) {
1899 srv->conn_src.sport_range->ports[i] =
1900 src->conn_src.sport_range->ports[i];
1901 }
1902 }
1903 }
1904}
1905
1906/*
1907 * Copy <src> server connection source settings to <srv> server everything needed.
1908 */
1909static void srv_conn_src_cpy(struct server *srv, struct server *src)
1910{
1911 srv->conn_src.opts = src->conn_src.opts;
1912 srv->conn_src.source_addr = src->conn_src.source_addr;
1913
1914 /* Source port range copy. */
1915 if (src->conn_src.sport_range != NULL)
1916 srv_conn_src_sport_range_cpy(srv, src);
1917
1918#ifdef CONFIG_HAP_TRANSPARENT
1919 if (src->conn_src.bind_hdr_name != NULL) {
1920 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1921 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1922 }
1923 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1924 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1925#endif
1926 if (src->conn_src.iface_name != NULL)
1927 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1928}
1929
1930/*
1931 * Copy <src> server SSL settings to <srv> server allocating
1932 * everything needed.
1933 */
1934#if defined(USE_OPENSSL)
1935static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1936{
1937 if (src->ssl_ctx.ca_file != NULL)
1938 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1939 if (src->ssl_ctx.crl_file != NULL)
1940 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001941
1942 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1943
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02001944 srv->ssl_ctx.ctx = src->ssl_ctx.ctx;
1945
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001946 if (src->ssl_ctx.verify_host != NULL)
1947 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1948 if (src->ssl_ctx.ciphers != NULL)
1949 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001950 if (src->ssl_ctx.options)
1951 srv->ssl_ctx.options = src->ssl_ctx.options;
1952 if (src->ssl_ctx.methods.flags)
1953 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1954 if (src->ssl_ctx.methods.min)
1955 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1956 if (src->ssl_ctx.methods.max)
1957 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1958
Ilya Shipitsin2aa4b3a2021-01-07 11:55:45 +05001959#ifdef HAVE_SSL_CTX_SET_CIPHERSUITES
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001960 if (src->ssl_ctx.ciphersuites != NULL)
1961 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1962#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001963 if (src->sni_expr != NULL)
1964 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001965
1966#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1967 if (src->ssl_ctx.alpn_str) {
1968 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1969 if (srv->ssl_ctx.alpn_str) {
1970 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1971 src->ssl_ctx.alpn_len);
1972 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1973 }
1974 }
1975#endif
1976#ifdef OPENSSL_NPN_NEGOTIATED
1977 if (src->ssl_ctx.npn_str) {
1978 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1979 if (srv->ssl_ctx.npn_str) {
1980 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1981 src->ssl_ctx.npn_len);
1982 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1983 }
1984 }
1985#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001986}
1987#endif
1988
1989/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001990 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001991 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001992 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001993 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01001994int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001995{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 char *hostname_dn;
1997 int hostname_len, hostname_dn_len;
1998
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001999 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002000 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002001
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002003 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01002004 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
2005 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02002006 if (hostname_dn_len == -1)
2007 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002008
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002009
Christopher Faulet67957bd2017-09-27 11:00:59 +02002010 free(srv->hostname);
2011 free(srv->hostname_dn);
2012 srv->hostname = strdup(hostname);
2013 srv->hostname_dn = strdup(hostname_dn);
2014 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002015 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002016 goto err;
2017
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002018 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002019
2020 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002021 ha_free(&srv->hostname);
2022 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002023 return -1;
2024}
2025
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002026/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002027 * Copy <src> server settings to <srv> server allocating
2028 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002029 * This function is not supposed to be called at any time, but only
2030 * during server settings parsing or during server allocations from
2031 * a server template, and just after having calloc()'ed a new server.
2032 * So, <src> may only be a default server (when parsing server settings)
2033 * or a server template (during server allocations from a server template).
2034 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2035 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002036 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002037static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002038{
2039 /* Connection source settings copy */
2040 srv_conn_src_cpy(srv, src);
2041
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002042 if (srv_tmpl) {
2043 srv->addr = src->addr;
2044 srv->svc_port = src->svc_port;
2045 }
2046
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002047 srv->pp_opts = src->pp_opts;
2048 if (src->rdr_pfx != NULL) {
2049 srv->rdr_pfx = strdup(src->rdr_pfx);
2050 srv->rdr_len = src->rdr_len;
2051 }
2052 if (src->cookie != NULL) {
2053 srv->cookie = strdup(src->cookie);
2054 srv->cklen = src->cklen;
2055 }
2056 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002057 srv->check.addr = src->check.addr;
2058 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002059 srv->check.use_ssl = src->check.use_ssl;
2060 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002061 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002062 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002063 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002064 /* Note: 'flags' field has potentially been already initialized. */
2065 srv->flags |= src->flags;
2066 srv->do_check = src->do_check;
2067 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002068 srv->check.inter = src->check.inter;
2069 srv->check.fastinter = src->check.fastinter;
2070 srv->check.downinter = src->check.downinter;
2071 srv->agent.use_ssl = src->agent.use_ssl;
2072 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002073
2074 if (src->agent.tcpcheck_rules) {
2075 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2076 if (srv->agent.tcpcheck_rules) {
2077 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2078 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2079 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2080 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2081 &src->agent.tcpcheck_rules->preset_vars);
2082 }
2083 }
2084
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002085 srv->agent.inter = src->agent.inter;
2086 srv->agent.fastinter = src->agent.fastinter;
2087 srv->agent.downinter = src->agent.downinter;
2088 srv->maxqueue = src->maxqueue;
2089 srv->minconn = src->minconn;
2090 srv->maxconn = src->maxconn;
2091 srv->slowstart = src->slowstart;
2092 srv->observe = src->observe;
2093 srv->onerror = src->onerror;
2094 srv->onmarkeddown = src->onmarkeddown;
2095 srv->onmarkedup = src->onmarkedup;
2096 if (src->trackit != NULL)
2097 srv->trackit = strdup(src->trackit);
2098 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2099 srv->uweight = srv->iweight = src->iweight;
2100
2101 srv->check.send_proxy = src->check.send_proxy;
2102 /* health: up, but will fall down at first failure */
2103 srv->check.rise = srv->check.health = src->check.rise;
2104 srv->check.fall = src->check.fall;
2105
2106 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002107 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2108 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2109 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002110 srv->check.state |= CHK_ST_PAUSED;
2111 srv->check.health = 0;
2112 }
2113
2114 /* health: up but will fall down at first failure */
2115 srv->agent.rise = srv->agent.health = src->agent.rise;
2116 srv->agent.fall = src->agent.fall;
2117
2118 if (src->resolvers_id != NULL)
2119 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002120 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2121 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2122 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2123 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2124 srv->resolv_opts.family_prio = AF_INET6;
2125 memcpy(srv->resolv_opts.pref_net,
2126 src->resolv_opts.pref_net,
2127 sizeof srv->resolv_opts.pref_net);
2128 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002129
2130 srv->init_addr_methods = src->init_addr_methods;
2131 srv->init_addr = src->init_addr;
2132#if defined(USE_OPENSSL)
2133 srv_ssl_settings_cpy(srv, src);
2134#endif
2135#ifdef TCP_USER_TIMEOUT
2136 srv->tcp_ut = src->tcp_ut;
2137#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002138 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002139 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002140 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002141 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002142 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002143
Olivier Houchard8da5f982017-08-04 18:35:36 +02002144 if (srv_tmpl)
2145 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002146
2147 srv->check.via_socks4 = src->check.via_socks4;
2148 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002149}
2150
Willy Tarreau198e92a2021-03-05 10:23:32 +01002151/* allocate a server and attach it to the global servers_list. Returns
2152 * the server on success, otherwise NULL.
2153 */
William Lallemand313bfd12018-10-26 14:47:32 +02002154struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002155{
2156 struct server *srv;
2157
2158 srv = calloc(1, sizeof *srv);
2159 if (!srv)
2160 return NULL;
2161
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002162 srv_take(srv);
2163
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002164 srv->obj_type = OBJ_TYPE_SERVER;
2165 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002166 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002167 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002168 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002169 LIST_INIT(&srv->ip_rec_item);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002170
Emeric Brun52a91d32017-08-31 14:41:55 +02002171 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002172 srv->last_change = now.tv_sec;
2173
Christopher Faulet38290462020-04-21 11:46:40 +02002174 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002175 srv->check.status = HCHK_STATUS_INI;
2176 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002177 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002178 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002179
Christopher Faulet38290462020-04-21 11:46:40 +02002180 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002181 srv->agent.status = HCHK_STATUS_INI;
2182 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002183 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002184 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002185#if defined(USE_QUIC)
2186 srv->cids = EB_ROOT_UNIQUE;
2187#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002188
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002189 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002190#ifdef USE_OPENSSL
2191 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2192#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002193
Willy Tarreau975b1552019-06-06 16:25:55 +02002194 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002195 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002196 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002197 return srv;
2198}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002199
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002200/* Increment the server refcount. */
2201void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002202{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002203 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002204}
2205
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002206/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2207 * dynamic servers, its refcount is decremented first. The free operations are
2208 * conducted only if the refcount is nul, unless the process is stopping.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002209 *
2210 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002211 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002212 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002213struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002214{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002215 struct server *next = NULL;
2216
William Lallemand4c395fc2021-08-20 10:10:15 +02002217 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002218 goto end;
2219
2220 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002221
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002222 /* For dynamic servers, decrement the reference counter. Only free the
2223 * server when reaching zero.
2224 */
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002225 if (likely(!(global.mode & MODE_STOPPING))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002226 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2227 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002228 }
2229
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002230 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002231 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002232
2233 free(srv->id);
2234 free(srv->cookie);
2235 free(srv->hostname);
2236 free(srv->hostname_dn);
2237 free((char*)srv->conf.file);
2238 free(srv->per_thr);
2239 free(srv->curr_idle_thr);
2240 free(srv->resolvers_id);
2241 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002242 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002243
2244 if (srv->use_ssl == 1 || srv->check.use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL)) {
2245 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2246 xprt_get(XPRT_SSL)->destroy_srv(srv);
2247 }
2248 HA_SPIN_DESTROY(&srv->lock);
2249
Willy Tarreau2b718102021-04-21 07:32:39 +02002250 LIST_DELETE(&srv->global_list);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002251
2252 EXTRA_COUNTERS_FREE(srv->extra_counters);
2253
2254 free(srv);
2255 srv = NULL;
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002256
2257 end:
2258 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002259}
2260
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002261/* Remove a server <srv> from a tracking list if <srv> is tracking another
2262 * server. No special care is taken if <srv> is tracked itself by another one :
2263 * this situation should be avoided by the caller.
2264 *
2265 * Not thread-safe.
2266 */
2267static void release_server_track(struct server *srv)
2268{
2269 struct server *strack = srv->track;
2270 struct server **base;
2271
2272 if (!strack)
2273 return;
2274
2275 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2276 if (*base == srv) {
2277 *base = srv->tracknext;
2278 return;
2279 }
2280 }
2281
2282 /* srv not found on the tracking list, this should never happen */
2283 BUG_ON(!*base);
2284}
2285
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002286/*
2287 * Parse as much as possible such a range string argument: low[-high]
2288 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2289 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2290 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2291 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002292 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002293static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2294 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002295{
2296 char *nb_high_arg;
2297
2298 *nb_high = 0;
2299 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002300 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002301
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002302 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002303 *nb_high_arg++ = '\0';
2304 *nb_high = atoi(nb_high_arg);
2305 }
2306 else {
2307 *nb_high += *nb_low;
2308 *nb_low = 1;
2309 }
2310
2311 if (*nb_low < 0 || *nb_high < *nb_low)
2312 return -1;
2313
2314 return 0;
2315}
2316
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002317/* Parse as much as possible such a range string argument: low[-high]
2318 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2319 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2320 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002321 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002322 * initialize a new server on startup.
2323 *
2324 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2325 * Returns 0 if succeeded, -1 if not.
2326 */
2327static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2328 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002329{
2330 chunk_printf(&trash, "%s%d", prefix, nb);
2331 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002332 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002333}
2334
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002335/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002336 * Note that a server template is a special server with
2337 * a few different parameters than a server which has
2338 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002339 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002340 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002341 * initialize a new server on startup.
2342 *
Joseph Herlant44466822018-11-15 08:57:51 -08002343 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002344 * 'srv' template included.
2345 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002346static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002347{
2348 int i;
2349 struct server *newsrv;
2350
2351 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 +02002352 newsrv = new_server(px);
2353 if (!newsrv)
2354 goto err;
2355
Christopher Faulet75bef002020-11-02 22:04:55 +01002356 newsrv->conf.file = strdup(srv->conf.file);
2357 newsrv->conf.line = srv->conf.line;
2358
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002359 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002360 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002361#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2362 if (newsrv->sni_expr) {
2363 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2364 if (!newsrv->ssl_ctx.sni)
2365 goto err;
2366 }
2367#endif
Emeric Brun34067662021-06-11 10:48:45 +02002368 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002369 if (newsrv->srvrq)
2370 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002371
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002372 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002373 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002374
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002375 /* Linked backwards first. This will be restablished after parsing. */
2376 newsrv->next = px->srv;
2377 px->srv = newsrv;
2378 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002379 _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 +02002380
2381 return i - srv->tmpl_info.nb_low;
2382
2383 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002384 _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 +02002385 if (newsrv) {
2386#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2387 release_sample_expr(newsrv->ssl_ctx.sni);
2388#endif
2389 free_check(&newsrv->agent);
2390 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002391 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002392 }
2393 free(newsrv);
2394 return i - srv->tmpl_info.nb_low;
2395}
2396
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002397/* Allocate a new server pointed by <srv> and try to parse the first arguments
2398 * in <args> as an address for a server or an address-range for a template or
2399 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2400 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002401 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002402 * initialize a new server on startup.
2403 *
2404 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2405 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2406 * <srv> will be set to NULL.
2407 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002408static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2409 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002410 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002411{
2412 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002413 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002414 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002415 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002416 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002417 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002418
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002419 *srv = NULL;
2420
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002421 /* There is no mandatory first arguments for default server. */
2422 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2423 if (parse_flags & SRV_PARSE_TEMPLATE) {
2424 if (!*args[3]) {
2425 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002426 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2427 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002428 err_code |= ERR_ALERT | ERR_FATAL;
2429 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002430 }
2431
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002432 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002433 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002434 else {
2435 if (!*args[2]) {
2436 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002437 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2438 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002439 err_code |= ERR_ALERT | ERR_FATAL;
2440 goto out;
2441 }
2442
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002443 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002444 }
2445
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002446 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002447 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2448 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002449 err_code |= ERR_ALERT | ERR_FATAL;
2450 goto out;
2451 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002452 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002453
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002454 *cur_arg = 2;
2455 if (parse_flags & SRV_PARSE_TEMPLATE) {
2456 /* Parse server-template <nb | range> arg. */
2457 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002458 ha_alert("Wrong %s number or range arg '%s'.\n",
2459 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002462 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002463 (*cur_arg)++;
2464 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002465
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002466 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2467 struct sockaddr_storage *sk;
2468 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002469
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002470 *srv = newsrv = new_server(curproxy);
2471 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002472 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002473 err_code |= ERR_ALERT | ERR_ABORT;
2474 goto out;
2475 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002476 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002477
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002478 if (parse_flags & SRV_PARSE_TEMPLATE) {
2479 newsrv->tmpl_info.nb_low = tmpl_range_low;
2480 newsrv->tmpl_info.nb_high = tmpl_range_high;
2481 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002482
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002483 if (parse_flags & SRV_PARSE_DYNAMIC)
2484 newsrv->flags |= SRV_F_DYNAMIC;
2485
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002486 /* Note: for a server template, its id is its prefix.
2487 * This is a temporary id which will be used for server allocations to come
2488 * after parsing.
2489 */
2490 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2491 newsrv->id = strdup(args[1]);
2492 else
2493 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002494
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002495 /* several ways to check the port component :
2496 * - IP => port=+0, relative (IPv4 only)
2497 * - IP: => port=+0, relative
2498 * - IP:N => port=N, absolute
2499 * - IP:+N => port=+N, relative
2500 * - IP:-N => port=-N, relative
2501 */
2502 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2503 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002504
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002505 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002506 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002507 (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);
2508 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002509 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002510 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002511 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002512 goto out;
2513 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002514
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002515 if (!port1 || !port2) {
2516 /* no port specified, +offset, -offset */
2517 newsrv->flags |= SRV_F_MAPPORTS;
2518 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002519
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002520 /* save hostname and create associated name resolution */
2521 if (fqdn) {
2522 if (fqdn[0] == '_') { /* SRV record */
2523 /* Check if a SRV request already exists, and if not, create it */
2524 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2525 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2526 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002527 err_code |= ERR_ALERT | ERR_FATAL;
2528 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002529 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002530 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002531 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002532 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002533 ha_alert("Can't create DNS resolution for server '%s'\n",
2534 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002535 err_code |= ERR_ALERT | ERR_FATAL;
2536 goto out;
2537 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002538 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002539
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002540 newsrv->addr = *sk;
2541 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002542 /*
2543 * we don't need to lock the server here, because
2544 * we are in the process of initializing.
2545 *
2546 * Note that the server is not attached into the proxy tree if
2547 * this is a dynamic server.
2548 */
2549 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002550
2551 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002552 ha_alert("Unknown protocol family %d '%s'\n",
2553 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002554 err_code |= ERR_ALERT | ERR_FATAL;
2555 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002556 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002557
2558 (*cur_arg)++;
2559 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002560 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2561 /* Copy default server settings to new server */
2562 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2563 } else {
2564 /* Initialize dynamic server weight to 1 */
2565 newsrv->uweight = newsrv->iweight = 1;
2566
2567 /* A dynamic server is disabled on startup */
2568 newsrv->next_admin = SRV_ADMF_FMAINT;
2569 newsrv->next_state = SRV_ST_STOPPED;
2570 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002571
2572 /* Set default values for checks */
2573 newsrv->check.inter = DEF_CHKINTR;
2574 newsrv->check.rise = DEF_RISETIME;
2575 newsrv->check.fall = DEF_FALLTIME;
2576
2577 newsrv->agent.inter = DEF_CHKINTR;
2578 newsrv->agent.rise = DEF_AGENT_RISETIME;
2579 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002580 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002581 HA_SPIN_INIT(&newsrv->lock);
2582 }
2583 else {
2584 *srv = newsrv = &curproxy->defsrv;
2585 *cur_arg = 1;
2586 newsrv->resolv_opts.family_prio = AF_INET6;
2587 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002588 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002589
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002590 free(fqdn);
2591 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002592
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002593out:
2594 free(fqdn);
2595 return err_code;
2596}
Willy Tarreau272adea2014-03-31 10:39:59 +02002597
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002598/* Parse the server keyword in <args>.
2599 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2600 * might not be the case if an error is reported.
2601 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002602 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002603 * initialize a new server on startup.
2604 *
2605 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2606 * interrupted.
2607 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002608static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2609 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002610 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002611{
2612 int err_code = 0;
2613 struct srv_kw *kw;
2614 const char *best;
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 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002618 if (!kw) {
2619 best = srv_find_best_kw(args[*cur_arg]);
2620 if (best)
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002621 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?\n",
2622 args[*cur_arg], best);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002623 else
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002624 ha_alert("unknown keyword '%s'.\n", args[*cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002625
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002626 return ERR_ALERT | ERR_FATAL;
2627 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002628
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002629 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002630 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2631 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002632 err_code = ERR_ALERT | ERR_FATAL;
2633 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002634 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002635
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002636 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002637 ha_alert("'%s' option is not accepted in default-server sections\n",
2638 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002639 err_code = ERR_ALERT;
2640 goto out;
2641 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002642 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002643 ha_alert("'%s' option is not accepted for dynamic server\n",
2644 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002645 err_code |= ERR_ALERT;
2646 goto out;
2647 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002648
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002649 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2650 if (err_code) {
2651 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2652 free(errmsg);
2653 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002654
2655out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002656 if (kw->skip != -1)
2657 *cur_arg += 1 + kw->skip;
2658
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002659 return err_code;
2660}
2661
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002662#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002663/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002664 * initialize a new server on startup.
2665 */
2666static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2667 struct server *srv, struct proxy *proxy,
2668 char **errmsg)
2669{
2670 int ret;
2671
2672 if (!srv->sni_expr)
2673 return 0;
2674
2675 ret = server_parse_sni_expr(srv, proxy, errmsg);
2676 if (!ret)
2677 return 0;
2678
2679 return ret;
2680}
2681#endif
2682
2683/* Server initializations finalization.
2684 * Initialize health check, agent check and SNI expression if enabled.
2685 * Must not be called for a default server instance.
2686 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002687 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002688 * initialize a new server on startup.
2689 */
2690static int _srv_parse_finalize(char **args, int cur_arg,
2691 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002692 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002693{
2694#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2695 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002696 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002697#endif
2698
2699 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002700 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002701 return ERR_ALERT | ERR_FATAL;
2702 }
2703
2704 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002705 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2706 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002707 return ERR_ALERT | ERR_FATAL;
2708 }
2709
2710#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002711 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2712 if (errmsg) {
2713 ha_alert("%s\n", errmsg);
2714 free(errmsg);
2715 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002716 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002717 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002718#endif
2719
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002720 /* A dynamic server is disabled on startup. It must not be counted as
2721 * an active backend entry.
2722 */
2723 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2724 if (srv->flags & SRV_F_BACKUP)
2725 px->srv_bck++;
2726 else
2727 px->srv_act++;
2728 }
2729
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002730 srv_lb_commit_status(srv);
2731
2732 return 0;
2733}
2734
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002735int parse_server(const char *file, int linenum, char **args,
2736 struct proxy *curproxy, const struct proxy *defproxy,
2737 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002738{
2739 struct server *newsrv = NULL;
2740 int err_code = 0;
2741
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002742 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002743
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002744 set_usermsgs_ctx(file, linenum, NULL);
2745
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002746 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002747 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002748 err_code |= ERR_ALERT | ERR_FATAL;
2749 goto out;
2750 }
2751 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2752 err_code |= ERR_ALERT | ERR_FATAL;
2753 goto out;
2754 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002755
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002756 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2757 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2758 if (!*args[2])
2759 return 0;
2760 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002761
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002762 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002763 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002764
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002765 /* the servers are linked backwards first */
2766 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2767 newsrv->next = curproxy->srv;
2768 curproxy->srv = newsrv;
2769 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002770
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002771 if (err_code & ERR_CODE)
2772 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002773
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002774 newsrv->conf.file = strdup(file);
2775 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002776
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002777 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002778 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002779 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002780 if (err_code & ERR_FATAL)
2781 goto out;
2782 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002783
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002784 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002785 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002786 if (err_code & ERR_FATAL)
2787 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002788 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002789
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002790 if (parse_flags & SRV_PARSE_TEMPLATE)
2791 _srv_parse_tmpl_init(newsrv, curproxy);
2792
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02002793 /* If the server id is fixed, insert it in the proxy used_id tree.
2794 * This is needed to detect a later duplicate id via srv_parse_id.
2795 *
2796 * If no is specified, a dynamic one is generated in
2797 * check_config_validity.
2798 */
2799 if (newsrv->flags & SRV_F_FORCED_ID)
2800 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
2801
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02002802 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002803 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02002804
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002805 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002806 return 0;
2807
2808 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002809 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002810 return err_code;
2811}
2812
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002813/* Returns a pointer to the first server matching either id <id>.
2814 * NULL is returned if no match is found.
2815 * the lookup is performed in the backend <bk>
2816 */
2817struct server *server_find_by_id(struct proxy *bk, int id)
2818{
2819 struct eb32_node *eb32;
2820 struct server *curserver;
2821
2822 if (!bk || (id ==0))
2823 return NULL;
2824
2825 /* <bk> has no backend capabilities, so it can't have a server */
2826 if (!(bk->cap & PR_CAP_BE))
2827 return NULL;
2828
2829 curserver = NULL;
2830
2831 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2832 if (eb32)
2833 curserver = container_of(eb32, struct server, conf.id);
2834
2835 return curserver;
2836}
2837
2838/* Returns a pointer to the first server matching either name <name>, or id
2839 * if <name> starts with a '#'. NULL is returned if no match is found.
2840 * the lookup is performed in the backend <bk>
2841 */
2842struct server *server_find_by_name(struct proxy *bk, const char *name)
2843{
2844 struct server *curserver;
2845
2846 if (!bk || !name)
2847 return NULL;
2848
2849 /* <bk> has no backend capabilities, so it can't have a server */
2850 if (!(bk->cap & PR_CAP_BE))
2851 return NULL;
2852
2853 curserver = NULL;
2854 if (*name == '#') {
2855 curserver = server_find_by_id(bk, atoi(name + 1));
2856 if (curserver)
2857 return curserver;
2858 }
2859 else {
2860 curserver = bk->srv;
2861
2862 while (curserver && (strcmp(curserver->id, name) != 0))
2863 curserver = curserver->next;
2864
2865 if (curserver)
2866 return curserver;
2867 }
2868
2869 return NULL;
2870}
2871
2872struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2873{
2874 struct server *byname;
2875 struct server *byid;
2876
2877 if (!name && !id)
2878 return NULL;
2879
2880 if (diff)
2881 *diff = 0;
2882
2883 byname = byid = NULL;
2884
2885 if (name) {
2886 byname = server_find_by_name(bk, name);
2887 if (byname && (!id || byname->puid == id))
2888 return byname;
2889 }
2890
2891 /* remaining possibilities :
2892 * - name not set
2893 * - name set but not found
2894 * - name found but ID doesn't match
2895 */
2896 if (id) {
2897 byid = server_find_by_id(bk, id);
2898 if (byid) {
2899 if (byname) {
2900 /* use id only if forced by configuration */
2901 if (byid->flags & SRV_F_FORCED_ID) {
2902 if (diff)
2903 *diff |= 2;
2904 return byid;
2905 }
2906 else {
2907 if (diff)
2908 *diff |= 1;
2909 return byname;
2910 }
2911 }
2912
2913 /* remaining possibilities:
2914 * - name not set
2915 * - name set but not found
2916 */
2917 if (name && diff)
2918 *diff |= 2;
2919 return byid;
2920 }
2921
2922 /* id bot found */
2923 if (byname) {
2924 if (diff)
2925 *diff |= 1;
2926 return byname;
2927 }
2928 }
2929
2930 return NULL;
2931}
2932
Simon Horman7d09b9a2013-02-12 10:45:51 +09002933/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002934 * update a server's current IP address.
2935 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2936 * ip is in network format.
2937 * updater is a string which contains an information about the requester of the update.
2938 * updater is used if not NULL.
2939 *
2940 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02002941 *
2942 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02002943 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002944int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002945{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02002946 /* save the new IP family & address if necessary */
2947 switch (ip_sin_family) {
2948 case AF_INET:
2949 if (s->addr.ss_family == ip_sin_family &&
2950 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
2951 return 0;
2952 break;
2953 case AF_INET6:
2954 if (s->addr.ss_family == ip_sin_family &&
2955 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
2956 return 0;
2957 break;
2958 };
2959
Baptiste Assmann14e40142015-04-14 01:13:07 +02002960 /* generates a log line and a warning on stderr */
2961 if (1) {
2962 /* book enough space for both IPv4 and IPv6 */
2963 char oldip[INET6_ADDRSTRLEN];
2964 char newip[INET6_ADDRSTRLEN];
2965
2966 memset(oldip, '\0', INET6_ADDRSTRLEN);
2967 memset(newip, '\0', INET6_ADDRSTRLEN);
2968
2969 /* copy old IP address in a string */
2970 switch (s->addr.ss_family) {
2971 case AF_INET:
2972 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2973 break;
2974 case AF_INET6:
2975 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2976 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02002977 default:
2978 strcpy(oldip, "(none)");
2979 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02002980 };
2981
2982 /* copy new IP address in a string */
2983 switch (ip_sin_family) {
2984 case AF_INET:
2985 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2986 break;
2987 case AF_INET6:
2988 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2989 break;
2990 };
2991
2992 /* save log line into a buffer */
2993 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2994 s->proxy->id, s->id, oldip, newip, updater);
2995
2996 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002997 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002998
2999 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003000 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003001 }
3002
3003 /* save the new IP family */
3004 s->addr.ss_family = ip_sin_family;
3005 /* save the new IP address */
3006 switch (ip_sin_family) {
3007 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003008 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003009 break;
3010 case AF_INET6:
3011 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3012 break;
3013 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003014 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003015 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003016
3017 return 0;
3018}
3019
William Dauchy7cabc062021-02-11 22:51:24 +01003020/* update agent health check address and port
3021 * addr can be ip4/ip6 or a hostname
3022 * if one error occurs, don't apply anything
3023 * must be called with the server lock held.
3024 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003025const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003026{
3027 struct sockaddr_storage sk;
3028 struct buffer *msg;
3029 int new_port;
3030
3031 msg = get_trash_chunk();
3032 chunk_reset(msg);
3033
3034 if (!(s->agent.state & CHK_ST_ENABLED)) {
3035 chunk_strcat(msg, "agent checks are not enabled on this server");
3036 goto out;
3037 }
3038 if (addr) {
3039 memset(&sk, 0, sizeof(struct sockaddr_storage));
3040 if (str2ip(addr, &sk) == NULL) {
3041 chunk_appendf(msg, "invalid addr '%s'", addr);
3042 goto out;
3043 }
3044 }
3045 if (port) {
3046 if (strl2irc(port, strlen(port), &new_port) != 0) {
3047 chunk_appendf(msg, "provided port is not an integer");
3048 goto out;
3049 }
3050 if (new_port < 0 || new_port > 65535) {
3051 chunk_appendf(msg, "provided port is invalid");
3052 goto out;
3053 }
3054 }
3055out:
3056 if (msg->data)
3057 return msg->area;
3058 else {
3059 if (addr)
3060 set_srv_agent_addr(s, &sk);
3061 if (port)
3062 set_srv_agent_port(s, new_port);
3063 }
3064 return NULL;
3065}
3066
William Dauchyb456e1f2021-02-11 22:51:23 +01003067/* update server health check address and port
3068 * addr must be ip4 or ip6, it won't be resolved
3069 * if one error occurs, don't apply anything
3070 * must be called with the server lock held.
3071 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003072const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003073{
3074 struct sockaddr_storage sk;
3075 struct buffer *msg;
3076 int new_port;
3077
3078 msg = get_trash_chunk();
3079 chunk_reset(msg);
3080
3081 if (!(s->check.state & CHK_ST_ENABLED)) {
3082 chunk_strcat(msg, "health checks are not enabled on this server");
3083 goto out;
3084 }
3085 if (addr) {
3086 memset(&sk, 0, sizeof(struct sockaddr_storage));
3087 if (str2ip2(addr, &sk, 0) == NULL) {
3088 chunk_appendf(msg, "invalid addr '%s'", addr);
3089 goto out;
3090 }
3091 }
3092 if (port) {
3093 if (strl2irc(port, strlen(port), &new_port) != 0) {
3094 chunk_appendf(msg, "provided port is not an integer");
3095 goto out;
3096 }
3097 if (new_port < 0 || new_port > 65535) {
3098 chunk_appendf(msg, "provided port is invalid");
3099 goto out;
3100 }
3101 /* prevent the update of port to 0 if MAPPORTS are in use */
3102 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3103 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3104 goto out;
3105 }
3106 }
3107out:
3108 if (msg->data)
3109 return msg->area;
3110 else {
3111 if (addr)
3112 s->check.addr = sk;
3113 if (port)
3114 s->check.port = new_port;
3115 }
3116 return NULL;
3117}
3118
Baptiste Assmann14e40142015-04-14 01:13:07 +02003119/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003120 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3121 *
3122 * Caller can pass its name through <updater> to get it integrated in the response message
3123 * returned by the function.
3124 *
3125 * The function first does the following, in that order:
3126 * - validates the new addr and/or port
3127 * - checks if an update is required (new IP or port is different than current ones)
3128 * - checks the update is allowed:
3129 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3130 * - allow all changes if no CHECKS are configured
3131 * - if CHECK is configured:
3132 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3133 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3134 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003135 *
3136 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003137 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003138const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003139{
3140 struct sockaddr_storage sa;
3141 int ret, port_change_required;
3142 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003143 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003144 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003145 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003146
3147 msg = get_trash_chunk();
3148 chunk_reset(msg);
3149
3150 if (addr) {
3151 memset(&sa, 0, sizeof(struct sockaddr_storage));
3152 if (str2ip2(addr, &sa, 0) == NULL) {
3153 chunk_printf(msg, "Invalid addr '%s'", addr);
3154 goto out;
3155 }
3156
3157 /* changes are allowed on AF_INET* families only */
3158 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3159 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3160 goto out;
3161 }
3162
3163 /* collecting data currently setup */
3164 memset(current_addr, '\0', sizeof(current_addr));
3165 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3166 /* changes are allowed on AF_INET* families only */
3167 if ((ret != AF_INET) && (ret != AF_INET6)) {
3168 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3169 goto out;
3170 }
3171
3172 /* applying ADDR changes if required and allowed
3173 * ipcmp returns 0 when both ADDR are the same
3174 */
3175 if (ipcmp(&s->addr, &sa) == 0) {
3176 chunk_appendf(msg, "no need to change the addr");
3177 goto port;
3178 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003179 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003180 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003181
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003182 /* update report for caller */
3183 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3184 }
3185
3186 port:
3187 if (port) {
3188 char sign = '\0';
3189 char *endptr;
3190
3191 if (addr)
3192 chunk_appendf(msg, ", ");
3193
3194 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003195 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003196
3197 /* check if PORT change is required */
3198 port_change_required = 0;
3199
3200 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003201 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003202 new_port = strtol(port, &endptr, 10);
3203 if ((errno != 0) || (port == endptr)) {
3204 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3205 goto out;
3206 }
3207
3208 /* check if caller triggers a port mapped or offset */
3209 if (sign == '-' || (sign == '+')) {
3210 /* check if server currently uses port map */
3211 if (!(s->flags & SRV_F_MAPPORTS)) {
3212 /* switch from fixed port to port map mandatorily triggers
3213 * a port change */
3214 port_change_required = 1;
3215 /* check is configured
3216 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3217 * prevent PORT change if check doesn't have it's dedicated port while switching
3218 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003219 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003220 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.");
3221 goto out;
3222 }
3223 }
3224 /* we're already using port maps */
3225 else {
3226 port_change_required = current_port != new_port;
3227 }
3228 }
3229 /* fixed port */
3230 else {
3231 port_change_required = current_port != new_port;
3232 }
3233
3234 /* applying PORT changes if required and update response message */
3235 if (port_change_required) {
3236 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003237 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003238 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003239
3240 /* prepare message */
3241 chunk_appendf(msg, "port changed from '");
3242 if (s->flags & SRV_F_MAPPORTS)
3243 chunk_appendf(msg, "+");
3244 chunk_appendf(msg, "%d' to '", current_port);
3245
3246 if (sign == '-') {
3247 s->flags |= SRV_F_MAPPORTS;
3248 chunk_appendf(msg, "%c", sign);
3249 /* just use for result output */
3250 new_port = -new_port;
3251 }
3252 else if (sign == '+') {
3253 s->flags |= SRV_F_MAPPORTS;
3254 chunk_appendf(msg, "%c", sign);
3255 }
3256 else {
3257 s->flags &= ~SRV_F_MAPPORTS;
3258 }
3259
3260 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003261 }
3262 else {
3263 chunk_appendf(msg, "no need to change the port");
3264 }
3265 }
3266
3267out:
William Dauchy6318d332020-05-02 21:52:36 +02003268 if (changed) {
3269 /* force connection cleanup on the given server */
3270 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003271 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003272 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003273 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003274 if (updater)
3275 chunk_appendf(msg, " by '%s'", updater);
3276 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003277 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003278}
3279
Christopher Faulet5efdef22021-03-11 18:03:21 +01003280/*
3281 * update server status based on result of SRV resolution
3282 * returns:
3283 * 0 if server status is updated
3284 * 1 if server status has not changed
3285 *
3286 * Must be called with the server lock held.
3287 */
3288int srvrq_update_srv_status(struct server *s, int has_no_ip)
3289{
3290 if (!s->srvrq)
3291 return 1;
3292
3293 /* since this server has an IP, it can go back in production */
3294 if (has_no_ip == 0) {
3295 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3296 return 1;
3297 }
3298
3299 if (s->next_admin & SRV_ADMF_RMAINT)
3300 return 1;
3301
3302 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3303 return 0;
3304}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003305
3306/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003307 * update server status based on result of name resolution
3308 * returns:
3309 * 0 if server status is updated
3310 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003311 *
3312 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003313 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003314int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003315{
Emeric Brun750fe792020-12-23 16:51:12 +01003316 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003317 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003318 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003319
Jerome Magnin012261a2020-07-28 13:38:22 +02003320 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003321 if (resolution == NULL)
3322 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003323
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003324 switch (resolution->status) {
3325 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003326 /* status when HAProxy has just (re)started.
3327 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003328 break;
3329
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003330 case RSLV_STATUS_VALID:
3331 /*
3332 * resume health checks
3333 * server will be turned back on if health check is safe
3334 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003335 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003336 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003337 return 1;
3338 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3339 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003340 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003341 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003342
Emeric Brun52a91d32017-08-31 14:41:55 +02003343 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003344 return 1;
3345 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3346 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3347 s->proxy->id, s->id);
3348
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003349 ha_warning("%s.\n", trash.area);
3350 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003351 return 0;
3352
3353 case RSLV_STATUS_NX:
3354 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003355 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3356 if (!tick_is_expired(exp, now_ms))
3357 break;
3358
3359 if (s->next_admin & SRV_ADMF_RMAINT)
3360 return 1;
3361 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3362 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003363
3364 case RSLV_STATUS_TIMEOUT:
3365 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003366 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3367 if (!tick_is_expired(exp, now_ms))
3368 break;
3369
3370 if (s->next_admin & SRV_ADMF_RMAINT)
3371 return 1;
3372 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3373 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003374
3375 case RSLV_STATUS_REFUSED:
3376 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003377 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3378 if (!tick_is_expired(exp, now_ms))
3379 break;
3380
3381 if (s->next_admin & SRV_ADMF_RMAINT)
3382 return 1;
3383 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3384 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003385
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003386 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003387 /* stop server if resolution failed for a long enough period */
3388 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3389 if (!tick_is_expired(exp, now_ms))
3390 break;
3391
3392 if (s->next_admin & SRV_ADMF_RMAINT)
3393 return 1;
3394 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3395 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003396 }
3397
3398 return 1;
3399}
3400
3401/*
3402 * Server Name Resolution valid response callback
3403 * It expects:
3404 * - <nameserver>: the name server which answered the valid response
3405 * - <response>: buffer containing a valid DNS response
3406 * - <response_len>: size of <response>
3407 * It performs the following actions:
3408 * - ignore response if current ip found and server family not met
3409 * - update with first new ip found if family is met and current IP is not found
3410 * returns:
3411 * 0 on error
3412 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003413 *
3414 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003415 */
Emeric Brun08622d32020-12-23 17:41:43 +01003416int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003417{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003418 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003419 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003420 void *serverip, *firstip;
3421 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003422 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003423 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003424 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003425
Christopher Faulet67957bd2017-09-27 11:00:59 +02003426 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003427 if (!s)
3428 return 1;
3429
Christopher Faulet49531e82021-03-10 15:07:27 +01003430 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003431 /* If DNS resolution is disabled ignore it.
3432 * This is the case if the server was associated to
3433 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003434 */
Emeric Brun34067662021-06-11 10:48:45 +02003435 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003436 return 1;
3437 }
3438
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003439 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003440 if (!resolution)
3441 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003442
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003443 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003444 firstip = NULL; /* pointer to the first valid response found */
3445 /* it will be used as the new IP if a change is required */
3446 firstip_sin_family = AF_UNSPEC;
3447 serverip = NULL; /* current server IP address */
3448
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003449 /* initializing server IP pointer */
3450 server_sin_family = s->addr.ss_family;
3451 switch (server_sin_family) {
3452 case AF_INET:
3453 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3454 break;
3455
3456 case AF_INET6:
3457 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3458 break;
3459
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003460 case AF_UNSPEC:
3461 break;
3462
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003463 default:
3464 goto invalid;
3465 }
3466
Emeric Brund30e9a12020-12-23 18:49:16 +01003467 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3468 serverip, server_sin_family, &firstip,
3469 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003470
3471 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003472 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003473 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003474
Emeric Brun456de772020-12-23 18:17:31 +01003475 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003476 goto save_ip;
3477
Emeric Brun456de772020-12-23 18:17:31 +01003478 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003479 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003480 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003481
Emeric Brun456de772020-12-23 18:17:31 +01003482 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003483 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003484 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003485 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003486 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003487
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003488 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003489 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003490 goto invalid;
3491
3492 }
3493
3494 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003495 if (counters) {
3496 counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003497 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003498 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003499 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003500 else
3501 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003502 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003503
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003504 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003505 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3506 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003507 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003508
3509 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003510 if (counters) {
3511 counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003512 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003513 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003514 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3515 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003516 return 0;
3517}
3518
3519/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003520 * SRV record error management callback
3521 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003522 * 0 if we can trash answser items.
3523 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003524 *
3525 * Grabs the server's lock.
3526 */
3527int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3528{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003529 struct resolv_srvrq *srvrq;
3530 struct resolv_resolution *res;
3531 struct resolvers *resolvers;
3532 int exp;
3533
3534 /* SRV records */
3535 srvrq = objt_resolv_srvrq(requester->owner);
3536 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003537 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003538
3539 resolvers = srvrq->resolvers;
3540 res = requester->resolution;
3541
3542 switch (res->status) {
3543
3544 case RSLV_STATUS_NX:
3545 /* stop server if resolution is NX for a long enough period */
3546 exp = tick_add(res->last_valid, resolvers->hold.nx);
3547 if (!tick_is_expired(exp, now_ms))
3548 return 1;
3549 break;
3550
3551 case RSLV_STATUS_TIMEOUT:
3552 /* stop server if resolution is TIMEOUT for a long enough period */
3553 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3554 if (!tick_is_expired(exp, now_ms))
3555 return 1;
3556 break;
3557
3558 case RSLV_STATUS_REFUSED:
3559 /* stop server if resolution is REFUSED for a long enough period */
3560 exp = tick_add(res->last_valid, resolvers->hold.refused);
3561 if (!tick_is_expired(exp, now_ms))
3562 return 1;
3563 break;
3564
3565 default:
3566 /* stop server if resolution failed for a long enough period */
3567 exp = tick_add(res->last_valid, resolvers->hold.other);
3568 if (!tick_is_expired(exp, now_ms))
3569 return 1;
3570 }
3571
Emeric Brun34067662021-06-11 10:48:45 +02003572 /* Remove any associated server ref */
3573 resolv_detach_from_resolution_answer_items(res, requester, 1);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003574
Emeric Brun12ca6582021-06-10 15:25:25 +02003575 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003576}
3577
3578/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003579 * Server Name Resolution error management callback
3580 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003581 * 0 if we can trash answser items.
3582 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003583 *
3584 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003585 */
Emeric Brun08622d32020-12-23 17:41:43 +01003586int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003587{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003588 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003589
Christopher Faulet67957bd2017-09-27 11:00:59 +02003590 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003591 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003592 return 0;
3593
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003594 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003595 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003596 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003597 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun34067662021-06-11 10:48:45 +02003598 resolv_detach_from_resolution_answer_items(requester->resolution, requester, 1);
Emeric Brun12ca6582021-06-10 15:25:25 +02003599 return 0;
3600 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003601 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003602
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003603 return 1;
3604}
3605
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003606/*
3607 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003608 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003609 * It returns a pointer to the first server found or NULL if <ip> is not yet
3610 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003611 *
3612 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003613 */
3614struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3615{
3616 struct server *tmpsrv;
3617 struct proxy *be;
3618
3619 if (!srv)
3620 return NULL;
3621
3622 be = srv->proxy;
3623 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003624 /* we found the current server is the same, ignore it */
3625 if (srv == tmpsrv)
3626 continue;
3627
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003628 /* We want to compare the IP in the record with the IP of the servers in the
3629 * same backend, only if:
3630 * * DNS resolution is enabled on the server
3631 * * the hostname used for the resolution by our server is the same than the
3632 * one used for the server found in the backend
3633 * * the server found in the backend is not our current server
3634 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003635 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003636 if ((tmpsrv->hostname_dn == NULL) ||
3637 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003638 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003639 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003640 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003641 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003642 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003643
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003644 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003645 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003646 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003647 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003648 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003649
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003650 /* At this point, we have 2 different servers using the same DNS hostname
3651 * for their respective resolution.
3652 */
3653 if (*ip_family == tmpsrv->addr.ss_family &&
3654 ((tmpsrv->addr.ss_family == AF_INET &&
3655 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3656 (tmpsrv->addr.ss_family == AF_INET6 &&
3657 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003658 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003659 return tmpsrv;
3660 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003661 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003662 }
3663
Emeric Brune9fd6b52017-11-02 17:20:39 +01003664
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003665 return NULL;
3666}
3667
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003668/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3669 * resolver. This is suited for initial address configuration. Returns 0 on
3670 * success otherwise a non-zero error code. In case of error, *err_code, if
3671 * not NULL, is filled up.
3672 */
3673int srv_set_addr_via_libc(struct server *srv, int *err_code)
3674{
3675 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003676 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003677 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003678 return 1;
3679 }
3680 return 0;
3681}
3682
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003683/* Set the server's FDQN (->hostname) from <hostname>.
3684 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003685 *
3686 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003687 */
Emeric Brun08622d32020-12-23 17:41:43 +01003688int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003689{
Emeric Brun08622d32020-12-23 17:41:43 +01003690 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003691 char *hostname_dn;
3692 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003693
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003694 /* Note that the server lock is already held. */
3695 if (!srv->resolvers)
3696 return -1;
3697
Emeric Brun08622d32020-12-23 17:41:43 +01003698 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003699 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003700 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003701 * and we can't enable it at run time for now.
3702 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003703 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003704 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003705
3706 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003707 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003708 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01003709 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
3710 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003711 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003712 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003713
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003714 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003715 if (resolution &&
3716 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003717 resolution->hostname_dn_len == hostname_dn_len &&
3718 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003719 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003720
Christopher Faulet0efc0992021-03-11 18:09:53 +01003721 resolv_unlink_resolution(srv->resolv_requester, 0);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003722
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003723 free(srv->hostname);
3724 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003725 srv->hostname = strdup(hostname);
3726 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003727 srv->hostname_dn_len = hostname_dn_len;
3728 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003729 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003730
Baptiste Assmann13a92322019-06-07 09:40:55 +02003731 if (srv->flags & SRV_F_NO_RESOLUTION)
3732 goto end;
3733
Emeric Brund30e9a12020-12-23 18:49:16 +01003734 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003735 goto err;
3736
3737 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003738 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003739 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003740 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003741
3742 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003743 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003744 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003745 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003746}
3747
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003748/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3749 * from the state file. This is suited for initial address configuration.
3750 * Returns 0 on success otherwise a non-zero error code. In case of error,
3751 * *err_code, if not NULL, is filled up.
3752 */
3753static int srv_apply_lastaddr(struct server *srv, int *err_code)
3754{
3755 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3756 if (err_code)
3757 *err_code |= ERR_WARN;
3758 return 1;
3759 }
3760 return 0;
3761}
3762
Willy Tarreau25e51522016-11-04 15:10:17 +01003763/* returns 0 if no error, otherwise a combination of ERR_* flags */
3764static int srv_iterate_initaddr(struct server *srv)
3765{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003766 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003767 int return_code = 0;
3768 int err_code;
3769 unsigned int methods;
3770
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003771 /* If no addr and no hostname set, get the name from the DNS SRV request */
3772 if (!name && srv->srvrq)
3773 name = srv->srvrq->name;
3774
Willy Tarreau25e51522016-11-04 15:10:17 +01003775 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003776 if (!methods) {
3777 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003778 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3779 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003780 if (srv->resolvers_id) {
3781 /* dns resolution is configured, add "none" to not fail on startup */
3782 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3783 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003784 }
3785
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003786 /* "-dr" : always append "none" so that server addresses resolution
3787 * failures are silently ignored, this is convenient to validate some
3788 * configs out of their environment.
3789 */
3790 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3791 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3792
Willy Tarreau25e51522016-11-04 15:10:17 +01003793 while (methods) {
3794 err_code = 0;
3795 switch (srv_get_next_initaddr(&methods)) {
3796 case SRV_IADDR_LAST:
3797 if (!srv->lastaddr)
3798 continue;
3799 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003800 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003801 return_code |= err_code;
3802 break;
3803
3804 case SRV_IADDR_LIBC:
3805 if (!srv->hostname)
3806 continue;
3807 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003808 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003809 return_code |= err_code;
3810 break;
3811
Willy Tarreau37ebe122016-11-04 15:17:58 +01003812 case SRV_IADDR_NONE:
3813 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003814 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003815 ha_warning("could not resolve address '%s', disabling server.\n",
3816 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003817 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003818 return return_code;
3819
Willy Tarreau4310d362016-11-02 15:05:56 +01003820 case SRV_IADDR_IP:
3821 ipcpy(&srv->init_addr, &srv->addr);
3822 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003823 ha_warning("could not resolve address '%s', falling back to configured address.\n",
3824 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01003825 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003826 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003827
Willy Tarreau25e51522016-11-04 15:10:17 +01003828 default: /* unhandled method */
3829 break;
3830 }
3831 }
3832
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003833 if (!return_code)
3834 ha_alert("no method found to resolve address '%s'.\n", name);
3835 else
3836 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01003837
3838 return_code |= ERR_ALERT | ERR_FATAL;
3839 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003840out:
3841 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003842 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01003843 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003844}
3845
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003846/*
3847 * This function parses all backends and all servers within each backend
3848 * and performs servers' addr resolution based on information provided by:
3849 * - configuration file
3850 * - server-state file (states provided by an 'old' haproxy process)
3851 *
3852 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3853 */
3854int srv_init_addr(void)
3855{
3856 struct proxy *curproxy;
3857 int return_code = 0;
3858
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003859 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003860 while (curproxy) {
3861 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003862
3863 /* servers are in backend only */
Amaury Denoyellee3c41922021-01-06 14:28:50 +01003864 if (!(curproxy->cap & PR_CAP_BE) || curproxy->disabled)
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003865 goto srv_init_addr_next;
3866
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003867 for (srv = curproxy->srv; srv; srv = srv->next) {
3868 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003869 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003870 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003871 reset_usermsgs_ctx();
3872 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003873
3874 srv_init_addr_next:
3875 curproxy = curproxy->next;
3876 }
3877
3878 return return_code;
3879}
3880
Willy Tarreau46b7f532018-08-21 11:54:26 +02003881/*
3882 * Must be called with the server lock held.
3883 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003884const 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 +02003885{
3886
Willy Tarreau83061a82018-07-13 11:56:34 +02003887 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003888
3889 msg = get_trash_chunk();
3890 chunk_reset(msg);
3891
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003892 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003893 chunk_appendf(msg, "no need to change the FDQN");
3894 goto out;
3895 }
3896
3897 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3898 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3899 goto out;
3900 }
3901
3902 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3903 server->proxy->id, server->id, server->hostname, fqdn);
3904
Emeric Brun08622d32020-12-23 17:41:43 +01003905 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003906 chunk_reset(msg);
3907 chunk_appendf(msg, "could not update %s/%s FQDN",
3908 server->proxy->id, server->id);
3909 goto out;
3910 }
3911
3912 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003913 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003914
3915 out:
3916 if (updater)
3917 chunk_appendf(msg, " by '%s'", updater);
3918 chunk_appendf(msg, "\n");
3919
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003920 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003921}
3922
3923
Willy Tarreau21b069d2016-11-23 17:15:08 +01003924/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3925 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003926 * 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 +01003927 * used for CLI commands requiring a server name.
3928 * Important: the <arg> is modified to remove the '/'.
3929 */
3930struct server *cli_find_server(struct appctx *appctx, char *arg)
3931{
3932 struct proxy *px;
3933 struct server *sv;
3934 char *line;
3935
3936 /* split "backend/server" and make <line> point to server */
3937 for (line = arg; *line; line++)
3938 if (*line == '/') {
3939 *line++ = '\0';
3940 break;
3941 }
3942
3943 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003944 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003945 return NULL;
3946 }
3947
3948 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003949 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003950 return NULL;
3951 }
3952
Willy Tarreauc3914d42020-09-24 08:39:22 +02003953 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003954 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003955 return NULL;
3956 }
3957
3958 return sv;
3959}
3960
William Lallemand222baf22016-11-19 02:00:33 +01003961
Willy Tarreau46b7f532018-08-21 11:54:26 +02003962/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003963static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01003964{
3965 struct server *sv;
3966 const char *warning;
3967
3968 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3969 return 1;
3970
3971 sv = cli_find_server(appctx, args[2]);
3972 if (!sv)
3973 return 1;
3974
3975 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003976 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003977 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003978 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02003979 if (warning)
3980 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003981 }
3982 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003983 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003984 if (strcmp(args[4], "ready") == 0)
3985 srv_adm_set_ready(sv);
3986 else if (strcmp(args[4], "drain") == 0)
3987 srv_adm_set_drain(sv);
3988 else if (strcmp(args[4], "maint") == 0)
3989 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02003990 else
3991 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003992 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003993 }
3994 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003995 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02003996 if (sv->track)
3997 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003998 else if (strcmp(args[4], "up") == 0) {
3999 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004000 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004001 }
4002 else if (strcmp(args[4], "stopping") == 0) {
4003 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004004 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004005 }
4006 else if (strcmp(args[4], "down") == 0) {
4007 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004008 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004009 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004010 else
4011 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004012 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004013 }
4014 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004015 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004016 if (!(sv->agent.state & CHK_ST_ENABLED))
4017 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004018 else if (strcmp(args[4], "up") == 0) {
4019 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004020 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004021 }
4022 else if (strcmp(args[4], "down") == 0) {
4023 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004024 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004025 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004026 else
4027 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004028 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004029 }
Misiek2da082d2017-01-09 09:40:42 +01004030 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004031 char *addr = NULL;
4032 char *port = NULL;
4033 if (strlen(args[4]) == 0) {
4034 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4035 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004036 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004037 }
4038 addr = args[4];
4039 if (strcmp(args[5], "port") == 0)
4040 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004041 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004042 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004043 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004044 if (warning)
4045 cli_msg(appctx, LOG_WARNING, warning);
4046 }
4047 else if (strcmp(args[3], "agent-port") == 0) {
4048 char *port = NULL;
4049 if (strlen(args[4]) == 0) {
4050 cli_err(appctx, "set server <b>/<s> agent-port requires"
4051 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004052 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004053 }
4054 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004055 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004056 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004057 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004058 if (warning)
4059 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004060 }
4061 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004062 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004063 if (!(sv->agent.state & CHK_ST_ENABLED))
4064 cli_err(appctx, "agent checks are not enabled on this server.\n");
4065 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004066 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004067 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004068 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004069 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004070 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004071 else if (strcmp(args[3], "check-addr") == 0) {
4072 char *addr = NULL;
4073 char *port = NULL;
4074 if (strlen(args[4]) == 0) {
4075 cli_err(appctx, "set server <b>/<s> check-addr requires"
4076 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004077 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004078 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004079 addr = args[4];
4080 if (strcmp(args[5], "port") == 0)
4081 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004082 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004083 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004084 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004085 if (warning)
4086 cli_msg(appctx, LOG_WARNING, warning);
4087 }
4088 else if (strcmp(args[3], "check-port") == 0) {
4089 char *port = NULL;
4090 if (strlen(args[4]) == 0) {
4091 cli_err(appctx, "set server <b>/<s> check-port requires"
4092 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004093 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004094 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004095 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004096 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004097 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004098 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004099 if (warning)
4100 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004101 }
4102 else if (strcmp(args[3], "addr") == 0) {
4103 char *addr = NULL;
4104 char *port = NULL;
4105 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004106 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004107 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004108 }
4109 else {
4110 addr = args[4];
4111 }
4112 if (strcmp(args[5], "port") == 0) {
4113 port = args[6];
4114 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004115 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004116 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004117 if (warning)
4118 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004119 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004120 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004121 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004122 else if (strcmp(args[3], "fqdn") == 0) {
4123 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004124 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004125 goto out;
4126 }
4127 if (!sv->resolvers) {
4128 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4129 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004130 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004131 if (sv->srvrq) {
4132 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004133 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004134 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004135 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4136 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004137 /* ensure runtime resolver will process this new fqdn */
4138 if (sv->flags & SRV_F_NO_RESOLUTION) {
4139 sv->flags &= ~SRV_F_NO_RESOLUTION;
4140 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004141 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004142 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004143 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004144 if (warning)
4145 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004146 }
William Dauchyf6370442020-11-14 19:25:33 +01004147 else if (strcmp(args[3], "ssl") == 0) {
4148#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004149 if (sv->flags & SRV_F_DYNAMIC) {
4150 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4151 goto out;
4152 }
4153
William Dauchyf6370442020-11-14 19:25:33 +01004154 if (sv->ssl_ctx.ctx == NULL) {
4155 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4156 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004157 goto out;
4158 }
4159
4160 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4161 if (strcmp(args[4], "on") == 0) {
William Dauchyf6370442020-11-14 19:25:33 +01004162 ssl_sock_set_srv(sv, 1);
4163 } else if (strcmp(args[4], "off") == 0) {
4164 ssl_sock_set_srv(sv, 0);
4165 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004166 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004167 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004168 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004169 }
4170 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004171 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004172 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4173#else
4174 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4175#endif
4176 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004177 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004178 "usage: set server <backend>/<server> "
4179 "addr | agent | agent-addr | agent-port | agent-send | "
4180 "check-addr | check-port | fqdn | health | ssl | "
4181 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004182 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004183 out:
William Lallemand222baf22016-11-19 02:00:33 +01004184 return 1;
4185}
4186
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004187static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004188{
4189 struct stream_interface *si = appctx->owner;
4190 struct proxy *px;
4191 struct server *sv;
4192 char *line;
4193
4194
4195 /* split "backend/server" and make <line> point to server */
4196 for (line = args[2]; *line; line++)
4197 if (*line == '/') {
4198 *line++ = '\0';
4199 break;
4200 }
4201
Willy Tarreau9d008692019-08-09 11:21:01 +02004202 if (!*line)
4203 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004204
Willy Tarreau9d008692019-08-09 11:21:01 +02004205 if (!get_backend_server(args[2], line, &px, &sv))
4206 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004207
4208 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004209 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4210 sv->iweight);
4211 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004212 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004213 return 0;
4214 }
William Lallemand6b160942016-11-22 12:34:35 +01004215 return 1;
4216}
4217
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004218/* Parse a "set weight" command.
4219 *
4220 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004221 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004222static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004223{
4224 struct server *sv;
4225 const char *warning;
4226
4227 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4228 return 1;
4229
4230 sv = cli_find_server(appctx, args[2]);
4231 if (!sv)
4232 return 1;
4233
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004234 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4235
William Lallemand6b160942016-11-22 12:34:35 +01004236 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004237 if (warning)
4238 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004239
4240 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4241
William Lallemand6b160942016-11-22 12:34:35 +01004242 return 1;
4243}
4244
Willy Tarreau46b7f532018-08-21 11:54:26 +02004245/* parse a "set maxconn server" command. It always returns 1.
4246 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004247 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004248 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004249static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004250{
4251 struct server *sv;
4252 const char *warning;
4253
4254 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4255 return 1;
4256
4257 sv = cli_find_server(appctx, args[3]);
4258 if (!sv)
4259 return 1;
4260
Amaury Denoyelle02742862021-06-18 11:11:36 +02004261 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4262
Willy Tarreaub8026272016-11-23 11:26:56 +01004263 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004264 if (warning)
4265 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004266
Amaury Denoyelle02742862021-06-18 11:11:36 +02004267 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4268
Willy Tarreaub8026272016-11-23 11:26:56 +01004269 return 1;
4270}
William Lallemand6b160942016-11-22 12:34:35 +01004271
Willy Tarreau46b7f532018-08-21 11:54:26 +02004272/* parse a "disable agent" command. It always returns 1.
4273 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004274 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004275 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004276static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004277{
4278 struct server *sv;
4279
4280 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4281 return 1;
4282
4283 sv = cli_find_server(appctx, args[2]);
4284 if (!sv)
4285 return 1;
4286
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004287 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004288 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004289 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004290 return 1;
4291}
4292
Willy Tarreau46b7f532018-08-21 11:54:26 +02004293/* parse a "disable health" command. It always returns 1.
4294 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004295 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004296 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004297static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004298{
4299 struct server *sv;
4300
4301 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4302 return 1;
4303
4304 sv = cli_find_server(appctx, args[2]);
4305 if (!sv)
4306 return 1;
4307
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004308 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004309 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004310 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004311 return 1;
4312}
4313
Willy Tarreau46b7f532018-08-21 11:54:26 +02004314/* parse a "disable server" command. It always returns 1.
4315 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004316 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004317 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004318static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004319{
4320 struct server *sv;
4321
4322 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4323 return 1;
4324
4325 sv = cli_find_server(appctx, args[2]);
4326 if (!sv)
4327 return 1;
4328
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004329 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004330 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004331 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004332 return 1;
4333}
4334
Willy Tarreau46b7f532018-08-21 11:54:26 +02004335/* parse a "enable agent" command. It always returns 1.
4336 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004337 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004338 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004339static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004340{
4341 struct server *sv;
4342
4343 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4344 return 1;
4345
4346 sv = cli_find_server(appctx, args[2]);
4347 if (!sv)
4348 return 1;
4349
Willy Tarreau9d008692019-08-09 11:21:01 +02004350 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4351 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004352
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004353 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004354 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004355 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004356 return 1;
4357}
4358
Willy Tarreau46b7f532018-08-21 11:54:26 +02004359/* parse a "enable health" command. It always returns 1.
4360 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004361 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004362 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004363static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004364{
4365 struct server *sv;
4366
4367 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4368 return 1;
4369
4370 sv = cli_find_server(appctx, args[2]);
4371 if (!sv)
4372 return 1;
4373
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004374 if (!(sv->check.state & CHK_ST_CONFIGURED))
4375 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4376
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004377 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004378 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004379 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004380 return 1;
4381}
4382
Willy Tarreau46b7f532018-08-21 11:54:26 +02004383/* parse a "enable server" command. It always returns 1.
4384 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004385 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004386 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004387static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004388{
4389 struct server *sv;
4390
4391 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4392 return 1;
4393
4394 sv = cli_find_server(appctx, args[2]);
4395 if (!sv)
4396 return 1;
4397
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004398 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004399 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004400 if (!(sv->flags & SRV_F_COOKIESET)
4401 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4402 sv->cookie)
4403 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004404 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004405 return 1;
4406}
4407
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004408/* Allocates data structure related to load balancing for the server <sv>. It
4409 * is only required for dynamic servers.
4410 *
4411 * At the moment, the server lock is not used as this function is only called
4412 * for a dynamic server not yet registered.
4413 *
4414 * Returns 1 on success, 0 on allocation failure.
4415 */
4416static int srv_alloc_lb(struct server *sv, struct proxy *be)
4417{
4418 int node;
4419
4420 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4421 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4422 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4423 sv->lb_nodes_now = 0;
4424
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004425 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4426 ((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 +01004427 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4428
4429 if (!sv->lb_nodes)
4430 return 0;
4431
4432 for (node = 0; node < sv->lb_nodes_tot; node++) {
4433 sv->lb_nodes[node].server = sv;
4434 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4435 }
4436 }
4437
4438 return 1;
4439}
4440
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004441/* updates the server's weight during a warmup stage. Once the final weight is
4442 * reached, the task automatically stops. Note that any server status change
4443 * must have updated s->last_change accordingly.
4444 */
4445static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4446{
4447 struct server *s = context;
4448
4449 /* by default, plan on stopping the task */
4450 t->expire = TICK_ETERNITY;
4451 if ((s->next_admin & SRV_ADMF_MAINT) ||
4452 (s->next_state != SRV_ST_STARTING))
4453 return t;
4454
4455 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4456
4457 /* recalculate the weights and update the state */
4458 server_recalc_eweight(s, 1);
4459
4460 /* probably that we can refill this server with a bit more connections */
4461 pendconn_grab_from_px(s);
4462
4463 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4464
4465 /* get back there in 1 second or 1/20th of the slowstart interval,
4466 * whichever is greater, resulting in small 5% steps.
4467 */
4468 if (s->next_state == SRV_ST_STARTING)
4469 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4470 return t;
4471}
4472
4473/* Allocate the slowstart task if the server is configured with a slowstart
4474 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4475 *
4476 * Returns 0 on success else non-zero.
4477 */
4478static int init_srv_slowstart(struct server *srv)
4479{
4480 struct task *t;
4481
4482 if (srv->slowstart) {
4483 if ((t = task_new(MAX_THREADS_MASK)) == NULL) {
4484 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4485 return ERR_ALERT | ERR_FATAL;
4486 }
4487
4488 /* We need a warmup task that will be called when the server
4489 * state switches from down to up.
4490 */
4491 srv->warmup = t;
4492 t->process = server_warmup;
4493 t->context = srv;
4494
4495 /* server can be in this state only because of */
4496 if (srv->next_state == SRV_ST_STARTING) {
4497 task_schedule(srv->warmup,
4498 tick_add(now_ms,
4499 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4500 }
4501 }
4502
4503 return ERR_NONE;
4504}
4505REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4506
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004507/* Memory allocation and initialization of the per_thr field.
4508 * Returns 0 if the field has been successfully initialized, -1 on failure.
4509 */
4510int srv_init_per_thr(struct server *srv)
4511{
4512 int i;
4513
4514 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
4515 if (!srv->per_thr)
4516 return -1;
4517
4518 for (i = 0; i < global.nbthread; i++) {
4519 srv->per_thr[i].idle_conns = EB_ROOT;
4520 srv->per_thr[i].safe_conns = EB_ROOT;
4521 srv->per_thr[i].avail_conns = EB_ROOT;
4522 MT_LIST_INIT(&srv->per_thr[i].streams);
4523 }
4524
4525 return 0;
4526}
4527
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004528/* Parse a "add server" command
4529 * Returns 0 if the server has been successfully initialized, 1 on failure.
4530 */
4531static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4532{
4533 struct proxy *be;
4534 struct server *srv;
4535 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004536 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004537 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004538 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4539
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004540 usermsgs_clr("CLI");
4541
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004542 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4543 return 1;
4544
4545 ++args;
4546
4547 sv_name = be_name = args[1];
4548 /* split backend/server arg */
4549 while (*sv_name && *(++sv_name)) {
4550 if (*sv_name == '/') {
4551 *sv_name = '\0';
4552 ++sv_name;
4553 break;
4554 }
4555 }
4556
4557 if (!*sv_name)
4558 return cli_err(appctx, "Require 'backend/server'.");
4559
Amaury Denoyellecece9182021-04-20 17:09:08 +02004560 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004561 if (!be)
4562 return cli_err(appctx, "No such backend.");
4563
4564 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004565 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004566 return 1;
4567 }
4568
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004569 /* At this point, some operations might not be thread-safe anymore. This
4570 * might be the case for parsing handlers which were designed to run
4571 * only at the starting stage on single-thread mode.
4572 *
4573 * Activate thread isolation to ensure thread-safety.
4574 */
4575 thread_isolate();
4576
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004577 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004578 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4579 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004580 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004581
4582 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004583 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004584
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004585 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004586 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004587 }
4588
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004589 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4590 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004591 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004592
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004593 /* A dynamic server does not currently support resolution.
4594 *
4595 * Initialize it explicitly to the "none" method to ensure no
4596 * resolution will ever be executed.
4597 */
4598 srv->init_addr_methods = SRV_IADDR_NONE;
4599
Amaury Denoyelle30467232021-03-12 18:03:27 +01004600 if (srv->mux_proto) {
4601 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004602 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004603 goto out;
4604 }
4605 }
4606
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004607 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004608 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004609 goto out;
4610 }
4611
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004612 if (srv->max_idle_conns != 0) {
4613 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4614 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004615 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004616 goto out;
4617 }
4618 }
4619
4620 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004621 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004622 goto out;
4623 }
4624
4625 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4626 COUNTERS_SV,
4627 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004628 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004629 goto out;
4630 }
4631
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004632 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4633 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004634 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4635 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4636 goto out;
4637 }
4638 }
4639
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004640 if (srv->trackit) {
4641 if (srv_apply_track(srv, be))
4642 goto out;
4643 }
4644
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004645 /* Init check/agent if configured. The check is manually disabled
4646 * because a dynamic server is started in a disable state. It must be
4647 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004648 */
4649 if (srv->do_check) {
4650 if (init_srv_check(srv))
4651 goto out;
4652
4653 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004654 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004655
4656 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004657 if (init_srv_agent_check(srv))
4658 goto out;
4659
4660 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004661 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004662
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004663 /* Attach the server to the end of the proxy linked list. Note that this
4664 * operation is not thread-safe so this is executed under thread
4665 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004666 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004667 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004668 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004669
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004670 /* TODO use a double-linked list for px->srv */
4671 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004672 struct server *next = be->srv;
4673
4674 while (1) {
4675 /* check for duplicate server */
4676 if (!strcmp(srv->id, next->id)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004677 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004678 goto out;
4679 }
4680
4681 if (!next->next)
4682 break;
4683
4684 next = next->next;
4685 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004686
4687 next->next = srv;
4688 }
4689 else {
4690 srv->next = be->srv;
4691 be->srv = srv;
4692 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004693
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004694 /* generate the server id if not manually specified */
4695 if (!srv->puid) {
4696 next_id = get_next_id(&be->conf.used_server_id, 1);
4697 if (!next_id) {
4698 ha_alert("Cannot attach server : no id left in proxy\n");
4699 goto out;
4700 }
4701
4702 srv->conf.id.key = srv->puid = next_id;
4703 srv->conf.name.key = srv->id;
4704 }
4705
4706 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004707 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4708 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004709 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004710
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004711 thread_release();
4712
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004713 /* Start the check task. The server must be fully initialized.
4714 *
4715 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4716 * need to randomly spread the task interval for dynamic servers.
4717 */
4718 if (srv->check.state & CHK_ST_CONFIGURED) {
4719 if (!start_check_task(&srv->check, 0, 1, 1))
4720 ha_alert("System might be unstable, consider to execute a reload");
4721 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004722 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004723 if (!start_check_task(&srv->agent, 0, 1, 1))
4724 ha_alert("System might be unstable, consider to execute a reload");
4725 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004726
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004727 ha_notice("New server registered.\n");
4728 cli_msg(appctx, LOG_INFO, usermsgs_str());
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004729
4730 return 0;
4731
4732out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004733 if (srv) {
4734 if (srv->track)
4735 release_server_track(srv);
4736
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004737 if (srv->check.state & CHK_ST_CONFIGURED)
4738 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004739 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004740 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004741
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004742 /* remove the server from the proxy linked list */
4743 if (be->srv == srv) {
4744 be->srv = srv->next;
4745 }
4746 else {
4747 struct server *prev;
4748 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4749 ;
4750 if (prev)
4751 prev->next = srv->next;
4752 }
4753
4754 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004755
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004756 thread_release();
4757
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004758 if (!usermsgs_empty())
4759 cli_err(appctx, usermsgs_str());
4760
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004761 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004762 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004763
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004764 return 1;
4765}
4766
Amaury Denoyellee5580432021-04-15 14:41:20 +02004767/* Parse a "del server" command
4768 * Returns 0 if the server has been successfully initialized, 1 on failure.
4769 */
4770static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4771{
4772 struct proxy *be;
4773 struct server *srv;
4774 char *be_name, *sv_name;
4775
4776 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4777 return 1;
4778
4779 ++args;
4780
4781 sv_name = be_name = args[1];
4782 /* split backend/server arg */
4783 while (*sv_name && *(++sv_name)) {
4784 if (*sv_name == '/') {
4785 *sv_name = '\0';
4786 ++sv_name;
4787 break;
4788 }
4789 }
4790
4791 if (!*sv_name)
4792 return cli_err(appctx, "Require 'backend/server'.");
4793
4794 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02004795 * requires thread isolation. In addition, any place referencing the
4796 * server about to be deleted would be unsafe after our operation, so
4797 * we must be certain to be alone so that no other thread has even
4798 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02004799 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02004800 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02004801
4802 get_backend_server(be_name, sv_name, &be, &srv);
4803 if (!be) {
4804 cli_err(appctx, "No such backend.");
4805 goto out;
4806 }
4807
4808 if (!srv) {
4809 cli_err(appctx, "No such server.");
4810 goto out;
4811 }
4812
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02004813 if (srv->flags & SRV_F_NON_PURGEABLE) {
4814 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 +02004815 goto out;
4816 }
4817
4818 /* Only servers in maintenance can be deleted. This ensures that the
4819 * server is not present anymore in the lb structures (through
4820 * lbprm.set_server_status_down).
4821 */
4822 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
4823 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
4824 goto out;
4825 }
4826
4827 /* Ensure that there is no active/idle/pending connection on the server.
4828 *
4829 * TODO idle connections should not prevent server deletion. A proper
4830 * cleanup function should be implemented to be used here.
4831 */
4832 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02004833 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02004834 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
4835 goto out;
4836 }
4837
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004838 /* remove srv from tracking list */
4839 if (srv->track)
4840 release_server_track(srv);
4841
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004842 /* stop the check task if running */
4843 if (srv->check.state & CHK_ST_CONFIGURED)
4844 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004845 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004846 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02004847
4848 /* detach the server from the proxy linked list
4849 * The proxy servers list is currently not protected by a lock, so this
4850 * requires thread_isolate/release.
4851 */
4852
4853 /* be->srv cannot be empty since we have already found the server with
4854 * get_backend_server */
4855 BUG_ON(!be->srv);
4856 if (be->srv == srv) {
4857 be->srv = srv->next;
4858 }
4859 else {
4860 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02004861 for (next = be->srv; srv != next->next; next = next->next) {
4862 /* srv cannot be not found since we have already found
4863 * it with get_backend_server */
4864 BUG_ON(!next);
4865 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02004866
Amaury Denoyellee5580432021-04-15 14:41:20 +02004867 next->next = srv->next;
4868 }
4869
4870 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02004871 eb32_delete(&srv->conf.id);
4872 ebpt_delete(&srv->conf.name);
Amaury Denoyellee5580432021-04-15 14:41:20 +02004873 ebpt_delete(&srv->addr_node);
4874
4875 /* remove srv from idle_node tree for idle conn cleanup */
4876 eb32_delete(&srv->idle_node);
4877
4878 thread_release();
4879
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004880 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004881 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02004882
4883 cli_msg(appctx, LOG_INFO, "Server deleted.");
4884
4885 return 0;
4886
4887out:
4888 thread_release();
4889
4890 return 1;
4891}
4892
William Lallemand222baf22016-11-19 02:00:33 +01004893/* register cli keywords */
4894static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02004895 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
4896 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004897 { { "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 +02004898 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
4899 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004900 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
4901 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
4902 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
4903 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
4904 { { "set", "weight", NULL }, "set weight <bk>/<srv> (DEPRECATED) : change a server's weight (use 'set server' instead)", cli_parse_set_weight },
4905 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server (EXPERIMENTAL)", cli_parse_add_server, NULL, NULL, NULL, ACCESS_EXPERIMENTAL },
4906 { { "del", "server", NULL }, "del server <bk>/<srv> : remove a dynamically added server (EXPERIMENTAL)", cli_parse_delete_server, NULL, NULL, NULL, ACCESS_EXPERIMENTAL },
William Lallemand222baf22016-11-19 02:00:33 +01004907 {{},}
4908}};
4909
Willy Tarreau0108d902018-11-25 19:14:37 +01004910INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004911
Amaury Denoyelle669b6202021-07-13 10:35:23 +02004912/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
4913 * field is used to retrieve the identifier of the tracked server, either with
4914 * the format "proxy/server" or just "server". <curproxy> must point to the
4915 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
4916 * to find the tracked server.
4917 *
4918 * Returns 0 if the server track has been activated else non-zero.
4919 *
4920 * Not thread-safe.
4921 */
4922int srv_apply_track(struct server *srv, struct proxy *curproxy)
4923{
4924 struct proxy *px;
4925 struct server *strack, *loop;
4926 char *pname, *sname;
4927
4928 if (!srv->trackit)
4929 return 1;
4930
4931 pname = srv->trackit;
4932 sname = strrchr(pname, '/');
4933
4934 if (sname) {
4935 *sname++ = '\0';
4936 }
4937 else {
4938 sname = pname;
4939 pname = NULL;
4940 }
4941
4942 if (pname) {
4943 px = proxy_be_by_name(pname);
4944 if (!px) {
4945 ha_alert("unable to find required proxy '%s' for tracking.\n",
4946 pname);
4947 return 1;
4948 }
4949 }
4950 else {
4951 px = curproxy;
4952 }
4953
4954 strack = findserver(px, sname);
4955 if (!strack) {
4956 ha_alert("unable to find required server '%s' for tracking.\n",
4957 sname);
4958 return 1;
4959 }
4960
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02004961 if (strack->flags & SRV_F_DYNAMIC) {
4962 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
4963 px->id, strack->id);
4964 return 1;
4965 }
4966
Amaury Denoyelle669b6202021-07-13 10:35:23 +02004967 if (!strack->do_check && !strack->do_agent && !strack->track &&
4968 !strack->trackit) {
4969 ha_alert("unable to use %s/%s for "
4970 "tracking as it does not have any check nor agent enabled.\n",
4971 px->id, strack->id);
4972 return 1;
4973 }
4974
4975 for (loop = strack->track; loop && loop != srv; loop = loop->track)
4976 ;
4977
4978 if (srv == strack || loop) {
4979 ha_alert("unable to track %s/%s as it "
4980 "belongs to a tracking chain looping back to %s/%s.\n",
4981 px->id, strack->id, px->id,
4982 srv == strack ? strack->id : loop->id);
4983 return 1;
4984 }
4985
4986 if (curproxy != px &&
4987 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
4988 ha_alert("unable to use %s/%s for"
4989 "tracking: disable-on-404 option inconsistency.\n",
4990 px->id, strack->id);
4991 return 1;
4992 }
4993
4994 srv->track = strack;
4995 srv->tracknext = strack->trackers;
4996 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02004997 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02004998
4999 ha_free(&srv->trackit);
5000
5001 return 0;
5002}
5003
Emeric Brun64cc49c2017-10-03 14:46:45 +02005004/*
5005 * This function applies server's status changes, it is
5006 * is designed to be called asynchronously.
5007 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005008 * Must be called with the server lock held. This may also be called at init
5009 * time as the result of parsing the state file, in which case no lock will be
5010 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005011 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005012static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005013{
5014 struct check *check = &s->check;
5015 int xferred;
5016 struct proxy *px = s->proxy;
5017 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5018 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5019 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005020 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005021
Emeric Brun64cc49c2017-10-03 14:46:45 +02005022 /* If currently main is not set we try to apply pending state changes */
5023 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5024 int next_admin;
5025
5026 /* Backup next admin */
5027 next_admin = s->next_admin;
5028
5029 /* restore current admin state */
5030 s->next_admin = s->cur_admin;
5031
5032 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5033 s->last_change = now.tv_sec;
5034 if (s->proxy->lbprm.set_server_status_down)
5035 s->proxy->lbprm.set_server_status_down(s);
5036
5037 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5038 srv_shutdown_streams(s, SF_ERR_DOWN);
5039
5040 /* we might have streams queued on this server and waiting for
5041 * a connection. Those which are redispatchable will be queued
5042 * to another server or to the proxy itself.
5043 */
5044 xferred = pendconn_redistribute(s);
5045
5046 tmptrash = alloc_trash_chunk();
5047 if (tmptrash) {
5048 chunk_printf(tmptrash,
5049 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5050 s->proxy->id, s->id);
5051
Emeric Brun5a133512017-10-19 14:42:30 +02005052 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005053 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005054
5055 /* we don't send an alert if the server was previously paused */
5056 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005057 send_log(s->proxy, log_level, "%s.\n",
5058 tmptrash->area);
5059 send_email_alert(s, log_level, "%s",
5060 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005061 free_trash_chunk(tmptrash);
5062 tmptrash = NULL;
5063 }
5064 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5065 set_backend_down(s->proxy);
5066
5067 s->counters.down_trans++;
5068 }
5069 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5070 s->last_change = now.tv_sec;
5071 if (s->proxy->lbprm.set_server_status_down)
5072 s->proxy->lbprm.set_server_status_down(s);
5073
5074 /* we might have streams queued on this server and waiting for
5075 * a connection. Those which are redispatchable will be queued
5076 * to another server or to the proxy itself.
5077 */
5078 xferred = pendconn_redistribute(s);
5079
5080 tmptrash = alloc_trash_chunk();
5081 if (tmptrash) {
5082 chunk_printf(tmptrash,
5083 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5084 s->proxy->id, s->id);
5085
Emeric Brun5a133512017-10-19 14:42:30 +02005086 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005087
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005088 ha_warning("%s.\n", tmptrash->area);
5089 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5090 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005091 free_trash_chunk(tmptrash);
5092 tmptrash = NULL;
5093 }
5094
5095 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5096 set_backend_down(s->proxy);
5097 }
5098 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5099 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5100 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5101 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5102 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5103 s->proxy->last_change = now.tv_sec;
5104 }
5105
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01005106 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02005107 s->down_time += now.tv_sec - s->last_change;
5108
5109 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005110 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005111 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5112
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005113 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005114 /* now propagate the status change to any LB algorithms */
5115 if (px->lbprm.update_server_eweight)
5116 px->lbprm.update_server_eweight(s);
5117 else if (srv_willbe_usable(s)) {
5118 if (px->lbprm.set_server_status_up)
5119 px->lbprm.set_server_status_up(s);
5120 }
5121 else {
5122 if (px->lbprm.set_server_status_down)
5123 px->lbprm.set_server_status_down(s);
5124 }
5125
5126 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5127 * and it's not a backup server and its effective weight is > 0,
5128 * then it can accept new connections, so we shut down all streams
5129 * on all backup servers.
5130 */
5131 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5132 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5133 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5134
5135 /* check if we can handle some connections queued at the proxy. We
5136 * will take as many as we can handle.
5137 */
5138 xferred = pendconn_grab_from_px(s);
5139
5140 tmptrash = alloc_trash_chunk();
5141 if (tmptrash) {
5142 chunk_printf(tmptrash,
5143 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5144 s->proxy->id, s->id);
5145
Emeric Brun5a133512017-10-19 14:42:30 +02005146 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005147 ha_warning("%s.\n", tmptrash->area);
5148 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5149 tmptrash->area);
5150 send_email_alert(s, LOG_NOTICE, "%s",
5151 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005152 free_trash_chunk(tmptrash);
5153 tmptrash = NULL;
5154 }
5155
5156 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5157 set_backend_down(s->proxy);
5158 }
5159 else if (s->cur_eweight != s->next_eweight) {
5160 /* now propagate the status change to any LB algorithms */
5161 if (px->lbprm.update_server_eweight)
5162 px->lbprm.update_server_eweight(s);
5163 else if (srv_willbe_usable(s)) {
5164 if (px->lbprm.set_server_status_up)
5165 px->lbprm.set_server_status_up(s);
5166 }
5167 else {
5168 if (px->lbprm.set_server_status_down)
5169 px->lbprm.set_server_status_down(s);
5170 }
5171
5172 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5173 set_backend_down(s->proxy);
5174 }
5175
5176 s->next_admin = next_admin;
5177 }
5178
Emeric Brun5a133512017-10-19 14:42:30 +02005179 /* reset operational state change */
5180 *s->op_st_chg.reason = 0;
5181 s->op_st_chg.status = s->op_st_chg.code = -1;
5182 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005183
5184 /* Now we try to apply pending admin changes */
5185
5186 /* Maintenance must also disable health checks */
5187 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5188 if (s->check.state & CHK_ST_ENABLED) {
5189 s->check.state |= CHK_ST_PAUSED;
5190 check->health = 0;
5191 }
5192
5193 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005194 tmptrash = alloc_trash_chunk();
5195 if (tmptrash) {
5196 chunk_printf(tmptrash,
5197 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5198 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5199 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005200
Olivier Houchard796a2b32017-10-24 17:42:47 +02005201 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005202
Olivier Houchard796a2b32017-10-24 17:42:47 +02005203 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005204 ha_warning("%s.\n", tmptrash->area);
5205 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5206 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005207 }
5208 free_trash_chunk(tmptrash);
5209 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005210 }
Emeric Brun8f298292017-12-06 16:47:17 +01005211 /* commit new admin status */
5212
5213 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005214 }
5215 else { /* server was still running */
5216 check->health = 0; /* failure */
5217 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005218
5219 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005220 if (s->proxy->lbprm.set_server_status_down)
5221 s->proxy->lbprm.set_server_status_down(s);
5222
Emeric Brun64cc49c2017-10-03 14:46:45 +02005223 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5224 srv_shutdown_streams(s, SF_ERR_DOWN);
5225
William Dauchy6318d332020-05-02 21:52:36 +02005226 /* force connection cleanup on the given server */
5227 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005228 /* we might have streams queued on this server and waiting for
5229 * a connection. Those which are redispatchable will be queued
5230 * to another server or to the proxy itself.
5231 */
5232 xferred = pendconn_redistribute(s);
5233
5234 tmptrash = alloc_trash_chunk();
5235 if (tmptrash) {
5236 chunk_printf(tmptrash,
5237 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5238 s->flags & SRV_F_BACKUP ? "Backup " : "",
5239 s->proxy->id, s->id,
5240 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5241
5242 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5243
5244 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005245 ha_warning("%s.\n", tmptrash->area);
5246 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5247 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005248 }
5249 free_trash_chunk(tmptrash);
5250 tmptrash = NULL;
5251 }
5252 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5253 set_backend_down(s->proxy);
5254
5255 s->counters.down_trans++;
5256 }
5257 }
5258 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5259 /* OK here we're leaving maintenance, we have many things to check,
5260 * because the server might possibly be coming back up depending on
5261 * its state. In practice, leaving maintenance means that we should
5262 * immediately turn to UP (more or less the slowstart) under the
5263 * following conditions :
5264 * - server is neither checked nor tracked
5265 * - server tracks another server which is not checked
5266 * - server tracks another server which is already up
5267 * Which sums up as something simpler :
5268 * "either the tracking server is up or the server's checks are disabled
5269 * or up". Otherwise we only re-enable health checks. There's a special
5270 * case associated to the stopping state which can be inherited. Note
5271 * that the server might still be in drain mode, which is naturally dealt
5272 * with by the lower level functions.
5273 */
5274
5275 if (s->check.state & CHK_ST_ENABLED) {
5276 s->check.state &= ~CHK_ST_PAUSED;
5277 check->health = check->rise; /* start OK but check immediately */
5278 }
5279
5280 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5281 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5282 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5283 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5284 s->next_state = SRV_ST_STOPPING;
5285 }
5286 else {
Willy Tarreaud332f132021-08-04 19:35:13 +02005287 s->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005288 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005289 if (s->slowstart > 0) {
5290 if (s->warmup)
5291 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5292 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005293 else
5294 s->next_state = SRV_ST_RUNNING;
5295 }
5296
5297 }
5298
5299 tmptrash = alloc_trash_chunk();
5300 if (tmptrash) {
5301 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5302 chunk_printf(tmptrash,
5303 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5304 s->flags & SRV_F_BACKUP ? "Backup " : "",
5305 s->proxy->id, s->id,
5306 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5307 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5308 }
5309 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5310 chunk_printf(tmptrash,
5311 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5312 s->flags & SRV_F_BACKUP ? "Backup " : "",
5313 s->proxy->id, s->id, s->hostname,
5314 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5315 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5316 }
5317 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5318 chunk_printf(tmptrash,
5319 "%sServer %s/%s is %s/%s (leaving maintenance)",
5320 s->flags & SRV_F_BACKUP ? "Backup " : "",
5321 s->proxy->id, s->id,
5322 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5323 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5324 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005325 ha_warning("%s.\n", tmptrash->area);
5326 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5327 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005328 free_trash_chunk(tmptrash);
5329 tmptrash = NULL;
5330 }
5331
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005332 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005333 /* now propagate the status change to any LB algorithms */
5334 if (px->lbprm.update_server_eweight)
5335 px->lbprm.update_server_eweight(s);
5336 else if (srv_willbe_usable(s)) {
5337 if (px->lbprm.set_server_status_up)
5338 px->lbprm.set_server_status_up(s);
5339 }
5340 else {
5341 if (px->lbprm.set_server_status_down)
5342 px->lbprm.set_server_status_down(s);
5343 }
5344
5345 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5346 set_backend_down(s->proxy);
Willy Tarreaud332f132021-08-04 19:35:13 +02005347 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act))
5348 s->proxy->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005349
Willy Tarreau6a78e612018-08-07 10:14:53 +02005350 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5351 * and it's not a backup server and its effective weight is > 0,
5352 * then it can accept new connections, so we shut down all streams
5353 * on all backup servers.
5354 */
5355 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5356 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5357 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5358
5359 /* check if we can handle some connections queued at the proxy. We
5360 * will take as many as we can handle.
5361 */
5362 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005363 }
5364 else if (s->next_admin & SRV_ADMF_MAINT) {
5365 /* remaining in maintenance mode, let's inform precisely about the
5366 * situation.
5367 */
5368 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5369 tmptrash = alloc_trash_chunk();
5370 if (tmptrash) {
5371 chunk_printf(tmptrash,
5372 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5373 s->flags & SRV_F_BACKUP ? "Backup " : "",
5374 s->proxy->id, s->id);
5375
5376 if (s->track) /* normally it's mandatory here */
5377 chunk_appendf(tmptrash, " via %s/%s",
5378 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005379 ha_warning("%s.\n", tmptrash->area);
5380 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5381 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005382 free_trash_chunk(tmptrash);
5383 tmptrash = NULL;
5384 }
5385 }
5386 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5387 tmptrash = alloc_trash_chunk();
5388 if (tmptrash) {
5389 chunk_printf(tmptrash,
5390 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5391 s->flags & SRV_F_BACKUP ? "Backup " : "",
5392 s->proxy->id, s->id, s->hostname);
5393
5394 if (s->track) /* normally it's mandatory here */
5395 chunk_appendf(tmptrash, " via %s/%s",
5396 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005397 ha_warning("%s.\n", tmptrash->area);
5398 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5399 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005400 free_trash_chunk(tmptrash);
5401 tmptrash = NULL;
5402 }
5403 }
5404 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5405 tmptrash = alloc_trash_chunk();
5406 if (tmptrash) {
5407 chunk_printf(tmptrash,
5408 "%sServer %s/%s remains in forced maintenance",
5409 s->flags & SRV_F_BACKUP ? "Backup " : "",
5410 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005411 ha_warning("%s.\n", tmptrash->area);
5412 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5413 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005414 free_trash_chunk(tmptrash);
5415 tmptrash = NULL;
5416 }
5417 }
5418 /* don't report anything when leaving drain mode and remaining in maintenance */
5419
5420 s->cur_admin = s->next_admin;
5421 }
5422
5423 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5424 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5425 /* drain state is applied only if not yet in maint */
5426
5427 s->last_change = now.tv_sec;
5428 if (px->lbprm.set_server_status_down)
5429 px->lbprm.set_server_status_down(s);
5430
5431 /* we might have streams queued on this server and waiting for
5432 * a connection. Those which are redispatchable will be queued
5433 * to another server or to the proxy itself.
5434 */
5435 xferred = pendconn_redistribute(s);
5436
5437 tmptrash = alloc_trash_chunk();
5438 if (tmptrash) {
5439 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5440 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5441 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5442
5443 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5444
5445 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005446 ha_warning("%s.\n", tmptrash->area);
5447 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5448 tmptrash->area);
5449 send_email_alert(s, LOG_NOTICE, "%s",
5450 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005451 }
5452 free_trash_chunk(tmptrash);
5453 tmptrash = NULL;
5454 }
5455
5456 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5457 set_backend_down(s->proxy);
5458 }
5459 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5460 /* OK completely leaving drain mode */
5461 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5462 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5463 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5464 s->proxy->last_change = now.tv_sec;
5465 }
5466
5467 if (s->last_change < now.tv_sec) // ignore negative times
5468 s->down_time += now.tv_sec - s->last_change;
5469 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005470 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005471
5472 tmptrash = alloc_trash_chunk();
5473 if (tmptrash) {
5474 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5475 chunk_printf(tmptrash,
5476 "%sServer %s/%s is %s (leaving forced drain)",
5477 s->flags & SRV_F_BACKUP ? "Backup " : "",
5478 s->proxy->id, s->id,
5479 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5480 }
5481 else {
5482 chunk_printf(tmptrash,
5483 "%sServer %s/%s is %s (leaving drain)",
5484 s->flags & SRV_F_BACKUP ? "Backup " : "",
5485 s->proxy->id, s->id,
5486 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5487 if (s->track) /* normally it's mandatory here */
5488 chunk_appendf(tmptrash, " via %s/%s",
5489 s->track->proxy->id, s->track->id);
5490 }
5491
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005492 ha_warning("%s.\n", tmptrash->area);
5493 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5494 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005495 free_trash_chunk(tmptrash);
5496 tmptrash = NULL;
5497 }
5498
5499 /* now propagate the status change to any LB algorithms */
5500 if (px->lbprm.update_server_eweight)
5501 px->lbprm.update_server_eweight(s);
5502 else if (srv_willbe_usable(s)) {
5503 if (px->lbprm.set_server_status_up)
5504 px->lbprm.set_server_status_up(s);
5505 }
5506 else {
5507 if (px->lbprm.set_server_status_down)
5508 px->lbprm.set_server_status_down(s);
5509 }
5510 }
5511 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5512 /* remaining in drain mode after removing one of its flags */
5513
5514 tmptrash = alloc_trash_chunk();
5515 if (tmptrash) {
5516 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5517 chunk_printf(tmptrash,
5518 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5519 s->flags & SRV_F_BACKUP ? "Backup " : "",
5520 s->proxy->id, s->id);
5521
5522 if (s->track) /* normally it's mandatory here */
5523 chunk_appendf(tmptrash, " via %s/%s",
5524 s->track->proxy->id, s->track->id);
5525 }
5526 else {
5527 chunk_printf(tmptrash,
5528 "%sServer %s/%s remains in forced drain mode",
5529 s->flags & SRV_F_BACKUP ? "Backup " : "",
5530 s->proxy->id, s->id);
5531 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005532 ha_warning("%s.\n", tmptrash->area);
5533 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5534 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005535 free_trash_chunk(tmptrash);
5536 tmptrash = NULL;
5537 }
5538
5539 /* commit new admin status */
5540
5541 s->cur_admin = s->next_admin;
5542 }
5543 }
5544
5545 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005546 *s->adm_st_chg_cause = 0;
5547}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005548
Willy Tarreau144f84a2021-03-02 16:09:26 +01005549struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005550{
5551 struct connection *conn;
5552
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005553 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005554 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005555 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005556 }
5557
5558 return task;
5559}
5560
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005561/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005562 * moving them all.
5563 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005564 *
5565 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005566 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005567static 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 +02005568{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005569 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005570 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005571 int i = 0;
5572
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005573 node = eb_first(idle_tree);
5574 while (node) {
5575 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005576 if (toremove_nb != -1 && i >= toremove_nb)
5577 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005578
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005579 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005580 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005581 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005582 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005583
5584 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005585 }
5586 return i;
5587}
William Dauchy6318d332020-05-02 21:52:36 +02005588/* cleanup connections for a given server
5589 * might be useful when going on forced maintenance or live changing ip/port
5590 */
William Dauchy707ad322020-05-04 13:52:40 +02005591static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005592{
William Dauchy6318d332020-05-02 21:52:36 +02005593 int did_remove;
5594 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005595
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005596 /* nothing to do if pool-max-conn is null */
5597 if (!srv->max_idle_conns)
5598 return;
5599
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005600 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005601 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005602 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005603 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005604 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 +02005605 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005606 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 +02005607 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005608 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005609 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005610 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005611
5612 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5613 break;
William Dauchy6318d332020-05-02 21:52:36 +02005614 }
William Dauchy6318d332020-05-02 21:52:36 +02005615}
5616
Willy Tarreau144f84a2021-03-02 16:09:26 +01005617struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005618{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005619 struct server *srv;
5620 struct eb32_node *eb;
5621 int i;
5622 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005623
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005624 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005625 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5626 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005627 int exceed_conns;
5628 int to_kill;
5629 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005630
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005631 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5632 if (!eb) {
5633 /* we might have reached the end of the tree, typically because
5634 * <now_ms> is in the first half and we're first scanning the last
5635 * half. Let's loop back to the beginning of the tree now.
5636 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005637
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005638 eb = eb32_first(&idle_conn_srv);
5639 if (likely(!eb))
5640 break;
5641 }
5642 if (tick_is_lt(now_ms, eb->key)) {
5643 /* timer not expired yet, revisit it later */
5644 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005645 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005646 }
5647 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005648
5649 /* Calculate how many idle connections we want to kill :
5650 * we want to remove half the difference between the total
5651 * of established connections (used or idle) and the max
5652 * number of used connections.
5653 */
5654 curr_idle = srv->curr_idle_conns;
5655 if (curr_idle == 0)
5656 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005657 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005658 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005659
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005660 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005661 if (srv->est_need_conns < srv->max_used_conns)
5662 srv->est_need_conns = srv->max_used_conns;
5663
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005664 srv->max_used_conns = srv->curr_used_conns;
5665
Willy Tarreau18ed7892020-07-02 19:05:30 +02005666 if (exceed_conns <= 0)
5667 goto remove;
5668
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005669 /* check all threads starting with ours */
5670 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005671 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005672 int j;
5673 int did_remove = 0;
5674
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005675 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5676 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005677
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005678 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005679 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 +02005680 if (j > 0)
5681 did_remove = 1;
5682 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005683 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 +01005684 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005685 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005686
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005687 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005688 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005689
5690 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5691 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005692 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005693remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005694 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005695
5696 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005697 /* There are still more idle connections, add the
5698 * server back in the tree.
5699 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005700 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005701 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005702 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005703 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005704 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005705 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5706
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005707 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005708 return task;
5709}
Olivier Houchard88698d92019-04-16 19:07:22 +02005710
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02005711/* Close remaining idle connections. This functions is designed to be run on
5712 * process shutdown. This guarantees a proper socket shutdown to avoid
5713 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
5714 * bypassed.
5715 *
5716 * This function is not thread-safe so it must only be called via a global
5717 * deinit function.
5718 */
5719static void srv_close_idle_conns(struct server *srv)
5720{
5721 struct eb_root **cleaned_tree;
5722 int i;
5723
5724 for (i = 0; i < global.nbthread; ++i) {
5725 struct eb_root *conn_trees[] = {
5726 &srv->per_thr[i].idle_conns,
5727 &srv->per_thr[i].safe_conns,
5728 &srv->per_thr[i].avail_conns,
5729 NULL
5730 };
5731
5732 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
5733 while (!eb_is_empty(*cleaned_tree)) {
5734 struct ebmb_node *node = ebmb_first(*cleaned_tree);
5735 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
5736 struct connection *conn = conn_hash_node->conn;
5737
5738 if (conn->ctrl->ctrl_close)
5739 conn->ctrl->ctrl_close(conn);
5740 ebmb_delete(node);
5741 }
5742 }
5743 }
5744}
5745
5746REGISTER_SERVER_DEINIT(srv_close_idle_conns);
5747
Willy Tarreau76cc6992020-07-01 18:49:24 +02005748/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
5749static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01005750 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02005751 char **err)
5752{
5753 if (too_many_args(1, args, err, NULL))
5754 return -1;
5755
5756 if (strcmp(args[1], "on") == 0)
5757 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
5758 else if (strcmp(args[1], "off") == 0)
5759 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
5760 else {
5761 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
5762 return -1;
5763 }
5764 return 0;
5765}
5766
Olivier Houchard88698d92019-04-16 19:07:22 +02005767/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5768static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01005769 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02005770 char **err)
5771{
5772 int arg = -1;
5773
5774 if (too_many_args(1, args, err, NULL))
5775 return -1;
5776
5777 if (*(args[1]) != 0)
5778 arg = atoi(args[1]);
5779
5780 if (arg < 0 || arg > 100) {
5781 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5782 return -1;
5783 }
5784
5785 if (args[0][10] == 'h')
5786 global.tune.pool_high_ratio = arg;
5787 else
5788 global.tune.pool_low_ratio = arg;
5789 return 0;
5790}
5791
5792/* config keyword parsers */
5793static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02005794 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02005795 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5796 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5797 { 0, NULL, NULL }
5798}};
5799
5800INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5801
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005802/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005803 * Local variables:
5804 * c-indent-level: 8
5805 * c-basic-offset: 8
5806 * End:
5807 */