blob: 5d6e4b1389cde4a882407d447595d66758c7713b [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 Denoyellecd8a6f22021-09-21 11:51:54 +02001676 { "slowstart", srv_parse_slowstart, 1, 1, 1 }, /* 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écaille759ea982017-03-30 17:32:36 +02001826static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1827 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001828{
1829 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001830 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001831 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001832 NULL,
1833 };
1834
1835 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001836 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001837
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001838 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001839}
1840
1841static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1842{
1843 struct sample_expr *expr;
1844
1845 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 +01001846 if (!expr) {
1847 memprintf(err, "error detected while parsing sni expression : %s", *err);
1848 return ERR_ALERT | ERR_FATAL;
1849 }
1850
1851 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1852 memprintf(err, "error detected while parsing sni expression : "
1853 " fetch method '%s' extracts information from '%s', "
Amaury Denoyellec008a632021-05-28 11:01:22 +02001854 "none of which is available here.",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001855 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001856 return ERR_ALERT | ERR_FATAL;
1857 }
1858
1859 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1860 release_sample_expr(newsrv->ssl_ctx.sni);
1861 newsrv->ssl_ctx.sni = expr;
1862
1863 return 0;
1864}
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001865
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001866static 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 +01001867{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001868 char *msg = "error encountered while processing ";
1869 char *quote = "'";
1870 char *token = args[cur_arg];
1871
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001872 if (err && *err) {
1873 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001874 msg = *err;
1875 quote = "";
1876 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001877 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001878
1879 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02001880 ha_warning("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001881 else
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02001882 ha_alert("%s%s%s%s.\n", msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001883}
1884
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001885static void srv_conn_src_sport_range_cpy(struct server *srv,
1886 struct server *src)
1887{
1888 int range_sz;
1889
1890 range_sz = src->conn_src.sport_range->size;
1891 if (range_sz > 0) {
1892 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1893 if (srv->conn_src.sport_range != NULL) {
1894 int i;
1895
1896 for (i = 0; i < range_sz; i++) {
1897 srv->conn_src.sport_range->ports[i] =
1898 src->conn_src.sport_range->ports[i];
1899 }
1900 }
1901 }
1902}
1903
1904/*
1905 * Copy <src> server connection source settings to <srv> server everything needed.
1906 */
1907static void srv_conn_src_cpy(struct server *srv, struct server *src)
1908{
1909 srv->conn_src.opts = src->conn_src.opts;
1910 srv->conn_src.source_addr = src->conn_src.source_addr;
1911
1912 /* Source port range copy. */
1913 if (src->conn_src.sport_range != NULL)
1914 srv_conn_src_sport_range_cpy(srv, src);
1915
1916#ifdef CONFIG_HAP_TRANSPARENT
1917 if (src->conn_src.bind_hdr_name != NULL) {
1918 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1919 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1920 }
1921 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1922 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1923#endif
1924 if (src->conn_src.iface_name != NULL)
1925 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1926}
1927
1928/*
1929 * Copy <src> server SSL settings to <srv> server allocating
1930 * everything needed.
1931 */
1932#if defined(USE_OPENSSL)
1933static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1934{
1935 if (src->ssl_ctx.ca_file != NULL)
1936 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1937 if (src->ssl_ctx.crl_file != NULL)
1938 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001939
1940 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1941
Remi Tricot-Le Breton0498fa42021-07-13 18:28:22 +02001942 srv->ssl_ctx.ctx = src->ssl_ctx.ctx;
1943
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001944 if (src->ssl_ctx.verify_host != NULL)
1945 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1946 if (src->ssl_ctx.ciphers != NULL)
1947 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001948 if (src->ssl_ctx.options)
1949 srv->ssl_ctx.options = src->ssl_ctx.options;
1950 if (src->ssl_ctx.methods.flags)
1951 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1952 if (src->ssl_ctx.methods.min)
1953 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1954 if (src->ssl_ctx.methods.max)
1955 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1956
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001957 if (src->ssl_ctx.ciphersuites != NULL)
1958 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001959 if (src->sni_expr != NULL)
1960 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001961
Olivier Houchardc7566002018-11-20 23:33:50 +01001962 if (src->ssl_ctx.alpn_str) {
1963 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1964 if (srv->ssl_ctx.alpn_str) {
1965 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1966 src->ssl_ctx.alpn_len);
1967 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1968 }
1969 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02001970
Olivier Houchardc7566002018-11-20 23:33:50 +01001971 if (src->ssl_ctx.npn_str) {
1972 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1973 if (srv->ssl_ctx.npn_str) {
1974 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1975 src->ssl_ctx.npn_len);
1976 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1977 }
1978 }
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001979}
1980#endif
1981
1982/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001983 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001984 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001985 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001986 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01001987int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001988{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001989 char *hostname_dn;
1990 int hostname_len, hostname_dn_len;
1991
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001992 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001993 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001994
Christopher Faulet67957bd2017-09-27 11:00:59 +02001995 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001996 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01001997 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
1998 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02001999 if (hostname_dn_len == -1)
2000 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02002001
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002002
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 free(srv->hostname);
2004 free(srv->hostname_dn);
2005 srv->hostname = strdup(hostname);
2006 srv->hostname_dn = strdup(hostname_dn);
2007 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002008 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002009 goto err;
2010
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002011 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002012
2013 err:
Willy Tarreau61cfdf42021-02-20 10:46:51 +01002014 ha_free(&srv->hostname);
2015 ha_free(&srv->hostname_dn);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002016 return -1;
2017}
2018
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002019/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002020 * Copy <src> server settings to <srv> server allocating
2021 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002022 * This function is not supposed to be called at any time, but only
2023 * during server settings parsing or during server allocations from
2024 * a server template, and just after having calloc()'ed a new server.
2025 * So, <src> may only be a default server (when parsing server settings)
2026 * or a server template (during server allocations from a server template).
2027 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
2028 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002029 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002030static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002031{
2032 /* Connection source settings copy */
2033 srv_conn_src_cpy(srv, src);
2034
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002035 if (srv_tmpl) {
2036 srv->addr = src->addr;
2037 srv->svc_port = src->svc_port;
2038 }
2039
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002040 srv->pp_opts = src->pp_opts;
2041 if (src->rdr_pfx != NULL) {
2042 srv->rdr_pfx = strdup(src->rdr_pfx);
2043 srv->rdr_len = src->rdr_len;
2044 }
2045 if (src->cookie != NULL) {
2046 srv->cookie = strdup(src->cookie);
2047 srv->cklen = src->cklen;
2048 }
2049 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01002050 srv->check.addr = src->check.addr;
2051 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002052 srv->check.use_ssl = src->check.use_ssl;
2053 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01002054 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01002055 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05002056 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002057 /* Note: 'flags' field has potentially been already initialized. */
2058 srv->flags |= src->flags;
2059 srv->do_check = src->do_check;
2060 srv->do_agent = src->do_agent;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002061 srv->check.inter = src->check.inter;
2062 srv->check.fastinter = src->check.fastinter;
2063 srv->check.downinter = src->check.downinter;
2064 srv->agent.use_ssl = src->agent.use_ssl;
2065 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02002066
2067 if (src->agent.tcpcheck_rules) {
2068 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
2069 if (srv->agent.tcpcheck_rules) {
2070 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
2071 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
2072 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
2073 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
2074 &src->agent.tcpcheck_rules->preset_vars);
2075 }
2076 }
2077
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002078 srv->agent.inter = src->agent.inter;
2079 srv->agent.fastinter = src->agent.fastinter;
2080 srv->agent.downinter = src->agent.downinter;
2081 srv->maxqueue = src->maxqueue;
2082 srv->minconn = src->minconn;
2083 srv->maxconn = src->maxconn;
2084 srv->slowstart = src->slowstart;
2085 srv->observe = src->observe;
2086 srv->onerror = src->onerror;
2087 srv->onmarkeddown = src->onmarkeddown;
2088 srv->onmarkedup = src->onmarkedup;
2089 if (src->trackit != NULL)
2090 srv->trackit = strdup(src->trackit);
2091 srv->consecutive_errors_limit = src->consecutive_errors_limit;
2092 srv->uweight = srv->iweight = src->iweight;
2093
2094 srv->check.send_proxy = src->check.send_proxy;
2095 /* health: up, but will fall down at first failure */
2096 srv->check.rise = srv->check.health = src->check.rise;
2097 srv->check.fall = src->check.fall;
2098
2099 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02002100 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
2101 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
2102 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002103 srv->check.state |= CHK_ST_PAUSED;
2104 srv->check.health = 0;
2105 }
2106
2107 /* health: up but will fall down at first failure */
2108 srv->agent.rise = srv->agent.health = src->agent.rise;
2109 srv->agent.fall = src->agent.fall;
2110
2111 if (src->resolvers_id != NULL)
2112 srv->resolvers_id = strdup(src->resolvers_id);
Emeric Brun21fbeed2020-12-23 18:01:04 +01002113 srv->resolv_opts.family_prio = src->resolv_opts.family_prio;
2114 srv->resolv_opts.accept_duplicate_ip = src->resolv_opts.accept_duplicate_ip;
2115 srv->resolv_opts.ignore_weight = src->resolv_opts.ignore_weight;
2116 if (srv->resolv_opts.family_prio == AF_UNSPEC)
2117 srv->resolv_opts.family_prio = AF_INET6;
2118 memcpy(srv->resolv_opts.pref_net,
2119 src->resolv_opts.pref_net,
2120 sizeof srv->resolv_opts.pref_net);
2121 srv->resolv_opts.pref_net_nb = src->resolv_opts.pref_net_nb;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002122
2123 srv->init_addr_methods = src->init_addr_methods;
2124 srv->init_addr = src->init_addr;
2125#if defined(USE_OPENSSL)
2126 srv_ssl_settings_cpy(srv, src);
2127#endif
2128#ifdef TCP_USER_TIMEOUT
2129 srv->tcp_ut = src->tcp_ut;
2130#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002131 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01002132 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02002133 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01002134 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01002135 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02002136
Olivier Houchard8da5f982017-08-04 18:35:36 +02002137 if (srv_tmpl)
2138 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08002139
2140 srv->check.via_socks4 = src->check.via_socks4;
2141 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002142}
2143
Willy Tarreau198e92a2021-03-05 10:23:32 +01002144/* allocate a server and attach it to the global servers_list. Returns
2145 * the server on success, otherwise NULL.
2146 */
William Lallemand313bfd12018-10-26 14:47:32 +02002147struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002148{
2149 struct server *srv;
2150
2151 srv = calloc(1, sizeof *srv);
2152 if (!srv)
2153 return NULL;
2154
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002155 srv_take(srv);
2156
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002157 srv->obj_type = OBJ_TYPE_SERVER;
2158 srv->proxy = proxy;
Willy Tarreaucdc83e02021-06-23 16:11:02 +02002159 queue_init(&srv->queue, proxy, srv);
Willy Tarreau2b718102021-04-21 07:32:39 +02002160 LIST_APPEND(&servers_list, &srv->global_list);
Emeric Brun34067662021-06-11 10:48:45 +02002161 LIST_INIT(&srv->srv_rec_item);
Emeric Brunbd78c912021-06-11 10:08:05 +02002162 LIST_INIT(&srv->ip_rec_item);
Christopher Faulet40a007c2017-07-03 15:41:01 +02002163
Emeric Brun52a91d32017-08-31 14:41:55 +02002164 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002165 srv->last_change = now.tv_sec;
2166
Christopher Faulet38290462020-04-21 11:46:40 +02002167 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002168 srv->check.status = HCHK_STATUS_INI;
2169 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01002170 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02002171 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002172
Christopher Faulet38290462020-04-21 11:46:40 +02002173 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002174 srv->agent.status = HCHK_STATUS_INI;
2175 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01002176 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002177 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
Frédéric Lécaillef46c10c2020-11-23 14:29:28 +01002178#if defined(USE_QUIC)
2179 srv->cids = EB_ROOT_UNIQUE;
2180#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002181
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002182 srv->extra_counters = NULL;
William Lallemand3ce6eed2021-02-08 10:43:44 +01002183#ifdef USE_OPENSSL
2184 HA_RWLOCK_INIT(&srv->ssl_ctx.lock);
2185#endif
Amaury Denoyelle7f8f6cb2020-11-10 14:24:31 +01002186
Willy Tarreau975b1552019-06-06 16:25:55 +02002187 /* please don't put default server settings here, they are set in
Willy Tarreau144289b2021-02-12 08:19:01 +01002188 * proxy_preset_defaults().
Willy Tarreau975b1552019-06-06 16:25:55 +02002189 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002190 return srv;
2191}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002192
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002193/* Increment the server refcount. */
2194void srv_take(struct server *srv)
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002195{
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002196 HA_ATOMIC_INC(&srv->refcount);
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002197}
2198
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002199/* Deallocate a server <srv> and its member. <srv> must be allocated. For
2200 * dynamic servers, its refcount is decremented first. The free operations are
2201 * conducted only if the refcount is nul, unless the process is stopping.
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002202 *
2203 * As a convenience, <srv.next> is returned if srv is not NULL. It may be useful
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002204 * when calling srv_drop on the list of servers.
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002205 */
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002206struct server *srv_drop(struct server *srv)
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002207{
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002208 struct server *next = NULL;
2209
William Lallemand4c395fc2021-08-20 10:10:15 +02002210 if (!srv)
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002211 goto end;
2212
2213 next = srv->next;
William Lallemand4c395fc2021-08-20 10:10:15 +02002214
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002215 /* For dynamic servers, decrement the reference counter. Only free the
2216 * server when reaching zero.
2217 */
Amaury Denoyelle13f2e2c2021-08-09 15:08:54 +02002218 if (likely(!(global.mode & MODE_STOPPING))) {
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02002219 if (HA_ATOMIC_SUB_FETCH(&srv->refcount, 1))
2220 goto end;
Amaury Denoyelled6b70802021-08-02 15:50:00 +02002221 }
2222
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002223 task_destroy(srv->warmup);
Christopher Fauletdcac4182021-06-15 16:17:17 +02002224 task_destroy(srv->srvrq_check);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002225
2226 free(srv->id);
2227 free(srv->cookie);
2228 free(srv->hostname);
2229 free(srv->hostname_dn);
2230 free((char*)srv->conf.file);
2231 free(srv->per_thr);
2232 free(srv->curr_idle_thr);
2233 free(srv->resolvers_id);
2234 free(srv->addr_node.key);
Amaury Denoyellefb247942021-04-20 16:48:22 +02002235 free(srv->lb_nodes);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002236
2237 if (srv->use_ssl == 1 || srv->check.use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL)) {
2238 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->destroy_srv)
2239 xprt_get(XPRT_SSL)->destroy_srv(srv);
2240 }
2241 HA_SPIN_DESTROY(&srv->lock);
2242
Willy Tarreau2b718102021-04-21 07:32:39 +02002243 LIST_DELETE(&srv->global_list);
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002244
2245 EXTRA_COUNTERS_FREE(srv->extra_counters);
2246
2247 free(srv);
2248 srv = NULL;
Amaury Denoyellef5c1e122021-08-25 15:03:46 +02002249
2250 end:
2251 return next;
Amaury Denoyelle828adf02021-03-16 17:20:15 +01002252}
2253
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02002254/* Remove a server <srv> from a tracking list if <srv> is tracking another
2255 * server. No special care is taken if <srv> is tracked itself by another one :
2256 * this situation should be avoided by the caller.
2257 *
2258 * Not thread-safe.
2259 */
2260static void release_server_track(struct server *srv)
2261{
2262 struct server *strack = srv->track;
2263 struct server **base;
2264
2265 if (!strack)
2266 return;
2267
2268 for (base = &strack->trackers; *base; base = &((*base)->tracknext)) {
2269 if (*base == srv) {
2270 *base = srv->tracknext;
2271 return;
2272 }
2273 }
2274
2275 /* srv not found on the tracking list, this should never happen */
2276 BUG_ON(!*base);
2277}
2278
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002279/*
2280 * Parse as much as possible such a range string argument: low[-high]
2281 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2282 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2283 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2284 * Returns 0 if succeeded, -1 if not.
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002285 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002286static int _srv_parse_tmpl_range(struct server *srv, const char *arg,
2287 int *nb_low, int *nb_high)
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002288{
2289 char *nb_high_arg;
2290
2291 *nb_high = 0;
2292 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002293 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002294
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002295 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002296 *nb_high_arg++ = '\0';
2297 *nb_high = atoi(nb_high_arg);
2298 }
2299 else {
2300 *nb_high += *nb_low;
2301 *nb_low = 1;
2302 }
2303
2304 if (*nb_low < 0 || *nb_high < *nb_low)
2305 return -1;
2306
2307 return 0;
2308}
2309
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002310/* Parse as much as possible such a range string argument: low[-high]
2311 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
2312 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
2313 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002314 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002315 * initialize a new server on startup.
2316 *
2317 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
2318 * Returns 0 if succeeded, -1 if not.
2319 */
2320static inline void _srv_parse_set_id_from_prefix(struct server *srv,
2321 const char *prefix, int nb)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002322{
2323 chunk_printf(&trash, "%s%d", prefix, nb);
2324 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002325 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002326}
2327
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002328/* Initialize as much as possible servers from <srv> server template.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002329 * Note that a server template is a special server with
2330 * a few different parameters than a server which has
2331 * been parsed mostly the same way as a server.
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002332 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002333 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002334 * initialize a new server on startup.
2335 *
Joseph Herlant44466822018-11-15 08:57:51 -08002336 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002337 * 'srv' template included.
2338 */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002339static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002340{
2341 int i;
2342 struct server *newsrv;
2343
2344 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 +02002345 newsrv = new_server(px);
2346 if (!newsrv)
2347 goto err;
2348
Christopher Faulet75bef002020-11-02 22:04:55 +01002349 newsrv->conf.file = strdup(srv->conf.file);
2350 newsrv->conf.line = srv->conf.line;
2351
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002352 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002353 srv_prepare_for_resolution(newsrv, srv->hostname);
Willy Tarreau80527bc2021-10-06 14:48:37 +02002354
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002355 if (newsrv->sni_expr) {
2356 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2357 if (!newsrv->ssl_ctx.sni)
2358 goto err;
2359 }
Willy Tarreau80527bc2021-10-06 14:48:37 +02002360
Emeric Brun34067662021-06-11 10:48:45 +02002361 /* append to list of servers available to receive an hostname */
Emeric Bruncaef19e2021-06-14 10:02:18 +02002362 if (newsrv->srvrq)
2363 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun34067662021-06-11 10:48:45 +02002364
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002365 /* Set this new server ID. */
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002366 _srv_parse_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002367
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002368 /* Linked backwards first. This will be restablished after parsing. */
2369 newsrv->next = px->srv;
2370 px->srv = newsrv;
2371 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002372 _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 +02002373
2374 return i - srv->tmpl_info.nb_low;
2375
2376 err:
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002377 _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 +02002378 if (newsrv) {
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002379 release_sample_expr(newsrv->ssl_ctx.sni);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002380 free_check(&newsrv->agent);
2381 free_check(&newsrv->check);
Willy Tarreau2b718102021-04-21 07:32:39 +02002382 LIST_DELETE(&newsrv->global_list);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002383 }
2384 free(newsrv);
2385 return i - srv->tmpl_info.nb_low;
2386}
2387
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002388/* Allocate a new server pointed by <srv> and try to parse the first arguments
2389 * in <args> as an address for a server or an address-range for a template or
2390 * nothing for a default-server. <cur_arg> is incremented to the next argument.
2391 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002392 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002393 * initialize a new server on startup.
2394 *
2395 * A mask of errors is returned. On a parsing error, ERR_FATAL is set. In case
2396 * of memory exhaustion, ERR_ABORT is set. If the server cannot be allocated,
2397 * <srv> will be set to NULL.
2398 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002399static int _srv_parse_init(struct server **srv, char **args, int *cur_arg,
2400 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002401 int parse_flags)
Willy Tarreau272adea2014-03-31 10:39:59 +02002402{
2403 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002404 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002405 int err_code = 0;
Willy Tarreau07101d52015-09-08 16:16:35 +02002406 char *fqdn = NULL;
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002407 int tmpl_range_low = 0, tmpl_range_high = 0;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002408 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002409
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002410 *srv = NULL;
2411
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002412 /* There is no mandatory first arguments for default server. */
2413 if (parse_flags & SRV_PARSE_PARSE_ADDR) {
2414 if (parse_flags & SRV_PARSE_TEMPLATE) {
2415 if (!*args[3]) {
2416 /* 'server-template' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002417 ha_alert("'%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
2418 args[0]);
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002419 err_code |= ERR_ALERT | ERR_FATAL;
2420 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002421 }
2422
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002423 err = invalid_prefix_char(args[1]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002424 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002425 else {
2426 if (!*args[2]) {
2427 /* 'server' line number of argument check. */
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002428 ha_alert("'%s' expects <name> and <addr>[:<port>] as arguments.\n",
2429 args[0]);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002430 err_code |= ERR_ALERT | ERR_FATAL;
2431 goto out;
2432 }
2433
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002434 err = invalid_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002435 }
2436
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002437 if (err) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002438 ha_alert("character '%c' is not permitted in %s %s '%s'.\n",
2439 *err, args[0], !(parse_flags & SRV_PARSE_TEMPLATE) ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002440 err_code |= ERR_ALERT | ERR_FATAL;
2441 goto out;
2442 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002443 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002444
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002445 *cur_arg = 2;
2446 if (parse_flags & SRV_PARSE_TEMPLATE) {
2447 /* Parse server-template <nb | range> arg. */
2448 if (_srv_parse_tmpl_range(newsrv, args[*cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002449 ha_alert("Wrong %s number or range arg '%s'.\n",
2450 args[0], args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002451 err_code |= ERR_ALERT | ERR_FATAL;
2452 goto out;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002453 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002454 (*cur_arg)++;
2455 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002456
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002457 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2458 struct sockaddr_storage *sk;
2459 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002460
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002461 *srv = newsrv = new_server(curproxy);
2462 if (!newsrv) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002463 ha_alert("out of memory.\n");
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002464 err_code |= ERR_ALERT | ERR_ABORT;
2465 goto out;
2466 }
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002467 register_parsing_obj(&newsrv->obj_type);
Willy Tarreau272adea2014-03-31 10:39:59 +02002468
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002469 if (parse_flags & SRV_PARSE_TEMPLATE) {
2470 newsrv->tmpl_info.nb_low = tmpl_range_low;
2471 newsrv->tmpl_info.nb_high = tmpl_range_high;
2472 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002473
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002474 if (parse_flags & SRV_PARSE_DYNAMIC)
2475 newsrv->flags |= SRV_F_DYNAMIC;
2476
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002477 /* Note: for a server template, its id is its prefix.
2478 * This is a temporary id which will be used for server allocations to come
2479 * after parsing.
2480 */
2481 if (!(parse_flags & SRV_PARSE_TEMPLATE))
2482 newsrv->id = strdup(args[1]);
2483 else
2484 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002485
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002486 /* several ways to check the port component :
2487 * - IP => port=+0, relative (IPv4 only)
2488 * - IP: => port=+0, relative
2489 * - IP:N => port=N, absolute
2490 * - IP:+N => port=+N, relative
2491 * - IP:-N => port=-N, relative
2492 */
2493 if (!(parse_flags & SRV_PARSE_PARSE_ADDR))
2494 goto skip_addr;
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002495
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002496 sk = str2sa_range(args[*cur_arg], &port, &port1, &port2, NULL, NULL,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002497 &errmsg, NULL, &fqdn,
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002498 (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);
2499 if (!sk) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002500 ha_alert("%s\n", errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002501 err_code |= ERR_ALERT | ERR_FATAL;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002502 ha_free(&errmsg);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002503 goto out;
2504 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002505
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002506 if (!port1 || !port2) {
2507 /* no port specified, +offset, -offset */
2508 newsrv->flags |= SRV_F_MAPPORTS;
2509 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002510
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002511 /* save hostname and create associated name resolution */
2512 if (fqdn) {
2513 if (fqdn[0] == '_') { /* SRV record */
2514 /* Check if a SRV request already exists, and if not, create it */
2515 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2516 newsrv->srvrq = new_resolv_srvrq(newsrv, fqdn);
2517 if (newsrv->srvrq == NULL) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002518 err_code |= ERR_ALERT | ERR_FATAL;
2519 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002520 }
Christopher Faulet81ba74a2021-06-29 20:52:35 +02002521 LIST_APPEND(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002522 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002523 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002524 ha_alert("Can't create DNS resolution for server '%s'\n",
2525 newsrv->id);
Willy Tarreau272adea2014-03-31 10:39:59 +02002526 err_code |= ERR_ALERT | ERR_FATAL;
2527 goto out;
2528 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002529 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002530
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002531 newsrv->addr = *sk;
2532 newsrv->svc_port = port;
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02002533 /*
2534 * we don't need to lock the server here, because
2535 * we are in the process of initializing.
2536 *
2537 * Note that the server is not attached into the proxy tree if
2538 * this is a dynamic server.
2539 */
2540 srv_set_addr_desc(newsrv, !(parse_flags & SRV_PARSE_DYNAMIC));
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002541
2542 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002543 ha_alert("Unknown protocol family %d '%s'\n",
2544 newsrv->addr.ss_family, args[*cur_arg]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002545 err_code |= ERR_ALERT | ERR_FATAL;
2546 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002547 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002548
2549 (*cur_arg)++;
2550 skip_addr:
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002551 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2552 /* Copy default server settings to new server */
2553 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
2554 } else {
2555 /* Initialize dynamic server weight to 1 */
2556 newsrv->uweight = newsrv->iweight = 1;
2557
2558 /* A dynamic server is disabled on startup */
2559 newsrv->next_admin = SRV_ADMF_FMAINT;
2560 newsrv->next_state = SRV_ST_STOPPED;
2561 server_recalc_eweight(newsrv, 0);
Amaury Denoyellefca18172021-07-22 16:03:36 +02002562
2563 /* Set default values for checks */
2564 newsrv->check.inter = DEF_CHKINTR;
2565 newsrv->check.rise = DEF_RISETIME;
2566 newsrv->check.fall = DEF_FALLTIME;
2567
2568 newsrv->agent.inter = DEF_CHKINTR;
2569 newsrv->agent.rise = DEF_AGENT_RISETIME;
2570 newsrv->agent.fall = DEF_AGENT_FALLTIME;
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002571 }
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002572 HA_SPIN_INIT(&newsrv->lock);
2573 }
2574 else {
2575 *srv = newsrv = &curproxy->defsrv;
2576 *cur_arg = 1;
2577 newsrv->resolv_opts.family_prio = AF_INET6;
2578 newsrv->resolv_opts.accept_duplicate_ip = 0;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002579 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002580
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002581 free(fqdn);
2582 return 0;
Willy Tarreau9faebe32019-06-07 19:00:37 +02002583
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002584out:
2585 free(fqdn);
2586 return err_code;
2587}
Willy Tarreau272adea2014-03-31 10:39:59 +02002588
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002589/* Parse the server keyword in <args>.
2590 * <cur_arg> is incremented beyond the keyword optional value. Note that this
2591 * might not be the case if an error is reported.
2592 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002593 * This function is first intended to be used through parse_server to
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002594 * initialize a new server on startup.
2595 *
2596 * A mask of errors is returned. ERR_FATAL is set if the parsing should be
2597 * interrupted.
2598 */
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002599static int _srv_parse_kw(struct server *srv, char **args, int *cur_arg,
2600 struct proxy *curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002601 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002602{
2603 int err_code = 0;
2604 struct srv_kw *kw;
2605 const char *best;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002606 char *errmsg = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002607
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002608 kw = srv_find_kw(args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002609 if (!kw) {
2610 best = srv_find_best_kw(args[*cur_arg]);
2611 if (best)
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002612 ha_alert("unknown keyword '%s'; did you mean '%s' maybe ?\n",
2613 args[*cur_arg], best);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002614 else
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002615 ha_alert("unknown keyword '%s'.\n", args[*cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002616
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002617 return ERR_ALERT | ERR_FATAL;
2618 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002619
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002620 if (!kw->parse) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002621 ha_alert("'%s' option is not implemented in this version (check build options)\n",
2622 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002623 err_code = ERR_ALERT | ERR_FATAL;
2624 goto out;
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002625 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002626
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002627 if ((parse_flags & SRV_PARSE_DEFAULT_SERVER) && !kw->default_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002628 ha_alert("'%s' option is not accepted in default-server sections\n",
2629 args[*cur_arg]);
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002630 err_code = ERR_ALERT;
2631 goto out;
2632 }
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002633 else if ((parse_flags & SRV_PARSE_DYNAMIC) && !kw->dynamic_ok) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002634 ha_alert("'%s' option is not accepted for dynamic server\n",
2635 args[*cur_arg]);
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002636 err_code |= ERR_ALERT;
2637 goto out;
2638 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002639
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002640 err_code = kw->parse(args, cur_arg, curproxy, srv, &errmsg);
2641 if (err_code) {
2642 display_parser_err(NULL, 0, args, *cur_arg, err_code, &errmsg);
2643 free(errmsg);
2644 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002645
2646out:
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002647 if (kw->skip != -1)
2648 *cur_arg += 1 + kw->skip;
2649
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002650 return err_code;
2651}
2652
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002653/* This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002654 * initialize a new server on startup.
2655 */
2656static int _srv_parse_sni_expr_init(char **args, int cur_arg,
2657 struct server *srv, struct proxy *proxy,
2658 char **errmsg)
2659{
2660 int ret;
2661
2662 if (!srv->sni_expr)
2663 return 0;
2664
2665 ret = server_parse_sni_expr(srv, proxy, errmsg);
2666 if (!ret)
2667 return 0;
2668
2669 return ret;
2670}
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002671
2672/* Server initializations finalization.
2673 * Initialize health check, agent check and SNI expression if enabled.
2674 * Must not be called for a default server instance.
2675 *
Ilya Shipitsinba13f162021-03-19 22:21:44 +05002676 * This function is first intended to be used through parse_server to
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002677 * initialize a new server on startup.
2678 */
2679static int _srv_parse_finalize(char **args, int cur_arg,
2680 struct server *srv, struct proxy *px,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002681 int parse_flags)
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002682{
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002683 int ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002684 char *errmsg = NULL;
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002685
2686 if (srv->do_check && srv->trackit) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002687 ha_alert("unable to enable checks and tracking at the same time!\n");
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002688 return ERR_ALERT | ERR_FATAL;
2689 }
2690
2691 if (srv->do_agent && !srv->agent.port) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002692 ha_alert("server %s does not have agent port. Agent check has been disabled.\n",
2693 srv->id);
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002694 return ERR_ALERT | ERR_FATAL;
2695 }
2696
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002697 if ((ret = _srv_parse_sni_expr_init(args, cur_arg, srv, px, &errmsg)) != 0) {
2698 if (errmsg) {
2699 ha_alert("%s\n", errmsg);
2700 free(errmsg);
2701 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002702 return ret;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002703 }
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002704
Amaury Denoyelle76e10e72021-03-08 17:08:01 +01002705 /* A dynamic server is disabled on startup. It must not be counted as
2706 * an active backend entry.
2707 */
2708 if (!(parse_flags & SRV_PARSE_DYNAMIC)) {
2709 if (srv->flags & SRV_F_BACKUP)
2710 px->srv_bck++;
2711 else
2712 px->srv_act++;
2713 }
2714
Amaury Denoyelle7d27efe2021-03-17 14:25:39 +01002715 srv_lb_commit_status(srv);
2716
2717 return 0;
2718}
2719
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002720int parse_server(const char *file, int linenum, char **args,
2721 struct proxy *curproxy, const struct proxy *defproxy,
2722 int parse_flags)
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002723{
2724 struct server *newsrv = NULL;
2725 int err_code = 0;
2726
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002727 int cur_arg;
Willy Tarreau272adea2014-03-31 10:39:59 +02002728
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002729 set_usermsgs_ctx(file, linenum, NULL);
2730
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002731 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER) && curproxy == defproxy) {
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002732 ha_alert("'%s' not allowed in 'defaults' section.\n", args[0]);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002733 err_code |= ERR_ALERT | ERR_FATAL;
2734 goto out;
2735 }
2736 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
2737 err_code |= ERR_ALERT | ERR_FATAL;
2738 goto out;
2739 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002740
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002741 if ((parse_flags & (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) ==
2742 (SRV_PARSE_IN_PEER_SECTION|SRV_PARSE_PARSE_ADDR)) {
2743 if (!*args[2])
2744 return 0;
2745 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002746
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002747 err_code = _srv_parse_init(&newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002748 parse_flags);
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002749
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002750 /* the servers are linked backwards first */
2751 if (newsrv && !(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
2752 newsrv->next = curproxy->srv;
2753 curproxy->srv = newsrv;
2754 }
Amaury Denoyellea8f442e2021-03-08 10:29:33 +01002755
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002756 if (err_code & ERR_CODE)
2757 goto out;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002758
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002759 newsrv->conf.file = strdup(file);
2760 newsrv->conf.line = linenum;
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002761
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002762 while (*args[cur_arg]) {
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002763 err_code = _srv_parse_kw(newsrv, args, &cur_arg, curproxy,
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002764 parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002765 if (err_code & ERR_FATAL)
2766 goto out;
2767 }
Amaury Denoyelle9394a942021-03-08 11:20:52 +01002768
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002769 if (!(parse_flags & SRV_PARSE_DEFAULT_SERVER)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02002770 err_code |= _srv_parse_finalize(args, cur_arg, newsrv, curproxy, parse_flags);
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002771 if (err_code & ERR_FATAL)
2772 goto out;
Willy Tarreau272adea2014-03-31 10:39:59 +02002773 }
Amaury Denoyellecf58dd72021-03-08 16:35:54 +01002774
Amaury Denoyelle30c05372021-03-08 16:36:46 +01002775 if (parse_flags & SRV_PARSE_TEMPLATE)
2776 _srv_parse_tmpl_init(newsrv, curproxy);
2777
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02002778 /* If the server id is fixed, insert it in the proxy used_id tree.
2779 * This is needed to detect a later duplicate id via srv_parse_id.
2780 *
2781 * If no is specified, a dynamic one is generated in
2782 * check_config_validity.
2783 */
2784 if (newsrv->flags & SRV_F_FORCED_ID)
2785 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
2786
Amaury Denoyelle24abb0c2021-05-07 15:13:51 +02002787 HA_DIAG_WARNING_COND((curproxy->cap & PR_CAP_LB) && !newsrv->uweight,
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002788 "configured with weight of 0 will never be selected by load balancing algorithms\n");
Amaury Denoyelleda0e7f62021-03-30 10:26:27 +02002789
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002790 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002791 return 0;
2792
2793 out:
Amaury Denoyelle0fc136c2021-05-28 11:00:18 +02002794 reset_usermsgs_ctx();
Willy Tarreau272adea2014-03-31 10:39:59 +02002795 return err_code;
2796}
2797
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002798/* Returns a pointer to the first server matching either id <id>.
2799 * NULL is returned if no match is found.
2800 * the lookup is performed in the backend <bk>
2801 */
2802struct server *server_find_by_id(struct proxy *bk, int id)
2803{
2804 struct eb32_node *eb32;
2805 struct server *curserver;
2806
2807 if (!bk || (id ==0))
2808 return NULL;
2809
2810 /* <bk> has no backend capabilities, so it can't have a server */
2811 if (!(bk->cap & PR_CAP_BE))
2812 return NULL;
2813
2814 curserver = NULL;
2815
2816 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2817 if (eb32)
2818 curserver = container_of(eb32, struct server, conf.id);
2819
2820 return curserver;
2821}
2822
2823/* Returns a pointer to the first server matching either name <name>, or id
2824 * if <name> starts with a '#'. NULL is returned if no match is found.
2825 * the lookup is performed in the backend <bk>
2826 */
2827struct server *server_find_by_name(struct proxy *bk, const char *name)
2828{
2829 struct server *curserver;
2830
2831 if (!bk || !name)
2832 return NULL;
2833
2834 /* <bk> has no backend capabilities, so it can't have a server */
2835 if (!(bk->cap & PR_CAP_BE))
2836 return NULL;
2837
2838 curserver = NULL;
2839 if (*name == '#') {
2840 curserver = server_find_by_id(bk, atoi(name + 1));
2841 if (curserver)
2842 return curserver;
2843 }
2844 else {
2845 curserver = bk->srv;
2846
2847 while (curserver && (strcmp(curserver->id, name) != 0))
2848 curserver = curserver->next;
2849
2850 if (curserver)
2851 return curserver;
2852 }
2853
2854 return NULL;
2855}
2856
2857struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2858{
2859 struct server *byname;
2860 struct server *byid;
2861
2862 if (!name && !id)
2863 return NULL;
2864
2865 if (diff)
2866 *diff = 0;
2867
2868 byname = byid = NULL;
2869
2870 if (name) {
2871 byname = server_find_by_name(bk, name);
2872 if (byname && (!id || byname->puid == id))
2873 return byname;
2874 }
2875
2876 /* remaining possibilities :
2877 * - name not set
2878 * - name set but not found
2879 * - name found but ID doesn't match
2880 */
2881 if (id) {
2882 byid = server_find_by_id(bk, id);
2883 if (byid) {
2884 if (byname) {
2885 /* use id only if forced by configuration */
2886 if (byid->flags & SRV_F_FORCED_ID) {
2887 if (diff)
2888 *diff |= 2;
2889 return byid;
2890 }
2891 else {
2892 if (diff)
2893 *diff |= 1;
2894 return byname;
2895 }
2896 }
2897
2898 /* remaining possibilities:
2899 * - name not set
2900 * - name set but not found
2901 */
2902 if (name && diff)
2903 *diff |= 2;
2904 return byid;
2905 }
2906
2907 /* id bot found */
2908 if (byname) {
2909 if (diff)
2910 *diff |= 1;
2911 return byname;
2912 }
2913 }
2914
2915 return NULL;
2916}
2917
Simon Horman7d09b9a2013-02-12 10:45:51 +09002918/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02002919 * update a server's current IP address.
2920 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
2921 * ip is in network format.
2922 * updater is a string which contains an information about the requester of the update.
2923 * updater is used if not NULL.
2924 *
2925 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02002926 *
2927 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02002928 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01002929int srv_update_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02002930{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02002931 /* save the new IP family & address if necessary */
2932 switch (ip_sin_family) {
2933 case AF_INET:
2934 if (s->addr.ss_family == ip_sin_family &&
2935 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
2936 return 0;
2937 break;
2938 case AF_INET6:
2939 if (s->addr.ss_family == ip_sin_family &&
2940 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
2941 return 0;
2942 break;
2943 };
2944
Baptiste Assmann14e40142015-04-14 01:13:07 +02002945 /* generates a log line and a warning on stderr */
2946 if (1) {
2947 /* book enough space for both IPv4 and IPv6 */
2948 char oldip[INET6_ADDRSTRLEN];
2949 char newip[INET6_ADDRSTRLEN];
2950
2951 memset(oldip, '\0', INET6_ADDRSTRLEN);
2952 memset(newip, '\0', INET6_ADDRSTRLEN);
2953
2954 /* copy old IP address in a string */
2955 switch (s->addr.ss_family) {
2956 case AF_INET:
2957 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
2958 break;
2959 case AF_INET6:
2960 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
2961 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02002962 default:
2963 strcpy(oldip, "(none)");
2964 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02002965 };
2966
2967 /* copy new IP address in a string */
2968 switch (ip_sin_family) {
2969 case AF_INET:
2970 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
2971 break;
2972 case AF_INET6:
2973 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
2974 break;
2975 };
2976
2977 /* save log line into a buffer */
2978 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
2979 s->proxy->id, s->id, oldip, newip, updater);
2980
2981 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002982 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002983
2984 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002985 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002986 }
2987
2988 /* save the new IP family */
2989 s->addr.ss_family = ip_sin_family;
2990 /* save the new IP address */
2991 switch (ip_sin_family) {
2992 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02002993 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02002994 break;
2995 case AF_INET6:
2996 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
2997 break;
2998 };
Olivier Houchard4e694042017-03-14 20:01:29 +01002999 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003000 srv_set_addr_desc(s, 1);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003001
3002 return 0;
3003}
3004
William Dauchy7cabc062021-02-11 22:51:24 +01003005/* update agent health check address and port
3006 * addr can be ip4/ip6 or a hostname
3007 * if one error occurs, don't apply anything
3008 * must be called with the server lock held.
3009 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003010const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port)
William Dauchy7cabc062021-02-11 22:51:24 +01003011{
3012 struct sockaddr_storage sk;
3013 struct buffer *msg;
3014 int new_port;
3015
3016 msg = get_trash_chunk();
3017 chunk_reset(msg);
3018
3019 if (!(s->agent.state & CHK_ST_ENABLED)) {
3020 chunk_strcat(msg, "agent checks are not enabled on this server");
3021 goto out;
3022 }
3023 if (addr) {
3024 memset(&sk, 0, sizeof(struct sockaddr_storage));
3025 if (str2ip(addr, &sk) == NULL) {
3026 chunk_appendf(msg, "invalid addr '%s'", addr);
3027 goto out;
3028 }
3029 }
3030 if (port) {
3031 if (strl2irc(port, strlen(port), &new_port) != 0) {
3032 chunk_appendf(msg, "provided port is not an integer");
3033 goto out;
3034 }
3035 if (new_port < 0 || new_port > 65535) {
3036 chunk_appendf(msg, "provided port is invalid");
3037 goto out;
3038 }
3039 }
3040out:
3041 if (msg->data)
3042 return msg->area;
3043 else {
3044 if (addr)
3045 set_srv_agent_addr(s, &sk);
3046 if (port)
3047 set_srv_agent_port(s, new_port);
3048 }
3049 return NULL;
3050}
3051
William Dauchyb456e1f2021-02-11 22:51:23 +01003052/* update server health check address and port
3053 * addr must be ip4 or ip6, it won't be resolved
3054 * if one error occurs, don't apply anything
3055 * must be called with the server lock held.
3056 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003057const char *srv_update_check_addr_port(struct server *s, const char *addr, const char *port)
William Dauchyb456e1f2021-02-11 22:51:23 +01003058{
3059 struct sockaddr_storage sk;
3060 struct buffer *msg;
3061 int new_port;
3062
3063 msg = get_trash_chunk();
3064 chunk_reset(msg);
3065
3066 if (!(s->check.state & CHK_ST_ENABLED)) {
3067 chunk_strcat(msg, "health checks are not enabled on this server");
3068 goto out;
3069 }
3070 if (addr) {
3071 memset(&sk, 0, sizeof(struct sockaddr_storage));
3072 if (str2ip2(addr, &sk, 0) == NULL) {
3073 chunk_appendf(msg, "invalid addr '%s'", addr);
3074 goto out;
3075 }
3076 }
3077 if (port) {
3078 if (strl2irc(port, strlen(port), &new_port) != 0) {
3079 chunk_appendf(msg, "provided port is not an integer");
3080 goto out;
3081 }
3082 if (new_port < 0 || new_port > 65535) {
3083 chunk_appendf(msg, "provided port is invalid");
3084 goto out;
3085 }
3086 /* prevent the update of port to 0 if MAPPORTS are in use */
3087 if ((s->flags & SRV_F_MAPPORTS) && new_port == 0) {
3088 chunk_appendf(msg, "can't unset 'port' since MAPPORTS is in use");
3089 goto out;
3090 }
3091 }
3092out:
3093 if (msg->data)
3094 return msg->area;
3095 else {
3096 if (addr)
3097 s->check.addr = sk;
3098 if (port)
3099 s->check.port = new_port;
3100 }
3101 return NULL;
3102}
3103
Baptiste Assmann14e40142015-04-14 01:13:07 +02003104/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003105 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3106 *
3107 * Caller can pass its name through <updater> to get it integrated in the response message
3108 * returned by the function.
3109 *
3110 * The function first does the following, in that order:
3111 * - validates the new addr and/or port
3112 * - checks if an update is required (new IP or port is different than current ones)
3113 * - checks the update is allowed:
3114 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3115 * - allow all changes if no CHECKS are configured
3116 * - if CHECK is configured:
3117 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3118 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3119 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003120 *
3121 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003122 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003123const char *srv_update_addr_port(struct server *s, const char *addr, const char *port, char *updater)
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003124{
3125 struct sockaddr_storage sa;
3126 int ret, port_change_required;
3127 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003128 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003129 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003130 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003131
3132 msg = get_trash_chunk();
3133 chunk_reset(msg);
3134
3135 if (addr) {
3136 memset(&sa, 0, sizeof(struct sockaddr_storage));
3137 if (str2ip2(addr, &sa, 0) == NULL) {
3138 chunk_printf(msg, "Invalid addr '%s'", addr);
3139 goto out;
3140 }
3141
3142 /* changes are allowed on AF_INET* families only */
3143 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3144 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3145 goto out;
3146 }
3147
3148 /* collecting data currently setup */
3149 memset(current_addr, '\0', sizeof(current_addr));
3150 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3151 /* changes are allowed on AF_INET* families only */
3152 if ((ret != AF_INET) && (ret != AF_INET6)) {
3153 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3154 goto out;
3155 }
3156
3157 /* applying ADDR changes if required and allowed
3158 * ipcmp returns 0 when both ADDR are the same
3159 */
3160 if (ipcmp(&s->addr, &sa) == 0) {
3161 chunk_appendf(msg, "no need to change the addr");
3162 goto port;
3163 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003164 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003165 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003166
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003167 /* update report for caller */
3168 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3169 }
3170
3171 port:
3172 if (port) {
3173 char sign = '\0';
3174 char *endptr;
3175
3176 if (addr)
3177 chunk_appendf(msg, ", ");
3178
3179 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003180 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003181
3182 /* check if PORT change is required */
3183 port_change_required = 0;
3184
3185 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003186 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003187 new_port = strtol(port, &endptr, 10);
3188 if ((errno != 0) || (port == endptr)) {
3189 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3190 goto out;
3191 }
3192
3193 /* check if caller triggers a port mapped or offset */
3194 if (sign == '-' || (sign == '+')) {
3195 /* check if server currently uses port map */
3196 if (!(s->flags & SRV_F_MAPPORTS)) {
3197 /* switch from fixed port to port map mandatorily triggers
3198 * a port change */
3199 port_change_required = 1;
3200 /* check is configured
3201 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3202 * prevent PORT change if check doesn't have it's dedicated port while switching
3203 * to port mapping */
William Dauchy69f118d2021-02-03 22:30:07 +01003204 if (!s->check.port) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003205 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.");
3206 goto out;
3207 }
3208 }
3209 /* we're already using port maps */
3210 else {
3211 port_change_required = current_port != new_port;
3212 }
3213 }
3214 /* fixed port */
3215 else {
3216 port_change_required = current_port != new_port;
3217 }
3218
3219 /* applying PORT changes if required and update response message */
3220 if (port_change_required) {
3221 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003222 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003223 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003224
3225 /* prepare message */
3226 chunk_appendf(msg, "port changed from '");
3227 if (s->flags & SRV_F_MAPPORTS)
3228 chunk_appendf(msg, "+");
3229 chunk_appendf(msg, "%d' to '", current_port);
3230
3231 if (sign == '-') {
3232 s->flags |= SRV_F_MAPPORTS;
3233 chunk_appendf(msg, "%c", sign);
3234 /* just use for result output */
3235 new_port = -new_port;
3236 }
3237 else if (sign == '+') {
3238 s->flags |= SRV_F_MAPPORTS;
3239 chunk_appendf(msg, "%c", sign);
3240 }
3241 else {
3242 s->flags &= ~SRV_F_MAPPORTS;
3243 }
3244
3245 chunk_appendf(msg, "%d'", new_port);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003246 }
3247 else {
3248 chunk_appendf(msg, "no need to change the port");
3249 }
3250 }
3251
3252out:
William Dauchy6318d332020-05-02 21:52:36 +02003253 if (changed) {
3254 /* force connection cleanup on the given server */
3255 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003256 srv_set_dyncookie(s);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003257 srv_set_addr_desc(s, 1);
William Dauchy6318d332020-05-02 21:52:36 +02003258 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003259 if (updater)
3260 chunk_appendf(msg, " by '%s'", updater);
3261 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003262 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003263}
3264
Christopher Faulet5efdef22021-03-11 18:03:21 +01003265/*
3266 * update server status based on result of SRV resolution
3267 * returns:
3268 * 0 if server status is updated
3269 * 1 if server status has not changed
3270 *
3271 * Must be called with the server lock held.
3272 */
3273int srvrq_update_srv_status(struct server *s, int has_no_ip)
3274{
3275 if (!s->srvrq)
3276 return 1;
3277
3278 /* since this server has an IP, it can go back in production */
3279 if (has_no_ip == 0) {
3280 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3281 return 1;
3282 }
3283
3284 if (s->next_admin & SRV_ADMF_RMAINT)
3285 return 1;
3286
3287 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3288 return 0;
3289}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003290
3291/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003292 * update server status based on result of name resolution
3293 * returns:
3294 * 0 if server status is updated
3295 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003296 *
3297 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003298 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003299int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003300{
Emeric Brun750fe792020-12-23 16:51:12 +01003301 struct resolvers *resolvers = s->resolvers;
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003302 struct resolv_resolution *resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003303 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003304
Jerome Magnin012261a2020-07-28 13:38:22 +02003305 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet5efdef22021-03-11 18:03:21 +01003306 if (resolution == NULL)
3307 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003308
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003309 switch (resolution->status) {
3310 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003311 /* status when HAProxy has just (re)started.
3312 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003313 break;
3314
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003315 case RSLV_STATUS_VALID:
3316 /*
3317 * resume health checks
3318 * server will be turned back on if health check is safe
3319 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003320 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003321 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003322 return 1;
3323 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3324 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003325 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003326 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003327
Emeric Brun52a91d32017-08-31 14:41:55 +02003328 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003329 return 1;
3330 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3331 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3332 s->proxy->id, s->id);
3333
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003334 ha_warning("%s.\n", trash.area);
3335 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003336 return 0;
3337
3338 case RSLV_STATUS_NX:
3339 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003340 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3341 if (!tick_is_expired(exp, now_ms))
3342 break;
3343
3344 if (s->next_admin & SRV_ADMF_RMAINT)
3345 return 1;
3346 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3347 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003348
3349 case RSLV_STATUS_TIMEOUT:
3350 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003351 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3352 if (!tick_is_expired(exp, now_ms))
3353 break;
3354
3355 if (s->next_admin & SRV_ADMF_RMAINT)
3356 return 1;
3357 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3358 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003359
3360 case RSLV_STATUS_REFUSED:
3361 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003362 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3363 if (!tick_is_expired(exp, now_ms))
3364 break;
3365
3366 if (s->next_admin & SRV_ADMF_RMAINT)
3367 return 1;
3368 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3369 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003370
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003371 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003372 /* stop server if resolution failed for a long enough period */
3373 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3374 if (!tick_is_expired(exp, now_ms))
3375 break;
3376
3377 if (s->next_admin & SRV_ADMF_RMAINT)
3378 return 1;
3379 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3380 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003381 }
3382
3383 return 1;
3384}
3385
3386/*
3387 * Server Name Resolution valid response callback
3388 * It expects:
3389 * - <nameserver>: the name server which answered the valid response
3390 * - <response>: buffer containing a valid DNS response
3391 * - <response_len>: size of <response>
3392 * It performs the following actions:
3393 * - ignore response if current ip found and server family not met
3394 * - update with first new ip found if family is met and current IP is not found
3395 * returns:
3396 * 0 on error
3397 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003398 *
3399 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003400 */
Emeric Brun08622d32020-12-23 17:41:43 +01003401int snr_resolution_cb(struct resolv_requester *requester, struct dns_counters *counters)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003402{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003403 struct server *s = NULL;
Emeric Brun08622d32020-12-23 17:41:43 +01003404 struct resolv_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003405 void *serverip, *firstip;
3406 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003407 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003408 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003409 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003410
Christopher Faulet67957bd2017-09-27 11:00:59 +02003411 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003412 if (!s)
3413 return 1;
3414
Christopher Faulet49531e82021-03-10 15:07:27 +01003415 if (s->srvrq) {
Emeric Brun34067662021-06-11 10:48:45 +02003416 /* If DNS resolution is disabled ignore it.
3417 * This is the case if the server was associated to
3418 * a SRV record and this record is now expired.
Christopher Faulet49531e82021-03-10 15:07:27 +01003419 */
Emeric Brun34067662021-06-11 10:48:45 +02003420 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Faulet49531e82021-03-10 15:07:27 +01003421 return 1;
3422 }
3423
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003424 resolution = (s->resolv_requester ? s->resolv_requester->resolution : NULL);
Christopher Faulet49531e82021-03-10 15:07:27 +01003425 if (!resolution)
3426 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003427
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003428 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003429 firstip = NULL; /* pointer to the first valid response found */
3430 /* it will be used as the new IP if a change is required */
3431 firstip_sin_family = AF_UNSPEC;
3432 serverip = NULL; /* current server IP address */
3433
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003434 /* initializing server IP pointer */
3435 server_sin_family = s->addr.ss_family;
3436 switch (server_sin_family) {
3437 case AF_INET:
3438 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3439 break;
3440
3441 case AF_INET6:
3442 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3443 break;
3444
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003445 case AF_UNSPEC:
3446 break;
3447
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003448 default:
3449 goto invalid;
3450 }
3451
Emeric Brund30e9a12020-12-23 18:49:16 +01003452 ret = resolv_get_ip_from_response(&resolution->response, &s->resolv_opts,
3453 serverip, server_sin_family, &firstip,
3454 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003455
3456 switch (ret) {
Emeric Brun456de772020-12-23 18:17:31 +01003457 case RSLV_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003458 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003459
Emeric Brun456de772020-12-23 18:17:31 +01003460 case RSLV_UPD_SRVIP_NOT_FOUND:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003461 goto save_ip;
3462
Emeric Brun456de772020-12-23 18:17:31 +01003463 case RSLV_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003464 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003465 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003466
Emeric Brun456de772020-12-23 18:17:31 +01003467 case RSLV_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003468 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003469 resolution->status = RSLV_STATUS_OTHER;
Christopher Faulet07ecff52021-06-24 15:33:52 +02003470 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003471 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003472
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003473 default:
Christopher Faulet07ecff52021-06-24 15:33:52 +02003474 has_no_ip = 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003475 goto invalid;
3476
3477 }
3478
3479 save_ip:
Emeric Brun50c870e2021-01-04 10:40:46 +01003480 if (counters) {
3481 counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003482 /* save the first ip we found */
Emeric Brun50c870e2021-01-04 10:40:46 +01003483 chunk_printf(chk, "%s/%s", counters->pid, counters->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003484 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003485 else
3486 chunk_printf(chk, "DNS cache");
Christopher Faulet69beaa92021-02-16 12:07:47 +01003487 srv_update_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003488
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003489 update_status:
Christopher Fauleta8ce4972021-06-24 15:26:03 +02003490 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3491 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003492 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003493
3494 invalid:
Emeric Brun50c870e2021-01-04 10:40:46 +01003495 if (counters) {
3496 counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003497 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003498 }
Christopher Faulet07ecff52021-06-24 15:33:52 +02003499 if (!snr_update_srv_status(s, has_no_ip) && has_no_ip)
3500 memset(&s->addr, 0, sizeof(s->addr));
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003501 return 0;
3502}
3503
3504/*
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003505 * SRV record error management callback
3506 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003507 * 0 if we can trash answser items.
3508 * 1 when safely ignored and we must kept answer items
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003509 *
3510 * Grabs the server's lock.
3511 */
3512int srvrq_resolution_error_cb(struct resolv_requester *requester, int error_code)
3513{
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003514 struct resolv_srvrq *srvrq;
3515 struct resolv_resolution *res;
3516 struct resolvers *resolvers;
3517 int exp;
3518
3519 /* SRV records */
3520 srvrq = objt_resolv_srvrq(requester->owner);
3521 if (!srvrq)
Emeric Brun12ca6582021-06-10 15:25:25 +02003522 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003523
3524 resolvers = srvrq->resolvers;
3525 res = requester->resolution;
3526
3527 switch (res->status) {
3528
3529 case RSLV_STATUS_NX:
3530 /* stop server if resolution is NX for a long enough period */
3531 exp = tick_add(res->last_valid, resolvers->hold.nx);
3532 if (!tick_is_expired(exp, now_ms))
3533 return 1;
3534 break;
3535
3536 case RSLV_STATUS_TIMEOUT:
3537 /* stop server if resolution is TIMEOUT for a long enough period */
3538 exp = tick_add(res->last_valid, resolvers->hold.timeout);
3539 if (!tick_is_expired(exp, now_ms))
3540 return 1;
3541 break;
3542
3543 case RSLV_STATUS_REFUSED:
3544 /* stop server if resolution is REFUSED for a long enough period */
3545 exp = tick_add(res->last_valid, resolvers->hold.refused);
3546 if (!tick_is_expired(exp, now_ms))
3547 return 1;
3548 break;
3549
3550 default:
3551 /* stop server if resolution failed for a long enough period */
3552 exp = tick_add(res->last_valid, resolvers->hold.other);
3553 if (!tick_is_expired(exp, now_ms))
3554 return 1;
3555 }
3556
Emeric Brun34067662021-06-11 10:48:45 +02003557 /* Remove any associated server ref */
3558 resolv_detach_from_resolution_answer_items(res, requester, 1);
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003559
Emeric Brun12ca6582021-06-10 15:25:25 +02003560 return 0;
Baptiste Assmannb4badf72020-11-19 22:38:33 +01003561}
3562
3563/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003564 * Server Name Resolution error management callback
3565 * returns:
Emeric Brun12ca6582021-06-10 15:25:25 +02003566 * 0 if we can trash answser items.
3567 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02003568 *
3569 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003570 */
Emeric Brun08622d32020-12-23 17:41:43 +01003571int snr_resolution_error_cb(struct resolv_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003572{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003573 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003574
Christopher Faulet67957bd2017-09-27 11:00:59 +02003575 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003576 if (!s)
Emeric Brun12ca6582021-06-10 15:25:25 +02003577 return 0;
3578
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003579 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003580 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet5130c212021-03-10 20:31:40 +01003581 memset(&s->addr, 0, sizeof(s->addr));
Emeric Brun12ca6582021-06-10 15:25:25 +02003582 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun34067662021-06-11 10:48:45 +02003583 resolv_detach_from_resolution_answer_items(requester->resolution, requester, 1);
Emeric Brun12ca6582021-06-10 15:25:25 +02003584 return 0;
3585 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003586 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun12ca6582021-06-10 15:25:25 +02003587
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003588 return 1;
3589}
3590
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003591/*
3592 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003593 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003594 * It returns a pointer to the first server found or NULL if <ip> is not yet
3595 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003596 *
3597 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003598 */
3599struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3600{
3601 struct server *tmpsrv;
3602 struct proxy *be;
3603
3604 if (!srv)
3605 return NULL;
3606
3607 be = srv->proxy;
3608 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003609 /* we found the current server is the same, ignore it */
3610 if (srv == tmpsrv)
3611 continue;
3612
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003613 /* We want to compare the IP in the record with the IP of the servers in the
3614 * same backend, only if:
3615 * * DNS resolution is enabled on the server
3616 * * the hostname used for the resolution by our server is the same than the
3617 * one used for the server found in the backend
3618 * * the server found in the backend is not our current server
3619 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003620 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003621 if ((tmpsrv->hostname_dn == NULL) ||
3622 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Faulet59b29252021-03-16 11:21:04 +01003623 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003624 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003625 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003626 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003627 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003628
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003629 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003630 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003631 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003632 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003633 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003634
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003635 /* At this point, we have 2 different servers using the same DNS hostname
3636 * for their respective resolution.
3637 */
3638 if (*ip_family == tmpsrv->addr.ss_family &&
3639 ((tmpsrv->addr.ss_family == AF_INET &&
3640 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3641 (tmpsrv->addr.ss_family == AF_INET6 &&
3642 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003643 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003644 return tmpsrv;
3645 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003646 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003647 }
3648
Emeric Brune9fd6b52017-11-02 17:20:39 +01003649
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003650 return NULL;
3651}
3652
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003653/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3654 * resolver. This is suited for initial address configuration. Returns 0 on
3655 * success otherwise a non-zero error code. In case of error, *err_code, if
3656 * not NULL, is filled up.
3657 */
3658int srv_set_addr_via_libc(struct server *srv, int *err_code)
3659{
3660 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003661 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003662 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003663 return 1;
3664 }
3665 return 0;
3666}
3667
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003668/* Set the server's FDQN (->hostname) from <hostname>.
3669 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003670 *
3671 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003672 */
Emeric Brun08622d32020-12-23 17:41:43 +01003673int srv_set_fqdn(struct server *srv, const char *hostname, int resolv_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003674{
Emeric Brun08622d32020-12-23 17:41:43 +01003675 struct resolv_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003676 char *hostname_dn;
3677 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003678
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003679 /* Note that the server lock is already held. */
3680 if (!srv->resolvers)
3681 return -1;
3682
Emeric Brun08622d32020-12-23 17:41:43 +01003683 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003684 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003685 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003686 * and we can't enable it at run time for now.
3687 */
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003688 if (!srv->resolv_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003689 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003690
3691 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003692 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003693 hostname_dn = trash.area;
Emeric Brund30e9a12020-12-23 18:49:16 +01003694 hostname_dn_len = resolv_str_to_dn_label(hostname, hostname_len + 1,
3695 hostname_dn, trash.size);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003696 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003697 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003698
Christopher Fauletd83a6df2021-03-12 10:23:05 +01003699 resolution = (srv->resolv_requester ? srv->resolv_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003700 if (resolution &&
3701 resolution->hostname_dn &&
Christopher Faulet59b29252021-03-16 11:21:04 +01003702 resolution->hostname_dn_len == hostname_dn_len &&
3703 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003704 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003705
Christopher Faulet0efc0992021-03-11 18:09:53 +01003706 resolv_unlink_resolution(srv->resolv_requester, 0);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003708 free(srv->hostname);
3709 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003710 srv->hostname = strdup(hostname);
3711 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003712 srv->hostname_dn_len = hostname_dn_len;
3713 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003714 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003715
Baptiste Assmann13a92322019-06-07 09:40:55 +02003716 if (srv->flags & SRV_F_NO_RESOLUTION)
3717 goto end;
3718
Emeric Brund30e9a12020-12-23 18:49:16 +01003719 if (resolv_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003720 goto err;
3721
3722 end:
Emeric Brun08622d32020-12-23 17:41:43 +01003723 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003724 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003725 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003726
3727 err:
Emeric Brun08622d32020-12-23 17:41:43 +01003728 if (!resolv_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003729 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003730 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003731}
3732
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003733/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3734 * from the state file. This is suited for initial address configuration.
3735 * Returns 0 on success otherwise a non-zero error code. In case of error,
3736 * *err_code, if not NULL, is filled up.
3737 */
3738static int srv_apply_lastaddr(struct server *srv, int *err_code)
3739{
3740 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3741 if (err_code)
3742 *err_code |= ERR_WARN;
3743 return 1;
3744 }
3745 return 0;
3746}
3747
Willy Tarreau25e51522016-11-04 15:10:17 +01003748/* returns 0 if no error, otherwise a combination of ERR_* flags */
3749static int srv_iterate_initaddr(struct server *srv)
3750{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003751 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01003752 int return_code = 0;
3753 int err_code;
3754 unsigned int methods;
3755
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003756 /* If no addr and no hostname set, get the name from the DNS SRV request */
3757 if (!name && srv->srvrq)
3758 name = srv->srvrq->name;
3759
Willy Tarreau25e51522016-11-04 15:10:17 +01003760 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003761 if (!methods) {
3762 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01003763 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3764 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003765 if (srv->resolvers_id) {
3766 /* dns resolution is configured, add "none" to not fail on startup */
3767 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3768 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003769 }
3770
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003771 /* "-dr" : always append "none" so that server addresses resolution
3772 * failures are silently ignored, this is convenient to validate some
3773 * configs out of their environment.
3774 */
3775 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3776 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3777
Willy Tarreau25e51522016-11-04 15:10:17 +01003778 while (methods) {
3779 err_code = 0;
3780 switch (srv_get_next_initaddr(&methods)) {
3781 case SRV_IADDR_LAST:
3782 if (!srv->lastaddr)
3783 continue;
3784 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003785 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003786 return_code |= err_code;
3787 break;
3788
3789 case SRV_IADDR_LIBC:
3790 if (!srv->hostname)
3791 continue;
3792 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003793 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003794 return_code |= err_code;
3795 break;
3796
Willy Tarreau37ebe122016-11-04 15:17:58 +01003797 case SRV_IADDR_NONE:
3798 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003799 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003800 ha_warning("could not resolve address '%s', disabling server.\n",
3801 name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003802 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003803 return return_code;
3804
Willy Tarreau4310d362016-11-02 15:05:56 +01003805 case SRV_IADDR_IP:
3806 ipcpy(&srv->init_addr, &srv->addr);
3807 if (return_code) {
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003808 ha_warning("could not resolve address '%s', falling back to configured address.\n",
3809 name);
Willy Tarreau4310d362016-11-02 15:05:56 +01003810 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003811 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003812
Willy Tarreau25e51522016-11-04 15:10:17 +01003813 default: /* unhandled method */
3814 break;
3815 }
3816 }
3817
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003818 if (!return_code)
3819 ha_alert("no method found to resolve address '%s'.\n", name);
3820 else
3821 ha_alert("could not resolve address '%s'.\n", name);
Willy Tarreau25e51522016-11-04 15:10:17 +01003822
3823 return_code |= ERR_ALERT | ERR_FATAL;
3824 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003825out:
3826 srv_set_dyncookie(srv);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02003827 srv_set_addr_desc(srv, 1);
Olivier Houchard4e694042017-03-14 20:01:29 +01003828 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003829}
3830
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003831/*
3832 * This function parses all backends and all servers within each backend
3833 * and performs servers' addr resolution based on information provided by:
3834 * - configuration file
3835 * - server-state file (states provided by an 'old' haproxy process)
3836 *
3837 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3838 */
3839int srv_init_addr(void)
3840{
3841 struct proxy *curproxy;
3842 int return_code = 0;
3843
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003844 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003845 while (curproxy) {
3846 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003847
3848 /* servers are in backend only */
Amaury Denoyellee3c41922021-01-06 14:28:50 +01003849 if (!(curproxy->cap & PR_CAP_BE) || curproxy->disabled)
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003850 goto srv_init_addr_next;
3851
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003852 for (srv = curproxy->srv; srv; srv = srv->next) {
3853 set_usermsgs_ctx(srv->conf.file, srv->conf.line, &srv->obj_type);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01003854 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003855 return_code |= srv_iterate_initaddr(srv);
Amaury Denoyelle9d0138a2021-05-28 11:01:52 +02003856 reset_usermsgs_ctx();
3857 }
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003858
3859 srv_init_addr_next:
3860 curproxy = curproxy->next;
3861 }
3862
3863 return return_code;
3864}
3865
Willy Tarreau46b7f532018-08-21 11:54:26 +02003866/*
3867 * Must be called with the server lock held.
3868 */
Christopher Faulet69beaa92021-02-16 12:07:47 +01003869const 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 +02003870{
3871
Willy Tarreau83061a82018-07-13 11:56:34 +02003872 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003873
3874 msg = get_trash_chunk();
3875 chunk_reset(msg);
3876
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003877 if (server->hostname && strcmp(fqdn, server->hostname) == 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003878 chunk_appendf(msg, "no need to change the FDQN");
3879 goto out;
3880 }
3881
3882 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3883 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3884 goto out;
3885 }
3886
3887 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3888 server->proxy->id, server->id, server->hostname, fqdn);
3889
Emeric Brun08622d32020-12-23 17:41:43 +01003890 if (srv_set_fqdn(server, fqdn, resolv_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003891 chunk_reset(msg);
3892 chunk_appendf(msg, "could not update %s/%s FQDN",
3893 server->proxy->id, server->id);
3894 goto out;
3895 }
3896
3897 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003898 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003899
3900 out:
3901 if (updater)
3902 chunk_appendf(msg, " by '%s'", updater);
3903 chunk_appendf(msg, "\n");
3904
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003905 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003906}
3907
3908
Willy Tarreau21b069d2016-11-23 17:15:08 +01003909/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3910 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003911 * 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 +01003912 * used for CLI commands requiring a server name.
3913 * Important: the <arg> is modified to remove the '/'.
3914 */
3915struct server *cli_find_server(struct appctx *appctx, char *arg)
3916{
3917 struct proxy *px;
3918 struct server *sv;
3919 char *line;
3920
3921 /* split "backend/server" and make <line> point to server */
3922 for (line = arg; *line; line++)
3923 if (*line == '/') {
3924 *line++ = '\0';
3925 break;
3926 }
3927
3928 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003929 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003930 return NULL;
3931 }
3932
3933 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003934 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003935 return NULL;
3936 }
3937
Willy Tarreauc3914d42020-09-24 08:39:22 +02003938 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02003939 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01003940 return NULL;
3941 }
3942
3943 return sv;
3944}
3945
William Lallemand222baf22016-11-19 02:00:33 +01003946
Willy Tarreau46b7f532018-08-21 11:54:26 +02003947/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003948static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01003949{
3950 struct server *sv;
3951 const char *warning;
3952
3953 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3954 return 1;
3955
3956 sv = cli_find_server(appctx, args[2]);
3957 if (!sv)
3958 return 1;
3959
3960 if (strcmp(args[3], "weight") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003961 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003962 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003963 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02003964 if (warning)
3965 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01003966 }
3967 else if (strcmp(args[3], "state") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003968 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003969 if (strcmp(args[4], "ready") == 0)
3970 srv_adm_set_ready(sv);
3971 else if (strcmp(args[4], "drain") == 0)
3972 srv_adm_set_drain(sv);
3973 else if (strcmp(args[4], "maint") == 0)
3974 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02003975 else
3976 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003977 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003978 }
3979 else if (strcmp(args[3], "health") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003980 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02003981 if (sv->track)
3982 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01003983 else if (strcmp(args[4], "up") == 0) {
3984 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003985 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003986 }
3987 else if (strcmp(args[4], "stopping") == 0) {
3988 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003989 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003990 }
3991 else if (strcmp(args[4], "down") == 0) {
3992 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003993 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01003994 }
Willy Tarreau9d008692019-08-09 11:21:01 +02003995 else
3996 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02003997 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01003998 }
3999 else if (strcmp(args[3], "agent") == 0) {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004000 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004001 if (!(sv->agent.state & CHK_ST_ENABLED))
4002 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004003 else if (strcmp(args[4], "up") == 0) {
4004 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004005 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004006 }
4007 else if (strcmp(args[4], "down") == 0) {
4008 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004009 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004010 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004011 else
4012 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004013 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004014 }
Misiek2da082d2017-01-09 09:40:42 +01004015 else if (strcmp(args[3], "agent-addr") == 0) {
William Dauchy7cabc062021-02-11 22:51:24 +01004016 char *addr = NULL;
4017 char *port = NULL;
4018 if (strlen(args[4]) == 0) {
4019 cli_err(appctx, "set server <b>/<s> agent-addr requires"
4020 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004021 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004022 }
4023 addr = args[4];
4024 if (strcmp(args[5], "port") == 0)
4025 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004026 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004027 warning = srv_update_agent_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004028 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004029 if (warning)
4030 cli_msg(appctx, LOG_WARNING, warning);
4031 }
4032 else if (strcmp(args[3], "agent-port") == 0) {
4033 char *port = NULL;
4034 if (strlen(args[4]) == 0) {
4035 cli_err(appctx, "set server <b>/<s> agent-port requires"
4036 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004037 goto out;
William Dauchy7cabc062021-02-11 22:51:24 +01004038 }
4039 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004040 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004041 warning = srv_update_agent_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004042 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchy7cabc062021-02-11 22:51:24 +01004043 if (warning)
4044 cli_msg(appctx, LOG_WARNING, warning);
Misiek2da082d2017-01-09 09:40:42 +01004045 }
4046 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004047 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004048 if (!(sv->agent.state & CHK_ST_ENABLED))
4049 cli_err(appctx, "agent checks are not enabled on this server.\n");
4050 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004051 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004052 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004053 }
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004054 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004055 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004056 else if (strcmp(args[3], "check-addr") == 0) {
4057 char *addr = NULL;
4058 char *port = NULL;
4059 if (strlen(args[4]) == 0) {
4060 cli_err(appctx, "set server <b>/<s> check-addr requires"
4061 " an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004062 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004063 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004064 addr = args[4];
4065 if (strcmp(args[5], "port") == 0)
4066 port = args[6];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004067 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004068 warning = srv_update_check_addr_port(sv, addr, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004069 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004070 if (warning)
4071 cli_msg(appctx, LOG_WARNING, warning);
4072 }
4073 else if (strcmp(args[3], "check-port") == 0) {
4074 char *port = NULL;
4075 if (strlen(args[4]) == 0) {
4076 cli_err(appctx, "set server <b>/<s> check-port requires"
4077 " a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004078 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004079 }
William Dauchyb456e1f2021-02-11 22:51:23 +01004080 port = args[4];
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004081 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004082 warning = srv_update_check_addr_port(sv, NULL, port);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004083 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyb456e1f2021-02-11 22:51:23 +01004084 if (warning)
4085 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004086 }
4087 else if (strcmp(args[3], "addr") == 0) {
4088 char *addr = NULL;
4089 char *port = NULL;
4090 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004091 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004092 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004093 }
4094 else {
4095 addr = args[4];
4096 }
4097 if (strcmp(args[5], "port") == 0) {
4098 port = args[6];
4099 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004100 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet69beaa92021-02-16 12:07:47 +01004101 warning = srv_update_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004102 if (warning)
4103 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004104 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004105 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004106 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004107 else if (strcmp(args[3], "fqdn") == 0) {
4108 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004109 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004110 goto out;
4111 }
4112 if (!sv->resolvers) {
4113 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4114 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004115 }
Christopher Fauleta386e782021-06-15 11:37:40 +02004116 if (sv->srvrq) {
4117 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004118 goto out;
Christopher Fauleta386e782021-06-15 11:37:40 +02004119 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004120 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4121 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004122 /* ensure runtime resolver will process this new fqdn */
4123 if (sv->flags & SRV_F_NO_RESOLUTION) {
4124 sv->flags &= ~SRV_F_NO_RESOLUTION;
4125 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004126 warning = srv_update_fqdn(sv, args[4], "stats socket command", 1);
Christopher Faulet0ba54bb2021-06-18 08:47:14 +02004127 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004128 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004129 if (warning)
4130 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004131 }
William Dauchyf6370442020-11-14 19:25:33 +01004132 else if (strcmp(args[3], "ssl") == 0) {
4133#ifdef USE_OPENSSL
Amaury Denoyelleb89d3d32021-05-19 15:00:54 +02004134 if (sv->flags & SRV_F_DYNAMIC) {
4135 cli_err(appctx, "'set server <srv> ssl' not supported on dynamic servers\n");
4136 goto out;
4137 }
4138
William Dauchyf6370442020-11-14 19:25:33 +01004139 if (sv->ssl_ctx.ctx == NULL) {
4140 cli_err(appctx, "'set server <srv> ssl' cannot be set. "
4141 " default-server should define ssl settings\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004142 goto out;
4143 }
4144
4145 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4146 if (strcmp(args[4], "on") == 0) {
William Dauchyf6370442020-11-14 19:25:33 +01004147 ssl_sock_set_srv(sv, 1);
4148 } else if (strcmp(args[4], "off") == 0) {
4149 ssl_sock_set_srv(sv, 0);
4150 } else {
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004151 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004152 cli_err(appctx, "'set server <srv> ssl' expects 'on' or 'off'.\n");
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004153 goto out;
William Dauchyf6370442020-11-14 19:25:33 +01004154 }
4155 srv_cleanup_connections(sv);
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004156 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Dauchyf6370442020-11-14 19:25:33 +01004157 cli_msg(appctx, LOG_NOTICE, "server ssl setting updated.\n");
4158#else
4159 cli_msg(appctx, LOG_NOTICE, "server ssl setting not supported.\n");
4160#endif
4161 } else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004162 cli_err(appctx,
William Dauchy3f4ec7d2021-02-15 17:22:16 +01004163 "usage: set server <backend>/<server> "
4164 "addr | agent | agent-addr | agent-port | agent-send | "
4165 "check-addr | check-port | fqdn | health | ssl | "
4166 "state | weight\n");
William Lallemand222baf22016-11-19 02:00:33 +01004167 }
Christopher Fauletc7b391a2021-06-15 12:01:29 +02004168 out:
William Lallemand222baf22016-11-19 02:00:33 +01004169 return 1;
4170}
4171
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004172static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004173{
4174 struct stream_interface *si = appctx->owner;
4175 struct proxy *px;
4176 struct server *sv;
4177 char *line;
4178
4179
4180 /* split "backend/server" and make <line> point to server */
4181 for (line = args[2]; *line; line++)
4182 if (*line == '/') {
4183 *line++ = '\0';
4184 break;
4185 }
4186
Willy Tarreau9d008692019-08-09 11:21:01 +02004187 if (!*line)
4188 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004189
Willy Tarreau9d008692019-08-09 11:21:01 +02004190 if (!get_backend_server(args[2], line, &px, &sv))
4191 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004192
4193 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004194 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4195 sv->iweight);
4196 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004197 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004198 return 0;
4199 }
William Lallemand6b160942016-11-22 12:34:35 +01004200 return 1;
4201}
4202
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004203/* Parse a "set weight" command.
4204 *
4205 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004206 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004207static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004208{
4209 struct server *sv;
4210 const char *warning;
4211
4212 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4213 return 1;
4214
4215 sv = cli_find_server(appctx, args[2]);
4216 if (!sv)
4217 return 1;
4218
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004219 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4220
William Lallemand6b160942016-11-22 12:34:35 +01004221 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004222 if (warning)
4223 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004224
4225 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4226
William Lallemand6b160942016-11-22 12:34:35 +01004227 return 1;
4228}
4229
Willy Tarreau46b7f532018-08-21 11:54:26 +02004230/* parse a "set maxconn server" command. It always returns 1.
4231 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004232 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004233 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004234static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004235{
4236 struct server *sv;
4237 const char *warning;
4238
4239 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4240 return 1;
4241
4242 sv = cli_find_server(appctx, args[3]);
4243 if (!sv)
4244 return 1;
4245
Amaury Denoyelle02742862021-06-18 11:11:36 +02004246 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4247
Willy Tarreaub8026272016-11-23 11:26:56 +01004248 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004249 if (warning)
4250 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004251
Amaury Denoyelle02742862021-06-18 11:11:36 +02004252 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4253
Willy Tarreaub8026272016-11-23 11:26:56 +01004254 return 1;
4255}
William Lallemand6b160942016-11-22 12:34:35 +01004256
Willy Tarreau46b7f532018-08-21 11:54:26 +02004257/* parse a "disable agent" command. It always returns 1.
4258 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004259 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004260 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004261static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004262{
4263 struct server *sv;
4264
4265 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4266 return 1;
4267
4268 sv = cli_find_server(appctx, args[2]);
4269 if (!sv)
4270 return 1;
4271
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004272 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004273 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004274 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004275 return 1;
4276}
4277
Willy Tarreau46b7f532018-08-21 11:54:26 +02004278/* parse a "disable health" command. It always returns 1.
4279 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004280 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004281 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004282static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004283{
4284 struct server *sv;
4285
4286 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4287 return 1;
4288
4289 sv = cli_find_server(appctx, args[2]);
4290 if (!sv)
4291 return 1;
4292
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004293 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004294 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004295 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004296 return 1;
4297}
4298
Willy Tarreau46b7f532018-08-21 11:54:26 +02004299/* parse a "disable server" command. It always returns 1.
4300 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004301 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004302 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004303static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004304{
4305 struct server *sv;
4306
4307 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4308 return 1;
4309
4310 sv = cli_find_server(appctx, args[2]);
4311 if (!sv)
4312 return 1;
4313
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004314 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004315 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004316 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004317 return 1;
4318}
4319
Willy Tarreau46b7f532018-08-21 11:54:26 +02004320/* parse a "enable agent" command. It always returns 1.
4321 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004322 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004323 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004324static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004325{
4326 struct server *sv;
4327
4328 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4329 return 1;
4330
4331 sv = cli_find_server(appctx, args[2]);
4332 if (!sv)
4333 return 1;
4334
Willy Tarreau9d008692019-08-09 11:21:01 +02004335 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4336 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004337
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004338 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004339 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004340 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004341 return 1;
4342}
4343
Willy Tarreau46b7f532018-08-21 11:54:26 +02004344/* parse a "enable health" command. It always returns 1.
4345 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004346 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004347 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004348static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004349{
4350 struct server *sv;
4351
4352 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4353 return 1;
4354
4355 sv = cli_find_server(appctx, args[2]);
4356 if (!sv)
4357 return 1;
4358
Amaury Denoyelle0f456d52021-09-21 10:29:09 +02004359 if (!(sv->check.state & CHK_ST_CONFIGURED))
4360 return cli_err(appctx, "Health check was not configured on this server, cannot enable.\n");
4361
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004362 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004363 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004364 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004365 return 1;
4366}
4367
Willy Tarreau46b7f532018-08-21 11:54:26 +02004368/* parse a "enable server" command. It always returns 1.
4369 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004370 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004371 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004372static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004373{
4374 struct server *sv;
4375
4376 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4377 return 1;
4378
4379 sv = cli_find_server(appctx, args[2]);
4380 if (!sv)
4381 return 1;
4382
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004383 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004384 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004385 if (!(sv->flags & SRV_F_COOKIESET)
4386 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4387 sv->cookie)
4388 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004389 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004390 return 1;
4391}
4392
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004393/* Allocates data structure related to load balancing for the server <sv>. It
4394 * is only required for dynamic servers.
4395 *
4396 * At the moment, the server lock is not used as this function is only called
4397 * for a dynamic server not yet registered.
4398 *
4399 * Returns 1 on success, 0 on allocation failure.
4400 */
4401static int srv_alloc_lb(struct server *sv, struct proxy *be)
4402{
4403 int node;
4404
4405 sv->lb_tree = (sv->flags & SRV_F_BACKUP) ?
4406 &be->lbprm.chash.bck : &be->lbprm.chash.act;
4407 sv->lb_nodes_tot = sv->uweight * BE_WEIGHT_SCALE;
4408 sv->lb_nodes_now = 0;
4409
Willy Tarreaudcb121f2021-04-20 11:37:45 +02004410 if (((be->lbprm.algo & (BE_LB_KIND | BE_LB_PARM)) == (BE_LB_KIND_RR | BE_LB_RR_RANDOM)) ||
4411 ((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 +01004412 sv->lb_nodes = calloc(sv->lb_nodes_tot, sizeof(*sv->lb_nodes));
4413
4414 if (!sv->lb_nodes)
4415 return 0;
4416
4417 for (node = 0; node < sv->lb_nodes_tot; node++) {
4418 sv->lb_nodes[node].server = sv;
4419 sv->lb_nodes[node].node.key = full_hash(sv->puid * SRV_EWGHT_RANGE + node);
4420 }
4421 }
4422
4423 return 1;
4424}
4425
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004426/* updates the server's weight during a warmup stage. Once the final weight is
4427 * reached, the task automatically stops. Note that any server status change
4428 * must have updated s->last_change accordingly.
4429 */
4430static struct task *server_warmup(struct task *t, void *context, unsigned int state)
4431{
4432 struct server *s = context;
4433
4434 /* by default, plan on stopping the task */
4435 t->expire = TICK_ETERNITY;
4436 if ((s->next_admin & SRV_ADMF_MAINT) ||
4437 (s->next_state != SRV_ST_STARTING))
4438 return t;
4439
4440 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
4441
4442 /* recalculate the weights and update the state */
4443 server_recalc_eweight(s, 1);
4444
4445 /* probably that we can refill this server with a bit more connections */
4446 pendconn_grab_from_px(s);
4447
4448 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
4449
4450 /* get back there in 1 second or 1/20th of the slowstart interval,
4451 * whichever is greater, resulting in small 5% steps.
4452 */
4453 if (s->next_state == SRV_ST_STARTING)
4454 t->expire = tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20)));
4455 return t;
4456}
4457
4458/* Allocate the slowstart task if the server is configured with a slowstart
4459 * timer. If server next_state is SRV_ST_STARTING, the task is scheduled.
4460 *
4461 * Returns 0 on success else non-zero.
4462 */
4463static int init_srv_slowstart(struct server *srv)
4464{
4465 struct task *t;
4466
4467 if (srv->slowstart) {
Willy Tarreaubeeabf52021-10-01 18:23:30 +02004468 if ((t = task_new_anywhere()) == NULL) {
Amaury Denoyelle29d1ac12021-09-21 11:51:29 +02004469 ha_alert("Cannot activate slowstart for server %s/%s: out of memory.\n", srv->proxy->id, srv->id);
4470 return ERR_ALERT | ERR_FATAL;
4471 }
4472
4473 /* We need a warmup task that will be called when the server
4474 * state switches from down to up.
4475 */
4476 srv->warmup = t;
4477 t->process = server_warmup;
4478 t->context = srv;
4479
4480 /* server can be in this state only because of */
4481 if (srv->next_state == SRV_ST_STARTING) {
4482 task_schedule(srv->warmup,
4483 tick_add(now_ms,
4484 MS_TO_TICKS(MAX(1000, (now.tv_sec - srv->last_change)) / 20)));
4485 }
4486 }
4487
4488 return ERR_NONE;
4489}
4490REGISTER_POST_SERVER_CHECK(init_srv_slowstart);
4491
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004492/* Memory allocation and initialization of the per_thr field.
4493 * Returns 0 if the field has been successfully initialized, -1 on failure.
4494 */
4495int srv_init_per_thr(struct server *srv)
4496{
4497 int i;
4498
4499 srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
4500 if (!srv->per_thr)
4501 return -1;
4502
4503 for (i = 0; i < global.nbthread; i++) {
4504 srv->per_thr[i].idle_conns = EB_ROOT;
4505 srv->per_thr[i].safe_conns = EB_ROOT;
4506 srv->per_thr[i].avail_conns = EB_ROOT;
4507 MT_LIST_INIT(&srv->per_thr[i].streams);
4508 }
4509
4510 return 0;
4511}
4512
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004513/* Parse a "add server" command
4514 * Returns 0 if the server has been successfully initialized, 1 on failure.
4515 */
4516static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private)
4517{
4518 struct proxy *be;
4519 struct server *srv;
4520 char *be_name, *sv_name;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004521 int errcode, argc;
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004522 int next_id;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004523 const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
4524
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004525 usermsgs_clr("CLI");
4526
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004527 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4528 return 1;
4529
4530 ++args;
4531
4532 sv_name = be_name = args[1];
4533 /* split backend/server arg */
4534 while (*sv_name && *(++sv_name)) {
4535 if (*sv_name == '/') {
4536 *sv_name = '\0';
4537 ++sv_name;
4538 break;
4539 }
4540 }
4541
4542 if (!*sv_name)
4543 return cli_err(appctx, "Require 'backend/server'.");
4544
Amaury Denoyellecece9182021-04-20 17:09:08 +02004545 be = proxy_be_by_name(be_name);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004546 if (!be)
4547 return cli_err(appctx, "No such backend.");
4548
4549 if (!(be->lbprm.algo & BE_LB_PROP_DYN)) {
Amaury Denoyelleeafd7012021-04-29 14:59:42 +02004550 cli_err(appctx, "Backend must use a dynamic load balancing to support dynamic servers.");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004551 return 1;
4552 }
4553
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004554 /* At this point, some operations might not be thread-safe anymore. This
4555 * might be the case for parsing handlers which were designed to run
4556 * only at the starting stage on single-thread mode.
4557 *
4558 * Activate thread isolation to ensure thread-safety.
4559 */
4560 thread_isolate();
4561
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004562 args[1] = sv_name;
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004563 errcode = _srv_parse_init(&srv, args, &argc, be, parse_flags);
4564 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004565 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004566
4567 while (*args[argc]) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004568 errcode = _srv_parse_kw(srv, args, &argc, be, parse_flags);
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004569
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004570 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004571 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004572 }
4573
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004574 errcode = _srv_parse_finalize(args, argc, srv, be, parse_flags);
4575 if (errcode)
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004576 goto out;
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004577
Amaury Denoyelleefbf35c2021-06-10 17:34:10 +02004578 /* A dynamic server does not currently support resolution.
4579 *
4580 * Initialize it explicitly to the "none" method to ensure no
4581 * resolution will ever be executed.
4582 */
4583 srv->init_addr_methods = SRV_IADDR_NONE;
4584
Amaury Denoyelle30467232021-03-12 18:03:27 +01004585 if (srv->mux_proto) {
4586 if (!conn_get_best_mux_entry(srv->mux_proto->token, PROTO_SIDE_BE, be->mode)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004587 ha_alert("MUX protocol is not usable for server.\n");
Amaury Denoyelle30467232021-03-12 18:03:27 +01004588 goto out;
4589 }
4590 }
4591
Miroslav Zagorac8a8f2702021-06-15 15:33:20 +02004592 if (srv_init_per_thr(srv) == -1) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004593 ha_alert("failed to allocate per-thread lists for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004594 goto out;
4595 }
4596
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004597 if (srv->max_idle_conns != 0) {
4598 srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
4599 if (!srv->curr_idle_thr) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004600 ha_alert("failed to allocate counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004601 goto out;
4602 }
4603 }
4604
4605 if (!srv_alloc_lb(srv, be)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004606 ha_alert("Failed to initialize load-balancing data.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004607 goto out;
4608 }
4609
4610 if (!stats_allocate_proxy_counters_internal(&srv->extra_counters,
4611 COUNTERS_SV,
4612 STATS_PX_CAP_SRV)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004613 ha_alert("failed to allocate extra counters for server.\n");
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004614 goto out;
4615 }
4616
Amaury Denoyelle79b90e82021-09-20 15:15:19 +02004617 if (srv->use_ssl == 1 || (srv->proxy->options & PR_O_TCPCHK_SSL) ||
4618 srv->check.use_ssl == 1) {
Amaury Denoyelle34897d22021-05-19 09:49:41 +02004619 if (xprt_get(XPRT_SSL) && xprt_get(XPRT_SSL)->prepare_srv) {
4620 if (xprt_get(XPRT_SSL)->prepare_srv(srv))
4621 goto out;
4622 }
4623 }
4624
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004625 if (srv->trackit) {
4626 if (srv_apply_track(srv, be))
4627 goto out;
4628 }
4629
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004630 /* Init check/agent if configured. The check is manually disabled
4631 * because a dynamic server is started in a disable state. It must be
4632 * manually activated via a "enable health/agent" command.
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004633 */
4634 if (srv->do_check) {
4635 if (init_srv_check(srv))
4636 goto out;
4637
4638 srv->check.state &= ~CHK_ST_ENABLED;
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004639 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004640
4641 if (srv->do_agent) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004642 if (init_srv_agent_check(srv))
4643 goto out;
4644
4645 srv->agent.state &= ~CHK_ST_ENABLED;
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004646 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004647
Amaury Denoyellecd8a6f22021-09-21 11:51:54 +02004648 /* Init slowstart if needed. */
4649 if (init_srv_slowstart(srv))
4650 goto out;
4651
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004652 /* Attach the server to the end of the proxy linked list. Note that this
4653 * operation is not thread-safe so this is executed under thread
4654 * isolation.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004655 *
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004656 * If a server with the same name is found, reject the new one.
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004657 */
Amaury Denoyellecece9182021-04-20 17:09:08 +02004658
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004659 /* TODO use a double-linked list for px->srv */
4660 if (be->srv) {
Amaury Denoyellecece9182021-04-20 17:09:08 +02004661 struct server *next = be->srv;
4662
4663 while (1) {
4664 /* check for duplicate server */
4665 if (!strcmp(srv->id, next->id)) {
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004666 ha_alert("Already exists a server with the same name in backend.\n");
Amaury Denoyellecece9182021-04-20 17:09:08 +02004667 goto out;
4668 }
4669
4670 if (!next->next)
4671 break;
4672
4673 next = next->next;
4674 }
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004675
4676 next->next = srv;
4677 }
4678 else {
4679 srv->next = be->srv;
4680 be->srv = srv;
4681 }
Amaury Denoyellecece9182021-04-20 17:09:08 +02004682
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004683 /* generate the server id if not manually specified */
4684 if (!srv->puid) {
4685 next_id = get_next_id(&be->conf.used_server_id, 1);
4686 if (!next_id) {
4687 ha_alert("Cannot attach server : no id left in proxy\n");
4688 goto out;
4689 }
4690
4691 srv->conf.id.key = srv->puid = next_id;
4692 srv->conf.name.key = srv->id;
4693 }
4694
4695 /* insert the server in the backend trees */
Amaury Denoyelle1613b4a2021-06-08 17:00:20 +02004696 eb32_insert(&be->conf.used_server_id, &srv->conf.id);
4697 ebis_insert(&be->conf.used_server_name, &srv->conf.name);
Amaury Denoyelle8ff04342021-06-08 15:19:51 +02004698 ebis_insert(&be->used_server_addr, &srv->addr_node);
Amaury Denoyelle406aaef2021-06-09 09:58:47 +02004699
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004700 thread_release();
4701
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004702 /* Start the check task. The server must be fully initialized.
4703 *
4704 * <srvpos> and <nbcheck> parameters are set to 1 as there should be no
4705 * need to randomly spread the task interval for dynamic servers.
4706 */
4707 if (srv->check.state & CHK_ST_CONFIGURED) {
4708 if (!start_check_task(&srv->check, 0, 1, 1))
4709 ha_alert("System might be unstable, consider to execute a reload");
4710 }
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004711 if (srv->agent.state & CHK_ST_CONFIGURED) {
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004712 if (!start_check_task(&srv->agent, 0, 1, 1))
4713 ha_alert("System might be unstable, consider to execute a reload");
4714 }
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004715
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004716 ha_notice("New server registered.\n");
4717 cli_msg(appctx, LOG_INFO, usermsgs_str());
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004718
4719 return 0;
4720
4721out:
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004722 if (srv) {
4723 if (srv->track)
4724 release_server_track(srv);
4725
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004726 if (srv->check.state & CHK_ST_CONFIGURED)
4727 free_check(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004728 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004729 free_check(&srv->agent);
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004730
Amaury Denoyellebd8dd842021-08-04 11:20:05 +02004731 /* remove the server from the proxy linked list */
4732 if (be->srv == srv) {
4733 be->srv = srv->next;
4734 }
4735 else {
4736 struct server *prev;
4737 for (prev = be->srv; prev && prev->next != srv; prev = prev->next)
4738 ;
4739 if (prev)
4740 prev->next = srv->next;
4741 }
4742
4743 }
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004744
Amaury Denoyelle31ddd762021-06-10 15:26:44 +02004745 thread_release();
4746
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004747 if (!usermsgs_empty())
4748 cli_err(appctx, usermsgs_str());
4749
Amaury Denoyelle08be72b2021-07-28 10:06:52 +02004750 if (srv)
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004751 srv_drop(srv);
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004752
Amaury Denoyellef99f77a2021-03-08 17:13:32 +01004753 return 1;
4754}
4755
Amaury Denoyellee5580432021-04-15 14:41:20 +02004756/* Parse a "del server" command
4757 * Returns 0 if the server has been successfully initialized, 1 on failure.
4758 */
4759static int cli_parse_delete_server(char **args, char *payload, struct appctx *appctx, void *private)
4760{
4761 struct proxy *be;
4762 struct server *srv;
4763 char *be_name, *sv_name;
4764
4765 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4766 return 1;
4767
4768 ++args;
4769
4770 sv_name = be_name = args[1];
4771 /* split backend/server arg */
4772 while (*sv_name && *(++sv_name)) {
4773 if (*sv_name == '/') {
4774 *sv_name = '\0';
4775 ++sv_name;
4776 break;
4777 }
4778 }
4779
4780 if (!*sv_name)
4781 return cli_err(appctx, "Require 'backend/server'.");
4782
4783 /* The proxy servers list is currently not protected by a lock so this
Willy Tarreauba3ab792021-08-04 14:42:37 +02004784 * requires thread isolation. In addition, any place referencing the
4785 * server about to be deleted would be unsafe after our operation, so
4786 * we must be certain to be alone so that no other thread has even
4787 * started to grab a temporary reference to this server.
Amaury Denoyellee5580432021-04-15 14:41:20 +02004788 */
Willy Tarreauba3ab792021-08-04 14:42:37 +02004789 thread_isolate_full();
Amaury Denoyellee5580432021-04-15 14:41:20 +02004790
4791 get_backend_server(be_name, sv_name, &be, &srv);
4792 if (!be) {
4793 cli_err(appctx, "No such backend.");
4794 goto out;
4795 }
4796
4797 if (!srv) {
4798 cli_err(appctx, "No such server.");
4799 goto out;
4800 }
4801
Amaury Denoyelle14c3c5c2021-08-23 14:10:51 +02004802 if (srv->flags & SRV_F_NON_PURGEABLE) {
4803 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 +02004804 goto out;
4805 }
4806
4807 /* Only servers in maintenance can be deleted. This ensures that the
4808 * server is not present anymore in the lb structures (through
4809 * lbprm.set_server_status_down).
4810 */
4811 if (!(srv->cur_admin & SRV_ADMF_MAINT)) {
4812 cli_err(appctx, "Only servers in maintenance mode can be deleted.");
4813 goto out;
4814 }
4815
4816 /* Ensure that there is no active/idle/pending connection on the server.
4817 *
4818 * TODO idle connections should not prevent server deletion. A proper
4819 * cleanup function should be implemented to be used here.
4820 */
4821 if (srv->cur_sess || srv->curr_idle_conns ||
Willy Tarreaua0570452021-06-18 09:30:30 +02004822 !eb_is_empty(&srv->queue.head)) {
Amaury Denoyellee5580432021-04-15 14:41:20 +02004823 cli_err(appctx, "Server still has connections attached to it, cannot remove it.");
4824 goto out;
4825 }
4826
Amaury Denoyelle56eb8ed2021-07-13 10:36:03 +02004827 /* remove srv from tracking list */
4828 if (srv->track)
4829 release_server_track(srv);
4830
Amaury Denoyelle2fc4d392021-07-22 16:04:59 +02004831 /* stop the check task if running */
4832 if (srv->check.state & CHK_ST_CONFIGURED)
4833 check_purge(&srv->check);
Amaury Denoyelle3eb42f92021-08-10 16:24:36 +02004834 if (srv->agent.state & CHK_ST_CONFIGURED)
Amaury Denoyelleb65f4ca2021-08-04 11:33:14 +02004835 check_purge(&srv->agent);
Amaury Denoyellee5580432021-04-15 14:41:20 +02004836
4837 /* detach the server from the proxy linked list
4838 * The proxy servers list is currently not protected by a lock, so this
4839 * requires thread_isolate/release.
4840 */
4841
4842 /* be->srv cannot be empty since we have already found the server with
4843 * get_backend_server */
4844 BUG_ON(!be->srv);
4845 if (be->srv == srv) {
4846 be->srv = srv->next;
4847 }
4848 else {
4849 struct server *next;
Amaury Denoyelled6b4b6d2021-04-21 11:50:26 +02004850 for (next = be->srv; srv != next->next; next = next->next) {
4851 /* srv cannot be not found since we have already found
4852 * it with get_backend_server */
4853 BUG_ON(!next);
4854 }
Amaury Denoyellee5580432021-04-15 14:41:20 +02004855
Amaury Denoyellee5580432021-04-15 14:41:20 +02004856 next->next = srv->next;
4857 }
4858
4859 /* remove srv from addr_node tree */
Amaury Denoyelle82d7f772021-06-09 16:00:43 +02004860 eb32_delete(&srv->conf.id);
4861 ebpt_delete(&srv->conf.name);
Amaury Denoyellee5580432021-04-15 14:41:20 +02004862 ebpt_delete(&srv->addr_node);
4863
4864 /* remove srv from idle_node tree for idle conn cleanup */
4865 eb32_delete(&srv->idle_node);
4866
4867 thread_release();
4868
Amaury Denoyelle5e560e82021-05-28 16:35:05 +02004869 ha_notice("Server deleted.\n");
Amaury Denoyellebc2ebfa2021-08-25 15:34:53 +02004870 srv_drop(srv);
Amaury Denoyellee5580432021-04-15 14:41:20 +02004871
4872 cli_msg(appctx, LOG_INFO, "Server deleted.");
4873
4874 return 0;
4875
4876out:
4877 thread_release();
4878
4879 return 1;
4880}
4881
William Lallemand222baf22016-11-19 02:00:33 +01004882/* register cli keywords */
4883static struct cli_kw_list cli_kws = {{ },{
Amaury Denoyellec755efd2021-08-03 18:05:37 +02004884 { { "disable", "agent", NULL }, "disable agent : disable agent checks", cli_parse_disable_agent, NULL },
4885 { { "disable", "health", NULL }, "disable health : disable health checks", cli_parse_disable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004886 { { "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 +02004887 { { "enable", "agent", NULL }, "enable agent : enable agent checks", cli_parse_enable_agent, NULL },
4888 { { "enable", "health", NULL }, "enable health : enable health checks", cli_parse_enable_health, NULL },
Willy Tarreaub205bfd2021-05-07 11:38:37 +02004889 { { "enable", "server", NULL }, "enable server (DEPRECATED) : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
4890 { { "set", "maxconn", "server", NULL }, "set maxconn server <bk>/<srv> : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
4891 { { "set", "server", NULL }, "set server <bk>/<srv> [opts] : change a server's state, weight, address or ssl", cli_parse_set_server },
4892 { { "get", "weight", NULL }, "get weight <bk>/<srv> : report a server's current weight", cli_parse_get_weight },
4893 { { "set", "weight", NULL }, "set weight <bk>/<srv> (DEPRECATED) : change a server's weight (use 'set server' instead)", cli_parse_set_weight },
4894 { { "add", "server", NULL }, "add server <bk>/<srv> : create a new server (EXPERIMENTAL)", cli_parse_add_server, NULL, NULL, NULL, ACCESS_EXPERIMENTAL },
4895 { { "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 +01004896 {{},}
4897}};
4898
Willy Tarreau0108d902018-11-25 19:14:37 +01004899INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004900
Amaury Denoyelle669b6202021-07-13 10:35:23 +02004901/* Prepare a server <srv> to track check status of another one. <srv>.<trackit>
4902 * field is used to retrieve the identifier of the tracked server, either with
4903 * the format "proxy/server" or just "server". <curproxy> must point to the
4904 * backend owning <srv>; if no proxy is specified in <trackit>, it will be used
4905 * to find the tracked server.
4906 *
4907 * Returns 0 if the server track has been activated else non-zero.
4908 *
4909 * Not thread-safe.
4910 */
4911int srv_apply_track(struct server *srv, struct proxy *curproxy)
4912{
4913 struct proxy *px;
4914 struct server *strack, *loop;
4915 char *pname, *sname;
4916
4917 if (!srv->trackit)
4918 return 1;
4919
4920 pname = srv->trackit;
4921 sname = strrchr(pname, '/');
4922
4923 if (sname) {
4924 *sname++ = '\0';
4925 }
4926 else {
4927 sname = pname;
4928 pname = NULL;
4929 }
4930
4931 if (pname) {
4932 px = proxy_be_by_name(pname);
4933 if (!px) {
4934 ha_alert("unable to find required proxy '%s' for tracking.\n",
4935 pname);
4936 return 1;
4937 }
4938 }
4939 else {
4940 px = curproxy;
4941 }
4942
4943 strack = findserver(px, sname);
4944 if (!strack) {
4945 ha_alert("unable to find required server '%s' for tracking.\n",
4946 sname);
4947 return 1;
4948 }
4949
Amaury Denoyelle79f68be2021-07-13 10:35:50 +02004950 if (strack->flags & SRV_F_DYNAMIC) {
4951 ha_alert("unable to use %s/%s for tracking as it is a dynamic server.\n",
4952 px->id, strack->id);
4953 return 1;
4954 }
4955
Amaury Denoyelle669b6202021-07-13 10:35:23 +02004956 if (!strack->do_check && !strack->do_agent && !strack->track &&
4957 !strack->trackit) {
4958 ha_alert("unable to use %s/%s for "
4959 "tracking as it does not have any check nor agent enabled.\n",
4960 px->id, strack->id);
4961 return 1;
4962 }
4963
4964 for (loop = strack->track; loop && loop != srv; loop = loop->track)
4965 ;
4966
4967 if (srv == strack || loop) {
4968 ha_alert("unable to track %s/%s as it "
4969 "belongs to a tracking chain looping back to %s/%s.\n",
4970 px->id, strack->id, px->id,
4971 srv == strack ? strack->id : loop->id);
4972 return 1;
4973 }
4974
4975 if (curproxy != px &&
4976 (curproxy->options & PR_O_DISABLE404) != (px->options & PR_O_DISABLE404)) {
4977 ha_alert("unable to use %s/%s for"
4978 "tracking: disable-on-404 option inconsistency.\n",
4979 px->id, strack->id);
4980 return 1;
4981 }
4982
4983 srv->track = strack;
4984 srv->tracknext = strack->trackers;
4985 strack->trackers = srv;
Amaury Denoyelle06269612021-08-23 14:05:07 +02004986 strack->flags |= SRV_F_NON_PURGEABLE;
Amaury Denoyelle669b6202021-07-13 10:35:23 +02004987
4988 ha_free(&srv->trackit);
4989
4990 return 0;
4991}
4992
Emeric Brun64cc49c2017-10-03 14:46:45 +02004993/*
4994 * This function applies server's status changes, it is
4995 * is designed to be called asynchronously.
4996 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004997 * Must be called with the server lock held. This may also be called at init
4998 * time as the result of parsing the state file, in which case no lock will be
4999 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02005000 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005001static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005002{
5003 struct check *check = &s->check;
5004 int xferred;
5005 struct proxy *px = s->proxy;
5006 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
5007 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
5008 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02005009 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005010
Emeric Brun64cc49c2017-10-03 14:46:45 +02005011 /* If currently main is not set we try to apply pending state changes */
5012 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
5013 int next_admin;
5014
5015 /* Backup next admin */
5016 next_admin = s->next_admin;
5017
5018 /* restore current admin state */
5019 s->next_admin = s->cur_admin;
5020
5021 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
5022 s->last_change = now.tv_sec;
5023 if (s->proxy->lbprm.set_server_status_down)
5024 s->proxy->lbprm.set_server_status_down(s);
5025
5026 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5027 srv_shutdown_streams(s, SF_ERR_DOWN);
5028
5029 /* we might have streams queued on this server and waiting for
5030 * a connection. Those which are redispatchable will be queued
5031 * to another server or to the proxy itself.
5032 */
5033 xferred = pendconn_redistribute(s);
5034
5035 tmptrash = alloc_trash_chunk();
5036 if (tmptrash) {
5037 chunk_printf(tmptrash,
5038 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
5039 s->proxy->id, s->id);
5040
Emeric Brun5a133512017-10-19 14:42:30 +02005041 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005042 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005043
5044 /* we don't send an alert if the server was previously paused */
5045 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005046 send_log(s->proxy, log_level, "%s.\n",
5047 tmptrash->area);
5048 send_email_alert(s, log_level, "%s",
5049 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005050 free_trash_chunk(tmptrash);
5051 tmptrash = NULL;
5052 }
5053 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5054 set_backend_down(s->proxy);
5055
5056 s->counters.down_trans++;
5057 }
5058 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
5059 s->last_change = now.tv_sec;
5060 if (s->proxy->lbprm.set_server_status_down)
5061 s->proxy->lbprm.set_server_status_down(s);
5062
5063 /* we might have streams queued on this server and waiting for
5064 * a connection. Those which are redispatchable will be queued
5065 * to another server or to the proxy itself.
5066 */
5067 xferred = pendconn_redistribute(s);
5068
5069 tmptrash = alloc_trash_chunk();
5070 if (tmptrash) {
5071 chunk_printf(tmptrash,
5072 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
5073 s->proxy->id, s->id);
5074
Emeric Brun5a133512017-10-19 14:42:30 +02005075 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005076
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005077 ha_warning("%s.\n", tmptrash->area);
5078 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5079 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005080 free_trash_chunk(tmptrash);
5081 tmptrash = NULL;
5082 }
5083
5084 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5085 set_backend_down(s->proxy);
5086 }
5087 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
5088 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
5089 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5090 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5091 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5092 s->proxy->last_change = now.tv_sec;
5093 }
5094
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01005095 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02005096 s->down_time += now.tv_sec - s->last_change;
5097
5098 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005099 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02005100 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5101
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005102 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005103 /* now propagate the status change to any LB algorithms */
5104 if (px->lbprm.update_server_eweight)
5105 px->lbprm.update_server_eweight(s);
5106 else if (srv_willbe_usable(s)) {
5107 if (px->lbprm.set_server_status_up)
5108 px->lbprm.set_server_status_up(s);
5109 }
5110 else {
5111 if (px->lbprm.set_server_status_down)
5112 px->lbprm.set_server_status_down(s);
5113 }
5114
5115 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5116 * and it's not a backup server and its effective weight is > 0,
5117 * then it can accept new connections, so we shut down all streams
5118 * on all backup servers.
5119 */
5120 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5121 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5122 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5123
5124 /* check if we can handle some connections queued at the proxy. We
5125 * will take as many as we can handle.
5126 */
5127 xferred = pendconn_grab_from_px(s);
5128
5129 tmptrash = alloc_trash_chunk();
5130 if (tmptrash) {
5131 chunk_printf(tmptrash,
5132 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
5133 s->proxy->id, s->id);
5134
Emeric Brun5a133512017-10-19 14:42:30 +02005135 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005136 ha_warning("%s.\n", tmptrash->area);
5137 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5138 tmptrash->area);
5139 send_email_alert(s, LOG_NOTICE, "%s",
5140 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005141 free_trash_chunk(tmptrash);
5142 tmptrash = NULL;
5143 }
5144
5145 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5146 set_backend_down(s->proxy);
5147 }
5148 else if (s->cur_eweight != s->next_eweight) {
5149 /* now propagate the status change to any LB algorithms */
5150 if (px->lbprm.update_server_eweight)
5151 px->lbprm.update_server_eweight(s);
5152 else if (srv_willbe_usable(s)) {
5153 if (px->lbprm.set_server_status_up)
5154 px->lbprm.set_server_status_up(s);
5155 }
5156 else {
5157 if (px->lbprm.set_server_status_down)
5158 px->lbprm.set_server_status_down(s);
5159 }
5160
5161 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5162 set_backend_down(s->proxy);
5163 }
5164
5165 s->next_admin = next_admin;
5166 }
5167
Emeric Brun5a133512017-10-19 14:42:30 +02005168 /* reset operational state change */
5169 *s->op_st_chg.reason = 0;
5170 s->op_st_chg.status = s->op_st_chg.code = -1;
5171 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005172
5173 /* Now we try to apply pending admin changes */
5174
5175 /* Maintenance must also disable health checks */
5176 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
5177 if (s->check.state & CHK_ST_ENABLED) {
5178 s->check.state |= CHK_ST_PAUSED;
5179 check->health = 0;
5180 }
5181
5182 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02005183 tmptrash = alloc_trash_chunk();
5184 if (tmptrash) {
5185 chunk_printf(tmptrash,
5186 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5187 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5188 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005189
Olivier Houchard796a2b32017-10-24 17:42:47 +02005190 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005191
Olivier Houchard796a2b32017-10-24 17:42:47 +02005192 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005193 ha_warning("%s.\n", tmptrash->area);
5194 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5195 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005196 }
5197 free_trash_chunk(tmptrash);
5198 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005199 }
Emeric Brun8f298292017-12-06 16:47:17 +01005200 /* commit new admin status */
5201
5202 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005203 }
5204 else { /* server was still running */
5205 check->health = 0; /* failure */
5206 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005207
5208 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005209 if (s->proxy->lbprm.set_server_status_down)
5210 s->proxy->lbprm.set_server_status_down(s);
5211
Emeric Brun64cc49c2017-10-03 14:46:45 +02005212 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5213 srv_shutdown_streams(s, SF_ERR_DOWN);
5214
William Dauchy6318d332020-05-02 21:52:36 +02005215 /* force connection cleanup on the given server */
5216 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005217 /* we might have streams queued on this server and waiting for
5218 * a connection. Those which are redispatchable will be queued
5219 * to another server or to the proxy itself.
5220 */
5221 xferred = pendconn_redistribute(s);
5222
5223 tmptrash = alloc_trash_chunk();
5224 if (tmptrash) {
5225 chunk_printf(tmptrash,
5226 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5227 s->flags & SRV_F_BACKUP ? "Backup " : "",
5228 s->proxy->id, s->id,
5229 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5230
5231 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5232
5233 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005234 ha_warning("%s.\n", tmptrash->area);
5235 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5236 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005237 }
5238 free_trash_chunk(tmptrash);
5239 tmptrash = NULL;
5240 }
5241 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5242 set_backend_down(s->proxy);
5243
5244 s->counters.down_trans++;
5245 }
5246 }
5247 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5248 /* OK here we're leaving maintenance, we have many things to check,
5249 * because the server might possibly be coming back up depending on
5250 * its state. In practice, leaving maintenance means that we should
5251 * immediately turn to UP (more or less the slowstart) under the
5252 * following conditions :
5253 * - server is neither checked nor tracked
5254 * - server tracks another server which is not checked
5255 * - server tracks another server which is already up
5256 * Which sums up as something simpler :
5257 * "either the tracking server is up or the server's checks are disabled
5258 * or up". Otherwise we only re-enable health checks. There's a special
5259 * case associated to the stopping state which can be inherited. Note
5260 * that the server might still be in drain mode, which is naturally dealt
5261 * with by the lower level functions.
5262 */
5263
5264 if (s->check.state & CHK_ST_ENABLED) {
5265 s->check.state &= ~CHK_ST_PAUSED;
5266 check->health = check->rise; /* start OK but check immediately */
5267 }
5268
5269 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5270 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5271 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5272 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5273 s->next_state = SRV_ST_STOPPING;
5274 }
5275 else {
Willy Tarreaud332f132021-08-04 19:35:13 +02005276 s->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005277 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005278 if (s->slowstart > 0) {
5279 if (s->warmup)
5280 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5281 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005282 else
5283 s->next_state = SRV_ST_RUNNING;
5284 }
5285
5286 }
5287
5288 tmptrash = alloc_trash_chunk();
5289 if (tmptrash) {
5290 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5291 chunk_printf(tmptrash,
5292 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5293 s->flags & SRV_F_BACKUP ? "Backup " : "",
5294 s->proxy->id, s->id,
5295 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5296 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5297 }
5298 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5299 chunk_printf(tmptrash,
5300 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5301 s->flags & SRV_F_BACKUP ? "Backup " : "",
5302 s->proxy->id, s->id, s->hostname,
5303 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5304 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5305 }
5306 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5307 chunk_printf(tmptrash,
5308 "%sServer %s/%s is %s/%s (leaving maintenance)",
5309 s->flags & SRV_F_BACKUP ? "Backup " : "",
5310 s->proxy->id, s->id,
5311 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5312 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5313 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005314 ha_warning("%s.\n", tmptrash->area);
5315 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5316 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005317 free_trash_chunk(tmptrash);
5318 tmptrash = NULL;
5319 }
5320
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005321 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005322 /* now propagate the status change to any LB algorithms */
5323 if (px->lbprm.update_server_eweight)
5324 px->lbprm.update_server_eweight(s);
5325 else if (srv_willbe_usable(s)) {
5326 if (px->lbprm.set_server_status_up)
5327 px->lbprm.set_server_status_up(s);
5328 }
5329 else {
5330 if (px->lbprm.set_server_status_down)
5331 px->lbprm.set_server_status_down(s);
5332 }
5333
5334 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5335 set_backend_down(s->proxy);
Willy Tarreaud332f132021-08-04 19:35:13 +02005336 else if (!prev_srv_count && (s->proxy->srv_bck || s->proxy->srv_act))
5337 s->proxy->last_change = now.tv_sec;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005338
Willy Tarreau6a78e612018-08-07 10:14:53 +02005339 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5340 * and it's not a backup server and its effective weight is > 0,
5341 * then it can accept new connections, so we shut down all streams
5342 * on all backup servers.
5343 */
5344 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5345 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5346 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5347
5348 /* check if we can handle some connections queued at the proxy. We
5349 * will take as many as we can handle.
5350 */
5351 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005352 }
5353 else if (s->next_admin & SRV_ADMF_MAINT) {
5354 /* remaining in maintenance mode, let's inform precisely about the
5355 * situation.
5356 */
5357 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5358 tmptrash = alloc_trash_chunk();
5359 if (tmptrash) {
5360 chunk_printf(tmptrash,
5361 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5362 s->flags & SRV_F_BACKUP ? "Backup " : "",
5363 s->proxy->id, s->id);
5364
5365 if (s->track) /* normally it's mandatory here */
5366 chunk_appendf(tmptrash, " via %s/%s",
5367 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005368 ha_warning("%s.\n", tmptrash->area);
5369 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5370 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005371 free_trash_chunk(tmptrash);
5372 tmptrash = NULL;
5373 }
5374 }
5375 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5376 tmptrash = alloc_trash_chunk();
5377 if (tmptrash) {
5378 chunk_printf(tmptrash,
5379 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5380 s->flags & SRV_F_BACKUP ? "Backup " : "",
5381 s->proxy->id, s->id, s->hostname);
5382
5383 if (s->track) /* normally it's mandatory here */
5384 chunk_appendf(tmptrash, " via %s/%s",
5385 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005386 ha_warning("%s.\n", tmptrash->area);
5387 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5388 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005389 free_trash_chunk(tmptrash);
5390 tmptrash = NULL;
5391 }
5392 }
5393 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5394 tmptrash = alloc_trash_chunk();
5395 if (tmptrash) {
5396 chunk_printf(tmptrash,
5397 "%sServer %s/%s remains in forced maintenance",
5398 s->flags & SRV_F_BACKUP ? "Backup " : "",
5399 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005400 ha_warning("%s.\n", tmptrash->area);
5401 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5402 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005403 free_trash_chunk(tmptrash);
5404 tmptrash = NULL;
5405 }
5406 }
5407 /* don't report anything when leaving drain mode and remaining in maintenance */
5408
5409 s->cur_admin = s->next_admin;
5410 }
5411
5412 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5413 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5414 /* drain state is applied only if not yet in maint */
5415
5416 s->last_change = now.tv_sec;
5417 if (px->lbprm.set_server_status_down)
5418 px->lbprm.set_server_status_down(s);
5419
5420 /* we might have streams queued on this server and waiting for
5421 * a connection. Those which are redispatchable will be queued
5422 * to another server or to the proxy itself.
5423 */
5424 xferred = pendconn_redistribute(s);
5425
5426 tmptrash = alloc_trash_chunk();
5427 if (tmptrash) {
5428 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5429 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5430 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5431
5432 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5433
5434 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005435 ha_warning("%s.\n", tmptrash->area);
5436 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5437 tmptrash->area);
5438 send_email_alert(s, LOG_NOTICE, "%s",
5439 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005440 }
5441 free_trash_chunk(tmptrash);
5442 tmptrash = NULL;
5443 }
5444
5445 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5446 set_backend_down(s->proxy);
5447 }
5448 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5449 /* OK completely leaving drain mode */
5450 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5451 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5452 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5453 s->proxy->last_change = now.tv_sec;
5454 }
5455
5456 if (s->last_change < now.tv_sec) // ignore negative times
5457 s->down_time += now.tv_sec - s->last_change;
5458 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005459 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005460
5461 tmptrash = alloc_trash_chunk();
5462 if (tmptrash) {
5463 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5464 chunk_printf(tmptrash,
5465 "%sServer %s/%s is %s (leaving forced drain)",
5466 s->flags & SRV_F_BACKUP ? "Backup " : "",
5467 s->proxy->id, s->id,
5468 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5469 }
5470 else {
5471 chunk_printf(tmptrash,
5472 "%sServer %s/%s is %s (leaving drain)",
5473 s->flags & SRV_F_BACKUP ? "Backup " : "",
5474 s->proxy->id, s->id,
5475 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5476 if (s->track) /* normally it's mandatory here */
5477 chunk_appendf(tmptrash, " via %s/%s",
5478 s->track->proxy->id, s->track->id);
5479 }
5480
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005481 ha_warning("%s.\n", tmptrash->area);
5482 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5483 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005484 free_trash_chunk(tmptrash);
5485 tmptrash = NULL;
5486 }
5487
5488 /* now propagate the status change to any LB algorithms */
5489 if (px->lbprm.update_server_eweight)
5490 px->lbprm.update_server_eweight(s);
5491 else if (srv_willbe_usable(s)) {
5492 if (px->lbprm.set_server_status_up)
5493 px->lbprm.set_server_status_up(s);
5494 }
5495 else {
5496 if (px->lbprm.set_server_status_down)
5497 px->lbprm.set_server_status_down(s);
5498 }
5499 }
5500 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5501 /* remaining in drain mode after removing one of its flags */
5502
5503 tmptrash = alloc_trash_chunk();
5504 if (tmptrash) {
5505 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5506 chunk_printf(tmptrash,
5507 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5508 s->flags & SRV_F_BACKUP ? "Backup " : "",
5509 s->proxy->id, s->id);
5510
5511 if (s->track) /* normally it's mandatory here */
5512 chunk_appendf(tmptrash, " via %s/%s",
5513 s->track->proxy->id, s->track->id);
5514 }
5515 else {
5516 chunk_printf(tmptrash,
5517 "%sServer %s/%s remains in forced drain mode",
5518 s->flags & SRV_F_BACKUP ? "Backup " : "",
5519 s->proxy->id, s->id);
5520 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005521 ha_warning("%s.\n", tmptrash->area);
5522 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5523 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005524 free_trash_chunk(tmptrash);
5525 tmptrash = NULL;
5526 }
5527
5528 /* commit new admin status */
5529
5530 s->cur_admin = s->next_admin;
5531 }
5532 }
5533
5534 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005535 *s->adm_st_chg_cause = 0;
5536}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005537
Willy Tarreau144f84a2021-03-02 16:09:26 +01005538struct task *srv_cleanup_toremove_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005539{
5540 struct connection *conn;
5541
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005542 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005543 struct connection *, toremove_list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005544 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005545 }
5546
5547 return task;
5548}
5549
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005550/* Move toremove_nb connections from idle_tree to toremove_list, -1 means
Olivier Houchardff1d0922020-06-29 20:15:59 +02005551 * moving them all.
5552 * Returns the number of connections moved.
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005553 *
5554 * Must be called with idle_conns_lock held.
Olivier Houchardff1d0922020-06-29 20:15:59 +02005555 */
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005556static 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 +02005557{
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005558 struct eb_node *node, *next;
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005559 struct conn_hash_node *hash_node;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005560 int i = 0;
5561
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005562 node = eb_first(idle_tree);
5563 while (node) {
5564 next = eb_next(node);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005565 if (toremove_nb != -1 && i >= toremove_nb)
5566 break;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005567
Amaury Denoyelle8990b012021-02-19 15:29:16 +01005568 hash_node = ebmb_entry(node, struct conn_hash_node, node);
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005569 eb_delete(node);
Willy Tarreau2b718102021-04-21 07:32:39 +02005570 MT_LIST_APPEND(toremove_list, &hash_node->conn->toremove_list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005571 i++;
Amaury Denoyellef232cb32021-01-06 16:14:12 +01005572
5573 node = next;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005574 }
5575 return i;
5576}
William Dauchy6318d332020-05-02 21:52:36 +02005577/* cleanup connections for a given server
5578 * might be useful when going on forced maintenance or live changing ip/port
5579 */
William Dauchy707ad322020-05-04 13:52:40 +02005580static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005581{
William Dauchy6318d332020-05-02 21:52:36 +02005582 int did_remove;
5583 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005584
Amaury Denoyelle10d5c312021-01-06 14:28:51 +01005585 /* nothing to do if pool-max-conn is null */
5586 if (!srv->max_idle_conns)
5587 return;
5588
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005589 /* check all threads starting with ours */
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005590 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005591 did_remove = 0;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005592 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005593 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 +02005594 did_remove = 1;
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005595 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 +02005596 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005597 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
William Dauchy6318d332020-05-02 21:52:36 +02005598 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005599 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005600
5601 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5602 break;
William Dauchy6318d332020-05-02 21:52:36 +02005603 }
William Dauchy6318d332020-05-02 21:52:36 +02005604}
5605
Willy Tarreau144f84a2021-03-02 16:09:26 +01005606struct task *srv_cleanup_idle_conns(struct task *task, void *context, unsigned int state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005607{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005608 struct server *srv;
5609 struct eb32_node *eb;
5610 int i;
5611 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005612
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005613 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005614 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5615 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005616 int exceed_conns;
5617 int to_kill;
5618 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005619
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005620 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5621 if (!eb) {
5622 /* we might have reached the end of the tree, typically because
5623 * <now_ms> is in the first half and we're first scanning the last
5624 * half. Let's loop back to the beginning of the tree now.
5625 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005626
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005627 eb = eb32_first(&idle_conn_srv);
5628 if (likely(!eb))
5629 break;
5630 }
5631 if (tick_is_lt(now_ms, eb->key)) {
5632 /* timer not expired yet, revisit it later */
5633 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005634 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005635 }
5636 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005637
5638 /* Calculate how many idle connections we want to kill :
5639 * we want to remove half the difference between the total
5640 * of established connections (used or idle) and the max
5641 * number of used connections.
5642 */
5643 curr_idle = srv->curr_idle_conns;
5644 if (curr_idle == 0)
5645 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005646 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005647 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005648
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005649 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005650 if (srv->est_need_conns < srv->max_used_conns)
5651 srv->est_need_conns = srv->max_used_conns;
5652
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005653 srv->max_used_conns = srv->curr_used_conns;
5654
Willy Tarreau18ed7892020-07-02 19:05:30 +02005655 if (exceed_conns <= 0)
5656 goto remove;
5657
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005658 /* check all threads starting with ours */
5659 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005660 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005661 int j;
5662 int did_remove = 0;
5663
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005664 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5665 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005666
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005667 HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005668 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 +02005669 if (j > 0)
5670 did_remove = 1;
5671 if (max_conn - j > 0 &&
Willy Tarreau430bf4a2021-03-04 09:45:32 +01005672 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 +01005673 did_remove = 1;
Amaury Denoyelle5c7086f2021-01-11 09:21:52 +01005674 HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[i].idle_conns_lock);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005675
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005676 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005677 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005678
5679 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5680 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005681 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005682remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005683 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005684
5685 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005686 /* There are still more idle connections, add the
5687 * server back in the tree.
5688 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005689 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005690 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005691 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005692 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005693 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005694 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5695
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005696 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005697 return task;
5698}
Olivier Houchard88698d92019-04-16 19:07:22 +02005699
Amaury Denoyelle3109ccf2021-04-29 17:30:05 +02005700/* Close remaining idle connections. This functions is designed to be run on
5701 * process shutdown. This guarantees a proper socket shutdown to avoid
5702 * TIME_WAIT state. For a quick operation, only ctrl is closed, xprt stack is
5703 * bypassed.
5704 *
5705 * This function is not thread-safe so it must only be called via a global
5706 * deinit function.
5707 */
5708static void srv_close_idle_conns(struct server *srv)
5709{
5710 struct eb_root **cleaned_tree;
5711 int i;
5712
5713 for (i = 0; i < global.nbthread; ++i) {
5714 struct eb_root *conn_trees[] = {
5715 &srv->per_thr[i].idle_conns,
5716 &srv->per_thr[i].safe_conns,
5717 &srv->per_thr[i].avail_conns,
5718 NULL
5719 };
5720
5721 for (cleaned_tree = conn_trees; *cleaned_tree; ++cleaned_tree) {
5722 while (!eb_is_empty(*cleaned_tree)) {
5723 struct ebmb_node *node = ebmb_first(*cleaned_tree);
5724 struct conn_hash_node *conn_hash_node = ebmb_entry(node, struct conn_hash_node, node);
5725 struct connection *conn = conn_hash_node->conn;
5726
5727 if (conn->ctrl->ctrl_close)
5728 conn->ctrl->ctrl_close(conn);
5729 ebmb_delete(node);
5730 }
5731 }
5732 }
5733}
5734
5735REGISTER_SERVER_DEINIT(srv_close_idle_conns);
5736
Willy Tarreau76cc6992020-07-01 18:49:24 +02005737/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
5738static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01005739 const struct proxy *defpx, const char *file, int line,
Willy Tarreau76cc6992020-07-01 18:49:24 +02005740 char **err)
5741{
5742 if (too_many_args(1, args, err, NULL))
5743 return -1;
5744
5745 if (strcmp(args[1], "on") == 0)
5746 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
5747 else if (strcmp(args[1], "off") == 0)
5748 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
5749 else {
5750 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
5751 return -1;
5752 }
5753 return 0;
5754}
5755
Olivier Houchard88698d92019-04-16 19:07:22 +02005756/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5757static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01005758 const struct proxy *defpx, const char *file, int line,
Olivier Houchard88698d92019-04-16 19:07:22 +02005759 char **err)
5760{
5761 int arg = -1;
5762
5763 if (too_many_args(1, args, err, NULL))
5764 return -1;
5765
5766 if (*(args[1]) != 0)
5767 arg = atoi(args[1]);
5768
5769 if (arg < 0 || arg > 100) {
5770 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5771 return -1;
5772 }
5773
5774 if (args[0][10] == 'h')
5775 global.tune.pool_high_ratio = arg;
5776 else
5777 global.tune.pool_low_ratio = arg;
5778 return 0;
5779}
5780
5781/* config keyword parsers */
5782static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02005783 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02005784 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5785 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5786 { 0, NULL, NULL }
5787}};
5788
5789INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5790
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005791/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005792 * Local variables:
5793 * c-indent-level: 8
5794 * c-basic-offset: 8
5795 * End:
5796 */