blob: 15dd73b20cb21b1786a7a96dbccf7e3e8aea9950 [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 <import/ebsttree.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <import/xxhash.h>
21
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020023#include <haproxy/applet-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020024#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020025#include <haproxy/cfgparse.h>
Willy Tarreau4aa573d2020-06-04 18:21:56 +020026#include <haproxy/check.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020027#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020028#include <haproxy/connection.h>
Willy Tarreau3afc4c42020-06-03 18:23:19 +020029#include <haproxy/dict-t.h>
Willy Tarreaueb92deb2020-06-04 10:53:16 +020030#include <haproxy/dns.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020031#include <haproxy/errors.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020032#include <haproxy/global.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020033#include <haproxy/log.h>
Willy Tarreaucee013e2020-06-05 11:40:38 +020034#include <haproxy/mailers.h>
Willy Tarreau7a00efb2020-06-02 17:02:59 +020035#include <haproxy/namespace.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020036#include <haproxy/port_range.h>
37#include <haproxy/protocol.h>
Willy Tarreaua55c4542020-06-04 22:59:39 +020038#include <haproxy/queue.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020039#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020040#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020041#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020042#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020043#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020044#include <haproxy/task.h>
Willy Tarreau51cd5952020-06-05 12:25:38 +020045#include <haproxy/tcpcheck.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020046#include <haproxy/time.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020047
Baptiste Assmannda29fe22019-06-13 13:24:29 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020053static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
54static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020055static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau21faa912012-10-10 08:27:36 +020057/* List head of all known server keywords */
58static struct srv_kw_list srv_keywords = {
59 .list = LIST_HEAD_INIT(srv_keywords.list)
60};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061
Willy Tarreauaf613e82020-06-05 08:40:51 +020062__decl_thread(HA_SPINLOCK_T idle_conn_srv_lock);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010063struct eb_root idle_conn_srv = EB_ROOT;
64struct task *idle_conn_task = NULL;
Olivier Houchard9ea5d362019-02-14 18:29:09 +010065
Frédéric Lécaille7da71292019-05-20 09:47:07 +020066/* The server names dictionary */
67struct dict server_name_dict = {
68 .name = "server names",
69 .values = EB_ROOT_UNIQUE,
70};
71
Baptiste Assmannda29fe22019-06-13 13:24:29 +020072/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010073struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020074
Simon Hormana3608442013-11-01 16:46:15 +090075int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020076{
Amaury Denoyellee6ba7912020-10-29 15:59:05 +010077 if ((s->cur_state != SRV_ST_STOPPED) || s->last_change >= now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020078 return s->down_time;
79
80 return now.tv_sec - s->last_change + s->down_time;
81}
Willy Tarreaubaaee002006-06-26 02:48:02 +020082
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050083int srv_lastsession(const struct server *s)
84{
85 if (s->counters.last_sess)
86 return now.tv_sec - s->counters.last_sess;
87
88 return -1;
89}
90
Simon Horman4a741432013-02-23 15:35:38 +090091int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020092{
Simon Horman4a741432013-02-23 15:35:38 +090093 const struct server *s = check->server;
94
Willy Tarreauff5ae352013-12-11 20:36:34 +010095 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090096 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010097
Emeric Brun52a91d32017-08-31 14:41:55 +020098 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090099 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100100
Simon Horman4a741432013-02-23 15:35:38 +0900101 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100102}
103
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100104/*
105 * Check that we did not get a hash collision.
Willy Tarreau595e7672020-10-20 17:30:08 +0200106 * Unlikely, but it can happen. The server's proxy must be at least
107 * read-locked.
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100108 */
109static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100110{
111 struct proxy *p = s->proxy;
112 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100113
114 for (tmpserv = p->srv; tmpserv != NULL;
115 tmpserv = tmpserv->next) {
116 if (tmpserv == s)
117 continue;
118 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
119 continue;
120 if (tmpserv->cookie &&
121 strcmp(tmpserv->cookie, s->cookie) == 0) {
122 ha_warning("We generated two equal cookies for two different servers.\n"
123 "Please change the secret key for '%s'.\n",
124 s->proxy->id);
125 }
126 }
127
128}
129
Willy Tarreau46b7f532018-08-21 11:54:26 +0200130/*
Willy Tarreau595e7672020-10-20 17:30:08 +0200131 * Must be called with the server lock held, and will read-lock the proxy.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200132 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100133void srv_set_dyncookie(struct server *s)
134{
135 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100136 char *tmpbuf;
137 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100138 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100139 size_t buffer_len;
140 int addr_len;
141 int port;
142
Willy Tarreau595e7672020-10-20 17:30:08 +0200143 HA_RWLOCK_RDLOCK(PROXY_LOCK, &p->lock);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200144
Olivier Houchard4e694042017-03-14 20:01:29 +0100145 if ((s->flags & SRV_F_COOKIESET) ||
146 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
147 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200148 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100149 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100150
151 if (s->addr.ss_family != AF_INET &&
152 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200153 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100154 /*
155 * Buffer to calculate the cookie value.
156 * The buffer contains the secret key + the server IP address
157 * + the TCP port.
158 */
159 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
160 /*
161 * The TCP port should use only 2 bytes, but is stored in
162 * an unsigned int in struct server, so let's use 4, to be
163 * on the safe side.
164 */
165 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200166 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100167 memcpy(tmpbuf, p->dyncookie_key, key_len);
168 memcpy(&(tmpbuf[key_len]),
169 s->addr.ss_family == AF_INET ?
170 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
171 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
172 addr_len);
173 /*
174 * Make sure it's the same across all the load balancers,
175 * no matter their endianness.
176 */
177 port = htonl(s->svc_port);
178 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
179 hash_value = XXH64(tmpbuf, buffer_len, 0);
180 memprintf(&s->cookie, "%016llx", hash_value);
181 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200182 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100183 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100184
185 /* Don't bother checking if the dyncookie is duplicated if
186 * the server is marked as "disabled", maybe it doesn't have
187 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100188 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100189 if (!(s->next_admin & SRV_ADMF_FMAINT))
190 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200191 out:
Willy Tarreau595e7672020-10-20 17:30:08 +0200192 HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100193}
194
Willy Tarreau21faa912012-10-10 08:27:36 +0200195/*
196 * Registers the server keyword list <kwl> as a list of valid keywords for next
197 * parsing sessions.
198 */
199void srv_register_keywords(struct srv_kw_list *kwl)
200{
201 LIST_ADDQ(&srv_keywords.list, &kwl->list);
202}
203
204/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
205 * keyword is found with a NULL ->parse() function, then an attempt is made to
206 * find one with a valid ->parse() function. This way it is possible to declare
207 * platform-dependant, known keywords as NULL, then only declare them as valid
208 * if some options are met. Note that if the requested keyword contains an
209 * opening parenthesis, everything from this point is ignored.
210 */
211struct srv_kw *srv_find_kw(const char *kw)
212{
213 int index;
214 const char *kwend;
215 struct srv_kw_list *kwl;
216 struct srv_kw *ret = NULL;
217
218 kwend = strchr(kw, '(');
219 if (!kwend)
220 kwend = kw + strlen(kw);
221
222 list_for_each_entry(kwl, &srv_keywords.list, list) {
223 for (index = 0; kwl->kw[index].kw != NULL; index++) {
224 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
225 kwl->kw[index].kw[kwend-kw] == 0) {
226 if (kwl->kw[index].parse)
227 return &kwl->kw[index]; /* found it !*/
228 else
229 ret = &kwl->kw[index]; /* may be OK */
230 }
231 }
232 }
233 return ret;
234}
235
236/* Dumps all registered "server" keywords to the <out> string pointer. The
237 * unsupported keywords are only dumped if their supported form was not
238 * found.
239 */
240void srv_dump_kws(char **out)
241{
242 struct srv_kw_list *kwl;
243 int index;
244
Christopher Faulet784063e2020-05-18 12:14:18 +0200245 if (!out)
246 return;
247
Willy Tarreau21faa912012-10-10 08:27:36 +0200248 *out = NULL;
249 list_for_each_entry(kwl, &srv_keywords.list, list) {
250 for (index = 0; kwl->kw[index].kw != NULL; index++) {
251 if (kwl->kw[index].parse ||
252 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
253 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
254 kwl->scope,
255 kwl->kw[index].kw,
256 kwl->kw[index].skip ? " <arg>" : "",
257 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
258 kwl->kw[index].parse ? "" : " (not supported)");
259 }
260 }
261 }
262}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100263
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100264/* Parse the "backup" server keyword */
265static int srv_parse_backup(char **args, int *cur_arg,
266 struct proxy *curproxy, struct server *newsrv, char **err)
267{
268 newsrv->flags |= SRV_F_BACKUP;
269 return 0;
270}
271
Alexander Liu2a54bb72019-05-22 19:44:48 +0800272
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100273/* Parse the "cookie" server keyword */
274static int srv_parse_cookie(char **args, int *cur_arg,
275 struct proxy *curproxy, struct server *newsrv, char **err)
276{
277 char *arg;
278
279 arg = args[*cur_arg + 1];
280 if (!*arg) {
281 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
282 return ERR_ALERT | ERR_FATAL;
283 }
284
285 free(newsrv->cookie);
286 newsrv->cookie = strdup(arg);
287 newsrv->cklen = strlen(arg);
288 newsrv->flags |= SRV_F_COOKIESET;
289 return 0;
290}
291
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100292/* Parse the "disabled" server keyword */
293static int srv_parse_disabled(char **args, int *cur_arg,
294 struct proxy *curproxy, struct server *newsrv, char **err)
295{
Emeric Brun52a91d32017-08-31 14:41:55 +0200296 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
297 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100298 newsrv->check.state |= CHK_ST_PAUSED;
299 newsrv->check.health = 0;
300 return 0;
301}
302
303/* Parse the "enabled" server keyword */
304static int srv_parse_enabled(char **args, int *cur_arg,
305 struct proxy *curproxy, struct server *newsrv, char **err)
306{
Emeric Brun52a91d32017-08-31 14:41:55 +0200307 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
308 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100309 newsrv->check.state &= ~CHK_ST_PAUSED;
310 newsrv->check.health = newsrv->check.rise;
311 return 0;
312}
313
Willy Tarreau9c538e02019-01-23 10:21:49 +0100314static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
315{
316 char *arg;
317
318 arg = args[*cur_arg + 1];
319 if (!*arg) {
320 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
321 return ERR_ALERT | ERR_FATAL;
322 }
323 newsrv->max_reuse = atoi(arg);
324
325 return 0;
326}
327
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100328static 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 +0100329{
330 const char *res;
331 char *arg;
332 unsigned int time;
333
334 arg = args[*cur_arg + 1];
335 if (!*arg) {
336 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
337 return ERR_ALERT | ERR_FATAL;
338 }
339 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200340 if (res == PARSE_TIME_OVER) {
341 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
342 args[*cur_arg+1], args[*cur_arg]);
343 return ERR_ALERT | ERR_FATAL;
344 }
345 else if (res == PARSE_TIME_UNDER) {
346 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
347 args[*cur_arg+1], args[*cur_arg]);
348 return ERR_ALERT | ERR_FATAL;
349 }
350 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100351 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
352 *res, args[*cur_arg]);
353 return ERR_ALERT | ERR_FATAL;
354 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100355 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100356
357 return 0;
358}
359
Willy Tarreau2f3f4d32020-07-01 07:43:51 +0200360static int srv_parse_pool_low_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
361{
362 char *arg;
363
364 arg = args[*cur_arg + 1];
365 if (!*arg) {
366 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
367 return ERR_ALERT | ERR_FATAL;
368 }
369
370 newsrv->low_idle_conns = atoi(arg);
371 return 0;
372}
373
Olivier Houchard006e3102018-12-10 18:30:32 +0100374static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
375{
376 char *arg;
377
378 arg = args[*cur_arg + 1];
379 if (!*arg) {
380 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
381 return ERR_ALERT | ERR_FATAL;
382 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100383
Olivier Houchard006e3102018-12-10 18:30:32 +0100384 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100385 if ((int)newsrv->max_idle_conns < -1) {
386 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
387 return ERR_ALERT | ERR_FATAL;
388 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100389
390 return 0;
391}
392
Willy Tarreaudff55432012-10-10 17:51:05 +0200393/* parse the "id" server keyword */
394static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
395{
396 struct eb32_node *node;
397
398 if (!*args[*cur_arg + 1]) {
399 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
400 return ERR_ALERT | ERR_FATAL;
401 }
402
403 newsrv->puid = atol(args[*cur_arg + 1]);
404 newsrv->conf.id.key = newsrv->puid;
405
406 if (newsrv->puid <= 0) {
407 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
408 return ERR_ALERT | ERR_FATAL;
409 }
410
411 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
412 if (node) {
413 struct server *target = container_of(node, struct server, conf.id);
414 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
415 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
416 target->id);
417 return ERR_ALERT | ERR_FATAL;
418 }
419
420 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200421 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200422 return 0;
423}
424
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100425/* Parse the "namespace" server keyword */
426static int srv_parse_namespace(char **args, int *cur_arg,
427 struct proxy *curproxy, struct server *newsrv, char **err)
428{
Willy Tarreaue5733232019-05-22 19:24:06 +0200429#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100430 char *arg;
431
432 arg = args[*cur_arg + 1];
433 if (!*arg) {
434 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
435 return ERR_ALERT | ERR_FATAL;
436 }
437
438 if (!strcmp(arg, "*")) {
439 /* Use the namespace associated with the connection (if present). */
440 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
441 return 0;
442 }
443
444 /*
445 * As this parser may be called several times for the same 'default-server'
446 * object, or for a new 'server' instance deriving from a 'default-server'
447 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
448 */
449 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
450
451 newsrv->netns = netns_store_lookup(arg, strlen(arg));
452 if (!newsrv->netns)
453 newsrv->netns = netns_store_insert(arg);
454
455 if (!newsrv->netns) {
456 memprintf(err, "Cannot open namespace '%s'", arg);
457 return ERR_ALERT | ERR_FATAL;
458 }
459
460 return 0;
461#else
462 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
463 return ERR_ALERT | ERR_FATAL;
464#endif
465}
466
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100467/* Parse the "no-backup" server keyword */
468static int srv_parse_no_backup(char **args, int *cur_arg,
469 struct proxy *curproxy, struct server *newsrv, char **err)
470{
471 newsrv->flags &= ~SRV_F_BACKUP;
472 return 0;
473}
474
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100475
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100476/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200477static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100478{
479 srv->pp_opts &= ~flags;
480 return 0;
481}
482
483/* Parse the "no-send-proxy" server keyword */
484static int srv_parse_no_send_proxy(char **args, int *cur_arg,
485 struct proxy *curproxy, struct server *newsrv, char **err)
486{
487 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
488}
489
490/* Parse the "no-send-proxy-v2" server keyword */
491static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
492 struct proxy *curproxy, struct server *newsrv, char **err)
493{
494 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
495}
496
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200497/* Parse the "no-tfo" server keyword */
498static int srv_parse_no_tfo(char **args, int *cur_arg,
499 struct proxy *curproxy, struct server *newsrv, char **err)
500{
501 newsrv->flags &= ~SRV_F_FASTOPEN;
502 return 0;
503}
504
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100505/* Parse the "non-stick" server keyword */
506static int srv_parse_non_stick(char **args, int *cur_arg,
507 struct proxy *curproxy, struct server *newsrv, char **err)
508{
509 newsrv->flags |= SRV_F_NON_STICK;
510 return 0;
511}
512
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100513/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200514static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100515{
516 srv->pp_opts |= flags;
517 return 0;
518}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200519/* parse the "proto" server keyword */
520static int srv_parse_proto(char **args, int *cur_arg,
521 struct proxy *px, struct server *newsrv, char **err)
522{
523 struct ist proto;
524
525 if (!*args[*cur_arg + 1]) {
526 memprintf(err, "'%s' : missing value", args[*cur_arg]);
527 return ERR_ALERT | ERR_FATAL;
528 }
529 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
530 newsrv->mux_proto = get_mux_proto(proto);
531 if (!newsrv->mux_proto) {
532 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
533 return ERR_ALERT | ERR_FATAL;
534 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200535 return 0;
536}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100537
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100538/* parse the "proxy-v2-options" */
539static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
540 struct proxy *px, struct server *newsrv, char **err)
541{
542 char *p, *n;
543 for (p = args[*cur_arg+1]; p; p = n) {
544 n = strchr(p, ',');
545 if (n)
546 *n++ = '\0';
547 if (!strcmp(p, "ssl")) {
548 newsrv->pp_opts |= SRV_PP_V2_SSL;
549 } else if (!strcmp(p, "cert-cn")) {
550 newsrv->pp_opts |= SRV_PP_V2_SSL;
551 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100552 } else if (!strcmp(p, "cert-key")) {
553 newsrv->pp_opts |= SRV_PP_V2_SSL;
554 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
555 } else if (!strcmp(p, "cert-sig")) {
556 newsrv->pp_opts |= SRV_PP_V2_SSL;
557 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
558 } else if (!strcmp(p, "ssl-cipher")) {
559 newsrv->pp_opts |= SRV_PP_V2_SSL;
560 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100561 } else if (!strcmp(p, "authority")) {
562 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100563 } else if (!strcmp(p, "crc32c")) {
564 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100565 } else if (!strcmp(p, "unique-id")) {
566 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100567 } else
568 goto fail;
569 }
570 return 0;
571 fail:
572 if (err)
573 memprintf(err, "'%s' : proxy v2 option not implemented", p);
574 return ERR_ALERT | ERR_FATAL;
575}
576
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100577/* Parse the "observe" server keyword */
578static int srv_parse_observe(char **args, int *cur_arg,
579 struct proxy *curproxy, struct server *newsrv, char **err)
580{
581 char *arg;
582
583 arg = args[*cur_arg + 1];
584 if (!*arg) {
585 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
586 return ERR_ALERT | ERR_FATAL;
587 }
588
589 if (!strcmp(arg, "none")) {
590 newsrv->observe = HANA_OBS_NONE;
591 }
592 else if (!strcmp(arg, "layer4")) {
593 newsrv->observe = HANA_OBS_LAYER4;
594 }
595 else if (!strcmp(arg, "layer7")) {
596 if (curproxy->mode != PR_MODE_HTTP) {
597 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
598 return ERR_ALERT;
599 }
600 newsrv->observe = HANA_OBS_LAYER7;
601 }
602 else {
603 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
604 "but got '%s'\n", args[*cur_arg], arg);
605 return ERR_ALERT | ERR_FATAL;
606 }
607
608 return 0;
609}
610
Frédéric Lécaille16186232017-03-14 16:42:49 +0100611/* Parse the "redir" server keyword */
612static int srv_parse_redir(char **args, int *cur_arg,
613 struct proxy *curproxy, struct server *newsrv, char **err)
614{
615 char *arg;
616
617 arg = args[*cur_arg + 1];
618 if (!*arg) {
619 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
620 return ERR_ALERT | ERR_FATAL;
621 }
622
623 free(newsrv->rdr_pfx);
624 newsrv->rdr_pfx = strdup(arg);
625 newsrv->rdr_len = strlen(arg);
626
627 return 0;
628}
629
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100630/* Parse the "send-proxy" server keyword */
631static int srv_parse_send_proxy(char **args, int *cur_arg,
632 struct proxy *curproxy, struct server *newsrv, char **err)
633{
634 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
635}
636
637/* Parse the "send-proxy-v2" server keyword */
638static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
639 struct proxy *curproxy, struct server *newsrv, char **err)
640{
641 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
642}
643
Frédéric Lécailledba97072017-03-17 15:33:50 +0100644
645/* Parse the "source" server keyword */
646static int srv_parse_source(char **args, int *cur_arg,
647 struct proxy *curproxy, struct server *newsrv, char **err)
648{
649 char *errmsg;
650 int port_low, port_high;
651 struct sockaddr_storage *sk;
Frédéric Lécailledba97072017-03-17 15:33:50 +0100652
653 errmsg = NULL;
654
655 if (!*args[*cur_arg + 1]) {
656 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
657 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
658 goto err;
659 }
660
661 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200662 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
663 &errmsg, NULL, NULL,
664 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 +0100665 if (!sk) {
666 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
667 goto err;
668 }
669
Frédéric Lécailledba97072017-03-17 15:33:50 +0100670 newsrv->conn_src.opts |= CO_SRC_BIND;
671 newsrv->conn_src.source_addr = *sk;
672
673 if (port_low != port_high) {
674 int i;
675
Frédéric Lécailledba97072017-03-17 15:33:50 +0100676 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
Remi Tricot-Le Bretonaf821672021-05-12 09:44:06 +0200677 if (!newsrv->conn_src.sport_range) {
678 ha_alert("Server '%s': Out of memory (sport_range)\n", args[0]);
679 goto err;
680 }
Frédéric Lécailledba97072017-03-17 15:33:50 +0100681 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
682 newsrv->conn_src.sport_range->ports[i] = port_low + i;
683 }
684
685 *cur_arg += 2;
686 while (*(args[*cur_arg])) {
687 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
688#if defined(CONFIG_HAP_TRANSPARENT)
689 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100690 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
691 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100692 goto err;
693 }
694 if (!strcmp(args[*cur_arg + 1], "client")) {
695 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
696 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
697 }
698 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
699 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
700 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
701 }
702 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
703 char *name, *end;
704
705 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100706 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100707 name++;
708
709 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100710 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100711 end++;
712
713 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
714 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
715 free(newsrv->conn_src.bind_hdr_name);
716 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
Remi Tricot-Le Bretonaf821672021-05-12 09:44:06 +0200717 if (!newsrv->conn_src.bind_hdr_name) {
718 ha_alert("Server '%s': Out of memory (bind_hdr_name)\n", args[0]);
719 goto err;
720 }
Frédéric Lécailledba97072017-03-17 15:33:50 +0100721 newsrv->conn_src.bind_hdr_len = end - name;
722 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
723 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
724 newsrv->conn_src.bind_hdr_occ = -1;
725
726 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100727 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100728 end++;
729 if (*end == ',') {
730 end++;
731 name = end;
732 if (*end == '-')
733 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100734 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100735 end++;
736 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
737 }
738
739 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100740 ha_alert("usesrc hdr_ip(name,num) does not support negative"
741 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100742 goto err;
743 }
744 }
745 else {
746 struct sockaddr_storage *sk;
747 int port1, port2;
748
749 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200750 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, NULL, NULL,
751 &errmsg, NULL, NULL,
752 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_STREAM | PA_O_CONNECT);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100754 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100755 goto err;
756 }
757
Frédéric Lécailledba97072017-03-17 15:33:50 +0100758 newsrv->conn_src.tproxy_addr = *sk;
759 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
760 }
761 global.last_checks |= LSTCHK_NETADM;
762 *cur_arg += 2;
763 continue;
764#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100765 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 +0100766 goto err;
767#endif /* defined(CONFIG_HAP_TRANSPARENT) */
768 } /* "usesrc" */
769
770 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
771#ifdef SO_BINDTODEVICE
772 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100773 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100774 goto err;
775 }
776 free(newsrv->conn_src.iface_name);
777 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
778 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
779 global.last_checks |= LSTCHK_NETADM;
780#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100781 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100782 goto err;
783#endif
784 *cur_arg += 2;
785 continue;
786 }
787 /* this keyword in not an option of "source" */
788 break;
789 } /* while */
790
791 return 0;
792
793 err:
794 free(errmsg);
795 return ERR_ALERT | ERR_FATAL;
796}
797
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100798/* Parse the "stick" server keyword */
799static int srv_parse_stick(char **args, int *cur_arg,
800 struct proxy *curproxy, struct server *newsrv, char **err)
801{
802 newsrv->flags &= ~SRV_F_NON_STICK;
803 return 0;
804}
805
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100806/* Parse the "track" server keyword */
807static int srv_parse_track(char **args, int *cur_arg,
808 struct proxy *curproxy, struct server *newsrv, char **err)
809{
810 char *arg;
811
812 arg = args[*cur_arg + 1];
813 if (!*arg) {
814 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
815 return ERR_ALERT | ERR_FATAL;
816 }
817
818 free(newsrv->trackit);
819 newsrv->trackit = strdup(arg);
820
821 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800822}
823
824/* Parse the "socks4" server keyword */
825static int srv_parse_socks4(char **args, int *cur_arg,
826 struct proxy *curproxy, struct server *newsrv, char **err)
827{
828 char *errmsg;
829 int port_low, port_high;
830 struct sockaddr_storage *sk;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800831
832 errmsg = NULL;
833
834 if (!*args[*cur_arg + 1]) {
835 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
836 goto err;
837 }
838
839 /* 'sk' is statically allocated (no need to be freed). */
Willy Tarreau65ec4e32020-09-16 19:17:08 +0200840 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, NULL, NULL,
841 &errmsg, NULL, NULL,
842 PA_O_RESOLVE | PA_O_PORT_OK | PA_O_PORT_MAND | PA_O_STREAM | PA_O_CONNECT);
Alexander Liu2a54bb72019-05-22 19:44:48 +0800843 if (!sk) {
844 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
845 goto err;
846 }
847
Alexander Liu2a54bb72019-05-22 19:44:48 +0800848 newsrv->flags |= SRV_F_SOCKS4_PROXY;
849 newsrv->socks4_addr = *sk;
850
Alexander Liu2a54bb72019-05-22 19:44:48 +0800851 return 0;
852
853 err:
854 free(errmsg);
855 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100856}
857
Frédéric Lécailledba97072017-03-17 15:33:50 +0100858
Willy Tarreau034c88c2017-01-23 23:36:45 +0100859/* parse the "tfo" server keyword */
860static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
861{
862 newsrv->flags |= SRV_F_FASTOPEN;
863 return 0;
864}
865
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200866/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200867 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200868 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200869 *
870 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200871 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200872void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200873{
Willy Tarreau49e10802021-02-17 13:33:24 +0100874 struct stream *stream;
875 struct mt_list *elt1, elt2;
Willy Tarreau72653c52021-03-04 10:47:54 +0100876 int thr;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200877
Willy Tarreau72653c52021-03-04 10:47:54 +0100878 for (thr = 0; thr < global.nbthread; thr++)
879 mt_list_for_each_entry_safe(stream, &srv->actconns[thr], by_srv, elt1, elt2)
880 if (stream->srv_conn == srv)
881 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200882}
883
884/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200885 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200886 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200887 *
888 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200889 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200890void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200891{
892 struct server *srv;
893
894 for (srv = px->srv; srv != NULL; srv = srv->next)
895 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200896 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200897}
898
Willy Tarreaubda92272014-05-20 21:55:30 +0200899/* Appends some information to a message string related to a server going UP or
900 * DOWN. If both <forced> and <reason> are null and the server tracks another
901 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200902 * If <check> is non-null, an entire string describing the check result will be
903 * appended after a comma and a space (eg: to report some information from the
904 * check that changed the state). In the other case, the string will be built
905 * using the check results stored into the struct server if present.
906 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200907 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200908 *
909 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200910 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200911void srv_append_status(struct buffer *msg, struct server *s,
912 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200913{
Emeric Brun5a133512017-10-19 14:42:30 +0200914 short status = s->op_st_chg.status;
915 short code = s->op_st_chg.code;
916 long duration = s->op_st_chg.duration;
917 char *desc = s->op_st_chg.reason;
918
919 if (check) {
920 status = check->status;
921 code = check->code;
922 duration = check->duration;
923 desc = check->desc;
924 }
925
926 if (status != -1) {
927 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
928
929 if (status >= HCHK_STATUS_L57DATA)
930 chunk_appendf(msg, ", code: %d", code);
931
932 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200933 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200934
935 chunk_appendf(msg, ", info: \"");
936
937 chunk_initlen(&src, desc, 0, strlen(desc));
938 chunk_asciiencode(msg, &src, '"');
939
940 chunk_appendf(msg, "\"");
941 }
942
943 if (duration >= 0)
944 chunk_appendf(msg, ", check duration: %ldms", duration);
945 }
946 else if (desc && *desc) {
947 chunk_appendf(msg, ", %s", desc);
948 }
949 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200950 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200951 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200952
953 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200954 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200955 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
956 " %d sessions active, %d requeued, %d remaining in queue",
957 s->proxy->srv_act, s->proxy->srv_bck,
958 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
959 s->cur_sess, xferred, s->nbpend);
960 else
961 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
962 " %d sessions requeued, %d total in queue",
963 s->proxy->srv_act, s->proxy->srv_bck,
964 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
965 xferred, s->nbpend);
966 }
967}
968
Emeric Brun5a133512017-10-19 14:42:30 +0200969/* Marks server <s> down, regardless of its checks' statuses. The server is
970 * registered in a list to postpone the counting of the remaining servers on
971 * the proxy and transfers queued streams whenever possible to other servers at
972 * a sync point. Maintenance servers are ignored. It stores the <reason> if
973 * non-null as the reason for going down or the available data from the check
974 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200975 *
976 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200977 */
Emeric Brun5a133512017-10-19 14:42:30 +0200978void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200979{
980 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200981
Emeric Brun64cc49c2017-10-03 14:46:45 +0200982 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200983 return;
984
Emeric Brun52a91d32017-08-31 14:41:55 +0200985 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200986 *s->op_st_chg.reason = 0;
987 s->op_st_chg.status = -1;
988 if (reason) {
989 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
990 }
991 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100992 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200993 s->op_st_chg.code = check->code;
994 s->op_st_chg.status = check->status;
995 s->op_st_chg.duration = check->duration;
996 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200998 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200999 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001000
Emeric Brun9f0b4582017-10-23 14:39:51 +02001001 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001002 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001003 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001004 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001005 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001006}
1007
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001008/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001009 * in maintenance. The server is registered in a list to postpone the counting
1010 * of the remaining servers on the proxy and tries to grab requests from the
1011 * proxy at a sync point. Maintenance servers are ignored. It stores the
1012 * <reason> if non-null as the reason for going down or the available data
1013 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001014 *
1015 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001016 */
Emeric Brun5a133512017-10-19 14:42:30 +02001017void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001018{
1019 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001020
Emeric Brun64cc49c2017-10-03 14:46:45 +02001021 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001022 return;
1023
Emeric Brun52a91d32017-08-31 14:41:55 +02001024 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001025 return;
1026
Emeric Brun52a91d32017-08-31 14:41:55 +02001027 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001028 *s->op_st_chg.reason = 0;
1029 s->op_st_chg.status = -1;
1030 if (reason) {
1031 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1032 }
1033 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001034 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001035 s->op_st_chg.code = check->code;
1036 s->op_st_chg.status = check->status;
1037 s->op_st_chg.duration = check->duration;
1038 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001039
Emeric Brun64cc49c2017-10-03 14:46:45 +02001040 if (s->slowstart <= 0)
1041 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001042
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001043 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001044 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001045
Emeric Brun9f0b4582017-10-23 14:39:51 +02001046 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001047 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001048 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001049 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001050 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001051}
1052
Willy Tarreau8eb77842014-05-21 13:54:57 +02001053/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001054 * isn't in maintenance. The server is registered in a list to postpone the
1055 * counting of the remaining servers on the proxy and tries to grab requests
1056 * from the proxy. Maintenance servers are ignored. It stores the
1057 * <reason> if non-null as the reason for going down or the available data
1058 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001059 * up. Note that it makes use of the trash to build the log strings, so <reason>
1060 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001061 *
1062 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001063 */
Emeric Brun5a133512017-10-19 14:42:30 +02001064void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001065{
1066 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001067
Emeric Brun64cc49c2017-10-03 14:46:45 +02001068 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001069 return;
1070
Emeric Brun52a91d32017-08-31 14:41:55 +02001071 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001072 return;
1073
Emeric Brun52a91d32017-08-31 14:41:55 +02001074 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001075 *s->op_st_chg.reason = 0;
1076 s->op_st_chg.status = -1;
1077 if (reason) {
1078 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1079 }
1080 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001081 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001082 s->op_st_chg.code = check->code;
1083 s->op_st_chg.status = check->status;
1084 s->op_st_chg.duration = check->duration;
1085 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001086
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001087 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001088 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001089
Emeric Brun9f0b4582017-10-23 14:39:51 +02001090 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001091 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001092 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001093 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001094 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001095}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001096
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001097/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1098 * enforce either maint mode or drain mode. It is not allowed to set more than
1099 * one flag at once. The equivalent "inherited" flag is propagated to all
1100 * tracking servers. Maintenance mode disables health checks (but not agent
1101 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001102 * is done. If <cause> is non-null, it will be displayed at the end of the log
1103 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001104 *
1105 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001106 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001107void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001108{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001109 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001110
1111 if (!mode)
1112 return;
1113
1114 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001115 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001116 return;
1117
Emeric Brun52a91d32017-08-31 14:41:55 +02001118 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001119 if (cause)
1120 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1121
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001122 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001123 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001124
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001125 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001126 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1127 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001128 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001129
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001130 /* compute the inherited flag to propagate */
1131 if (mode & SRV_ADMF_MAINT)
1132 mode = SRV_ADMF_IMAINT;
1133 else if (mode & SRV_ADMF_DRAIN)
1134 mode = SRV_ADMF_IDRAIN;
1135
Emeric Brun9f0b4582017-10-23 14:39:51 +02001136 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001137 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001138 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001139 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001140 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001141}
1142
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001143/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1144 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1145 * than one flag at once. The equivalent "inherited" flag is propagated to all
1146 * tracking servers. Leaving maintenance mode re-enables health checks. When
1147 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001148 *
1149 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001150 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001151void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001152{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001153 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001154
1155 if (!mode)
1156 return;
1157
1158 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001159 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001160 return;
1161
Emeric Brun52a91d32017-08-31 14:41:55 +02001162 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001163
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001164 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001165 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001166
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001167 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001168 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1169 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001170 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001171
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001172 if (mode & SRV_ADMF_MAINT)
1173 mode = SRV_ADMF_IMAINT;
1174 else if (mode & SRV_ADMF_DRAIN)
1175 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001176
Emeric Brun9f0b4582017-10-23 14:39:51 +02001177 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001178 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001179 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001180 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001181 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001182}
1183
Willy Tarreau757478e2016-11-03 19:22:19 +01001184/* principle: propagate maint and drain to tracking servers. This is useful
1185 * upon startup so that inherited states are correct.
1186 */
1187static void srv_propagate_admin_state(struct server *srv)
1188{
1189 struct server *srv2;
1190
1191 if (!srv->trackers)
1192 return;
1193
1194 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001195 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001196 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001197 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001198
Emeric Brun52a91d32017-08-31 14:41:55 +02001199 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001200 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001201 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001202 }
1203}
1204
1205/* Compute and propagate the admin states for all servers in proxy <px>.
1206 * Only servers *not* tracking another one are considered, because other
1207 * ones will be handled when the server they track is visited.
1208 */
1209void srv_compute_all_admin_states(struct proxy *px)
1210{
1211 struct server *srv;
1212
1213 for (srv = px->srv; srv; srv = srv->next) {
1214 if (srv->track)
1215 continue;
1216 srv_propagate_admin_state(srv);
1217 }
1218}
1219
Willy Tarreaudff55432012-10-10 17:51:05 +02001220/* Note: must not be declared <const> as its list will be overwritten.
1221 * Please take care of keeping this list alphabetically sorted, doing so helps
1222 * all code contributors.
1223 * Optional keywords are also declared with a NULL ->parse() function so that
1224 * the config parser can report an appropriate error when a known keyword was
1225 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001226 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001227 */
1228static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001229 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001230 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001231 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1232 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001233 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001234 { "max-reuse", srv_parse_max_reuse, 1, 1 }, /* Set the max number of requests on a connection, -1 means unlimited */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001235 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001236 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001237 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1238 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +02001239 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001240 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001241 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001242 { "pool-low-conn", srv_parse_pool_low_conn, 1, 1 }, /* Set the min number of orphan idle connecbefore being allowed to pick from other threads */
Olivier Houchard006e3102018-12-10 18:30:32 +01001243 { "pool-max-conn", srv_parse_pool_max_conn, 1, 1 }, /* Set the max number of orphan idle connections, 0 means unlimited */
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001244 { "pool-purge-delay", srv_parse_pool_purge_delay, 1, 1 }, /* Set the time before we destroy orphan idle connections, defaults to 1s */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001245 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001246 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001247 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001248 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1249 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001250 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001251 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001252 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001253 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001254 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001255 { NULL, NULL, 0 },
1256}};
1257
Willy Tarreau0108d902018-11-25 19:14:37 +01001258INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001259
Willy Tarreau004e0452013-11-21 11:22:01 +01001260/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001261 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001262 * state is automatically disabled if the time is elapsed. If <must_update> is
1263 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001264 *
1265 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001266 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001267void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001268{
1269 struct proxy *px = sv->proxy;
1270 unsigned w;
1271
1272 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1273 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001274 if (sv->next_state == SRV_ST_STARTING)
1275 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001276 }
1277
1278 /* We must take care of not pushing the server to full throttle during slow starts.
1279 * It must also start immediately, at least at the minimal step when leaving maintenance.
1280 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001281 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001282 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1283 else
1284 w = px->lbprm.wdiv;
1285
Emeric Brun52a91d32017-08-31 14:41:55 +02001286 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001287
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001288 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001289 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001290 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001291}
1292
Willy Tarreaubaaee002006-06-26 02:48:02 +02001293/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001294 * Parses weight_str and configures sv accordingly.
1295 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001296 *
1297 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001298 */
1299const char *server_parse_weight_change_request(struct server *sv,
1300 const char *weight_str)
1301{
1302 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001303 long int w;
1304 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001305
1306 px = sv->proxy;
1307
1308 /* if the weight is terminated with '%', it is set relative to
1309 * the initial weight, otherwise it is absolute.
1310 */
1311 if (!*weight_str)
1312 return "Require <weight> or <weight%>.\n";
1313
Simon Hormanb796afa2013-02-12 10:45:53 +09001314 w = strtol(weight_str, &end, 10);
1315 if (end == weight_str)
1316 return "Empty weight string empty or preceded by garbage";
1317 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001318 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001319 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001320 /* Avoid integer overflow */
1321 if (w > 25600)
1322 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001323 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001324 if (w > 256)
1325 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001326 }
1327 else if (w < 0 || w > 256)
1328 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001329 else if (end[0] != '\0')
1330 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001331
1332 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1333 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1334
1335 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001336 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001337
1338 return NULL;
1339}
1340
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001341/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001342 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1343 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001344 * Returns:
1345 * - error string on error
1346 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001347 *
1348 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001349 */
1350const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001351 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001352{
1353 unsigned char ip[INET6_ADDRSTRLEN];
1354
1355 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001356 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001357 return NULL;
1358 }
1359 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001360 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001361 return NULL;
1362 }
1363
1364 return "Could not understand IP address format.\n";
1365}
1366
Willy Tarreau46b7f532018-08-21 11:54:26 +02001367/*
1368 * Must be called with the server lock held.
1369 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001370const char *server_parse_maxconn_change_request(struct server *sv,
1371 const char *maxconn_str)
1372{
1373 long int v;
1374 char *end;
1375
1376 if (!*maxconn_str)
1377 return "Require <maxconn>.\n";
1378
1379 v = strtol(maxconn_str, &end, 10);
1380 if (end == maxconn_str)
1381 return "maxconn string empty or preceded by garbage";
1382 else if (end[0] != '\0')
1383 return "Trailing garbage in maxconn string";
1384
1385 if (sv->maxconn == sv->minconn) { // static maxconn
1386 sv->maxconn = sv->minconn = v;
1387 } else { // dynamic maxconn
1388 sv->maxconn = v;
1389 }
1390
Amaury Denoyelle43b0c622021-06-18 11:11:36 +02001391 /* server_parse_maxconn_change_request requires the server lock held.
1392 * Specify it to process_srv_queue to prevent a deadlock.
1393 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001394 if (may_dequeue_tasks(sv, sv->proxy))
Amaury Denoyelle43b0c622021-06-18 11:11:36 +02001395 process_srv_queue(sv, 1);
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001396
1397 return NULL;
1398}
1399
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001400#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001401static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1402 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001403{
1404 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001405 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001406 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001407 NULL,
1408 };
1409
1410 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001411 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001412
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001413 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001414}
1415
1416static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1417{
1418 struct sample_expr *expr;
1419
1420 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 +01001421 if (!expr) {
1422 memprintf(err, "error detected while parsing sni expression : %s", *err);
1423 return ERR_ALERT | ERR_FATAL;
1424 }
1425
1426 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1427 memprintf(err, "error detected while parsing sni expression : "
1428 " fetch method '%s' extracts information from '%s', "
1429 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001430 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001431 return ERR_ALERT | ERR_FATAL;
1432 }
1433
1434 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1435 release_sample_expr(newsrv->ssl_ctx.sni);
1436 newsrv->ssl_ctx.sni = expr;
1437
1438 return 0;
1439}
1440#endif
1441
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001442static 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 +01001443{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001444 char *msg = "error encountered while processing ";
1445 char *quote = "'";
1446 char *token = args[cur_arg];
1447
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001448 if (err && *err) {
1449 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001450 msg = *err;
1451 quote = "";
1452 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001453 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001454
1455 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1456 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1457 file, linenum, args[0], args[1],
1458 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001459 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001460 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1461 file, linenum, args[0], args[1],
1462 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001463}
1464
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001465static void srv_conn_src_sport_range_cpy(struct server *srv,
1466 struct server *src)
1467{
1468 int range_sz;
1469
1470 range_sz = src->conn_src.sport_range->size;
1471 if (range_sz > 0) {
1472 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1473 if (srv->conn_src.sport_range != NULL) {
1474 int i;
1475
1476 for (i = 0; i < range_sz; i++) {
1477 srv->conn_src.sport_range->ports[i] =
1478 src->conn_src.sport_range->ports[i];
1479 }
1480 }
1481 }
1482}
1483
1484/*
1485 * Copy <src> server connection source settings to <srv> server everything needed.
1486 */
1487static void srv_conn_src_cpy(struct server *srv, struct server *src)
1488{
1489 srv->conn_src.opts = src->conn_src.opts;
1490 srv->conn_src.source_addr = src->conn_src.source_addr;
1491
1492 /* Source port range copy. */
1493 if (src->conn_src.sport_range != NULL)
1494 srv_conn_src_sport_range_cpy(srv, src);
1495
1496#ifdef CONFIG_HAP_TRANSPARENT
1497 if (src->conn_src.bind_hdr_name != NULL) {
1498 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1499 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1500 }
1501 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1502 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1503#endif
1504 if (src->conn_src.iface_name != NULL)
1505 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1506}
1507
1508/*
1509 * Copy <src> server SSL settings to <srv> server allocating
1510 * everything needed.
1511 */
1512#if defined(USE_OPENSSL)
1513static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1514{
1515 if (src->ssl_ctx.ca_file != NULL)
1516 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1517 if (src->ssl_ctx.crl_file != NULL)
1518 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1519 if (src->ssl_ctx.client_crt != NULL)
1520 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1521
1522 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1523
1524 if (src->ssl_ctx.verify_host != NULL)
1525 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1526 if (src->ssl_ctx.ciphers != NULL)
1527 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001528 if (src->ssl_ctx.options)
1529 srv->ssl_ctx.options = src->ssl_ctx.options;
1530 if (src->ssl_ctx.methods.flags)
1531 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1532 if (src->ssl_ctx.methods.min)
1533 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1534 if (src->ssl_ctx.methods.max)
1535 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1536
Willy Tarreau5db847a2019-05-09 14:13:35 +02001537#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001538 if (src->ssl_ctx.ciphersuites != NULL)
1539 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1540#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001541 if (src->sni_expr != NULL)
1542 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001543
1544#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1545 if (src->ssl_ctx.alpn_str) {
1546 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1547 if (srv->ssl_ctx.alpn_str) {
1548 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1549 src->ssl_ctx.alpn_len);
1550 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1551 }
1552 }
1553#endif
1554#ifdef OPENSSL_NPN_NEGOTIATED
1555 if (src->ssl_ctx.npn_str) {
1556 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1557 if (srv->ssl_ctx.npn_str) {
1558 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1559 src->ssl_ctx.npn_len);
1560 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1561 }
1562 }
1563#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001564}
1565#endif
1566
1567/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001568 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001569 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001570 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001571 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001572static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001573{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001574 char *hostname_dn;
1575 int hostname_len, hostname_dn_len;
1576
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001577 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001578 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001579
Christopher Faulet67957bd2017-09-27 11:00:59 +02001580 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001581 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001582 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1583 hostname_dn, trash.size);
1584 if (hostname_dn_len == -1)
1585 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001586
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001587
Christopher Faulet67957bd2017-09-27 11:00:59 +02001588 free(srv->hostname);
1589 free(srv->hostname_dn);
1590 srv->hostname = strdup(hostname);
1591 srv->hostname_dn = strdup(hostname_dn);
1592 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001593 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001594 goto err;
1595
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001596 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001597
1598 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001599 free(srv->hostname); srv->hostname = NULL;
1600 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001601 return -1;
1602}
1603
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001604/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001605 * Copy <src> server settings to <srv> server allocating
1606 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001607 * This function is not supposed to be called at any time, but only
1608 * during server settings parsing or during server allocations from
1609 * a server template, and just after having calloc()'ed a new server.
1610 * So, <src> may only be a default server (when parsing server settings)
1611 * or a server template (during server allocations from a server template).
1612 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1613 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001614 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001615static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001616{
1617 /* Connection source settings copy */
1618 srv_conn_src_cpy(srv, src);
1619
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001620 if (srv_tmpl) {
1621 srv->addr = src->addr;
1622 srv->svc_port = src->svc_port;
1623 }
1624
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001625 srv->pp_opts = src->pp_opts;
1626 if (src->rdr_pfx != NULL) {
1627 srv->rdr_pfx = strdup(src->rdr_pfx);
1628 srv->rdr_len = src->rdr_len;
1629 }
1630 if (src->cookie != NULL) {
1631 srv->cookie = strdup(src->cookie);
1632 srv->cklen = src->cklen;
1633 }
1634 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001635 srv->check.addr = src->check.addr;
1636 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001637 srv->check.use_ssl = src->check.use_ssl;
1638 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001639 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001640 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001641 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001642 /* Note: 'flags' field has potentially been already initialized. */
1643 srv->flags |= src->flags;
1644 srv->do_check = src->do_check;
1645 srv->do_agent = src->do_agent;
1646 if (srv->check.port)
1647 srv->flags |= SRV_F_CHECKPORT;
1648 srv->check.inter = src->check.inter;
1649 srv->check.fastinter = src->check.fastinter;
1650 srv->check.downinter = src->check.downinter;
1651 srv->agent.use_ssl = src->agent.use_ssl;
1652 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001653
1654 if (src->agent.tcpcheck_rules) {
1655 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1656 if (srv->agent.tcpcheck_rules) {
1657 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1658 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1659 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1660 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1661 &src->agent.tcpcheck_rules->preset_vars);
1662 }
1663 }
1664
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001665 srv->agent.inter = src->agent.inter;
1666 srv->agent.fastinter = src->agent.fastinter;
1667 srv->agent.downinter = src->agent.downinter;
1668 srv->maxqueue = src->maxqueue;
1669 srv->minconn = src->minconn;
1670 srv->maxconn = src->maxconn;
1671 srv->slowstart = src->slowstart;
1672 srv->observe = src->observe;
1673 srv->onerror = src->onerror;
1674 srv->onmarkeddown = src->onmarkeddown;
1675 srv->onmarkedup = src->onmarkedup;
1676 if (src->trackit != NULL)
1677 srv->trackit = strdup(src->trackit);
1678 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1679 srv->uweight = srv->iweight = src->iweight;
1680
1681 srv->check.send_proxy = src->check.send_proxy;
1682 /* health: up, but will fall down at first failure */
1683 srv->check.rise = srv->check.health = src->check.rise;
1684 srv->check.fall = src->check.fall;
1685
1686 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001687 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1688 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1689 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001690 srv->check.state |= CHK_ST_PAUSED;
1691 srv->check.health = 0;
1692 }
1693
1694 /* health: up but will fall down at first failure */
1695 srv->agent.rise = srv->agent.health = src->agent.rise;
1696 srv->agent.fall = src->agent.fall;
1697
1698 if (src->resolvers_id != NULL)
1699 srv->resolvers_id = strdup(src->resolvers_id);
1700 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001701 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001702 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001703 if (srv->dns_opts.family_prio == AF_UNSPEC)
1704 srv->dns_opts.family_prio = AF_INET6;
1705 memcpy(srv->dns_opts.pref_net,
1706 src->dns_opts.pref_net,
1707 sizeof srv->dns_opts.pref_net);
1708 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1709
1710 srv->init_addr_methods = src->init_addr_methods;
1711 srv->init_addr = src->init_addr;
1712#if defined(USE_OPENSSL)
1713 srv_ssl_settings_cpy(srv, src);
1714#endif
1715#ifdef TCP_USER_TIMEOUT
1716 srv->tcp_ut = src->tcp_ut;
1717#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001718 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001719 srv->pool_purge_delay = src->pool_purge_delay;
Willy Tarreau2f3f4d32020-07-01 07:43:51 +02001720 srv->low_idle_conns = src->low_idle_conns;
Olivier Houchard006e3102018-12-10 18:30:32 +01001721 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001722 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001723
Olivier Houchard8da5f982017-08-04 18:35:36 +02001724 if (srv_tmpl)
1725 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001726
1727 srv->check.via_socks4 = src->check.via_socks4;
1728 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001729}
1730
William Lallemand313bfd12018-10-26 14:47:32 +02001731struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001732{
1733 struct server *srv;
Willy Tarreau72653c52021-03-04 10:47:54 +01001734 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001735
1736 srv = calloc(1, sizeof *srv);
1737 if (!srv)
1738 return NULL;
1739
1740 srv->obj_type = OBJ_TYPE_SERVER;
1741 srv->proxy = proxy;
Willy Tarreau72653c52021-03-04 10:47:54 +01001742 for (i = 0; i < MAX_THREADS; i++)
1743 MT_LIST_INIT(&srv->actconns[i]);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001744 srv->pendconns = EB_ROOT;
Emeric Brun32131d42021-06-11 10:48:45 +02001745 LIST_INIT(&srv->srv_rec_item);
Emeric Brunca8e3552021-06-11 10:08:05 +02001746 LIST_INIT(&srv->ip_rec_item);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001747
Emeric Brun52a91d32017-08-31 14:41:55 +02001748 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749 srv->last_change = now.tv_sec;
1750
Christopher Faulet38290462020-04-21 11:46:40 +02001751 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001752 srv->check.status = HCHK_STATUS_INI;
1753 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001754 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001755 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001756
Christopher Faulet38290462020-04-21 11:46:40 +02001757 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001758 srv->agent.status = HCHK_STATUS_INI;
1759 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001760 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001761 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1762
Willy Tarreau975b1552019-06-06 16:25:55 +02001763 /* please don't put default server settings here, they are set in
1764 * init_default_instance().
1765 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001766 return srv;
1767}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001768
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001769#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1770static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1771 struct server *srv, struct proxy *proxy)
1772{
1773 int ret;
1774 char *err = NULL;
1775
1776 if (!srv->sni_expr)
1777 return 0;
1778
1779 ret = server_parse_sni_expr(srv, proxy, &err);
1780 if (!ret)
1781 return 0;
1782
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001783 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001784 free(err);
1785
1786 return ret;
1787}
1788#endif
1789
1790/*
1791 * Server initializations finalization.
1792 * Initialize health check, agent check and SNI expression if enabled.
1793 * Must not be called for a default server instance.
1794 */
1795static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1796 struct server *srv, struct proxy *px)
1797{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001798#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001799 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001800#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001801
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001802 if (srv->do_check && srv->trackit) {
1803 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1804 file, linenum);
1805 return ERR_ALERT | ERR_FATAL;
1806 }
1807
1808 if (srv->do_agent && !srv->agent.port) {
1809 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1810 file, linenum, srv->id);
1811 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001812 }
1813
1814#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1815 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1816 return ret;
1817#endif
1818
1819 if (srv->flags & SRV_F_BACKUP)
1820 px->srv_bck++;
1821 else
1822 px->srv_act++;
1823 srv_lb_commit_status(srv);
1824
1825 return 0;
1826}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001827
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001828/*
1829 * Parse as much as possible such a range string argument: low[-high]
1830 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1831 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1832 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1833 * Returns 0 if succeeded, -1 if not.
1834 */
1835static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1836{
1837 char *nb_high_arg;
1838
1839 *nb_high = 0;
1840 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001841 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001842
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001843 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001844 *nb_high_arg++ = '\0';
1845 *nb_high = atoi(nb_high_arg);
1846 }
1847 else {
1848 *nb_high += *nb_low;
1849 *nb_low = 1;
1850 }
1851
1852 if (*nb_low < 0 || *nb_high < *nb_low)
1853 return -1;
1854
1855 return 0;
1856}
1857
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001858static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1859{
1860 chunk_printf(&trash, "%s%d", prefix, nb);
1861 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001862 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001863}
1864
1865/*
1866 * Initialize as much as possible servers from <srv> server template.
1867 * Note that a server template is a special server with
1868 * a few different parameters than a server which has
1869 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001870 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001871 * 'srv' template included.
1872 */
1873static int server_template_init(struct server *srv, struct proxy *px)
1874{
1875 int i;
1876 struct server *newsrv;
1877
1878 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 +02001879 newsrv = new_server(px);
1880 if (!newsrv)
1881 goto err;
1882
Christopher Faulet75bef002020-11-02 22:04:55 +01001883 newsrv->conf.file = strdup(srv->conf.file);
1884 newsrv->conf.line = srv->conf.line;
1885
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001886 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001887 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001888#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1889 if (newsrv->sni_expr) {
1890 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1891 if (!newsrv->ssl_ctx.sni)
1892 goto err;
1893 }
1894#endif
Emeric Brun32131d42021-06-11 10:48:45 +02001895 /* append to list of servers available to receive an hostname */
Emeric Brund671e9f2021-06-14 10:02:18 +02001896 if (newsrv->srvrq)
1897 LIST_ADDQ(&newsrv->srvrq->attached_servers, &newsrv->srv_rec_item);
Emeric Brun32131d42021-06-11 10:48:45 +02001898
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001899 /* Set this new server ID. */
1900 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1901
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001902 /* Linked backwards first. This will be restablished after parsing. */
1903 newsrv->next = px->srv;
1904 px->srv = newsrv;
1905 }
1906 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1907
1908 return i - srv->tmpl_info.nb_low;
1909
1910 err:
1911 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1912 if (newsrv) {
1913#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1914 release_sample_expr(newsrv->ssl_ctx.sni);
1915#endif
1916 free_check(&newsrv->agent);
1917 free_check(&newsrv->check);
1918 }
1919 free(newsrv);
1920 return i - srv->tmpl_info.nb_low;
1921}
1922
Emeric Brund3db3842020-07-21 16:54:36 +02001923int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy,
1924 struct proxy *defproxy, int parse_addr, int in_peers_section, int initial_resolve)
Willy Tarreau272adea2014-03-31 10:39:59 +02001925{
1926 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001927 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001928 char *errmsg = NULL;
1929 int err_code = 0;
1930 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001931 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001933 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001934 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001935 !strcmp(args[0], "default-server") ||
1936 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001937 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001938 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001939 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001940 int srv_tmpl = !defsrv && !srv;
1941 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001942
1943 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001944 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001945 err_code |= ERR_ALERT | ERR_FATAL;
1946 goto out;
1947 }
Christopher Faulet75ffbf92021-01-13 13:14:13 +01001948 else if (failifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL)) {
1949 err_code |= ERR_ALERT | ERR_FATAL;
1950 goto out;
1951 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001952
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001953 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001954 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001955 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001956 if (in_peers_section) {
1957 return 0;
1958 }
1959 else {
1960 /* 'server' line number of argument check. */
1961 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1962 file, linenum, args[0]);
1963 err_code |= ERR_ALERT | ERR_FATAL;
1964 goto out;
1965 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001966 }
1967
1968 err = invalid_char(args[1]);
1969 }
1970 else if (srv_tmpl) {
1971 if (!*args[3]) {
1972 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001973 ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001974 file, linenum, args[0]);
1975 err_code |= ERR_ALERT | ERR_FATAL;
1976 goto out;
1977 }
1978
1979 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001980 }
1981
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001982 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001983 ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001984 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 err_code |= ERR_ALERT | ERR_FATAL;
1986 goto out;
1987 }
1988
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001989 cur_arg = 2;
1990 if (srv_tmpl) {
1991 /* Parse server-template <nb | range> arg. */
1992 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001993 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001994 file, linenum, args[0], args[cur_arg]);
1995 err_code |= ERR_ALERT | ERR_FATAL;
1996 goto out;
1997 }
1998 cur_arg++;
1999 }
2000
Willy Tarreau272adea2014-03-31 10:39:59 +02002001 if (!defsrv) {
2002 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002003 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002004
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002005 newsrv = new_server(curproxy);
2006 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002007 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002008 err_code |= ERR_ALERT | ERR_ABORT;
2009 goto out;
2010 }
2011
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002012 if (srv_tmpl) {
2013 newsrv->tmpl_info.nb_low = tmpl_range_low;
2014 newsrv->tmpl_info.nb_high = tmpl_range_high;
2015 }
2016
Willy Tarreau272adea2014-03-31 10:39:59 +02002017 /* the servers are linked backwards first */
2018 newsrv->next = curproxy->srv;
2019 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002020 newsrv->conf.file = strdup(file);
2021 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002022 /* Note: for a server template, its id is its prefix.
2023 * This is a temporary id which will be used for server allocations to come
2024 * after parsing.
2025 */
2026 if (srv)
2027 newsrv->id = strdup(args[1]);
2028 else
2029 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002030
2031 /* several ways to check the port component :
2032 * - IP => port=+0, relative (IPv4 only)
2033 * - IP: => port=+0, relative
2034 * - IP:N => port=N, absolute
2035 * - IP:+N => port=+N, relative
2036 * - IP:-N => port=-N, relative
2037 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002038 if (!parse_addr)
2039 goto skip_addr;
2040
Willy Tarreau65ec4e32020-09-16 19:17:08 +02002041 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, NULL, NULL,
2042 &errmsg, NULL, &fqdn,
2043 (initial_resolve ? PA_O_RESOLVE : 0) | PA_O_PORT_OK | PA_O_PORT_OFS | PA_O_STREAM | PA_O_XPRT | PA_O_CONNECT);
Willy Tarreau272adea2014-03-31 10:39:59 +02002044 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002045 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 err_code |= ERR_ALERT | ERR_FATAL;
2047 goto out;
2048 }
2049
Willy Tarreau272adea2014-03-31 10:39:59 +02002050 if (!port1 || !port2) {
2051 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002052 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002053 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002054
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002055 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002056 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002057 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002058 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002059 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2060 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2061 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002062 err_code |= ERR_ALERT | ERR_FATAL;
2063 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002064 }
2065 }
2066 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002067 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002068 file, linenum, newsrv->id);
2069 err_code |= ERR_ALERT | ERR_FATAL;
2070 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002071 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002072 }
2073
Willy Tarreau272adea2014-03-31 10:39:59 +02002074 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002075 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002076
Olivier Houchard8da5f982017-08-04 18:35:36 +02002077 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002078 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002079 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002080 err_code |= ERR_ALERT | ERR_FATAL;
2081 goto out;
2082 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002083
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002084 cur_arg++;
2085 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002086 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002087 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002088 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 } else {
2090 newsrv = &curproxy->defsrv;
2091 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002092 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002093 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 }
2095
2096 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002097 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002098 char *p, *end;
2099 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002100 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002101
2102 newsrv->init_addr_methods = 0;
2103 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2104
2105 for (p = args[cur_arg + 1]; *p; p = end) {
2106 /* cut on next comma */
2107 for (end = p; *end && *end != ','; end++);
2108 if (*end)
2109 *(end++) = 0;
2110
Willy Tarreau4310d362016-11-02 15:05:56 +01002111 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002112 if (!strcmp(p, "libc")) {
2113 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2114 }
2115 else if (!strcmp(p, "last")) {
2116 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2117 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002118 else if (!strcmp(p, "none")) {
2119 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2120 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002121 else if (str2ip2(p, &sa, 0)) {
2122 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002123 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002124 file, linenum, args[cur_arg], p);
2125 err_code |= ERR_ALERT | ERR_FATAL;
2126 goto out;
2127 }
2128 newsrv->init_addr = sa;
2129 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2130 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002131 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002132 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002133 file, linenum, args[cur_arg], p);
2134 err_code |= ERR_ALERT | ERR_FATAL;
2135 goto out;
2136 }
2137 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002138 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002139 file, linenum, args[cur_arg], p);
2140 err_code |= ERR_ALERT | ERR_FATAL;
2141 goto out;
2142 }
2143 }
2144 cur_arg += 2;
2145 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002146 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002147 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002148 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2149 cur_arg += 2;
2150 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002151 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2152 char *p, *end;
2153
2154 for (p = args[cur_arg + 1]; *p; p = end) {
2155 /* cut on next comma */
2156 for (end = p; *end && *end != ','; end++);
2157 if (*end)
2158 *(end++) = 0;
2159
2160 if (!strcmp(p, "allow-dup-ip")) {
2161 newsrv->dns_opts.accept_duplicate_ip = 1;
2162 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002163 else if (!strcmp(p, "ignore-weight")) {
2164 newsrv->dns_opts.ignore_weight = 1;
2165 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002166 else if (!strcmp(p, "prevent-dup-ip")) {
2167 newsrv->dns_opts.accept_duplicate_ip = 0;
2168 }
2169 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002170 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip', 'ignore-weight', and 'prevent-dup-ip'.\n",
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002171 file, linenum, args[cur_arg], p);
2172 err_code |= ERR_ALERT | ERR_FATAL;
2173 goto out;
2174 }
2175 }
2176
2177 cur_arg += 2;
2178 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002179 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2180 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002181 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002182 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002183 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002184 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002185 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002186 file, linenum, args[cur_arg]);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190 cur_arg += 2;
2191 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002192 else if (!strcmp(args[cur_arg], "resolve-net")) {
2193 char *p, *e;
2194 unsigned char mask;
2195 struct dns_options *opt;
2196
2197 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002198 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002199 file, linenum, args[cur_arg]);
2200 err_code |= ERR_ALERT | ERR_FATAL;
2201 goto out;
2202 }
2203
2204 opt = &newsrv->dns_opts;
2205
2206 /* Split arguments by comma, and convert it from ipv4 or ipv6
2207 * string network in in_addr or in6_addr.
2208 */
2209 p = args[cur_arg + 1];
2210 e = p;
2211 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002212 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002213 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002214 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002215 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219 /* look for end or comma. */
2220 while (*e != ',' && *e != '\0')
2221 e++;
2222 if (*e == ',') {
2223 *e = '\0';
2224 e++;
2225 }
2226 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2227 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2228 /* Try to convert input string from ipv4 or ipv6 network. */
2229 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2230 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2231 &mask)) {
2232 /* Try to convert input string from ipv6 network. */
2233 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2234 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2235 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002236 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002237 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002238 file, linenum, args[cur_arg], p);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
2241 }
2242 opt->pref_net_nb++;
2243 p = e;
2244 }
2245
2246 cur_arg += 2;
2247 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002248 else if (!strcmp(args[cur_arg], "weight")) {
2249 int w;
2250 w = atol(args[cur_arg + 1]);
2251 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002252 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002253 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2254 err_code |= ERR_ALERT | ERR_FATAL;
2255 goto out;
2256 }
2257 newsrv->uweight = newsrv->iweight = w;
2258 cur_arg += 2;
2259 }
Emeric Brun97556472020-05-30 01:42:45 +02002260 else if (!strcmp(args[cur_arg], "log-proto")) {
2261 if (!strcmp(args[cur_arg + 1], "legacy"))
2262 newsrv->log_proto = SRV_LOG_PROTO_LEGACY;
2263 else if (!strcmp(args[cur_arg + 1], "octet-count"))
2264 newsrv->log_proto = SRV_LOG_PROTO_OCTET_COUNTING;
2265 else {
2266 ha_alert("parsing [%s:%d]: '%s' expects one of 'legacy' or "
2267 "'octet-count' but got '%s'\n",
2268 file, linenum, args[cur_arg], args[cur_arg + 1]);
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
2272 cur_arg += 2;
2273 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002274 else if (!strcmp(args[cur_arg], "minconn")) {
2275 newsrv->minconn = atol(args[cur_arg + 1]);
2276 cur_arg += 2;
2277 }
2278 else if (!strcmp(args[cur_arg], "maxconn")) {
2279 newsrv->maxconn = atol(args[cur_arg + 1]);
2280 cur_arg += 2;
2281 }
2282 else if (!strcmp(args[cur_arg], "maxqueue")) {
2283 newsrv->maxqueue = atol(args[cur_arg + 1]);
2284 cur_arg += 2;
2285 }
2286 else if (!strcmp(args[cur_arg], "slowstart")) {
2287 /* slowstart is stored in seconds */
2288 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002289
2290 if (err == PARSE_TIME_OVER) {
2291 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2292 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2293 err_code |= ERR_ALERT | ERR_FATAL;
2294 goto out;
2295 }
2296 else if (err == PARSE_TIME_UNDER) {
2297 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2298 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2299 err_code |= ERR_ALERT | ERR_FATAL;
2300 goto out;
2301 }
2302 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 file, linenum, *err, newsrv->id);
2305 err_code |= ERR_ALERT | ERR_FATAL;
2306 goto out;
2307 }
2308 newsrv->slowstart = (val + 999) / 1000;
2309 cur_arg += 2;
2310 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002311 else if (!strcmp(args[cur_arg], "on-error")) {
2312 if (!strcmp(args[cur_arg + 1], "fastinter"))
2313 newsrv->onerror = HANA_ONERR_FASTINTER;
2314 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2315 newsrv->onerror = HANA_ONERR_FAILCHK;
2316 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2317 newsrv->onerror = HANA_ONERR_SUDDTH;
2318 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2319 newsrv->onerror = HANA_ONERR_MARKDWN;
2320 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002321 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002322 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2323 file, linenum, args[cur_arg], args[cur_arg + 1]);
2324 err_code |= ERR_ALERT | ERR_FATAL;
2325 goto out;
2326 }
2327
2328 cur_arg += 2;
2329 }
2330 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2331 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2332 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2333 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002334 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002335 file, linenum, args[cur_arg], args[cur_arg + 1]);
2336 err_code |= ERR_ALERT | ERR_FATAL;
2337 goto out;
2338 }
2339
2340 cur_arg += 2;
2341 }
2342 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2343 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2344 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2345 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002346 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002347 file, linenum, args[cur_arg], args[cur_arg + 1]);
2348 err_code |= ERR_ALERT | ERR_FATAL;
2349 goto out;
2350 }
2351
2352 cur_arg += 2;
2353 }
2354 else if (!strcmp(args[cur_arg], "error-limit")) {
2355 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002356 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002357 file, linenum, args[cur_arg]);
2358 err_code |= ERR_ALERT | ERR_FATAL;
2359 goto out;
2360 }
2361
2362 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2363
2364 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002365 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002366 file, linenum, args[cur_arg]);
2367 err_code |= ERR_ALERT | ERR_FATAL;
2368 goto out;
2369 }
2370 cur_arg += 2;
2371 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002372 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002373 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 file, linenum, "usesrc", "source");
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002377 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002378 else {
2379 static int srv_dumped;
2380 struct srv_kw *kw;
2381 char *err;
2382
2383 kw = srv_find_kw(args[cur_arg]);
2384 if (kw) {
2385 char *err = NULL;
2386 int code;
2387
2388 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002389 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002390 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002391 if (kw->skip != -1)
2392 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 err_code |= ERR_ALERT | ERR_FATAL;
2394 goto out;
2395 }
2396
2397 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002398 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002399 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002400 if (kw->skip != -1)
2401 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002402 err_code |= ERR_ALERT;
2403 continue;
2404 }
2405
2406 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2407 err_code |= code;
2408
2409 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002410 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002411 if (code & ERR_FATAL) {
2412 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002413 if (kw->skip != -1)
2414 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 goto out;
2416 }
2417 }
2418 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002419 if (kw->skip != -1)
2420 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002421 continue;
2422 }
2423
2424 err = NULL;
2425 if (!srv_dumped) {
2426 srv_dump_kws(&err);
2427 indent_msg(&err, 4);
2428 srv_dumped = 1;
2429 }
2430
Christopher Faulet767a84b2017-11-24 16:50:31 +01002431 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002432 file, linenum, args[0], args[1], args[cur_arg],
2433 err ? " Registered keywords :" : "", err ? err : "");
2434 free(err);
2435
2436 err_code |= ERR_ALERT | ERR_FATAL;
2437 goto out;
2438 }
2439 }
2440
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002441 if (!defsrv)
2442 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2443 if (err_code & ERR_FATAL)
2444 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002445 if (srv_tmpl)
2446 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002447 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002448 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002449 return 0;
2450
2451 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002452 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 free(errmsg);
2454 return err_code;
2455}
2456
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002457/* Returns a pointer to the first server matching either id <id>.
2458 * NULL is returned if no match is found.
2459 * the lookup is performed in the backend <bk>
2460 */
2461struct server *server_find_by_id(struct proxy *bk, int id)
2462{
2463 struct eb32_node *eb32;
2464 struct server *curserver;
2465
2466 if (!bk || (id ==0))
2467 return NULL;
2468
2469 /* <bk> has no backend capabilities, so it can't have a server */
2470 if (!(bk->cap & PR_CAP_BE))
2471 return NULL;
2472
2473 curserver = NULL;
2474
2475 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2476 if (eb32)
2477 curserver = container_of(eb32, struct server, conf.id);
2478
2479 return curserver;
2480}
2481
2482/* Returns a pointer to the first server matching either name <name>, or id
2483 * if <name> starts with a '#'. NULL is returned if no match is found.
2484 * the lookup is performed in the backend <bk>
2485 */
2486struct server *server_find_by_name(struct proxy *bk, const char *name)
2487{
2488 struct server *curserver;
2489
2490 if (!bk || !name)
2491 return NULL;
2492
2493 /* <bk> has no backend capabilities, so it can't have a server */
2494 if (!(bk->cap & PR_CAP_BE))
2495 return NULL;
2496
2497 curserver = NULL;
2498 if (*name == '#') {
2499 curserver = server_find_by_id(bk, atoi(name + 1));
2500 if (curserver)
2501 return curserver;
2502 }
2503 else {
2504 curserver = bk->srv;
2505
2506 while (curserver && (strcmp(curserver->id, name) != 0))
2507 curserver = curserver->next;
2508
2509 if (curserver)
2510 return curserver;
2511 }
2512
2513 return NULL;
2514}
2515
2516struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2517{
2518 struct server *byname;
2519 struct server *byid;
2520
2521 if (!name && !id)
2522 return NULL;
2523
2524 if (diff)
2525 *diff = 0;
2526
2527 byname = byid = NULL;
2528
2529 if (name) {
2530 byname = server_find_by_name(bk, name);
2531 if (byname && (!id || byname->puid == id))
2532 return byname;
2533 }
2534
2535 /* remaining possibilities :
2536 * - name not set
2537 * - name set but not found
2538 * - name found but ID doesn't match
2539 */
2540 if (id) {
2541 byid = server_find_by_id(bk, id);
2542 if (byid) {
2543 if (byname) {
2544 /* use id only if forced by configuration */
2545 if (byid->flags & SRV_F_FORCED_ID) {
2546 if (diff)
2547 *diff |= 2;
2548 return byid;
2549 }
2550 else {
2551 if (diff)
2552 *diff |= 1;
2553 return byname;
2554 }
2555 }
2556
2557 /* remaining possibilities:
2558 * - name not set
2559 * - name set but not found
2560 */
2561 if (name && diff)
2562 *diff |= 2;
2563 return byid;
2564 }
2565
2566 /* id bot found */
2567 if (byname) {
2568 if (diff)
2569 *diff |= 1;
2570 return byname;
2571 }
2572 }
2573
2574 return NULL;
2575}
2576
Willy Tarreau46b7f532018-08-21 11:54:26 +02002577/* Update a server state using the parameters available in the params list.
2578 *
2579 * Grabs the server lock during operation.
2580 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002581static void srv_update_state(struct server *srv, int version, char **params)
2582{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002583 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002584 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002585
2586 /* fields since version 1
2587 * and common to all other upcoming versions
2588 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002589 enum srv_state srv_op_state;
2590 enum srv_admin srv_admin_state;
2591 unsigned srv_uweight, srv_iweight;
2592 unsigned long srv_last_time_change;
2593 short srv_check_status;
2594 enum chk_result srv_check_result;
2595 int srv_check_health;
2596 int srv_check_state, srv_agent_state;
2597 int bk_f_forced_id;
2598 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002599 int fqdn_set_by_cli;
2600 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002601 const char *port_str;
2602 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002603 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002604
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002605 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002606 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002607 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002608 switch (version) {
2609 case 1:
2610 /*
2611 * now we can proceed with server's state update:
2612 * srv_addr: params[0]
2613 * srv_op_state: params[1]
2614 * srv_admin_state: params[2]
2615 * srv_uweight: params[3]
2616 * srv_iweight: params[4]
2617 * srv_last_time_change: params[5]
2618 * srv_check_status: params[6]
2619 * srv_check_result: params[7]
2620 * srv_check_health: params[8]
2621 * srv_check_state: params[9]
2622 * srv_agent_state: params[10]
2623 * bk_f_forced_id: params[11]
2624 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002625 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002626 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002627 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002628 */
2629
2630 /* validating srv_op_state */
2631 p = NULL;
2632 errno = 0;
2633 srv_op_state = strtol(params[1], &p, 10);
2634 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2635 (srv_op_state != SRV_ST_STOPPED &&
2636 srv_op_state != SRV_ST_STARTING &&
2637 srv_op_state != SRV_ST_RUNNING &&
2638 srv_op_state != SRV_ST_STOPPING)) {
2639 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2640 }
2641
2642 /* validating srv_admin_state */
2643 p = NULL;
2644 errno = 0;
2645 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002646 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002647
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002648 /* inherited statuses will be recomputed later.
2649 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2650 */
Christopher Fauletc6111032021-02-12 17:36:08 +01002651 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT & ~SRV_ADMF_RMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002652
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002653 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2654 (srv_admin_state != 0 &&
2655 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002656 srv_admin_state != SRV_ADMF_CMAINT &&
2657 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002658 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002659 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002660 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2661 }
2662
2663 /* validating srv_uweight */
2664 p = NULL;
2665 errno = 0;
2666 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002667 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002668 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2669
2670 /* validating srv_iweight */
2671 p = NULL;
2672 errno = 0;
2673 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002674 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002675 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2676
2677 /* validating srv_last_time_change */
2678 p = NULL;
2679 errno = 0;
2680 srv_last_time_change = strtol(params[5], &p, 10);
2681 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2682 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2683
2684 /* validating srv_check_status */
2685 p = NULL;
2686 errno = 0;
2687 srv_check_status = strtol(params[6], &p, 10);
2688 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2689 (srv_check_status >= HCHK_STATUS_SIZE))
2690 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2691
2692 /* validating srv_check_result */
2693 p = NULL;
2694 errno = 0;
2695 srv_check_result = strtol(params[7], &p, 10);
2696 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2697 (srv_check_result != CHK_RES_UNKNOWN &&
2698 srv_check_result != CHK_RES_NEUTRAL &&
2699 srv_check_result != CHK_RES_FAILED &&
2700 srv_check_result != CHK_RES_PASSED &&
2701 srv_check_result != CHK_RES_CONDPASS)) {
2702 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2703 }
2704
2705 /* validating srv_check_health */
2706 p = NULL;
2707 errno = 0;
2708 srv_check_health = strtol(params[8], &p, 10);
2709 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2710 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2711
2712 /* validating srv_check_state */
2713 p = NULL;
2714 errno = 0;
2715 srv_check_state = strtol(params[9], &p, 10);
2716 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2717 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2718 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2719
2720 /* validating srv_agent_state */
2721 p = NULL;
2722 errno = 0;
2723 srv_agent_state = strtol(params[10], &p, 10);
2724 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2725 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2726 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2727
2728 /* validating bk_f_forced_id */
2729 p = NULL;
2730 errno = 0;
2731 bk_f_forced_id = strtol(params[11], &p, 10);
2732 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2733 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2734
2735 /* validating srv_f_forced_id */
2736 p = NULL;
2737 errno = 0;
2738 srv_f_forced_id = strtol(params[12], &p, 10);
2739 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2740 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2741
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002742 /* validating srv_fqdn */
2743 fqdn = params[13];
2744 if (fqdn && *fqdn == '-')
2745 fqdn = NULL;
2746 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2747 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2748 fqdn = NULL;
2749 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002750
Frédéric Lécaille31694712017-08-01 08:47:19 +02002751 port_str = params[14];
2752 if (port_str) {
2753 port = strl2uic(port_str, strlen(port_str));
2754 if (port > USHRT_MAX) {
2755 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2756 port_str = NULL;
2757 }
2758 }
2759
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002760 /* SRV record
2761 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2762 */
2763 srvrecord = params[15];
2764 if (srvrecord && *srvrecord != '_')
2765 srvrecord = NULL;
2766
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002767 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002768 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002769 goto out;
2770
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002771 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772 /* recover operational state and apply it to this server
2773 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002774 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002775 switch (srv_op_state) {
2776 case SRV_ST_STOPPED:
2777 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002778 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002779 break;
2780 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002781 /* If rise == 1 there is no STARTING state, let's switch to
2782 * RUNNING
2783 */
2784 if (srv->check.rise == 1) {
2785 srv->check.health = srv->check.rise + srv->check.fall - 1;
2786 srv_set_running(srv, "", NULL);
2787 break;
2788 }
2789 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2790 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002791 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002792 break;
2793 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002794 /* If fall == 1 there is no STOPPING state, let's switch to
2795 * STOPPED
2796 */
2797 if (srv->check.fall == 1) {
2798 srv->check.health = 0;
2799 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2800 break;
2801 }
2802 if (srv->check.health < srv->check.rise ||
2803 srv->check.health > srv->check.rise + srv->check.fall - 2)
2804 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002805 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002806 break;
2807 case SRV_ST_RUNNING:
2808 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002809 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002810 break;
2811 }
2812
2813 /* When applying server state, the following rules apply:
2814 * - in case of a configuration change, we apply the setting from the new
2815 * configuration, regardless of old running state
2816 * - if no configuration change, we apply old running state only if old running
2817 * state is different from new configuration state
2818 */
2819 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002820 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2821 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 srv_adm_set_maint(srv);
2823 else
2824 srv_adm_set_ready(srv);
2825 }
2826 /* configuration is the same, let's compate old running state and new conf state */
2827 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002828 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002829 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002830 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002831 srv_adm_set_ready(srv);
2832 }
2833 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002834 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002835 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002836 * (srv->iweight is the weight set up in configuration).
2837 * There are two possible reasons for FDRAIN to have been present :
2838 * - previous config weight was zero
2839 * - "set server b/s drain" was sent to the CLI
2840 *
2841 * In the first case, we simply want to drop this drain state
2842 * if the new weight is not zero anymore, meaning the administrator
2843 * has intentionally turned the weight back to a positive value to
2844 * enable the server again after an operation. In the second case,
2845 * the drain state was forced on the CLI regardless of the config's
2846 * weight so we don't want a change to the config weight to lose this
2847 * status. What this means is :
2848 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2849 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002850 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002851 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002852 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002853 }
2854
2855 srv->last_change = date.tv_sec - srv_last_time_change;
2856 srv->check.status = srv_check_status;
2857 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002858
2859 /* Only case we want to apply is removing ENABLED flag which could have been
2860 * done by the "disable health" command over the stats socket
2861 */
2862 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2863 (srv_check_state & CHK_ST_CONFIGURED) &&
2864 !(srv_check_state & CHK_ST_ENABLED))
2865 srv->check.state &= ~CHK_ST_ENABLED;
2866
2867 /* Only case we want to apply is removing ENABLED flag which could have been
2868 * done by the "disable agent" command over the stats socket
2869 */
2870 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2871 (srv_agent_state & CHK_ST_CONFIGURED) &&
2872 !(srv_agent_state & CHK_ST_ENABLED))
2873 srv->agent.state &= ~CHK_ST_ENABLED;
2874
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002875 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2876 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002877 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002878 * It means that a configuration file change has precedence over a unix socket change
2879 * for server's weight
2880 *
2881 * by default, HAProxy applies the following weight when parsing the configuration
2882 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002883 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002884 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002885 srv->uweight = srv_uweight;
2886 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002887 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002888
Willy Tarreaue5a60682016-11-09 14:54:53 +01002889 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002890 if (strcmp(params[0], "-"))
2891 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002892
2893 if (fqdn && srv->hostname) {
2894 if (!strcmp(srv->hostname, fqdn)) {
2895 /* Here we reset the 'set from stats socket FQDN' flag
2896 * to support such transitions:
2897 * Let's say initial FQDN value is foo1 (in configuration file).
2898 * - FQDN changed from stats socket, from foo1 to foo2 value,
2899 * - FQDN changed again from file configuration (with the same previous value
2900 set from stats socket, from foo1 to foo2 value),
2901 * - reload for any other reason than a FQDN modification,
2902 * the configuration file FQDN matches the fqdn server state file value.
2903 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002904 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002905 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002906 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002907 }
2908 else {
2909 /* If the FDQN has been changed from stats socket,
2910 * apply fqdn state file value (which is the value set
2911 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002912 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002913 */
2914 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002915 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002916 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002917 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002918 }
2919 }
2920 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002921 /* If all the conditions below are validated, this means
2922 * we're evaluating a server managed by SRV resolution
2923 */
2924 else if (fqdn && !srv->hostname && srvrecord) {
2925 int res;
Emeric Brun32131d42021-06-11 10:48:45 +02002926 int i;
2927 char *tmp;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002928
2929 /* we can't apply previous state if SRV record has changed */
Christopher Fauletcc1fc1b2021-06-10 16:59:53 +02002930 if (!srv->srvrq) {
2931 chunk_appendf(msg, ", no SRV resolution for server '%s'. Previous state not applied", srv->id);
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002932 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2933 goto out;
2934 }
2935
Christopher Fauletcc1fc1b2021-06-10 16:59:53 +02002936 if (strcmp(srv->srvrq->name, srvrecord) != 0) {
2937 chunk_appendf(msg, ", SRV record mismatch between configuration ('%s') and state file ('%s) for server '%s'. Previous state not applied", srv->srvrq->name, srvrecord, srv->id);
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002938 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2939 goto out;
2940 }
2941
2942 /* prepare DNS resolution for this server */
2943 res = srv_prepare_for_resolution(srv, fqdn);
2944 if (res == -1) {
2945 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2946 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2947 goto out;
2948 }
2949
2950 /* configure check.port accordingly */
2951 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2952 !(srv->flags & SRV_F_CHECKPORT))
2953 srv->check.port = port;
2954
Emeric Brun32131d42021-06-11 10:48:45 +02002955 /* Remove from available list and insert in tree
2956 * since this server has an hostname
2957 */
2958 LIST_DEL_INIT(&srv->srv_rec_item);
2959 srv->host_dn.key = tmp = strdup(srv->hostname_dn);
2960
2961 /* convert the key in lowercase because tree
2962 * lookup is case sensitive but we don't care
2963 */
2964 for (i = 0; tmp[i]; i++)
2965 tmp[i] = tolower(tmp[i]);
2966
Christopher Faulet25fbca42021-06-15 16:17:17 +02002967 /* insert in tree and set the srvrq expiration date */
Emeric Brun32131d42021-06-11 10:48:45 +02002968 ebis_insert(&srv->srvrq->named_servers, &srv->host_dn);
Christopher Faulet25fbca42021-06-15 16:17:17 +02002969 task_schedule(srv->srvrq_check, tick_add(now_ms, srv->srvrq->resolvers->hold.timeout));
Emeric Brun32131d42021-06-11 10:48:45 +02002970
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002971 /* Unset SRV_F_MAPPORTS for SRV records.
2972 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2973 * because no ports are provided in the configuration file.
2974 * This is because HAProxy will use the port found into the SRV record.
2975 */
2976 srv->flags &= ~SRV_F_MAPPORTS;
2977 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002978
Frédéric Lécaille31694712017-08-01 08:47:19 +02002979 if (port_str)
2980 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002981 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002982
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002983 break;
2984 default:
2985 chunk_appendf(msg, ", version '%d' not supported", version);
2986 }
2987
2988 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002989 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002990 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002991 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002992 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002993 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002994}
2995
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002996
2997/*
2998 * read next line from file <f> and return the server state version if one found.
2999 * If no version is found, then 0 is returned
3000 * Note that this should be the first read on <f>
3001 */
3002static int srv_state_get_version(FILE *f) {
3003 char buf[2];
3004 int ret;
3005
3006 /* first character of first line of the file must contain the version of the export */
3007 if (fgets(buf, 2, f) == NULL) {
3008 return 0;
3009 }
3010
3011 ret = atoi(buf);
3012 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
3013 (ret > SRV_STATE_FILE_VERSION_MAX))
3014 return 0;
3015
3016 return ret;
3017}
3018
3019
3020/*
3021 * parses server state line stored in <buf> and supposedly in version <version>.
3022 * Set <params> and <srv_params> accordingly.
3023 * In case of error, params[0] is set to NULL.
3024 */
3025static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3026{
3027 int buflen, arg, srv_arg;
3028 char *cur, *end;
3029
3030 buflen = strlen(buf);
3031 cur = buf;
3032 end = cur + buflen;
3033
3034 /* we need at least one character */
3035 if (buflen == 0) {
3036 params[0] = NULL;
3037 return;
3038 }
3039
3040 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003041 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003042 ++cur;
3043
3044 /* Ignore empty or commented lines */
3045 if (cur == end || *cur == '#') {
3046 params[0] = NULL;
3047 return;
3048 }
3049
3050 /* truncated lines */
3051 if (buf[buflen - 1] != '\n') {
3052 //ha_warning("server-state file '%s': truncated line\n", filepath);
3053 params[0] = NULL;
3054 return;
3055 }
3056
3057 /* Removes trailing '\n' */
3058 buf[buflen - 1] = '\0';
3059
3060 /* we're now ready to move the line into *srv_params[] */
Christopher Fauletd119e562021-02-19 16:47:11 +01003061 memset(params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*params));
3062 memset(srv_params, 0, SRV_STATE_FILE_MAX_FIELDS * sizeof(*srv_params));
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003063
3064 arg = 0;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003065 srv_arg = 0;
3066 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003067 /* Search begining of the current field */
3068 while (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003069 ++cur;
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003070 if (!*cur)
3071 goto end;
3072 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003073
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003074 /* v1
3075 * srv_addr: params[4] => srv_params[0]
3076 * srv_op_state: params[5] => srv_params[1]
3077 * srv_admin_state: params[6] => srv_params[2]
3078 * srv_uweight: params[7] => srv_params[3]
3079 * srv_iweight: params[8] => srv_params[4]
3080 * srv_last_time_change: params[9] => srv_params[5]
3081 * srv_check_status: params[10] => srv_params[6]
3082 * srv_check_result: params[11] => srv_params[7]
3083 * srv_check_health: params[12] => srv_params[8]
3084 * srv_check_state: params[13] => srv_params[9]
3085 * srv_agent_state: params[14] => srv_params[10]
3086 * bk_f_forced_id: params[15] => srv_params[11]
3087 * srv_f_forced_id: params[16] => srv_params[12]
3088 * srv_fqdn: params[17] => srv_params[13]
3089 * srv_port: params[18] => srv_params[14]
3090 * srvrecord: params[19] => srv_params[15]
3091 */
3092 if (version == 1 && arg >= 4) {
3093 srv_params[srv_arg] = cur;
3094 ++srv_arg;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003095 }
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003096 params[arg] = cur;
3097 ++arg;
3098
3099 /* Search end of the current field: first space or \0 */
3100 /* Search begining of the current field */
3101 while (!isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003102 ++cur;
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003103 if (!*cur)
3104 goto end;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003105 }
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003106 *cur++ = '\0';
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003107 }
3108
Christopher Faulet8c5b9742021-02-19 16:57:20 +01003109 end:
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003110 /* if line is incomplete line, then ignore it.
3111 * otherwise, update useful flags */
3112 switch (version) {
3113 case 1:
3114 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3115 params[0] = NULL;
3116 return;
3117 }
3118 break;
3119 }
3120
3121 return;
3122}
3123
3124
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003125/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3126 * For each proxy, it does the following:
3127 * - opens its server state file (either one or local one)
3128 * - read whole file, line by line
3129 * - analyse each line to check if it matches our current backend:
3130 * - backend name matches
3131 * - backend id matches if id is forced and name doesn't match
3132 * - if the server pointed by the line is found, then state is applied
3133 *
3134 * If the running backend uuid or id differs from the state file, then HAProxy reports
3135 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003136 *
3137 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003138 */
3139void apply_server_state(void)
3140{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003141 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003142 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3143 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003144 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003145 FILE *f;
3146 char *filepath;
3147 char globalfilepath[MAXPATHLEN + 1];
3148 char localfilepath[MAXPATHLEN + 1];
3149 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003150 struct proxy *curproxy, *bk;
3151 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003152 char *line;
3153 char *bkname, *srvname;
3154 struct state_line *st;
3155 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003156
Willy Tarreau2d067f92020-07-16 06:40:27 +02003157 f = NULL;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003158 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003159 globalfilepathlen = 0;
3160 /* create the globalfilepath variable */
3161 if (global.server_state_file) {
3162 /* absolute path or no base directory provided */
3163 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3164 len = strlen(global.server_state_file);
3165 if (len > MAXPATHLEN) {
3166 globalfilepathlen = 0;
3167 goto globalfileerror;
3168 }
3169 memcpy(globalfilepath, global.server_state_file, len);
3170 globalfilepath[len] = '\0';
3171 globalfilepathlen = len;
3172 }
3173 else if (global.server_state_base) {
3174 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003175 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003176 globalfilepathlen = 0;
3177 goto globalfileerror;
3178 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003179 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003180 globalfilepath[len] = 0;
3181 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003182
3183 /* append a slash if needed */
3184 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3185 if (globalfilepathlen + 1 > MAXPATHLEN) {
3186 globalfilepathlen = 0;
3187 goto globalfileerror;
3188 }
3189 globalfilepath[globalfilepathlen++] = '/';
3190 }
3191
3192 len = strlen(global.server_state_file);
3193 if (globalfilepathlen + len > MAXPATHLEN) {
3194 globalfilepathlen = 0;
3195 goto globalfileerror;
3196 }
3197 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3198 globalfilepathlen += len;
3199 globalfilepath[globalfilepathlen++] = 0;
3200 }
3201 }
3202 globalfileerror:
3203 if (globalfilepathlen == 0)
3204 globalfilepath[0] = '\0';
3205
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003206 /* Load global server state in a tree */
3207 if (globalfilepathlen > 0) {
3208 errno = 0;
3209 f = fopen(globalfilepath, "r");
3210 if (errno)
3211 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003212 if (!f)
3213 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003214
3215 global_file_version = srv_state_get_version(f);
3216 if (global_file_version == 0)
3217 goto out_load_server_state_in_tree;
3218
3219 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3220 line = NULL;
3221
3222 line = strdup(mybuf);
3223 if (line == NULL)
3224 continue;
3225
3226 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3227 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003228 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003229
3230 /* bkname */
3231 bkname = params[1];
3232 /* srvname */
3233 srvname = params[3];
3234
3235 /* key */
3236 chunk_printf(&trash, "%s %s", bkname, srvname);
3237
3238 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003239 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003240 if (st == NULL) {
3241 goto nextline;
3242 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003243 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003244 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3245 /* this is a duplicate key, probably a hand-crafted file,
3246 * drop it!
3247 */
3248 goto nextline;
3249 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003250
3251 /* save line */
3252 st->line = line;
3253
3254 continue;
3255
3256 nextline:
3257 /* free up memory in case of error during the processing of the line */
3258 free(line);
3259 }
3260 }
3261 out_load_server_state_in_tree:
3262
Ilya Shipitsindc6e8a92020-07-16 02:02:40 +05003263 if (f) {
3264 fclose(f);
3265 f = NULL;
3266 }
3267
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003268 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003269 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003270 /* servers are only in backends */
3271 if (!(curproxy->cap & PR_CAP_BE))
3272 continue;
3273 fileopenerr = 0;
3274 filepath = NULL;
3275
3276 /* search server state file path and name */
3277 switch (curproxy->load_server_state_from_file) {
3278 /* read servers state from global file */
3279 case PR_SRV_STATE_FILE_GLOBAL:
3280 /* there was an error while generating global server state file path */
3281 if (globalfilepathlen == 0)
3282 continue;
3283 filepath = globalfilepath;
3284 fileopenerr = 1;
3285 break;
3286 /* this backend has its own file */
3287 case PR_SRV_STATE_FILE_LOCAL:
3288 localfilepathlen = 0;
3289 localfilepath[0] = '\0';
3290 len = 0;
3291 /* create the localfilepath variable */
3292 /* absolute path or no base directory provided */
3293 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3294 len = strlen(curproxy->server_state_file_name);
3295 if (len > MAXPATHLEN) {
3296 localfilepathlen = 0;
3297 goto localfileerror;
3298 }
3299 memcpy(localfilepath, curproxy->server_state_file_name, len);
3300 localfilepath[len] = '\0';
3301 localfilepathlen = len;
3302 }
3303 else if (global.server_state_base) {
3304 len = strlen(global.server_state_base);
3305 localfilepathlen += len;
3306
3307 if (localfilepathlen > MAXPATHLEN) {
3308 localfilepathlen = 0;
3309 goto localfileerror;
3310 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003311 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003312 localfilepath[localfilepathlen] = 0;
3313
3314 /* append a slash if needed */
3315 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3316 if (localfilepathlen + 1 > MAXPATHLEN) {
3317 localfilepathlen = 0;
3318 goto localfileerror;
3319 }
3320 localfilepath[localfilepathlen++] = '/';
3321 }
3322
3323 len = strlen(curproxy->server_state_file_name);
3324 if (localfilepathlen + len > MAXPATHLEN) {
3325 localfilepathlen = 0;
3326 goto localfileerror;
3327 }
3328 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3329 localfilepathlen += len;
3330 localfilepath[localfilepathlen++] = 0;
3331 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003332 localfileerror:
3333 if (localfilepathlen == 0)
Christopher Fauletacef6b92021-02-12 16:31:03 +01003334 continue;
3335 filepath = localfilepath;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003336
3337 break;
3338 case PR_SRV_STATE_FILE_NONE:
3339 default:
3340 continue;
3341 }
3342
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003343 /* when global file is used, we get data from the tree
3344 * Note that in such case we don't check backend name neither uuid.
3345 * Backend name can't be wrong since it's used as a key to retrieve the server state
3346 * line from the tree.
3347 */
3348 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3349 struct server *srv = curproxy->srv;
3350 while (srv) {
3351 struct ebmb_node *node;
3352 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003353
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003354 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3355 node = ebst_lookup(&state_file, trash.area);
3356 if (!node)
3357 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003358
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003359 st = container_of(node, struct state_line, name_name);
3360 memcpy(mybuf, st->line, strlen(st->line));
3361 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003362
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003363 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3364 if (params[0] == NULL)
3365 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003366
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003367 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003368
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003369 next:
3370 srv = srv->next;
3371 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003372
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003373 continue; /* next proxy in list */
3374 }
3375 else {
3376 /* load 'local' state file */
3377 errno = 0;
3378 f = fopen(filepath, "r");
3379 if (errno && fileopenerr)
3380 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3381 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003382 continue;
3383
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003384 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003385
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003386 /* first character of first line of the file must contain the version of the export */
3387 version = srv_state_get_version(f);
3388 if (version == 0) {
3389 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3390 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003391 }
3392
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003393 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3394 int bk_f_forced_id = 0;
3395 int check_id = 0;
3396 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003397
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003398 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003399
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003400 if (params[0] == NULL) {
3401 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003402 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003403
3404 /* if line is incomplete line, then ignore it.
3405 * otherwise, update useful flags */
3406 switch (version) {
3407 case 1:
3408 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3409 check_id = (atoi(params[0]) == curproxy->uuid);
3410 check_name = (strcmp(curproxy->id, params[1]) == 0);
3411 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003412 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003413
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003414 bk = curproxy;
3415
3416 /* if backend can't be found, let's continue */
3417 if (!check_id && !check_name)
3418 continue;
3419 else if (!check_id && check_name) {
3420 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3421 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3422 }
3423 else if (check_id && !check_name) {
3424 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3425 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3426 /* if name doesn't match, we still want to update curproxy if the backend id
3427 * was forced in previous the previous configuration */
3428 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003429 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003430 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003431
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003432 /* look for the server by its name: param[3] */
3433 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003434
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003435 if (!srv) {
3436 /* if no server found, then warning and continue with next line */
3437 ha_warning("can't find server '%s' in backend '%s'\n",
3438 params[3], params[1]);
3439 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3440 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003441 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003442 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003443
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003444 /* now we can proceed with server's state update */
3445 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003446 }
Ilya Shipitsindc6e8a92020-07-16 02:02:40 +05003447
3448 fileclose:
3449 fclose(f);
3450
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003451 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003452 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003453
3454 /* now free memory allocated for the tree */
3455 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3456 node;
3457 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3458 st = container_of(node, struct state_line, name_name);
3459 ebmb_delete(&st->name_name);
3460 free(st->line);
3461 free(st);
3462 }
3463
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003464}
3465
Simon Horman7d09b9a2013-02-12 10:45:51 +09003466/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003467 * update a server's current IP address.
3468 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3469 * ip is in network format.
3470 * updater is a string which contains an information about the requester of the update.
3471 * updater is used if not NULL.
3472 *
3473 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003474 *
3475 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003476 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003477int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003478{
Christopher Fauletd6c6b5f2020-09-08 10:27:24 +02003479 /* save the new IP family & address if necessary */
3480 switch (ip_sin_family) {
3481 case AF_INET:
3482 if (s->addr.ss_family == ip_sin_family &&
3483 !memcmp(ip, &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, 4))
3484 return 0;
3485 break;
3486 case AF_INET6:
3487 if (s->addr.ss_family == ip_sin_family &&
3488 !memcmp(ip, &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, 16))
3489 return 0;
3490 break;
3491 };
3492
Baptiste Assmann14e40142015-04-14 01:13:07 +02003493 /* generates a log line and a warning on stderr */
3494 if (1) {
3495 /* book enough space for both IPv4 and IPv6 */
3496 char oldip[INET6_ADDRSTRLEN];
3497 char newip[INET6_ADDRSTRLEN];
3498
3499 memset(oldip, '\0', INET6_ADDRSTRLEN);
3500 memset(newip, '\0', INET6_ADDRSTRLEN);
3501
3502 /* copy old IP address in a string */
3503 switch (s->addr.ss_family) {
3504 case AF_INET:
3505 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3506 break;
3507 case AF_INET6:
3508 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3509 break;
Christopher Fauletb0b76072020-09-08 10:38:40 +02003510 default:
3511 strcpy(oldip, "(none)");
3512 break;
Baptiste Assmann14e40142015-04-14 01:13:07 +02003513 };
3514
3515 /* copy new IP address in a string */
3516 switch (ip_sin_family) {
3517 case AF_INET:
3518 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3519 break;
3520 case AF_INET6:
3521 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3522 break;
3523 };
3524
3525 /* save log line into a buffer */
3526 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3527 s->proxy->id, s->id, oldip, newip, updater);
3528
3529 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003530 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003531
3532 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003533 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003534 }
3535
3536 /* save the new IP family */
3537 s->addr.ss_family = ip_sin_family;
3538 /* save the new IP address */
3539 switch (ip_sin_family) {
3540 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003541 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003542 break;
3543 case AF_INET6:
3544 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3545 break;
3546 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003547 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003548
3549 return 0;
3550}
3551
3552/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003553 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3554 *
3555 * Caller can pass its name through <updater> to get it integrated in the response message
3556 * returned by the function.
3557 *
3558 * The function first does the following, in that order:
3559 * - validates the new addr and/or port
3560 * - checks if an update is required (new IP or port is different than current ones)
3561 * - checks the update is allowed:
3562 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3563 * - allow all changes if no CHECKS are configured
3564 * - if CHECK is configured:
3565 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3566 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3567 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003568 *
3569 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003570 */
3571const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3572{
3573 struct sockaddr_storage sa;
3574 int ret, port_change_required;
3575 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003576 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003577 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003578 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003579
3580 msg = get_trash_chunk();
3581 chunk_reset(msg);
3582
3583 if (addr) {
3584 memset(&sa, 0, sizeof(struct sockaddr_storage));
3585 if (str2ip2(addr, &sa, 0) == NULL) {
3586 chunk_printf(msg, "Invalid addr '%s'", addr);
3587 goto out;
3588 }
3589
3590 /* changes are allowed on AF_INET* families only */
3591 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3592 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3593 goto out;
3594 }
3595
3596 /* collecting data currently setup */
3597 memset(current_addr, '\0', sizeof(current_addr));
3598 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3599 /* changes are allowed on AF_INET* families only */
3600 if ((ret != AF_INET) && (ret != AF_INET6)) {
3601 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3602 goto out;
3603 }
3604
3605 /* applying ADDR changes if required and allowed
3606 * ipcmp returns 0 when both ADDR are the same
3607 */
3608 if (ipcmp(&s->addr, &sa) == 0) {
3609 chunk_appendf(msg, "no need to change the addr");
3610 goto port;
3611 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003612 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003613 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003614
3615 /* we also need to update check's ADDR only if it uses the server's one */
3616 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003617 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003618 }
3619
3620 /* we also need to update agent ADDR only if it use the server's one */
3621 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003622 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003623 }
3624
3625 /* update report for caller */
3626 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3627 }
3628
3629 port:
3630 if (port) {
3631 char sign = '\0';
3632 char *endptr;
3633
3634 if (addr)
3635 chunk_appendf(msg, ", ");
3636
3637 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003638 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003639
3640 /* check if PORT change is required */
3641 port_change_required = 0;
3642
3643 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003644 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003645 new_port = strtol(port, &endptr, 10);
3646 if ((errno != 0) || (port == endptr)) {
3647 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3648 goto out;
3649 }
3650
3651 /* check if caller triggers a port mapped or offset */
3652 if (sign == '-' || (sign == '+')) {
3653 /* check if server currently uses port map */
3654 if (!(s->flags & SRV_F_MAPPORTS)) {
3655 /* switch from fixed port to port map mandatorily triggers
3656 * a port change */
3657 port_change_required = 1;
3658 /* check is configured
3659 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3660 * prevent PORT change if check doesn't have it's dedicated port while switching
3661 * to port mapping */
3662 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3663 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.");
3664 goto out;
3665 }
3666 }
3667 /* we're already using port maps */
3668 else {
3669 port_change_required = current_port != new_port;
3670 }
3671 }
3672 /* fixed port */
3673 else {
3674 port_change_required = current_port != new_port;
3675 }
3676
3677 /* applying PORT changes if required and update response message */
3678 if (port_change_required) {
3679 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003680 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003681 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003682
3683 /* prepare message */
3684 chunk_appendf(msg, "port changed from '");
3685 if (s->flags & SRV_F_MAPPORTS)
3686 chunk_appendf(msg, "+");
3687 chunk_appendf(msg, "%d' to '", current_port);
3688
3689 if (sign == '-') {
3690 s->flags |= SRV_F_MAPPORTS;
3691 chunk_appendf(msg, "%c", sign);
3692 /* just use for result output */
3693 new_port = -new_port;
3694 }
3695 else if (sign == '+') {
3696 s->flags |= SRV_F_MAPPORTS;
3697 chunk_appendf(msg, "%c", sign);
3698 }
3699 else {
3700 s->flags &= ~SRV_F_MAPPORTS;
3701 }
3702
3703 chunk_appendf(msg, "%d'", new_port);
3704
3705 /* we also need to update health checks port only if it uses server's realport */
3706 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3707 s->check.port = new_port;
3708 }
3709 }
3710 else {
3711 chunk_appendf(msg, "no need to change the port");
3712 }
3713 }
3714
3715out:
William Dauchy6318d332020-05-02 21:52:36 +02003716 if (changed) {
3717 /* force connection cleanup on the given server */
3718 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003719 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003720 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003721 if (updater)
3722 chunk_appendf(msg, " by '%s'", updater);
3723 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003724 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003725}
3726
Christopher Faulet6d703682021-03-11 18:03:21 +01003727/*
3728 * update server status based on result of SRV resolution
3729 * returns:
3730 * 0 if server status is updated
3731 * 1 if server status has not changed
3732 *
3733 * Must be called with the server lock held.
3734 */
3735int srvrq_update_srv_status(struct server *s, int has_no_ip)
3736{
3737 if (!s->srvrq)
3738 return 1;
3739
3740 /* since this server has an IP, it can go back in production */
3741 if (has_no_ip == 0) {
3742 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3743 return 1;
3744 }
3745
3746 if (s->next_admin & SRV_ADMF_RMAINT)
3747 return 1;
3748
3749 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "entry removed from SRV record");
3750 return 0;
3751}
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003752
3753/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003754 * update server status based on result of name resolution
3755 * returns:
3756 * 0 if server status is updated
3757 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003758 *
3759 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003760 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003761int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003762{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003763 struct dns_resolvers *resolvers = s->resolvers;
Christopher Faulet55810262021-03-12 10:23:05 +01003764 struct dns_resolution *resolution = (s->dns_requester ? s->dns_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003765 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003766
Jerome Magnin012261a2020-07-28 13:38:22 +02003767 /* If resolution is NULL we're dealing with SRV records Additional records */
Christopher Faulet6d703682021-03-11 18:03:21 +01003768 if (resolution == NULL)
3769 return srvrq_update_srv_status(s, has_no_ip);
Jerome Magnin012261a2020-07-28 13:38:22 +02003770
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003771 switch (resolution->status) {
3772 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003773 /* status when HAProxy has just (re)started.
3774 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003775 break;
3776
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003777 case RSLV_STATUS_VALID:
3778 /*
3779 * resume health checks
3780 * server will be turned back on if health check is safe
3781 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003782 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003783 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003784 return 1;
3785 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3786 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003787 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003788 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003789
Emeric Brun52a91d32017-08-31 14:41:55 +02003790 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003791 return 1;
3792 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3793 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3794 s->proxy->id, s->id);
3795
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003796 ha_warning("%s.\n", trash.area);
3797 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003798 return 0;
3799
3800 case RSLV_STATUS_NX:
3801 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003802 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3803 if (!tick_is_expired(exp, now_ms))
3804 break;
3805
3806 if (s->next_admin & SRV_ADMF_RMAINT)
3807 return 1;
3808 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3809 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003810
3811 case RSLV_STATUS_TIMEOUT:
3812 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003813 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3814 if (!tick_is_expired(exp, now_ms))
3815 break;
3816
3817 if (s->next_admin & SRV_ADMF_RMAINT)
3818 return 1;
3819 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3820 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003821
3822 case RSLV_STATUS_REFUSED:
3823 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003824 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3825 if (!tick_is_expired(exp, now_ms))
3826 break;
3827
3828 if (s->next_admin & SRV_ADMF_RMAINT)
3829 return 1;
3830 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3831 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003832
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003833 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003834 /* stop server if resolution failed for a long enough period */
3835 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3836 if (!tick_is_expired(exp, now_ms))
3837 break;
3838
3839 if (s->next_admin & SRV_ADMF_RMAINT)
3840 return 1;
3841 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3842 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003843 }
3844
3845 return 1;
3846}
3847
3848/*
3849 * Server Name Resolution valid response callback
3850 * It expects:
3851 * - <nameserver>: the name server which answered the valid response
3852 * - <response>: buffer containing a valid DNS response
3853 * - <response_len>: size of <response>
3854 * It performs the following actions:
3855 * - ignore response if current ip found and server family not met
3856 * - update with first new ip found if family is met and current IP is not found
3857 * returns:
3858 * 0 on error
3859 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003860 *
3861 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003862 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003865 struct server *s = NULL;
3866 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003867 void *serverip, *firstip;
3868 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003869 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003870 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003871 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003872
Christopher Faulet67957bd2017-09-27 11:00:59 +02003873 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003874 if (!s)
3875 return 1;
3876
Christopher Fauletdb001942021-03-10 15:07:27 +01003877 if (s->srvrq) {
Emeric Brun32131d42021-06-11 10:48:45 +02003878 /* If DNS resolution is disabled ignore it.
3879 * This is the case if the server was associated to
3880 * a SRV record and this record is now expired.
Christopher Fauletdb001942021-03-10 15:07:27 +01003881 */
Emeric Brun32131d42021-06-11 10:48:45 +02003882 if (s->flags & SRV_F_NO_RESOLUTION)
Christopher Fauletdb001942021-03-10 15:07:27 +01003883 return 1;
3884 }
3885
Christopher Faulet55810262021-03-12 10:23:05 +01003886 resolution = (s->dns_requester ? s->dns_requester->resolution : NULL);
Christopher Fauletdb001942021-03-10 15:07:27 +01003887 if (!resolution)
3888 return 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003889
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003890 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003891 firstip = NULL; /* pointer to the first valid response found */
3892 /* it will be used as the new IP if a change is required */
3893 firstip_sin_family = AF_UNSPEC;
3894 serverip = NULL; /* current server IP address */
3895
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003896 /* initializing server IP pointer */
3897 server_sin_family = s->addr.ss_family;
3898 switch (server_sin_family) {
3899 case AF_INET:
3900 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3901 break;
3902
3903 case AF_INET6:
3904 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3905 break;
3906
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003907 case AF_UNSPEC:
3908 break;
3909
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003910 default:
3911 goto invalid;
3912 }
3913
Baptiste Assmann729c9012017-05-22 15:13:10 +02003914 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003915 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003916 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003917
3918 switch (ret) {
3919 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003920 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003921
3922 case DNS_UPD_SRVIP_NOT_FOUND:
3923 goto save_ip;
3924
3925 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926 goto invalid;
3927
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003928 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003929 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003930 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003931
Baptiste Assmannfad03182015-10-28 02:03:32 +01003932 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003933 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003934 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003935 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003936
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003937 default:
3938 goto invalid;
3939
3940 }
3941
3942 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003943 if (nameserver) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003944 nameserver->counters->update++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003945 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003946 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003947 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003948 else
3949 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003950 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003951
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003952 update_status:
Christopher Fauletdb001942021-03-10 15:07:27 +01003953
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003954 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003955 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003956
3957 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003958 if (nameserver) {
Amaury Denoyellefbd0bc92020-10-05 11:49:46 +02003959 nameserver->counters->invalid++;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003960 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003961 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003962 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003963 return 0;
3964}
3965
3966/*
Baptiste Assmann826060e2020-11-19 22:38:33 +01003967 * SRV record error management callback
3968 * returns:
Emeric Bruna8b60f22021-06-10 15:25:25 +02003969 * 0 if we can trash answser items.
3970 * 1 when safely ignored and we must kept answer items
Baptiste Assmann826060e2020-11-19 22:38:33 +01003971 *
3972 * Grabs the server's lock.
3973 */
3974int srvrq_resolution_error_cb(struct dns_requester *requester, int error_code)
3975{
Baptiste Assmann826060e2020-11-19 22:38:33 +01003976 struct dns_srvrq *srvrq;
3977 struct dns_resolution *res;
3978 struct dns_resolvers *resolvers;
3979 int exp;
3980
3981 /* SRV records */
3982 srvrq = objt_dns_srvrq(requester->owner);
3983 if (!srvrq)
Emeric Bruna8b60f22021-06-10 15:25:25 +02003984 return 0;
Baptiste Assmann826060e2020-11-19 22:38:33 +01003985
3986 resolvers = srvrq->resolvers;
3987 res = requester->resolution;
3988
3989 switch (res->status) {
3990
3991 case RSLV_STATUS_NX:
3992 /* stop server if resolution is NX for a long enough period */
3993 exp = tick_add(res->last_valid, resolvers->hold.nx);
3994 if (!tick_is_expired(exp, now_ms))
3995 return 1;
3996 break;
3997
3998 case RSLV_STATUS_TIMEOUT:
3999 /* stop server if resolution is TIMEOUT for a long enough period */
4000 exp = tick_add(res->last_valid, resolvers->hold.timeout);
4001 if (!tick_is_expired(exp, now_ms))
4002 return 1;
4003 break;
4004
4005 case RSLV_STATUS_REFUSED:
4006 /* stop server if resolution is REFUSED for a long enough period */
4007 exp = tick_add(res->last_valid, resolvers->hold.refused);
4008 if (!tick_is_expired(exp, now_ms))
4009 return 1;
4010 break;
4011
4012 default:
4013 /* stop server if resolution failed for a long enough period */
4014 exp = tick_add(res->last_valid, resolvers->hold.other);
4015 if (!tick_is_expired(exp, now_ms))
4016 return 1;
4017 }
4018
Emeric Brun32131d42021-06-11 10:48:45 +02004019 /* Remove any associated server ref */
4020 dns_detach_from_resolution_answer_items(res, requester, 1);
Baptiste Assmann826060e2020-11-19 22:38:33 +01004021
Emeric Bruna8b60f22021-06-10 15:25:25 +02004022 return 0;
Baptiste Assmann826060e2020-11-19 22:38:33 +01004023}
4024
4025/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004026 * Server Name Resolution error management callback
4027 * returns:
Emeric Bruna8b60f22021-06-10 15:25:25 +02004028 * 0 if we can trash answser items.
4029 * 1 when safely ignored and we must kept answer items
Willy Tarreau46b7f532018-08-21 11:54:26 +02004030 *
4031 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004032 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004033int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004034{
Christopher Faulet67957bd2017-09-27 11:00:59 +02004035 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004036
Christopher Faulet67957bd2017-09-27 11:00:59 +02004037 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004038 if (!s)
Emeric Bruna8b60f22021-06-10 15:25:25 +02004039 return 0;
4040
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004041 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Emeric Bruna8b60f22021-06-10 15:25:25 +02004042 if (!snr_update_srv_status(s, 1)) {
Christopher Faulet7c9201e2021-03-10 20:31:40 +01004043 memset(&s->addr, 0, sizeof(s->addr));
Emeric Bruna8b60f22021-06-10 15:25:25 +02004044 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Brun32131d42021-06-11 10:48:45 +02004045 dns_detach_from_resolution_answer_items(requester->resolution, requester, 1);
Emeric Bruna8b60f22021-06-10 15:25:25 +02004046 return 0;
4047 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004048 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Emeric Bruna8b60f22021-06-10 15:25:25 +02004049
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004050 return 1;
4051}
4052
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004053/*
4054 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004055 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004056 * It returns a pointer to the first server found or NULL if <ip> is not yet
4057 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01004058 *
4059 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004060 */
4061struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
4062{
4063 struct server *tmpsrv;
4064 struct proxy *be;
4065
4066 if (!srv)
4067 return NULL;
4068
4069 be = srv->proxy;
4070 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004071 /* we found the current server is the same, ignore it */
4072 if (srv == tmpsrv)
4073 continue;
4074
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004075 /* We want to compare the IP in the record with the IP of the servers in the
4076 * same backend, only if:
4077 * * DNS resolution is enabled on the server
4078 * * the hostname used for the resolution by our server is the same than the
4079 * one used for the server found in the backend
4080 * * the server found in the backend is not our current server
4081 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004082 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004083 if ((tmpsrv->hostname_dn == NULL) ||
4084 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
Christopher Fauletd31f6942021-03-16 11:21:04 +01004085 (strcasecmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004086 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004087 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004088 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004089 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004090
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004091 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004092 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004093 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004094 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004095 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004096
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004097 /* At this point, we have 2 different servers using the same DNS hostname
4098 * for their respective resolution.
4099 */
4100 if (*ip_family == tmpsrv->addr.ss_family &&
4101 ((tmpsrv->addr.ss_family == AF_INET &&
4102 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4103 (tmpsrv->addr.ss_family == AF_INET6 &&
4104 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004105 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004106 return tmpsrv;
4107 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004108 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004109 }
4110
Emeric Brune9fd6b52017-11-02 17:20:39 +01004111
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004112 return NULL;
4113}
4114
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004115/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4116 * resolver. This is suited for initial address configuration. Returns 0 on
4117 * success otherwise a non-zero error code. In case of error, *err_code, if
4118 * not NULL, is filled up.
4119 */
4120int srv_set_addr_via_libc(struct server *srv, int *err_code)
4121{
4122 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004123 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004124 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004125 return 1;
4126 }
4127 return 0;
4128}
4129
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004130/* Set the server's FDQN (->hostname) from <hostname>.
4131 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004132 *
4133 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004134 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004135int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004136{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004137 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004138 char *hostname_dn;
4139 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004140
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004141 /* Note that the server lock is already held. */
4142 if (!srv->resolvers)
4143 return -1;
4144
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004145 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004146 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Faulet55810262021-03-12 10:23:05 +01004147 /* run time DNS/SRV resolution was not active for this server
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004148 * and we can't enable it at run time for now.
4149 */
Christopher Faulet55810262021-03-12 10:23:05 +01004150 if (!srv->dns_requester && !srv->srvrq)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004151 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004152
4153 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004154 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004155 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004156 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4157 hostname_dn, trash.size);
4158 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004159 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004160
Christopher Faulet55810262021-03-12 10:23:05 +01004161 resolution = (srv->dns_requester ? srv->dns_requester->resolution : NULL);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004162 if (resolution &&
4163 resolution->hostname_dn &&
Christopher Fauletd31f6942021-03-16 11:21:04 +01004164 resolution->hostname_dn_len == hostname_dn_len &&
4165 strcasecmp(resolution->hostname_dn, hostname_dn) == 0)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004166 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004167
Christopher Faulet119ec522021-03-11 18:09:53 +01004168 dns_unlink_resolution(srv->dns_requester, 0);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004169
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004170 free(srv->hostname);
4171 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004172 srv->hostname = strdup(hostname);
4173 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004174 srv->hostname_dn_len = hostname_dn_len;
4175 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004176 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004177
Baptiste Assmann13a92322019-06-07 09:40:55 +02004178 if (srv->flags & SRV_F_NO_RESOLUTION)
4179 goto end;
4180
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004181 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004182 goto err;
4183
4184 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004185 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004186 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004187 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004188
4189 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004190 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004191 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004192 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004193}
4194
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004195/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4196 * from the state file. This is suited for initial address configuration.
4197 * Returns 0 on success otherwise a non-zero error code. In case of error,
4198 * *err_code, if not NULL, is filled up.
4199 */
4200static int srv_apply_lastaddr(struct server *srv, int *err_code)
4201{
4202 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4203 if (err_code)
4204 *err_code |= ERR_WARN;
4205 return 1;
4206 }
4207 return 0;
4208}
4209
Willy Tarreau25e51522016-11-04 15:10:17 +01004210/* returns 0 if no error, otherwise a combination of ERR_* flags */
4211static int srv_iterate_initaddr(struct server *srv)
4212{
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004213 char *name = srv->hostname;
Willy Tarreau25e51522016-11-04 15:10:17 +01004214 int return_code = 0;
4215 int err_code;
4216 unsigned int methods;
4217
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004218 /* If no addr and no hostname set, get the name from the DNS SRV request */
4219 if (!name && srv->srvrq)
4220 name = srv->srvrq->name;
4221
Willy Tarreau25e51522016-11-04 15:10:17 +01004222 methods = srv->init_addr_methods;
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004223 if (!methods) {
4224 /* otherwise default to "last,libc" */
Willy Tarreau25e51522016-11-04 15:10:17 +01004225 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4226 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004227 if (srv->resolvers_id) {
4228 /* dns resolution is configured, add "none" to not fail on startup */
4229 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4230 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004231 }
4232
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004233 /* "-dr" : always append "none" so that server addresses resolution
4234 * failures are silently ignored, this is convenient to validate some
4235 * configs out of their environment.
4236 */
4237 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4238 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4239
Willy Tarreau25e51522016-11-04 15:10:17 +01004240 while (methods) {
4241 err_code = 0;
4242 switch (srv_get_next_initaddr(&methods)) {
4243 case SRV_IADDR_LAST:
4244 if (!srv->lastaddr)
4245 continue;
4246 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004247 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004248 return_code |= err_code;
4249 break;
4250
4251 case SRV_IADDR_LIBC:
4252 if (!srv->hostname)
4253 continue;
4254 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004255 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004256 return_code |= err_code;
4257 break;
4258
Willy Tarreau37ebe122016-11-04 15:17:58 +01004259 case SRV_IADDR_NONE:
4260 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004261 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004262 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004263 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004264 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004265 return return_code;
4266
Willy Tarreau4310d362016-11-02 15:05:56 +01004267 case SRV_IADDR_IP:
4268 ipcpy(&srv->init_addr, &srv->addr);
4269 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004270 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004271 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau4310d362016-11-02 15:05:56 +01004272 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004273 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004274
Willy Tarreau25e51522016-11-04 15:10:17 +01004275 default: /* unhandled method */
4276 break;
4277 }
4278 }
4279
4280 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004281 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004282 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau25e51522016-11-04 15:10:17 +01004283 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004284 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004285 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004286 srv->conf.file, srv->conf.line, srv->id, name);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004287 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004288
4289 return_code |= ERR_ALERT | ERR_FATAL;
4290 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004291out:
4292 srv_set_dyncookie(srv);
4293 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004294}
4295
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004296/*
4297 * This function parses all backends and all servers within each backend
4298 * and performs servers' addr resolution based on information provided by:
4299 * - configuration file
4300 * - server-state file (states provided by an 'old' haproxy process)
4301 *
4302 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4303 */
4304int srv_init_addr(void)
4305{
4306 struct proxy *curproxy;
4307 int return_code = 0;
4308
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004309 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004310 while (curproxy) {
4311 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004312
4313 /* servers are in backend only */
Amaury Denoyelle96a4ff12021-01-06 14:28:50 +01004314 if (!(curproxy->cap & PR_CAP_BE) || curproxy->disabled)
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004315 goto srv_init_addr_next;
4316
Willy Tarreau25e51522016-11-04 15:10:17 +01004317 for (srv = curproxy->srv; srv; srv = srv->next)
Christopher Fauletac1c60fd2020-10-26 10:31:17 +01004318 if (srv->hostname || srv->srvrq)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004319 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004320
4321 srv_init_addr_next:
4322 curproxy = curproxy->next;
4323 }
4324
4325 return return_code;
4326}
4327
Willy Tarreau46b7f532018-08-21 11:54:26 +02004328/*
4329 * Must be called with the server lock held.
4330 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004331const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004332{
4333
Willy Tarreau83061a82018-07-13 11:56:34 +02004334 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004335
4336 msg = get_trash_chunk();
4337 chunk_reset(msg);
4338
Olivier Houchard8da5f982017-08-04 18:35:36 +02004339 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004340 chunk_appendf(msg, "no need to change the FDQN");
4341 goto out;
4342 }
4343
4344 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4345 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4346 goto out;
4347 }
4348
4349 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4350 server->proxy->id, server->id, server->hostname, fqdn);
4351
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004352 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004353 chunk_reset(msg);
4354 chunk_appendf(msg, "could not update %s/%s FQDN",
4355 server->proxy->id, server->id);
4356 goto out;
4357 }
4358
4359 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004360 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004361
4362 out:
4363 if (updater)
4364 chunk_appendf(msg, " by '%s'", updater);
4365 chunk_appendf(msg, "\n");
4366
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004367 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004368}
4369
4370
Willy Tarreau21b069d2016-11-23 17:15:08 +01004371/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4372 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004373 * 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 +01004374 * used for CLI commands requiring a server name.
4375 * Important: the <arg> is modified to remove the '/'.
4376 */
4377struct server *cli_find_server(struct appctx *appctx, char *arg)
4378{
4379 struct proxy *px;
4380 struct server *sv;
4381 char *line;
4382
4383 /* split "backend/server" and make <line> point to server */
4384 for (line = arg; *line; line++)
4385 if (*line == '/') {
4386 *line++ = '\0';
4387 break;
4388 }
4389
4390 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004391 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004392 return NULL;
4393 }
4394
4395 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004396 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004397 return NULL;
4398 }
4399
Willy Tarreauc3914d42020-09-24 08:39:22 +02004400 if (px->disabled) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004401 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004402 return NULL;
4403 }
4404
4405 return sv;
4406}
4407
William Lallemand222baf22016-11-19 02:00:33 +01004408
Willy Tarreau46b7f532018-08-21 11:54:26 +02004409/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004410static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004411{
4412 struct server *sv;
4413 const char *warning;
4414
4415 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4416 return 1;
4417
4418 sv = cli_find_server(appctx, args[2]);
4419 if (!sv)
4420 return 1;
4421
4422 if (strcmp(args[3], "weight") == 0) {
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004423 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004424 warning = server_parse_weight_change_request(sv, args[4]);
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004425 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004426 if (warning)
4427 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004428 }
4429 else if (strcmp(args[3], "state") == 0) {
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004430 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004431 if (strcmp(args[4], "ready") == 0)
4432 srv_adm_set_ready(sv);
4433 else if (strcmp(args[4], "drain") == 0)
4434 srv_adm_set_drain(sv);
4435 else if (strcmp(args[4], "maint") == 0)
4436 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004437 else
4438 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004439 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004440 }
4441 else if (strcmp(args[3], "health") == 0) {
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004442 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004443 if (sv->track)
4444 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004445 else if (strcmp(args[4], "up") == 0) {
4446 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004447 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004448 }
4449 else if (strcmp(args[4], "stopping") == 0) {
4450 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004451 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004452 }
4453 else if (strcmp(args[4], "down") == 0) {
4454 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004455 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004456 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004457 else
4458 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004459 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004460 }
4461 else if (strcmp(args[3], "agent") == 0) {
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004462 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004463 if (!(sv->agent.state & CHK_ST_ENABLED))
4464 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004465 else if (strcmp(args[4], "up") == 0) {
4466 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004467 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004468 }
4469 else if (strcmp(args[4], "down") == 0) {
4470 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004471 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004472 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004473 else
4474 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004475 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004476 }
Misiek2da082d2017-01-09 09:40:42 +01004477 else if (strcmp(args[3], "agent-addr") == 0) {
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004478 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004479 if (!(sv->agent.state & CHK_ST_ENABLED))
4480 cli_err(appctx, "agent checks are not enabled on this server.\n");
4481 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4482 cli_err(appctx, "incorrect addr address given for agent.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004483 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004484 }
4485 else if (strcmp(args[3], "agent-send") == 0) {
Christopher Fauletb123e2b2021-06-18 08:47:14 +02004486 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004487 if (!(sv->agent.state & CHK_ST_ENABLED))
4488 cli_err(appctx, "agent checks are not enabled on this server.\n");
4489 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004490 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004491 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004492 }
Christopher Fauletb123e2b2021-06-18 08:47:14 +02004493 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Misiek2da082d2017-01-09 09:40:42 +01004494 }
William Lallemand222baf22016-11-19 02:00:33 +01004495 else if (strcmp(args[3], "check-port") == 0) {
4496 int i = 0;
4497 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004498 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004499 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004500 }
4501 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004502 cli_err(appctx, "provided port is not valid.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004503 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004504 }
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004505 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004506 /* prevent the update of port to 0 if MAPPORTS are in use */
4507 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004508 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004509 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4510 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004511 }
4512 sv->check.port = i;
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004513 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004514 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004515 }
4516 else if (strcmp(args[3], "addr") == 0) {
4517 char *addr = NULL;
4518 char *port = NULL;
4519 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004520 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004521 goto out;
William Lallemand222baf22016-11-19 02:00:33 +01004522 }
4523 else {
4524 addr = args[4];
4525 }
4526 if (strcmp(args[5], "port") == 0) {
4527 port = args[6];
4528 }
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004529 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004530 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004531 if (warning)
4532 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004533 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004534 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004535 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004536 else if (strcmp(args[3], "fqdn") == 0) {
4537 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004538 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004539 goto out;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004540 }
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004541 if (!sv->resolvers) {
4542 cli_err(appctx, "set server <b>/<s> fqdn failed because no resolution is configured.\n");
4543 goto out;
4544 }
Christopher Fauleted13e2e2021-06-15 11:37:40 +02004545 if (sv->srvrq) {
4546 cli_err(appctx, "set server <b>/<s> fqdn failed because SRV resolution is configured.\n");
4547 goto out;
4548 }
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004549 HA_SPIN_LOCK(DNS_LOCK, &sv->resolvers->lock);
4550 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Baptiste Assmann13a92322019-06-07 09:40:55 +02004551 /* ensure runtime resolver will process this new fqdn */
4552 if (sv->flags & SRV_F_NO_RESOLUTION) {
4553 sv->flags &= ~SRV_F_NO_RESOLUTION;
4554 }
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004555 warning = update_server_fqdn(sv, args[4], "stats socket command", 1);
Christopher Fauletb123e2b2021-06-18 08:47:14 +02004556 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004557 HA_SPIN_UNLOCK(DNS_LOCK, &sv->resolvers->lock);
Willy Tarreau9d008692019-08-09 11:21:01 +02004558 if (warning)
4559 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004560 }
William Lallemand222baf22016-11-19 02:00:33 +01004561 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004562 cli_err(appctx,
4563 "'set server <srv>' only supports 'agent', 'health', 'state',"
4564 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004565 }
Christopher Faulet0139e0a2021-06-15 12:01:29 +02004566 out:
William Lallemand222baf22016-11-19 02:00:33 +01004567 return 1;
4568}
4569
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004570static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004571{
4572 struct stream_interface *si = appctx->owner;
4573 struct proxy *px;
4574 struct server *sv;
4575 char *line;
4576
4577
4578 /* split "backend/server" and make <line> point to server */
4579 for (line = args[2]; *line; line++)
4580 if (*line == '/') {
4581 *line++ = '\0';
4582 break;
4583 }
4584
Willy Tarreau9d008692019-08-09 11:21:01 +02004585 if (!*line)
4586 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004587
Willy Tarreau9d008692019-08-09 11:21:01 +02004588 if (!get_backend_server(args[2], line, &px, &sv))
4589 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004590
4591 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004592 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4593 sv->iweight);
4594 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004595 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004596 return 0;
4597 }
William Lallemand6b160942016-11-22 12:34:35 +01004598 return 1;
4599}
4600
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004601/* Parse a "set weight" command.
4602 *
4603 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004604 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004605static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004606{
4607 struct server *sv;
4608 const char *warning;
4609
4610 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4611 return 1;
4612
4613 sv = cli_find_server(appctx, args[2]);
4614 if (!sv)
4615 return 1;
4616
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004617 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4618
William Lallemand6b160942016-11-22 12:34:35 +01004619 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004620 if (warning)
4621 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004622
4623 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4624
William Lallemand6b160942016-11-22 12:34:35 +01004625 return 1;
4626}
4627
Willy Tarreau46b7f532018-08-21 11:54:26 +02004628/* parse a "set maxconn server" command. It always returns 1.
4629 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004630 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004631 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004632static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004633{
4634 struct server *sv;
4635 const char *warning;
4636
4637 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4638 return 1;
4639
4640 sv = cli_find_server(appctx, args[3]);
4641 if (!sv)
4642 return 1;
4643
Amaury Denoyelle43b0c622021-06-18 11:11:36 +02004644 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4645
Willy Tarreaub8026272016-11-23 11:26:56 +01004646 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004647 if (warning)
4648 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004649
Amaury Denoyelle43b0c622021-06-18 11:11:36 +02004650 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4651
Willy Tarreaub8026272016-11-23 11:26:56 +01004652 return 1;
4653}
William Lallemand6b160942016-11-22 12:34:35 +01004654
Willy Tarreau46b7f532018-08-21 11:54:26 +02004655/* parse a "disable agent" command. It always returns 1.
4656 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004657 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004658 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004659static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004660{
4661 struct server *sv;
4662
4663 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4664 return 1;
4665
4666 sv = cli_find_server(appctx, args[2]);
4667 if (!sv)
4668 return 1;
4669
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004670 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004671 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004672 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004673 return 1;
4674}
4675
Willy Tarreau46b7f532018-08-21 11:54:26 +02004676/* parse a "disable health" command. It always returns 1.
4677 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004678 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004679 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004680static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004681{
4682 struct server *sv;
4683
4684 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4685 return 1;
4686
4687 sv = cli_find_server(appctx, args[2]);
4688 if (!sv)
4689 return 1;
4690
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004691 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004692 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004693 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004694 return 1;
4695}
4696
Willy Tarreau46b7f532018-08-21 11:54:26 +02004697/* parse a "disable server" command. It always returns 1.
4698 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004699 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004700 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004701static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004702{
4703 struct server *sv;
4704
4705 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4706 return 1;
4707
4708 sv = cli_find_server(appctx, args[2]);
4709 if (!sv)
4710 return 1;
4711
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004712 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004713 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004714 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004715 return 1;
4716}
4717
Willy Tarreau46b7f532018-08-21 11:54:26 +02004718/* parse a "enable agent" command. It always returns 1.
4719 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004720 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004721 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004722static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004723{
4724 struct server *sv;
4725
4726 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4727 return 1;
4728
4729 sv = cli_find_server(appctx, args[2]);
4730 if (!sv)
4731 return 1;
4732
Willy Tarreau9d008692019-08-09 11:21:01 +02004733 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4734 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004735
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004736 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004737 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004738 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004739 return 1;
4740}
4741
Willy Tarreau46b7f532018-08-21 11:54:26 +02004742/* parse a "enable health" command. It always returns 1.
4743 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004744 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004745 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004746static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004747{
4748 struct server *sv;
4749
4750 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4751 return 1;
4752
4753 sv = cli_find_server(appctx, args[2]);
4754 if (!sv)
4755 return 1;
4756
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004757 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004758 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004759 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004760 return 1;
4761}
4762
Willy Tarreau46b7f532018-08-21 11:54:26 +02004763/* parse a "enable server" command. It always returns 1.
4764 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004765 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004766 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004767static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004768{
4769 struct server *sv;
4770
4771 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4772 return 1;
4773
4774 sv = cli_find_server(appctx, args[2]);
4775 if (!sv)
4776 return 1;
4777
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004778 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004779 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004780 if (!(sv->flags & SRV_F_COOKIESET)
4781 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4782 sv->cookie)
4783 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004784 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004785 return 1;
4786}
4787
William Lallemand222baf22016-11-19 02:00:33 +01004788/* register cli keywords */
4789static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004790 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004791 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004792 { { "disable", "server", NULL }, "disable server : disable a server for maintenance (use 'set server' instead)", cli_parse_disable_server, NULL },
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004793 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004794 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004795 { { "enable", "server", NULL }, "enable server : enable a disabled server (use 'set server' instead)", cli_parse_enable_server, NULL },
Willy Tarreaub8026272016-11-23 11:26:56 +01004796 { { "set", "maxconn", "server", NULL }, "set maxconn server : change a server's maxconn setting", cli_parse_set_maxconn_server, NULL },
William Lallemand222baf22016-11-19 02:00:33 +01004797 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004798 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4799 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4800
William Lallemand222baf22016-11-19 02:00:33 +01004801 {{},}
4802}};
4803
Willy Tarreau0108d902018-11-25 19:14:37 +01004804INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004805
Emeric Brun64cc49c2017-10-03 14:46:45 +02004806/*
4807 * This function applies server's status changes, it is
4808 * is designed to be called asynchronously.
4809 *
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004810 * Must be called with the server lock held. This may also be called at init
4811 * time as the result of parsing the state file, in which case no lock will be
4812 * held, and the server's warmup task can be null.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004813 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004814static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004815{
4816 struct check *check = &s->check;
4817 int xferred;
4818 struct proxy *px = s->proxy;
4819 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4820 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4821 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004822 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004823
Emeric Brun64cc49c2017-10-03 14:46:45 +02004824 /* If currently main is not set we try to apply pending state changes */
4825 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4826 int next_admin;
4827
4828 /* Backup next admin */
4829 next_admin = s->next_admin;
4830
4831 /* restore current admin state */
4832 s->next_admin = s->cur_admin;
4833
4834 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4835 s->last_change = now.tv_sec;
4836 if (s->proxy->lbprm.set_server_status_down)
4837 s->proxy->lbprm.set_server_status_down(s);
4838
4839 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4840 srv_shutdown_streams(s, SF_ERR_DOWN);
4841
4842 /* we might have streams queued on this server and waiting for
4843 * a connection. Those which are redispatchable will be queued
4844 * to another server or to the proxy itself.
4845 */
4846 xferred = pendconn_redistribute(s);
4847
4848 tmptrash = alloc_trash_chunk();
4849 if (tmptrash) {
4850 chunk_printf(tmptrash,
4851 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4852 s->proxy->id, s->id);
4853
Emeric Brun5a133512017-10-19 14:42:30 +02004854 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004855 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004856
4857 /* we don't send an alert if the server was previously paused */
4858 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004859 send_log(s->proxy, log_level, "%s.\n",
4860 tmptrash->area);
4861 send_email_alert(s, log_level, "%s",
4862 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004863 free_trash_chunk(tmptrash);
4864 tmptrash = NULL;
4865 }
4866 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4867 set_backend_down(s->proxy);
4868
4869 s->counters.down_trans++;
4870 }
4871 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4872 s->last_change = now.tv_sec;
4873 if (s->proxy->lbprm.set_server_status_down)
4874 s->proxy->lbprm.set_server_status_down(s);
4875
4876 /* we might have streams queued on this server and waiting for
4877 * a connection. Those which are redispatchable will be queued
4878 * to another server or to the proxy itself.
4879 */
4880 xferred = pendconn_redistribute(s);
4881
4882 tmptrash = alloc_trash_chunk();
4883 if (tmptrash) {
4884 chunk_printf(tmptrash,
4885 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4886 s->proxy->id, s->id);
4887
Emeric Brun5a133512017-10-19 14:42:30 +02004888 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004889
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004890 ha_warning("%s.\n", tmptrash->area);
4891 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4892 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004893 free_trash_chunk(tmptrash);
4894 tmptrash = NULL;
4895 }
4896
4897 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4898 set_backend_down(s->proxy);
4899 }
4900 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4901 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4902 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4903 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4904 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4905 s->proxy->last_change = now.tv_sec;
4906 }
4907
Amaury Denoyellefe2bf092020-10-29 15:59:04 +01004908 if (s->cur_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
Emeric Brun64cc49c2017-10-03 14:46:45 +02004909 s->down_time += now.tv_sec - s->last_change;
4910
4911 s->last_change = now.tv_sec;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02004912 if (s->next_state == SRV_ST_STARTING && s->warmup)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004913 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4914
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004915 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004916 /* now propagate the status change to any LB algorithms */
4917 if (px->lbprm.update_server_eweight)
4918 px->lbprm.update_server_eweight(s);
4919 else if (srv_willbe_usable(s)) {
4920 if (px->lbprm.set_server_status_up)
4921 px->lbprm.set_server_status_up(s);
4922 }
4923 else {
4924 if (px->lbprm.set_server_status_down)
4925 px->lbprm.set_server_status_down(s);
4926 }
4927
4928 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4929 * and it's not a backup server and its effective weight is > 0,
4930 * then it can accept new connections, so we shut down all streams
4931 * on all backup servers.
4932 */
4933 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4934 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4935 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4936
4937 /* check if we can handle some connections queued at the proxy. We
4938 * will take as many as we can handle.
4939 */
4940 xferred = pendconn_grab_from_px(s);
4941
4942 tmptrash = alloc_trash_chunk();
4943 if (tmptrash) {
4944 chunk_printf(tmptrash,
4945 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4946 s->proxy->id, s->id);
4947
Emeric Brun5a133512017-10-19 14:42:30 +02004948 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004949 ha_warning("%s.\n", tmptrash->area);
4950 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4951 tmptrash->area);
4952 send_email_alert(s, LOG_NOTICE, "%s",
4953 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004954 free_trash_chunk(tmptrash);
4955 tmptrash = NULL;
4956 }
4957
4958 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4959 set_backend_down(s->proxy);
4960 }
4961 else if (s->cur_eweight != s->next_eweight) {
4962 /* now propagate the status change to any LB algorithms */
4963 if (px->lbprm.update_server_eweight)
4964 px->lbprm.update_server_eweight(s);
4965 else if (srv_willbe_usable(s)) {
4966 if (px->lbprm.set_server_status_up)
4967 px->lbprm.set_server_status_up(s);
4968 }
4969 else {
4970 if (px->lbprm.set_server_status_down)
4971 px->lbprm.set_server_status_down(s);
4972 }
4973
4974 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4975 set_backend_down(s->proxy);
4976 }
4977
4978 s->next_admin = next_admin;
4979 }
4980
Emeric Brun5a133512017-10-19 14:42:30 +02004981 /* reset operational state change */
4982 *s->op_st_chg.reason = 0;
4983 s->op_st_chg.status = s->op_st_chg.code = -1;
4984 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004985
4986 /* Now we try to apply pending admin changes */
4987
4988 /* Maintenance must also disable health checks */
4989 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4990 if (s->check.state & CHK_ST_ENABLED) {
4991 s->check.state |= CHK_ST_PAUSED;
4992 check->health = 0;
4993 }
4994
4995 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004996 tmptrash = alloc_trash_chunk();
4997 if (tmptrash) {
4998 chunk_printf(tmptrash,
4999 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
5000 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5001 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02005002
Olivier Houchard796a2b32017-10-24 17:42:47 +02005003 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02005004
Olivier Houchard796a2b32017-10-24 17:42:47 +02005005 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005006 ha_warning("%s.\n", tmptrash->area);
5007 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5008 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02005009 }
5010 free_trash_chunk(tmptrash);
5011 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005012 }
Emeric Brun8f298292017-12-06 16:47:17 +01005013 /* commit new admin status */
5014
5015 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005016 }
5017 else { /* server was still running */
5018 check->health = 0; /* failure */
5019 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01005020
5021 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02005022 if (s->proxy->lbprm.set_server_status_down)
5023 s->proxy->lbprm.set_server_status_down(s);
5024
Emeric Brun64cc49c2017-10-03 14:46:45 +02005025 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
5026 srv_shutdown_streams(s, SF_ERR_DOWN);
5027
William Dauchy6318d332020-05-02 21:52:36 +02005028 /* force connection cleanup on the given server */
5029 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005030 /* we might have streams queued on this server and waiting for
5031 * a connection. Those which are redispatchable will be queued
5032 * to another server or to the proxy itself.
5033 */
5034 xferred = pendconn_redistribute(s);
5035
5036 tmptrash = alloc_trash_chunk();
5037 if (tmptrash) {
5038 chunk_printf(tmptrash,
5039 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5040 s->flags & SRV_F_BACKUP ? "Backup " : "",
5041 s->proxy->id, s->id,
5042 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5043
5044 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5045
5046 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005047 ha_warning("%s.\n", tmptrash->area);
5048 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5049 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005050 }
5051 free_trash_chunk(tmptrash);
5052 tmptrash = NULL;
5053 }
5054 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5055 set_backend_down(s->proxy);
5056
5057 s->counters.down_trans++;
5058 }
5059 }
5060 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5061 /* OK here we're leaving maintenance, we have many things to check,
5062 * because the server might possibly be coming back up depending on
5063 * its state. In practice, leaving maintenance means that we should
5064 * immediately turn to UP (more or less the slowstart) under the
5065 * following conditions :
5066 * - server is neither checked nor tracked
5067 * - server tracks another server which is not checked
5068 * - server tracks another server which is already up
5069 * Which sums up as something simpler :
5070 * "either the tracking server is up or the server's checks are disabled
5071 * or up". Otherwise we only re-enable health checks. There's a special
5072 * case associated to the stopping state which can be inherited. Note
5073 * that the server might still be in drain mode, which is naturally dealt
5074 * with by the lower level functions.
5075 */
5076
5077 if (s->check.state & CHK_ST_ENABLED) {
5078 s->check.state &= ~CHK_ST_PAUSED;
5079 check->health = check->rise; /* start OK but check immediately */
5080 }
5081
5082 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5083 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5084 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5085 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5086 s->next_state = SRV_ST_STOPPING;
5087 }
5088 else {
5089 s->next_state = SRV_ST_STARTING;
Willy Tarreau1e690bb2020-10-22 11:30:59 +02005090 if (s->slowstart > 0) {
5091 if (s->warmup)
5092 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5093 }
Emeric Brun64cc49c2017-10-03 14:46:45 +02005094 else
5095 s->next_state = SRV_ST_RUNNING;
5096 }
5097
5098 }
5099
5100 tmptrash = alloc_trash_chunk();
5101 if (tmptrash) {
5102 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5103 chunk_printf(tmptrash,
5104 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5105 s->flags & SRV_F_BACKUP ? "Backup " : "",
5106 s->proxy->id, s->id,
5107 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5108 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5109 }
5110 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5111 chunk_printf(tmptrash,
5112 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5113 s->flags & SRV_F_BACKUP ? "Backup " : "",
5114 s->proxy->id, s->id, s->hostname,
5115 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5116 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5117 }
5118 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5119 chunk_printf(tmptrash,
5120 "%sServer %s/%s is %s/%s (leaving maintenance)",
5121 s->flags & SRV_F_BACKUP ? "Backup " : "",
5122 s->proxy->id, s->id,
5123 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5124 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5125 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005126 ha_warning("%s.\n", tmptrash->area);
5127 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5128 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005129 free_trash_chunk(tmptrash);
5130 tmptrash = NULL;
5131 }
5132
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005133 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005134 /* now propagate the status change to any LB algorithms */
5135 if (px->lbprm.update_server_eweight)
5136 px->lbprm.update_server_eweight(s);
5137 else if (srv_willbe_usable(s)) {
5138 if (px->lbprm.set_server_status_up)
5139 px->lbprm.set_server_status_up(s);
5140 }
5141 else {
5142 if (px->lbprm.set_server_status_down)
5143 px->lbprm.set_server_status_down(s);
5144 }
5145
5146 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5147 set_backend_down(s->proxy);
5148
Willy Tarreau6a78e612018-08-07 10:14:53 +02005149 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5150 * and it's not a backup server and its effective weight is > 0,
5151 * then it can accept new connections, so we shut down all streams
5152 * on all backup servers.
5153 */
5154 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5155 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5156 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5157
5158 /* check if we can handle some connections queued at the proxy. We
5159 * will take as many as we can handle.
5160 */
5161 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005162 }
5163 else if (s->next_admin & SRV_ADMF_MAINT) {
5164 /* remaining in maintenance mode, let's inform precisely about the
5165 * situation.
5166 */
5167 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5168 tmptrash = alloc_trash_chunk();
5169 if (tmptrash) {
5170 chunk_printf(tmptrash,
5171 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5172 s->flags & SRV_F_BACKUP ? "Backup " : "",
5173 s->proxy->id, s->id);
5174
5175 if (s->track) /* normally it's mandatory here */
5176 chunk_appendf(tmptrash, " via %s/%s",
5177 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005178 ha_warning("%s.\n", tmptrash->area);
5179 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5180 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005181 free_trash_chunk(tmptrash);
5182 tmptrash = NULL;
5183 }
5184 }
5185 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5186 tmptrash = alloc_trash_chunk();
5187 if (tmptrash) {
5188 chunk_printf(tmptrash,
5189 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5190 s->flags & SRV_F_BACKUP ? "Backup " : "",
5191 s->proxy->id, s->id, s->hostname);
5192
5193 if (s->track) /* normally it's mandatory here */
5194 chunk_appendf(tmptrash, " via %s/%s",
5195 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005196 ha_warning("%s.\n", tmptrash->area);
5197 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5198 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005199 free_trash_chunk(tmptrash);
5200 tmptrash = NULL;
5201 }
5202 }
5203 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5204 tmptrash = alloc_trash_chunk();
5205 if (tmptrash) {
5206 chunk_printf(tmptrash,
5207 "%sServer %s/%s remains in forced maintenance",
5208 s->flags & SRV_F_BACKUP ? "Backup " : "",
5209 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005210 ha_warning("%s.\n", tmptrash->area);
5211 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5212 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005213 free_trash_chunk(tmptrash);
5214 tmptrash = NULL;
5215 }
5216 }
5217 /* don't report anything when leaving drain mode and remaining in maintenance */
5218
5219 s->cur_admin = s->next_admin;
5220 }
5221
5222 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5223 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5224 /* drain state is applied only if not yet in maint */
5225
5226 s->last_change = now.tv_sec;
5227 if (px->lbprm.set_server_status_down)
5228 px->lbprm.set_server_status_down(s);
5229
5230 /* we might have streams queued on this server and waiting for
5231 * a connection. Those which are redispatchable will be queued
5232 * to another server or to the proxy itself.
5233 */
5234 xferred = pendconn_redistribute(s);
5235
5236 tmptrash = alloc_trash_chunk();
5237 if (tmptrash) {
5238 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5239 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5240 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5241
5242 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5243
5244 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005245 ha_warning("%s.\n", tmptrash->area);
5246 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5247 tmptrash->area);
5248 send_email_alert(s, LOG_NOTICE, "%s",
5249 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005250 }
5251 free_trash_chunk(tmptrash);
5252 tmptrash = NULL;
5253 }
5254
5255 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5256 set_backend_down(s->proxy);
5257 }
5258 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5259 /* OK completely leaving drain mode */
5260 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5261 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5262 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5263 s->proxy->last_change = now.tv_sec;
5264 }
5265
5266 if (s->last_change < now.tv_sec) // ignore negative times
5267 s->down_time += now.tv_sec - s->last_change;
5268 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005269 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005270
5271 tmptrash = alloc_trash_chunk();
5272 if (tmptrash) {
5273 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5274 chunk_printf(tmptrash,
5275 "%sServer %s/%s is %s (leaving forced drain)",
5276 s->flags & SRV_F_BACKUP ? "Backup " : "",
5277 s->proxy->id, s->id,
5278 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5279 }
5280 else {
5281 chunk_printf(tmptrash,
5282 "%sServer %s/%s is %s (leaving drain)",
5283 s->flags & SRV_F_BACKUP ? "Backup " : "",
5284 s->proxy->id, s->id,
5285 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5286 if (s->track) /* normally it's mandatory here */
5287 chunk_appendf(tmptrash, " via %s/%s",
5288 s->track->proxy->id, s->track->id);
5289 }
5290
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005291 ha_warning("%s.\n", tmptrash->area);
5292 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5293 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005294 free_trash_chunk(tmptrash);
5295 tmptrash = NULL;
5296 }
5297
5298 /* now propagate the status change to any LB algorithms */
5299 if (px->lbprm.update_server_eweight)
5300 px->lbprm.update_server_eweight(s);
5301 else if (srv_willbe_usable(s)) {
5302 if (px->lbprm.set_server_status_up)
5303 px->lbprm.set_server_status_up(s);
5304 }
5305 else {
5306 if (px->lbprm.set_server_status_down)
5307 px->lbprm.set_server_status_down(s);
5308 }
5309 }
5310 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5311 /* remaining in drain mode after removing one of its flags */
5312
5313 tmptrash = alloc_trash_chunk();
5314 if (tmptrash) {
5315 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5316 chunk_printf(tmptrash,
5317 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5318 s->flags & SRV_F_BACKUP ? "Backup " : "",
5319 s->proxy->id, s->id);
5320
5321 if (s->track) /* normally it's mandatory here */
5322 chunk_appendf(tmptrash, " via %s/%s",
5323 s->track->proxy->id, s->track->id);
5324 }
5325 else {
5326 chunk_printf(tmptrash,
5327 "%sServer %s/%s remains in forced drain mode",
5328 s->flags & SRV_F_BACKUP ? "Backup " : "",
5329 s->proxy->id, s->id);
5330 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005331 ha_warning("%s.\n", tmptrash->area);
5332 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5333 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005334 free_trash_chunk(tmptrash);
5335 tmptrash = NULL;
5336 }
5337
5338 /* commit new admin status */
5339
5340 s->cur_admin = s->next_admin;
5341 }
5342 }
5343
5344 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005345 *s->adm_st_chg_cause = 0;
5346}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005347
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005348struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5349{
5350 struct connection *conn;
5351
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005352 while ((conn = MT_LIST_POP(&idle_conns[tid].toremove_conns,
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005353 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005354 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005355 }
5356
5357 return task;
5358}
5359
Olivier Houchardff1d0922020-06-29 20:15:59 +02005360/* Move toremove_nb connections from idle_list to toremove_list, -1 means
5361 * moving them all.
5362 * Returns the number of connections moved.
5363 */
5364static int srv_migrate_conns_to_remove(struct mt_list *idle_list, struct mt_list *toremove_list, int toremove_nb)
5365{
5366 struct mt_list *elt1, elt2;
5367 struct connection *conn;
5368 int i = 0;
5369
5370 mt_list_for_each_entry_safe(conn, idle_list, list, elt1, elt2) {
5371 if (toremove_nb != -1 && i >= toremove_nb)
5372 break;
5373 MT_LIST_DEL_SAFE_NOINIT(elt1);
Willy Tarreaude4db172020-07-10 08:10:29 +02005374 MT_LIST_ADDQ(toremove_list, &conn->list);
Olivier Houchardff1d0922020-06-29 20:15:59 +02005375 i++;
5376 }
5377 return i;
5378}
William Dauchy6318d332020-05-02 21:52:36 +02005379/* cleanup connections for a given server
5380 * might be useful when going on forced maintenance or live changing ip/port
5381 */
William Dauchy707ad322020-05-04 13:52:40 +02005382static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005383{
William Dauchy6318d332020-05-02 21:52:36 +02005384 int did_remove;
5385 int i;
William Dauchy6318d332020-05-02 21:52:36 +02005386
Amaury Denoyellec0a2ace2021-01-06 14:28:51 +01005387 /* nothing to do if pool-max-conn is null */
5388 if (!srv->max_idle_conns)
5389 return;
5390
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005391 /* check all threads starting with ours */
William Dauchy6318d332020-05-02 21:52:36 +02005392 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005393 for (i = tid;;) {
William Dauchy6318d332020-05-02 21:52:36 +02005394 did_remove = 0;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005395 if (srv_migrate_conns_to_remove(&srv->idle_conns[i], &idle_conns[i].toremove_conns, -1) > 0)
William Dauchy6318d332020-05-02 21:52:36 +02005396 did_remove = 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005397 if (srv_migrate_conns_to_remove(&srv->safe_conns[i], &idle_conns[i].toremove_conns, -1) > 0)
5398 did_remove = 1;
William Dauchy6318d332020-05-02 21:52:36 +02005399 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005400 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005401
5402 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5403 break;
William Dauchy6318d332020-05-02 21:52:36 +02005404 }
5405 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5406}
5407
Willy Tarreau980855b2019-02-07 14:59:29 +01005408struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005409{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005410 struct server *srv;
5411 struct eb32_node *eb;
5412 int i;
5413 unsigned int next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005414
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005415 next_wakeup = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005416 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5417 while (1) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005418 int exceed_conns;
5419 int to_kill;
5420 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005421
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005422 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5423 if (!eb) {
5424 /* we might have reached the end of the tree, typically because
5425 * <now_ms> is in the first half and we're first scanning the last
5426 * half. Let's loop back to the beginning of the tree now.
5427 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005428
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005429 eb = eb32_first(&idle_conn_srv);
5430 if (likely(!eb))
5431 break;
5432 }
5433 if (tick_is_lt(now_ms, eb->key)) {
5434 /* timer not expired yet, revisit it later */
5435 next_wakeup = eb->key;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005436 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005437 }
5438 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005439
5440 /* Calculate how many idle connections we want to kill :
5441 * we want to remove half the difference between the total
5442 * of established connections (used or idle) and the max
5443 * number of used connections.
5444 */
5445 curr_idle = srv->curr_idle_conns;
5446 if (curr_idle == 0)
5447 goto remove;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005448 exceed_conns = srv->curr_used_conns + curr_idle - MAX(srv->max_used_conns, srv->est_need_conns);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005449 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005450
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005451 srv->est_need_conns = (srv->est_need_conns + srv->max_used_conns) / 2;
Willy Tarreaubdb86bd2020-06-29 15:56:35 +02005452 if (srv->est_need_conns < srv->max_used_conns)
5453 srv->est_need_conns = srv->max_used_conns;
5454
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005455 srv->max_used_conns = srv->curr_used_conns;
5456
Willy Tarreau18ed7892020-07-02 19:05:30 +02005457 if (exceed_conns <= 0)
5458 goto remove;
5459
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005460 /* check all threads starting with ours */
5461 for (i = tid;;) {
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005462 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005463 int j;
5464 int did_remove = 0;
5465
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005466 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5467 curr_idle + 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005468
5469 j = srv_migrate_conns_to_remove(&srv->idle_conns[i], &idle_conns[i].toremove_conns, max_conn);
5470 if (j > 0)
5471 did_remove = 1;
5472 if (max_conn - j > 0 &&
5473 srv_migrate_conns_to_remove(&srv->safe_conns[i], &idle_conns[i].toremove_conns, max_conn - j) > 0)
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005474 did_remove = 1;
Olivier Houchardff1d0922020-06-29 20:15:59 +02005475
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005476 if (did_remove)
Willy Tarreau4d82bf52020-06-28 00:19:17 +02005477 task_wakeup(idle_conns[i].cleanup_task, TASK_WOKEN_OTHER);
Willy Tarreauc35bcfc2020-06-29 14:43:16 +02005478
5479 if ((i = ((i + 1 == global.nbthread) ? 0 : i + 1)) == tid)
5480 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005481 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005482remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005483 eb32_delete(&srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005484
5485 if (srv->curr_idle_conns) {
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005486 /* There are still more idle connections, add the
5487 * server back in the tree.
5488 */
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005489 srv->idle_node.key = tick_add(srv->pool_purge_delay, now_ms);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005490 eb32_insert(&idle_conn_srv, &srv->idle_node);
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005491 next_wakeup = tick_first(next_wakeup, srv->idle_node.key);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005492 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005493 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005494 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5495
Willy Tarreau21b9ff52020-11-05 09:12:20 +01005496 task->expire = next_wakeup;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005497 return task;
5498}
Olivier Houchard88698d92019-04-16 19:07:22 +02005499
Willy Tarreau76cc6992020-07-01 18:49:24 +02005500/* config parser for global "tune.idle-pool.shared", accepts "on" or "off" */
5501static int cfg_parse_idle_pool_shared(char **args, int section_type, struct proxy *curpx,
5502 struct proxy *defpx, const char *file, int line,
5503 char **err)
5504{
5505 if (too_many_args(1, args, err, NULL))
5506 return -1;
5507
5508 if (strcmp(args[1], "on") == 0)
5509 global.tune.options |= GTUNE_IDLE_POOL_SHARED;
5510 else if (strcmp(args[1], "off") == 0)
5511 global.tune.options &= ~GTUNE_IDLE_POOL_SHARED;
5512 else {
5513 memprintf(err, "'%s' expects either 'on' or 'off' but got '%s'.", args[0], args[1]);
5514 return -1;
5515 }
5516 return 0;
5517}
5518
Olivier Houchard88698d92019-04-16 19:07:22 +02005519/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5520static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5521 struct proxy *defpx, const char *file, int line,
5522 char **err)
5523{
5524 int arg = -1;
5525
5526 if (too_many_args(1, args, err, NULL))
5527 return -1;
5528
5529 if (*(args[1]) != 0)
5530 arg = atoi(args[1]);
5531
5532 if (arg < 0 || arg > 100) {
5533 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5534 return -1;
5535 }
5536
5537 if (args[0][10] == 'h')
5538 global.tune.pool_high_ratio = arg;
5539 else
5540 global.tune.pool_low_ratio = arg;
5541 return 0;
5542}
5543
5544/* config keyword parsers */
5545static struct cfg_kw_list cfg_kws = {ILH, {
Willy Tarreau76cc6992020-07-01 18:49:24 +02005546 { CFG_GLOBAL, "tune.idle-pool.shared", cfg_parse_idle_pool_shared },
Olivier Houchard88698d92019-04-16 19:07:22 +02005547 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5548 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5549 { 0, NULL, NULL }
5550}};
5551
5552INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5553
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005554/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005555 * Local variables:
5556 * c-indent-level: 8
5557 * c-basic-offset: 8
5558 * End:
5559 */