blob: 265106eb8715f5c8ff4f5861847342626abe7ec5 [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 Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010023#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020024#include <common/time.h>
25
William Lallemand222baf22016-11-19 02:00:33 +010026#include <types/applet.h>
27#include <types/cli.h>
Frédéric Lécaille7da71292019-05-20 09:47:07 +020028#include <types/dict.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020029#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010030#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020031#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010032#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020033
William Lallemand222baf22016-11-19 02:00:33 +010034#include <proto/applet.h>
35#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090036#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020037#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020038#include <proto/port_range.h>
39#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020040#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010041#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020042#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020043#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010044#include <proto/stream_interface.h>
45#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020047#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010048#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020049
Baptiste Assmannda29fe22019-06-13 13:24:29 +020050#include <ebsttree.h>
51
Willy Tarreau3ff577e2018-08-02 11:48:52 +020052static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020053static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010054static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010055static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Baptiste Assmannda29fe22019-06-13 13:24:29 +020056static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params);
57static int srv_state_get_version(FILE *f);
William Dauchy6318d332020-05-02 21:52:36 +020058static void srv_cleanup_connections(struct server *srv);
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau21faa912012-10-10 08:27:36 +020060/* List head of all known server keywords */
61static struct srv_kw_list srv_keywords = {
62 .list = LIST_HEAD_INIT(srv_keywords.list)
63};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020064
Olivier Houchard9ea5d362019-02-14 18:29:09 +010065__decl_hathreads(HA_SPINLOCK_T idle_conn_srv_lock);
66struct eb_root idle_conn_srv = EB_ROOT;
67struct task *idle_conn_task = NULL;
68struct task *idle_conn_cleanup[MAX_THREADS] = { NULL };
Olivier Houchard859dc802019-08-08 15:47:21 +020069struct mt_list toremove_connections[MAX_THREADS];
Olivier Houchard4be71902019-07-11 15:49:00 +020070__decl_hathreads(HA_SPINLOCK_T toremove_lock[MAX_THREADS]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +010071
Frédéric Lécaille7da71292019-05-20 09:47:07 +020072/* The server names dictionary */
73struct dict server_name_dict = {
74 .name = "server names",
75 .values = EB_ROOT_UNIQUE,
76};
77
Baptiste Assmannda29fe22019-06-13 13:24:29 +020078/* tree where global state_file is loaded */
Willy Tarreaufd1aa012019-12-20 17:23:40 +010079struct eb_root state_file = EB_ROOT_UNIQUE;
Baptiste Assmannda29fe22019-06-13 13:24:29 +020080
Simon Hormana3608442013-11-01 16:46:15 +090081int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020082{
Emeric Brun52a91d32017-08-31 14:41:55 +020083 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020084 return s->down_time;
85
86 return now.tv_sec - s->last_change + s->down_time;
87}
Willy Tarreaubaaee002006-06-26 02:48:02 +020088
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050089int srv_lastsession(const struct server *s)
90{
91 if (s->counters.last_sess)
92 return now.tv_sec - s->counters.last_sess;
93
94 return -1;
95}
96
Simon Horman4a741432013-02-23 15:35:38 +090097int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020098{
Simon Horman4a741432013-02-23 15:35:38 +090099 const struct server *s = check->server;
100
Willy Tarreauff5ae352013-12-11 20:36:34 +0100101 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +0900102 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100103
Emeric Brun52a91d32017-08-31 14:41:55 +0200104 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +0900105 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100106
Simon Horman4a741432013-02-23 15:35:38 +0900107 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100108}
109
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100110/*
111 * Check that we did not get a hash collision.
112 * Unlikely, but it can happen.
113 */
114static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +0100115{
116 struct proxy *p = s->proxy;
117 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100118
119 for (tmpserv = p->srv; tmpserv != NULL;
120 tmpserv = tmpserv->next) {
121 if (tmpserv == s)
122 continue;
123 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
124 continue;
125 if (tmpserv->cookie &&
126 strcmp(tmpserv->cookie, s->cookie) == 0) {
127 ha_warning("We generated two equal cookies for two different servers.\n"
128 "Please change the secret key for '%s'.\n",
129 s->proxy->id);
130 }
131 }
132
133}
134
Willy Tarreau46b7f532018-08-21 11:54:26 +0200135/*
Willy Tarreau5e83d992019-07-30 11:59:34 +0200136 * Must be called with the server lock held, and will grab the proxy lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200137 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100138void srv_set_dyncookie(struct server *s)
139{
140 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100141 char *tmpbuf;
142 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100143 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100144 size_t buffer_len;
145 int addr_len;
146 int port;
147
Willy Tarreau5e83d992019-07-30 11:59:34 +0200148 HA_SPIN_LOCK(PROXY_LOCK, &p->lock);
149
Olivier Houchard4e694042017-03-14 20:01:29 +0100150 if ((s->flags & SRV_F_COOKIESET) ||
151 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
152 s->proxy->dyncookie_key == NULL)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200153 goto out;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100154 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100155
156 if (s->addr.ss_family != AF_INET &&
157 s->addr.ss_family != AF_INET6)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200158 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100159 /*
160 * Buffer to calculate the cookie value.
161 * The buffer contains the secret key + the server IP address
162 * + the TCP port.
163 */
164 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
165 /*
166 * The TCP port should use only 2 bytes, but is stored in
167 * an unsigned int in struct server, so let's use 4, to be
168 * on the safe side.
169 */
170 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200171 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100172 memcpy(tmpbuf, p->dyncookie_key, key_len);
173 memcpy(&(tmpbuf[key_len]),
174 s->addr.ss_family == AF_INET ?
175 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
176 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
177 addr_len);
178 /*
179 * Make sure it's the same across all the load balancers,
180 * no matter their endianness.
181 */
182 port = htonl(s->svc_port);
183 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
184 hash_value = XXH64(tmpbuf, buffer_len, 0);
185 memprintf(&s->cookie, "%016llx", hash_value);
186 if (!s->cookie)
Willy Tarreau5e83d992019-07-30 11:59:34 +0200187 goto out;
Olivier Houchard4e694042017-03-14 20:01:29 +0100188 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100189
190 /* Don't bother checking if the dyncookie is duplicated if
191 * the server is marked as "disabled", maybe it doesn't have
192 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100193 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100194 if (!(s->next_admin & SRV_ADMF_FMAINT))
195 srv_check_for_dup_dyncookie(s);
Willy Tarreau5e83d992019-07-30 11:59:34 +0200196 out:
197 HA_SPIN_UNLOCK(PROXY_LOCK, &p->lock);
Olivier Houchard4e694042017-03-14 20:01:29 +0100198}
199
Willy Tarreau21faa912012-10-10 08:27:36 +0200200/*
201 * Registers the server keyword list <kwl> as a list of valid keywords for next
202 * parsing sessions.
203 */
204void srv_register_keywords(struct srv_kw_list *kwl)
205{
206 LIST_ADDQ(&srv_keywords.list, &kwl->list);
207}
208
209/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
210 * keyword is found with a NULL ->parse() function, then an attempt is made to
211 * find one with a valid ->parse() function. This way it is possible to declare
212 * platform-dependant, known keywords as NULL, then only declare them as valid
213 * if some options are met. Note that if the requested keyword contains an
214 * opening parenthesis, everything from this point is ignored.
215 */
216struct srv_kw *srv_find_kw(const char *kw)
217{
218 int index;
219 const char *kwend;
220 struct srv_kw_list *kwl;
221 struct srv_kw *ret = NULL;
222
223 kwend = strchr(kw, '(');
224 if (!kwend)
225 kwend = kw + strlen(kw);
226
227 list_for_each_entry(kwl, &srv_keywords.list, list) {
228 for (index = 0; kwl->kw[index].kw != NULL; index++) {
229 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
230 kwl->kw[index].kw[kwend-kw] == 0) {
231 if (kwl->kw[index].parse)
232 return &kwl->kw[index]; /* found it !*/
233 else
234 ret = &kwl->kw[index]; /* may be OK */
235 }
236 }
237 }
238 return ret;
239}
240
241/* Dumps all registered "server" keywords to the <out> string pointer. The
242 * unsupported keywords are only dumped if their supported form was not
243 * found.
244 */
245void srv_dump_kws(char **out)
246{
247 struct srv_kw_list *kwl;
248 int index;
249
250 *out = NULL;
251 list_for_each_entry(kwl, &srv_keywords.list, list) {
252 for (index = 0; kwl->kw[index].kw != NULL; index++) {
253 if (kwl->kw[index].parse ||
254 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
255 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
256 kwl->scope,
257 kwl->kw[index].kw,
258 kwl->kw[index].skip ? " <arg>" : "",
259 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
260 kwl->kw[index].parse ? "" : " (not supported)");
261 }
262 }
263 }
264}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100265
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100266/* Parse the "backup" server keyword */
267static int srv_parse_backup(char **args, int *cur_arg,
268 struct proxy *curproxy, struct server *newsrv, char **err)
269{
270 newsrv->flags |= SRV_F_BACKUP;
271 return 0;
272}
273
Alexander Liu2a54bb72019-05-22 19:44:48 +0800274
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100275/* Parse the "cookie" server keyword */
276static int srv_parse_cookie(char **args, int *cur_arg,
277 struct proxy *curproxy, struct server *newsrv, char **err)
278{
279 char *arg;
280
281 arg = args[*cur_arg + 1];
282 if (!*arg) {
283 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
284 return ERR_ALERT | ERR_FATAL;
285 }
286
287 free(newsrv->cookie);
288 newsrv->cookie = strdup(arg);
289 newsrv->cklen = strlen(arg);
290 newsrv->flags |= SRV_F_COOKIESET;
291 return 0;
292}
293
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100294/* Parse the "disabled" server keyword */
295static int srv_parse_disabled(char **args, int *cur_arg,
296 struct proxy *curproxy, struct server *newsrv, char **err)
297{
Emeric Brun52a91d32017-08-31 14:41:55 +0200298 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
299 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100300 newsrv->check.state |= CHK_ST_PAUSED;
301 newsrv->check.health = 0;
302 return 0;
303}
304
305/* Parse the "enabled" server keyword */
306static int srv_parse_enabled(char **args, int *cur_arg,
307 struct proxy *curproxy, struct server *newsrv, char **err)
308{
Emeric Brun52a91d32017-08-31 14:41:55 +0200309 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
310 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100311 newsrv->check.state &= ~CHK_ST_PAUSED;
312 newsrv->check.health = newsrv->check.rise;
313 return 0;
314}
315
Willy Tarreau9c538e02019-01-23 10:21:49 +0100316static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
317{
318 char *arg;
319
320 arg = args[*cur_arg + 1];
321 if (!*arg) {
322 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
323 return ERR_ALERT | ERR_FATAL;
324 }
325 newsrv->max_reuse = atoi(arg);
326
327 return 0;
328}
329
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100330static 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 +0100331{
332 const char *res;
333 char *arg;
334 unsigned int time;
335
336 arg = args[*cur_arg + 1];
337 if (!*arg) {
338 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
339 return ERR_ALERT | ERR_FATAL;
340 }
341 res = parse_time_err(arg, &time, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +0200342 if (res == PARSE_TIME_OVER) {
343 memprintf(err, "timer overflow in argument '%s' to '%s' (maximum value is 2147483647 ms or ~24.8 days)",
344 args[*cur_arg+1], args[*cur_arg]);
345 return ERR_ALERT | ERR_FATAL;
346 }
347 else if (res == PARSE_TIME_UNDER) {
348 memprintf(err, "timer underflow in argument '%s' to '%s' (minimum non-null value is 1 ms)",
349 args[*cur_arg+1], args[*cur_arg]);
350 return ERR_ALERT | ERR_FATAL;
351 }
352 else if (res) {
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100353 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
354 *res, args[*cur_arg]);
355 return ERR_ALERT | ERR_FATAL;
356 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100357 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100358
359 return 0;
360}
361
Olivier Houchard006e3102018-12-10 18:30:32 +0100362static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
363{
364 char *arg;
365
366 arg = args[*cur_arg + 1];
367 if (!*arg) {
368 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
369 return ERR_ALERT | ERR_FATAL;
370 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100371
Olivier Houchard006e3102018-12-10 18:30:32 +0100372 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100373 if ((int)newsrv->max_idle_conns < -1) {
374 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
375 return ERR_ALERT | ERR_FATAL;
376 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100377
378 return 0;
379}
380
Willy Tarreaudff55432012-10-10 17:51:05 +0200381/* parse the "id" server keyword */
382static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
383{
384 struct eb32_node *node;
385
386 if (!*args[*cur_arg + 1]) {
387 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
388 return ERR_ALERT | ERR_FATAL;
389 }
390
391 newsrv->puid = atol(args[*cur_arg + 1]);
392 newsrv->conf.id.key = newsrv->puid;
393
394 if (newsrv->puid <= 0) {
395 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
396 return ERR_ALERT | ERR_FATAL;
397 }
398
399 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
400 if (node) {
401 struct server *target = container_of(node, struct server, conf.id);
402 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
403 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
404 target->id);
405 return ERR_ALERT | ERR_FATAL;
406 }
407
408 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200409 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200410 return 0;
411}
412
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100413/* Parse the "namespace" server keyword */
414static int srv_parse_namespace(char **args, int *cur_arg,
415 struct proxy *curproxy, struct server *newsrv, char **err)
416{
Willy Tarreaue5733232019-05-22 19:24:06 +0200417#ifdef USE_NS
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100418 char *arg;
419
420 arg = args[*cur_arg + 1];
421 if (!*arg) {
422 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
423 return ERR_ALERT | ERR_FATAL;
424 }
425
426 if (!strcmp(arg, "*")) {
427 /* Use the namespace associated with the connection (if present). */
428 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
429 return 0;
430 }
431
432 /*
433 * As this parser may be called several times for the same 'default-server'
434 * object, or for a new 'server' instance deriving from a 'default-server'
435 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
436 */
437 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
438
439 newsrv->netns = netns_store_lookup(arg, strlen(arg));
440 if (!newsrv->netns)
441 newsrv->netns = netns_store_insert(arg);
442
443 if (!newsrv->netns) {
444 memprintf(err, "Cannot open namespace '%s'", arg);
445 return ERR_ALERT | ERR_FATAL;
446 }
447
448 return 0;
449#else
450 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
451 return ERR_ALERT | ERR_FATAL;
452#endif
453}
454
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100455/* Parse the "no-backup" server keyword */
456static int srv_parse_no_backup(char **args, int *cur_arg,
457 struct proxy *curproxy, struct server *newsrv, char **err)
458{
459 newsrv->flags &= ~SRV_F_BACKUP;
460 return 0;
461}
462
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100463
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100464/* Disable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200465static inline int srv_disable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100466{
467 srv->pp_opts &= ~flags;
468 return 0;
469}
470
471/* Parse the "no-send-proxy" server keyword */
472static int srv_parse_no_send_proxy(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
476}
477
478/* Parse the "no-send-proxy-v2" server keyword */
479static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
482 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
483}
484
Frédéric Lécaille1b9423d2019-07-04 14:19:06 +0200485/* Parse the "no-tfo" server keyword */
486static int srv_parse_no_tfo(char **args, int *cur_arg,
487 struct proxy *curproxy, struct server *newsrv, char **err)
488{
489 newsrv->flags &= ~SRV_F_FASTOPEN;
490 return 0;
491}
492
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100493/* Parse the "non-stick" server keyword */
494static int srv_parse_non_stick(char **args, int *cur_arg,
495 struct proxy *curproxy, struct server *newsrv, char **err)
496{
497 newsrv->flags |= SRV_F_NON_STICK;
498 return 0;
499}
500
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100501/* Enable server PROXY protocol flags. */
Willy Tarreau0e492e22019-04-15 21:25:03 +0200502static inline int srv_enable_pp_flags(struct server *srv, unsigned int flags)
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100503{
504 srv->pp_opts |= flags;
505 return 0;
506}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200507/* parse the "proto" server keyword */
508static int srv_parse_proto(char **args, int *cur_arg,
509 struct proxy *px, struct server *newsrv, char **err)
510{
511 struct ist proto;
512
513 if (!*args[*cur_arg + 1]) {
514 memprintf(err, "'%s' : missing value", args[*cur_arg]);
515 return ERR_ALERT | ERR_FATAL;
516 }
517 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
518 newsrv->mux_proto = get_mux_proto(proto);
519 if (!newsrv->mux_proto) {
520 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
521 return ERR_ALERT | ERR_FATAL;
522 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200523 return 0;
524}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100525
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100526/* parse the "proxy-v2-options" */
527static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
528 struct proxy *px, struct server *newsrv, char **err)
529{
530 char *p, *n;
531 for (p = args[*cur_arg+1]; p; p = n) {
532 n = strchr(p, ',');
533 if (n)
534 *n++ = '\0';
535 if (!strcmp(p, "ssl")) {
536 newsrv->pp_opts |= SRV_PP_V2_SSL;
537 } else if (!strcmp(p, "cert-cn")) {
538 newsrv->pp_opts |= SRV_PP_V2_SSL;
539 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100540 } else if (!strcmp(p, "cert-key")) {
541 newsrv->pp_opts |= SRV_PP_V2_SSL;
542 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
543 } else if (!strcmp(p, "cert-sig")) {
544 newsrv->pp_opts |= SRV_PP_V2_SSL;
545 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
546 } else if (!strcmp(p, "ssl-cipher")) {
547 newsrv->pp_opts |= SRV_PP_V2_SSL;
548 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100549 } else if (!strcmp(p, "authority")) {
550 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100551 } else if (!strcmp(p, "crc32c")) {
552 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Tim Duesterhuscf6e0c82020-03-13 12:34:24 +0100553 } else if (!strcmp(p, "unique-id")) {
554 newsrv->pp_opts |= SRV_PP_V2_UNIQUE_ID;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100555 } else
556 goto fail;
557 }
558 return 0;
559 fail:
560 if (err)
561 memprintf(err, "'%s' : proxy v2 option not implemented", p);
562 return ERR_ALERT | ERR_FATAL;
563}
564
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100565/* Parse the "observe" server keyword */
566static int srv_parse_observe(char **args, int *cur_arg,
567 struct proxy *curproxy, struct server *newsrv, char **err)
568{
569 char *arg;
570
571 arg = args[*cur_arg + 1];
572 if (!*arg) {
573 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
574 return ERR_ALERT | ERR_FATAL;
575 }
576
577 if (!strcmp(arg, "none")) {
578 newsrv->observe = HANA_OBS_NONE;
579 }
580 else if (!strcmp(arg, "layer4")) {
581 newsrv->observe = HANA_OBS_LAYER4;
582 }
583 else if (!strcmp(arg, "layer7")) {
584 if (curproxy->mode != PR_MODE_HTTP) {
585 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
586 return ERR_ALERT;
587 }
588 newsrv->observe = HANA_OBS_LAYER7;
589 }
590 else {
591 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
592 "but got '%s'\n", args[*cur_arg], arg);
593 return ERR_ALERT | ERR_FATAL;
594 }
595
596 return 0;
597}
598
Frédéric Lécaille16186232017-03-14 16:42:49 +0100599/* Parse the "redir" server keyword */
600static int srv_parse_redir(char **args, int *cur_arg,
601 struct proxy *curproxy, struct server *newsrv, char **err)
602{
603 char *arg;
604
605 arg = args[*cur_arg + 1];
606 if (!*arg) {
607 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
608 return ERR_ALERT | ERR_FATAL;
609 }
610
611 free(newsrv->rdr_pfx);
612 newsrv->rdr_pfx = strdup(arg);
613 newsrv->rdr_len = strlen(arg);
614
615 return 0;
616}
617
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100618/* Parse the "send-proxy" server keyword */
619static int srv_parse_send_proxy(char **args, int *cur_arg,
620 struct proxy *curproxy, struct server *newsrv, char **err)
621{
622 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
623}
624
625/* Parse the "send-proxy-v2" server keyword */
626static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
627 struct proxy *curproxy, struct server *newsrv, char **err)
628{
629 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
630}
631
Frédéric Lécailledba97072017-03-17 15:33:50 +0100632
633/* Parse the "source" server keyword */
634static int srv_parse_source(char **args, int *cur_arg,
635 struct proxy *curproxy, struct server *newsrv, char **err)
636{
637 char *errmsg;
638 int port_low, port_high;
639 struct sockaddr_storage *sk;
640 struct protocol *proto;
641
642 errmsg = NULL;
643
644 if (!*args[*cur_arg + 1]) {
645 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
646 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
647 goto err;
648 }
649
650 /* 'sk' is statically allocated (no need to be freed). */
651 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
652 if (!sk) {
653 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
654 goto err;
655 }
656
657 proto = protocol_by_family(sk->ss_family);
658 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100659 ha_alert("'%s %s' : connect() not supported for this address family.\n",
660 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100661 goto err;
662 }
663
664 newsrv->conn_src.opts |= CO_SRC_BIND;
665 newsrv->conn_src.source_addr = *sk;
666
667 if (port_low != port_high) {
668 int i;
669
670 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100671 ha_alert("'%s' does not support port offsets (found '%s').\n",
672 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100673 goto err;
674 }
675
676 if (port_low <= 0 || port_low > 65535 ||
677 port_high <= 0 || port_high > 65535 ||
678 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100679 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100680 goto err;
681 }
682 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
683 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
684 newsrv->conn_src.sport_range->ports[i] = port_low + i;
685 }
686
687 *cur_arg += 2;
688 while (*(args[*cur_arg])) {
689 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
690#if defined(CONFIG_HAP_TRANSPARENT)
691 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100692 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
693 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100694 goto err;
695 }
696 if (!strcmp(args[*cur_arg + 1], "client")) {
697 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
698 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
699 }
700 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
701 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
702 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
703 }
704 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
705 char *name, *end;
706
707 name = args[*cur_arg + 1] + 7;
Willy Tarreau90807112020-02-25 08:16:33 +0100708 while (isspace((unsigned char)*name))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100709 name++;
710
711 end = name;
Willy Tarreau90807112020-02-25 08:16:33 +0100712 while (*end && !isspace((unsigned char)*end) && *end != ',' && *end != ')')
Frédéric Lécailledba97072017-03-17 15:33:50 +0100713 end++;
714
715 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
716 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
717 free(newsrv->conn_src.bind_hdr_name);
718 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
719 newsrv->conn_src.bind_hdr_len = end - name;
720 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
721 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
722 newsrv->conn_src.bind_hdr_occ = -1;
723
724 /* now look for an occurrence number */
Willy Tarreau90807112020-02-25 08:16:33 +0100725 while (isspace((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100726 end++;
727 if (*end == ',') {
728 end++;
729 name = end;
730 if (*end == '-')
731 end++;
Willy Tarreau90807112020-02-25 08:16:33 +0100732 while (isdigit((unsigned char)*end))
Frédéric Lécailledba97072017-03-17 15:33:50 +0100733 end++;
734 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
735 }
736
737 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100738 ha_alert("usesrc hdr_ip(name,num) does not support negative"
739 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100740 goto err;
741 }
742 }
743 else {
744 struct sockaddr_storage *sk;
745 int port1, port2;
746
747 /* 'sk' is statically allocated (no need to be freed). */
748 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
749 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100750 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100751 goto err;
752 }
753
754 proto = protocol_by_family(sk->ss_family);
755 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100756 ha_alert("'%s %s' : connect() not supported for this address family.\n",
757 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100758 goto err;
759 }
760
761 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100762 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
763 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100764 goto err;
765 }
766 newsrv->conn_src.tproxy_addr = *sk;
767 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
768 }
769 global.last_checks |= LSTCHK_NETADM;
770 *cur_arg += 2;
771 continue;
772#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100773 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 +0100774 goto err;
775#endif /* defined(CONFIG_HAP_TRANSPARENT) */
776 } /* "usesrc" */
777
778 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
779#ifdef SO_BINDTODEVICE
780 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100781 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100782 goto err;
783 }
784 free(newsrv->conn_src.iface_name);
785 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
786 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
787 global.last_checks |= LSTCHK_NETADM;
788#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100789 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100790 goto err;
791#endif
792 *cur_arg += 2;
793 continue;
794 }
795 /* this keyword in not an option of "source" */
796 break;
797 } /* while */
798
799 return 0;
800
801 err:
802 free(errmsg);
803 return ERR_ALERT | ERR_FATAL;
804}
805
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100806/* Parse the "stick" server keyword */
807static int srv_parse_stick(char **args, int *cur_arg,
808 struct proxy *curproxy, struct server *newsrv, char **err)
809{
810 newsrv->flags &= ~SRV_F_NON_STICK;
811 return 0;
812}
813
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100814/* Parse the "track" server keyword */
815static int srv_parse_track(char **args, int *cur_arg,
816 struct proxy *curproxy, struct server *newsrv, char **err)
817{
818 char *arg;
819
820 arg = args[*cur_arg + 1];
821 if (!*arg) {
822 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
823 return ERR_ALERT | ERR_FATAL;
824 }
825
826 free(newsrv->trackit);
827 newsrv->trackit = strdup(arg);
828
829 return 0;
Alexander Liu2a54bb72019-05-22 19:44:48 +0800830}
831
832/* Parse the "socks4" server keyword */
833static int srv_parse_socks4(char **args, int *cur_arg,
834 struct proxy *curproxy, struct server *newsrv, char **err)
835{
836 char *errmsg;
837 int port_low, port_high;
838 struct sockaddr_storage *sk;
839 struct protocol *proto;
840
841 errmsg = NULL;
842
843 if (!*args[*cur_arg + 1]) {
844 memprintf(err, "'%s' expects <addr>:<port> as argument.\n", args[*cur_arg]);
845 goto err;
846 }
847
848 /* 'sk' is statically allocated (no need to be freed). */
849 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
850 if (!sk) {
851 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
852 goto err;
853 }
854
855 proto = protocol_by_family(sk->ss_family);
856 if (!proto || !proto->connect) {
857 ha_alert("'%s %s' : connect() not supported for this address family.\n", args[*cur_arg], args[*cur_arg + 1]);
858 goto err;
859 }
860
861 newsrv->flags |= SRV_F_SOCKS4_PROXY;
862 newsrv->socks4_addr = *sk;
863
864 if (port_low != port_high) {
865 ha_alert("'%s' does not support port offsets (found '%s').\n", args[*cur_arg], args[*cur_arg + 1]);
866 goto err;
867 }
868
869 if (!port_low) {
870 ha_alert("'%s': invalid port range %d-%d.\n", args[*cur_arg], port_low, port_high);
871 goto err;
872 }
873
874 return 0;
875
876 err:
877 free(errmsg);
878 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100879}
880
Frédéric Lécailledba97072017-03-17 15:33:50 +0100881
Willy Tarreau034c88c2017-01-23 23:36:45 +0100882/* parse the "tfo" server keyword */
883static int srv_parse_tfo(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err)
884{
885 newsrv->flags |= SRV_F_FASTOPEN;
886 return 0;
887}
888
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200889/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200890 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200891 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200892 *
893 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200894 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200895void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200896{
Willy Tarreau87b09662015-04-03 00:22:06 +0200897 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200898
Willy Tarreau87b09662015-04-03 00:22:06 +0200899 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
900 if (stream->srv_conn == srv)
901 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200902}
903
904/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200905 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200906 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200907 *
908 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200909 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200910void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200911{
912 struct server *srv;
913
914 for (srv = px->srv; srv != NULL; srv = srv->next)
915 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200916 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200917}
918
Willy Tarreaubda92272014-05-20 21:55:30 +0200919/* Appends some information to a message string related to a server going UP or
920 * DOWN. If both <forced> and <reason> are null and the server tracks another
921 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200922 * If <check> is non-null, an entire string describing the check result will be
923 * appended after a comma and a space (eg: to report some information from the
924 * check that changed the state). In the other case, the string will be built
925 * using the check results stored into the struct server if present.
926 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200927 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200928 *
929 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200930 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200931void srv_append_status(struct buffer *msg, struct server *s,
932 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200933{
Emeric Brun5a133512017-10-19 14:42:30 +0200934 short status = s->op_st_chg.status;
935 short code = s->op_st_chg.code;
936 long duration = s->op_st_chg.duration;
937 char *desc = s->op_st_chg.reason;
938
939 if (check) {
940 status = check->status;
941 code = check->code;
942 duration = check->duration;
943 desc = check->desc;
944 }
945
946 if (status != -1) {
947 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
948
949 if (status >= HCHK_STATUS_L57DATA)
950 chunk_appendf(msg, ", code: %d", code);
951
952 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200953 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200954
955 chunk_appendf(msg, ", info: \"");
956
957 chunk_initlen(&src, desc, 0, strlen(desc));
958 chunk_asciiencode(msg, &src, '"');
959
960 chunk_appendf(msg, "\"");
961 }
962
963 if (duration >= 0)
964 chunk_appendf(msg, ", check duration: %ldms", duration);
965 }
966 else if (desc && *desc) {
967 chunk_appendf(msg, ", %s", desc);
968 }
969 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200970 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200971 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200972
973 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200974 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200975 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
976 " %d sessions active, %d requeued, %d remaining in queue",
977 s->proxy->srv_act, s->proxy->srv_bck,
978 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
979 s->cur_sess, xferred, s->nbpend);
980 else
981 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
982 " %d sessions requeued, %d total in queue",
983 s->proxy->srv_act, s->proxy->srv_bck,
984 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
985 xferred, s->nbpend);
986 }
987}
988
Emeric Brun5a133512017-10-19 14:42:30 +0200989/* Marks server <s> down, regardless of its checks' statuses. The server is
990 * registered in a list to postpone the counting of the remaining servers on
991 * the proxy and transfers queued streams whenever possible to other servers at
992 * a sync point. Maintenance servers are ignored. It stores the <reason> if
993 * non-null as the reason for going down or the available data from the check
994 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200995 *
996 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200997 */
Emeric Brun5a133512017-10-19 14:42:30 +0200998void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200999{
1000 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001001
Emeric Brun64cc49c2017-10-03 14:46:45 +02001002 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001003 return;
1004
Emeric Brun52a91d32017-08-31 14:41:55 +02001005 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001006 *s->op_st_chg.reason = 0;
1007 s->op_st_chg.status = -1;
1008 if (reason) {
1009 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1010 }
1011 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001012 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001013 s->op_st_chg.code = check->code;
1014 s->op_st_chg.status = check->status;
1015 s->op_st_chg.duration = check->duration;
1016 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001017
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001018 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001019 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001020
Emeric Brun9f0b4582017-10-23 14:39:51 +02001021 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001022 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001023 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001024 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001025 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001026}
1027
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001028/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001029 * in maintenance. The server is registered in a list to postpone the counting
1030 * of the remaining servers on the proxy and tries to grab requests from the
1031 * proxy at a sync point. Maintenance servers are ignored. It stores the
1032 * <reason> if non-null as the reason for going down or the available data
1033 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001034 *
1035 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001036 */
Emeric Brun5a133512017-10-19 14:42:30 +02001037void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001038{
1039 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001040
Emeric Brun64cc49c2017-10-03 14:46:45 +02001041 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001042 return;
1043
Emeric Brun52a91d32017-08-31 14:41:55 +02001044 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001045 return;
1046
Emeric Brun52a91d32017-08-31 14:41:55 +02001047 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001048 *s->op_st_chg.reason = 0;
1049 s->op_st_chg.status = -1;
1050 if (reason) {
1051 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1052 }
1053 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001054 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001055 s->op_st_chg.code = check->code;
1056 s->op_st_chg.status = check->status;
1057 s->op_st_chg.duration = check->duration;
1058 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001059
Emeric Brun64cc49c2017-10-03 14:46:45 +02001060 if (s->slowstart <= 0)
1061 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001062
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001063 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001064 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001065
Emeric Brun9f0b4582017-10-23 14:39:51 +02001066 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001067 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001068 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001069 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001070 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001071}
1072
Willy Tarreau8eb77842014-05-21 13:54:57 +02001073/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001074 * isn't in maintenance. The server is registered in a list to postpone the
1075 * counting of the remaining servers on the proxy and tries to grab requests
1076 * from the proxy. Maintenance servers are ignored. It stores the
1077 * <reason> if non-null as the reason for going down or the available data
1078 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001079 * up. Note that it makes use of the trash to build the log strings, so <reason>
1080 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001081 *
1082 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001083 */
Emeric Brun5a133512017-10-19 14:42:30 +02001084void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001085{
1086 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001087
Emeric Brun64cc49c2017-10-03 14:46:45 +02001088 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001089 return;
1090
Emeric Brun52a91d32017-08-31 14:41:55 +02001091 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001092 return;
1093
Emeric Brun52a91d32017-08-31 14:41:55 +02001094 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001095 *s->op_st_chg.reason = 0;
1096 s->op_st_chg.status = -1;
1097 if (reason) {
1098 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1099 }
1100 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001101 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001102 s->op_st_chg.code = check->code;
1103 s->op_st_chg.status = check->status;
1104 s->op_st_chg.duration = check->duration;
1105 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001106
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001107 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001108 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001109
Emeric Brun9f0b4582017-10-23 14:39:51 +02001110 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001111 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001112 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001113 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001114 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001115}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001116
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001117/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1118 * enforce either maint mode or drain mode. It is not allowed to set more than
1119 * one flag at once. The equivalent "inherited" flag is propagated to all
1120 * tracking servers. Maintenance mode disables health checks (but not agent
1121 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001122 * is done. If <cause> is non-null, it will be displayed at the end of the log
1123 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001124 *
1125 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001127void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001128{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001130
1131 if (!mode)
1132 return;
1133
1134 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001135 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001136 return;
1137
Emeric Brun52a91d32017-08-31 14:41:55 +02001138 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001139 if (cause)
1140 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1141
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001142 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001143 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001144
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001145 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001146 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1147 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001148 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001149
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001150 /* compute the inherited flag to propagate */
1151 if (mode & SRV_ADMF_MAINT)
1152 mode = SRV_ADMF_IMAINT;
1153 else if (mode & SRV_ADMF_DRAIN)
1154 mode = SRV_ADMF_IDRAIN;
1155
Emeric Brun9f0b4582017-10-23 14:39:51 +02001156 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001157 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001158 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001159 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001160 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001161}
1162
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001163/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1164 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1165 * than one flag at once. The equivalent "inherited" flag is propagated to all
1166 * tracking servers. Leaving maintenance mode re-enables health checks. When
1167 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001168 *
1169 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001171void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001172{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001173 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001174
1175 if (!mode)
1176 return;
1177
1178 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001179 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001180 return;
1181
Emeric Brun52a91d32017-08-31 14:41:55 +02001182 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001183
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001184 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001185 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001186
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001187 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001188 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1189 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001190 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001191
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001192 if (mode & SRV_ADMF_MAINT)
1193 mode = SRV_ADMF_IMAINT;
1194 else if (mode & SRV_ADMF_DRAIN)
1195 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001196
Emeric Brun9f0b4582017-10-23 14:39:51 +02001197 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001198 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001199 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001200 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001201 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001202}
1203
Willy Tarreau757478e2016-11-03 19:22:19 +01001204/* principle: propagate maint and drain to tracking servers. This is useful
1205 * upon startup so that inherited states are correct.
1206 */
1207static void srv_propagate_admin_state(struct server *srv)
1208{
1209 struct server *srv2;
1210
1211 if (!srv->trackers)
1212 return;
1213
1214 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001215 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001216 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001217 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001218
Emeric Brun52a91d32017-08-31 14:41:55 +02001219 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001220 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001221 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001222 }
1223}
1224
1225/* Compute and propagate the admin states for all servers in proxy <px>.
1226 * Only servers *not* tracking another one are considered, because other
1227 * ones will be handled when the server they track is visited.
1228 */
1229void srv_compute_all_admin_states(struct proxy *px)
1230{
1231 struct server *srv;
1232
1233 for (srv = px->srv; srv; srv = srv->next) {
1234 if (srv->track)
1235 continue;
1236 srv_propagate_admin_state(srv);
1237 }
1238}
1239
Willy Tarreaudff55432012-10-10 17:51:05 +02001240/* Note: must not be declared <const> as its list will be overwritten.
1241 * Please take care of keeping this list alphabetically sorted, doing so helps
1242 * all code contributors.
1243 * Optional keywords are also declared with a NULL ->parse() function so that
1244 * the config parser can report an appropriate error when a known keyword was
1245 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001246 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001247 */
1248static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001249 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001250 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001251 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1252 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001253 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001254 { "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 +01001255 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001256 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001257 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1258 { "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 +02001259 { "no-tfo", srv_parse_no_tfo, 0, 1 }, /* Disable use of TCP Fast Open */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001260 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001261 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001262 { "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 +01001263 { "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 +02001264 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001265 { "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 +01001266 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001267 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1268 { "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 +02001269 { "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 +01001270 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Olivier Houchard8d82db72019-07-04 13:34:10 +02001271 { "tfo", srv_parse_tfo, 0, 1 }, /* enable TCP Fast Open of server */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001272 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Alexander Liu2a54bb72019-05-22 19:44:48 +08001273 { "socks4", srv_parse_socks4, 1, 1 }, /* Set the socks4 proxy of the server*/
Willy Tarreaudff55432012-10-10 17:51:05 +02001274 { NULL, NULL, 0 },
1275}};
1276
Willy Tarreau0108d902018-11-25 19:14:37 +01001277INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001278
Willy Tarreau004e0452013-11-21 11:22:01 +01001279/* Recomputes the server's eweight based on its state, uweight, the current time,
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001280 * and the proxy's algorithm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001281 * state is automatically disabled if the time is elapsed. If <must_update> is
1282 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001283 *
1284 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001285 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001286void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001287{
1288 struct proxy *px = sv->proxy;
1289 unsigned w;
1290
1291 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1292 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001293 if (sv->next_state == SRV_ST_STARTING)
1294 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001295 }
1296
1297 /* We must take care of not pushing the server to full throttle during slow starts.
1298 * It must also start immediately, at least at the minimal step when leaving maintenance.
1299 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001300 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001301 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1302 else
1303 w = px->lbprm.wdiv;
1304
Emeric Brun52a91d32017-08-31 14:41:55 +02001305 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001306
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001307 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001308 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001309 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001310}
1311
Willy Tarreaubaaee002006-06-26 02:48:02 +02001312/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001313 * Parses weight_str and configures sv accordingly.
1314 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001315 *
1316 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001317 */
1318const char *server_parse_weight_change_request(struct server *sv,
1319 const char *weight_str)
1320{
1321 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001322 long int w;
1323 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001324
1325 px = sv->proxy;
1326
1327 /* if the weight is terminated with '%', it is set relative to
1328 * the initial weight, otherwise it is absolute.
1329 */
1330 if (!*weight_str)
1331 return "Require <weight> or <weight%>.\n";
1332
Simon Hormanb796afa2013-02-12 10:45:53 +09001333 w = strtol(weight_str, &end, 10);
1334 if (end == weight_str)
1335 return "Empty weight string empty or preceded by garbage";
1336 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001337 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001338 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001339 /* Avoid integer overflow */
1340 if (w > 25600)
1341 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001342 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001343 if (w > 256)
1344 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001345 }
1346 else if (w < 0 || w > 256)
1347 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001348 else if (end[0] != '\0')
1349 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001350
1351 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1352 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1353
1354 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001355 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001356
1357 return NULL;
1358}
1359
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001360/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001361 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1362 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001363 * Returns:
1364 * - error string on error
1365 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001366 *
1367 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001368 */
1369const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001370 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001371{
1372 unsigned char ip[INET6_ADDRSTRLEN];
1373
1374 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001375 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001376 return NULL;
1377 }
1378 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001379 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001380 return NULL;
1381 }
1382
1383 return "Could not understand IP address format.\n";
1384}
1385
Willy Tarreau46b7f532018-08-21 11:54:26 +02001386/*
1387 * Must be called with the server lock held.
1388 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001389const char *server_parse_maxconn_change_request(struct server *sv,
1390 const char *maxconn_str)
1391{
1392 long int v;
1393 char *end;
1394
1395 if (!*maxconn_str)
1396 return "Require <maxconn>.\n";
1397
1398 v = strtol(maxconn_str, &end, 10);
1399 if (end == maxconn_str)
1400 return "maxconn string empty or preceded by garbage";
1401 else if (end[0] != '\0')
1402 return "Trailing garbage in maxconn string";
1403
1404 if (sv->maxconn == sv->minconn) { // static maxconn
1405 sv->maxconn = sv->minconn = v;
1406 } else { // dynamic maxconn
1407 sv->maxconn = v;
1408 }
1409
1410 if (may_dequeue_tasks(sv, sv->proxy))
1411 process_srv_queue(sv);
1412
1413 return NULL;
1414}
1415
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001416#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001417static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1418 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001419{
1420 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001421 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001422 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001423 NULL,
1424 };
1425
1426 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001427 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001428
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01001429 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args, NULL);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001430}
1431
1432static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1433{
1434 struct sample_expr *expr;
1435
1436 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 +01001437 if (!expr) {
1438 memprintf(err, "error detected while parsing sni expression : %s", *err);
1439 return ERR_ALERT | ERR_FATAL;
1440 }
1441
1442 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1443 memprintf(err, "error detected while parsing sni expression : "
1444 " fetch method '%s' extracts information from '%s', "
1445 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001446 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001447 return ERR_ALERT | ERR_FATAL;
1448 }
1449
1450 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1451 release_sample_expr(newsrv->ssl_ctx.sni);
1452 newsrv->ssl_ctx.sni = expr;
1453
1454 return 0;
1455}
1456#endif
1457
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001458static 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 +01001459{
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001460 char *msg = "error encountered while processing ";
1461 char *quote = "'";
1462 char *token = args[cur_arg];
1463
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001464 if (err && *err) {
1465 indent_msg(err, 2);
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001466 msg = *err;
1467 quote = "";
1468 token = "";
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001469 }
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001470
1471 if (err_code & ERR_WARN && !(err_code & ERR_ALERT))
1472 ha_warning("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1473 file, linenum, args[0], args[1],
1474 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001475 else
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001476 ha_alert("parsing [%s:%d] : '%s %s' : %s%s%s%s.\n",
1477 file, linenum, args[0], args[1],
1478 msg, quote, token, quote);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001479}
1480
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001481static void srv_conn_src_sport_range_cpy(struct server *srv,
1482 struct server *src)
1483{
1484 int range_sz;
1485
1486 range_sz = src->conn_src.sport_range->size;
1487 if (range_sz > 0) {
1488 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1489 if (srv->conn_src.sport_range != NULL) {
1490 int i;
1491
1492 for (i = 0; i < range_sz; i++) {
1493 srv->conn_src.sport_range->ports[i] =
1494 src->conn_src.sport_range->ports[i];
1495 }
1496 }
1497 }
1498}
1499
1500/*
1501 * Copy <src> server connection source settings to <srv> server everything needed.
1502 */
1503static void srv_conn_src_cpy(struct server *srv, struct server *src)
1504{
1505 srv->conn_src.opts = src->conn_src.opts;
1506 srv->conn_src.source_addr = src->conn_src.source_addr;
1507
1508 /* Source port range copy. */
1509 if (src->conn_src.sport_range != NULL)
1510 srv_conn_src_sport_range_cpy(srv, src);
1511
1512#ifdef CONFIG_HAP_TRANSPARENT
1513 if (src->conn_src.bind_hdr_name != NULL) {
1514 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1515 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1516 }
1517 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1518 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1519#endif
1520 if (src->conn_src.iface_name != NULL)
1521 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1522}
1523
1524/*
1525 * Copy <src> server SSL settings to <srv> server allocating
1526 * everything needed.
1527 */
1528#if defined(USE_OPENSSL)
1529static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1530{
1531 if (src->ssl_ctx.ca_file != NULL)
1532 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1533 if (src->ssl_ctx.crl_file != NULL)
1534 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1535 if (src->ssl_ctx.client_crt != NULL)
1536 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1537
1538 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1539
1540 if (src->ssl_ctx.verify_host != NULL)
1541 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1542 if (src->ssl_ctx.ciphers != NULL)
1543 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Jerome Magnin2e8d52f2020-04-22 11:40:18 +02001544 if (src->ssl_ctx.options)
1545 srv->ssl_ctx.options = src->ssl_ctx.options;
1546 if (src->ssl_ctx.methods.flags)
1547 srv->ssl_ctx.methods.flags = src->ssl_ctx.methods.flags;
1548 if (src->ssl_ctx.methods.min)
1549 srv->ssl_ctx.methods.min = src->ssl_ctx.methods.min;
1550 if (src->ssl_ctx.methods.max)
1551 srv->ssl_ctx.methods.max = src->ssl_ctx.methods.max;
1552
Willy Tarreau5db847a2019-05-09 14:13:35 +02001553#if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL)
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001554 if (src->ssl_ctx.ciphersuites != NULL)
1555 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1556#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001557 if (src->sni_expr != NULL)
1558 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001559
1560#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1561 if (src->ssl_ctx.alpn_str) {
1562 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1563 if (srv->ssl_ctx.alpn_str) {
1564 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1565 src->ssl_ctx.alpn_len);
1566 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1567 }
1568 }
1569#endif
1570#ifdef OPENSSL_NPN_NEGOTIATED
1571 if (src->ssl_ctx.npn_str) {
1572 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1573 if (srv->ssl_ctx.npn_str) {
1574 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1575 src->ssl_ctx.npn_len);
1576 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1577 }
1578 }
1579#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001580}
1581#endif
1582
1583/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001584 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001585 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001586 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001587 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001588static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001589{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001590 char *hostname_dn;
1591 int hostname_len, hostname_dn_len;
1592
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001593 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001594 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001595
Christopher Faulet67957bd2017-09-27 11:00:59 +02001596 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001597 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001598 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1599 hostname_dn, trash.size);
1600 if (hostname_dn_len == -1)
1601 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001602
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001603
Christopher Faulet67957bd2017-09-27 11:00:59 +02001604 free(srv->hostname);
1605 free(srv->hostname_dn);
1606 srv->hostname = strdup(hostname);
1607 srv->hostname_dn = strdup(hostname_dn);
1608 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001609 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001610 goto err;
1611
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001612 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001613
1614 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001615 free(srv->hostname); srv->hostname = NULL;
1616 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001617 return -1;
1618}
1619
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001620/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001621 * Copy <src> server settings to <srv> server allocating
1622 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001623 * This function is not supposed to be called at any time, but only
1624 * during server settings parsing or during server allocations from
1625 * a server template, and just after having calloc()'ed a new server.
1626 * So, <src> may only be a default server (when parsing server settings)
1627 * or a server template (during server allocations from a server template).
1628 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1629 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001630 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001631static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001632{
1633 /* Connection source settings copy */
1634 srv_conn_src_cpy(srv, src);
1635
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001636 if (srv_tmpl) {
1637 srv->addr = src->addr;
1638 srv->svc_port = src->svc_port;
1639 }
1640
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001641 srv->pp_opts = src->pp_opts;
1642 if (src->rdr_pfx != NULL) {
1643 srv->rdr_pfx = strdup(src->rdr_pfx);
1644 srv->rdr_len = src->rdr_len;
1645 }
1646 if (src->cookie != NULL) {
1647 srv->cookie = strdup(src->cookie);
1648 srv->cklen = src->cklen;
1649 }
1650 srv->use_ssl = src->use_ssl;
Willy Tarreau24441082019-12-11 15:43:45 +01001651 srv->check.addr = src->check.addr;
1652 srv->agent.addr = src->agent.addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001653 srv->check.use_ssl = src->check.use_ssl;
1654 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001655 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001656 srv->check.alpn_str = src->check.alpn_str;
Ilya Shipitsin0c50b1e2019-04-30 21:21:28 +05001657 srv->check.alpn_len = src->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001658 /* Note: 'flags' field has potentially been already initialized. */
1659 srv->flags |= src->flags;
1660 srv->do_check = src->do_check;
1661 srv->do_agent = src->do_agent;
1662 if (srv->check.port)
1663 srv->flags |= SRV_F_CHECKPORT;
1664 srv->check.inter = src->check.inter;
1665 srv->check.fastinter = src->check.fastinter;
1666 srv->check.downinter = src->check.downinter;
1667 srv->agent.use_ssl = src->agent.use_ssl;
1668 srv->agent.port = src->agent.port;
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02001669
1670 if (src->agent.tcpcheck_rules) {
1671 srv->agent.tcpcheck_rules = calloc(1, sizeof(*srv->agent.tcpcheck_rules));
1672 if (srv->agent.tcpcheck_rules) {
1673 srv->agent.tcpcheck_rules->flags = src->agent.tcpcheck_rules->flags;
1674 srv->agent.tcpcheck_rules->list = src->agent.tcpcheck_rules->list;
1675 LIST_INIT(&srv->agent.tcpcheck_rules->preset_vars);
1676 dup_tcpcheck_vars(&srv->agent.tcpcheck_rules->preset_vars,
1677 &src->agent.tcpcheck_rules->preset_vars);
1678 }
1679 }
1680
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001681 srv->agent.inter = src->agent.inter;
1682 srv->agent.fastinter = src->agent.fastinter;
1683 srv->agent.downinter = src->agent.downinter;
1684 srv->maxqueue = src->maxqueue;
1685 srv->minconn = src->minconn;
1686 srv->maxconn = src->maxconn;
1687 srv->slowstart = src->slowstart;
1688 srv->observe = src->observe;
1689 srv->onerror = src->onerror;
1690 srv->onmarkeddown = src->onmarkeddown;
1691 srv->onmarkedup = src->onmarkedup;
1692 if (src->trackit != NULL)
1693 srv->trackit = strdup(src->trackit);
1694 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1695 srv->uweight = srv->iweight = src->iweight;
1696
1697 srv->check.send_proxy = src->check.send_proxy;
1698 /* health: up, but will fall down at first failure */
1699 srv->check.rise = srv->check.health = src->check.rise;
1700 srv->check.fall = src->check.fall;
1701
1702 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001703 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1704 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1705 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001706 srv->check.state |= CHK_ST_PAUSED;
1707 srv->check.health = 0;
1708 }
1709
1710 /* health: up but will fall down at first failure */
1711 srv->agent.rise = srv->agent.health = src->agent.rise;
1712 srv->agent.fall = src->agent.fall;
1713
1714 if (src->resolvers_id != NULL)
1715 srv->resolvers_id = strdup(src->resolvers_id);
1716 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001717 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Daniel Corbettf8716912019-11-17 09:48:56 -05001718 srv->dns_opts.ignore_weight = src->dns_opts.ignore_weight;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001719 if (srv->dns_opts.family_prio == AF_UNSPEC)
1720 srv->dns_opts.family_prio = AF_INET6;
1721 memcpy(srv->dns_opts.pref_net,
1722 src->dns_opts.pref_net,
1723 sizeof srv->dns_opts.pref_net);
1724 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1725
1726 srv->init_addr_methods = src->init_addr_methods;
1727 srv->init_addr = src->init_addr;
1728#if defined(USE_OPENSSL)
1729 srv_ssl_settings_cpy(srv, src);
1730#endif
1731#ifdef TCP_USER_TIMEOUT
1732 srv->tcp_ut = src->tcp_ut;
1733#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001734 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001735 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001736 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001737 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001738
Olivier Houchard8da5f982017-08-04 18:35:36 +02001739 if (srv_tmpl)
1740 srv->srvrq = src->srvrq;
Alexander Liu2a54bb72019-05-22 19:44:48 +08001741
1742 srv->check.via_socks4 = src->check.via_socks4;
1743 srv->socks4_addr = src->socks4_addr;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001744}
1745
William Lallemand313bfd12018-10-26 14:47:32 +02001746struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001747{
1748 struct server *srv;
1749
1750 srv = calloc(1, sizeof *srv);
1751 if (!srv)
1752 return NULL;
1753
1754 srv->obj_type = OBJ_TYPE_SERVER;
1755 srv->proxy = proxy;
1756 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001757 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001758
Emeric Brun52a91d32017-08-31 14:41:55 +02001759 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001760 srv->last_change = now.tv_sec;
1761
Christopher Faulet38290462020-04-21 11:46:40 +02001762 srv->check.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001763 srv->check.status = HCHK_STATUS_INI;
1764 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001765 srv->check.proxy = proxy;
Christopher Faulet5d503fc2020-03-30 20:34:34 +02001766 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001767
Christopher Faulet38290462020-04-21 11:46:40 +02001768 srv->agent.obj_type = OBJ_TYPE_CHECK;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001769 srv->agent.status = HCHK_STATUS_INI;
1770 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001771 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001772 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1773
Willy Tarreau975b1552019-06-06 16:25:55 +02001774 /* please don't put default server settings here, they are set in
1775 * init_default_instance().
1776 */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001777 return srv;
1778}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001779
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001780#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1781static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1782 struct server *srv, struct proxy *proxy)
1783{
1784 int ret;
1785 char *err = NULL;
1786
1787 if (!srv->sni_expr)
1788 return 0;
1789
1790 ret = server_parse_sni_expr(srv, proxy, &err);
1791 if (!ret)
1792 return 0;
1793
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01001794 display_parser_err(file, linenum, args, cur_arg, ret, &err);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001795 free(err);
1796
1797 return ret;
1798}
1799#endif
1800
1801/*
1802 * Server initializations finalization.
1803 * Initialize health check, agent check and SNI expression if enabled.
1804 * Must not be called for a default server instance.
1805 */
1806static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1807 struct server *srv, struct proxy *px)
1808{
Christopher Fauletb3b53522020-04-27 11:17:10 +02001809#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001810 int ret;
Christopher Fauletb3b53522020-04-27 11:17:10 +02001811#endif
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001812
Christopher Faulet8892e5d2020-03-26 19:48:20 +01001813 if (srv->do_check && srv->trackit) {
1814 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1815 file, linenum);
1816 return ERR_ALERT | ERR_FATAL;
1817 }
1818
1819 if (srv->do_agent && !srv->agent.port) {
1820 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1821 file, linenum, srv->id);
1822 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001823 }
1824
1825#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1826 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1827 return ret;
1828#endif
1829
1830 if (srv->flags & SRV_F_BACKUP)
1831 px->srv_bck++;
1832 else
1833 px->srv_act++;
1834 srv_lb_commit_status(srv);
1835
1836 return 0;
1837}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001838
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001839/*
1840 * Parse as much as possible such a range string argument: low[-high]
1841 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1842 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1843 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1844 * Returns 0 if succeeded, -1 if not.
1845 */
1846static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1847{
1848 char *nb_high_arg;
1849
1850 *nb_high = 0;
1851 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001852 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001853
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001855 *nb_high_arg++ = '\0';
1856 *nb_high = atoi(nb_high_arg);
1857 }
1858 else {
1859 *nb_high += *nb_low;
1860 *nb_low = 1;
1861 }
1862
1863 if (*nb_low < 0 || *nb_high < *nb_low)
1864 return -1;
1865
1866 return 0;
1867}
1868
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001869static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1870{
1871 chunk_printf(&trash, "%s%d", prefix, nb);
1872 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001873 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001874}
1875
1876/*
1877 * Initialize as much as possible servers from <srv> server template.
1878 * Note that a server template is a special server with
1879 * a few different parameters than a server which has
1880 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001881 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001882 * 'srv' template included.
1883 */
1884static int server_template_init(struct server *srv, struct proxy *px)
1885{
1886 int i;
1887 struct server *newsrv;
1888
1889 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 +02001890 newsrv = new_server(px);
1891 if (!newsrv)
1892 goto err;
1893
1894 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001895 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001896#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1897 if (newsrv->sni_expr) {
1898 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1899 if (!newsrv->ssl_ctx.sni)
1900 goto err;
1901 }
1902#endif
1903 /* Set this new server ID. */
1904 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1905
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001906 /* Linked backwards first. This will be restablished after parsing. */
1907 newsrv->next = px->srv;
1908 px->srv = newsrv;
1909 }
1910 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1911
1912 return i - srv->tmpl_info.nb_low;
1913
1914 err:
1915 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1916 if (newsrv) {
1917#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1918 release_sample_expr(newsrv->ssl_ctx.sni);
1919#endif
1920 free_check(&newsrv->agent);
1921 free_check(&newsrv->check);
1922 }
1923 free(newsrv);
1924 return i - srv->tmpl_info.nb_low;
1925}
1926
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001927int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr, int in_peers_section)
Willy Tarreau272adea2014-03-31 10:39:59 +02001928{
1929 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001931 char *errmsg = NULL;
1932 int err_code = 0;
1933 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001934 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001935
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001936 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001937 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001938 !strcmp(args[0], "default-server") ||
1939 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001940 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001941 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02001942 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001943 int srv_tmpl = !defsrv && !srv;
1944 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001945
1946 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001947 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001948 err_code |= ERR_ALERT | ERR_FATAL;
1949 goto out;
1950 }
1951 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02001952 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02001953
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01001955 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001956 if (!*args[2]) {
Frédéric Lécaille8ba10fe2020-04-03 09:43:47 +02001957 if (in_peers_section) {
1958 return 0;
1959 }
1960 else {
1961 /* 'server' line number of argument check. */
1962 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1963 file, linenum, args[0]);
1964 err_code |= ERR_ALERT | ERR_FATAL;
1965 goto out;
1966 }
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 }
1968
1969 err = invalid_char(args[1]);
1970 }
1971 else if (srv_tmpl) {
1972 if (!*args[3]) {
1973 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01001974 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 +02001975 file, linenum, args[0]);
1976 err_code |= ERR_ALERT | ERR_FATAL;
1977 goto out;
1978 }
1979
1980 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001981 }
1982
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001983 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001984 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 +02001985 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001986 err_code |= ERR_ALERT | ERR_FATAL;
1987 goto out;
1988 }
1989
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001990 cur_arg = 2;
1991 if (srv_tmpl) {
1992 /* Parse server-template <nb | range> arg. */
1993 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001994 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001995 file, linenum, args[0], args[cur_arg]);
1996 err_code |= ERR_ALERT | ERR_FATAL;
1997 goto out;
1998 }
1999 cur_arg++;
2000 }
2001
Willy Tarreau272adea2014-03-31 10:39:59 +02002002 if (!defsrv) {
2003 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002004 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002005 struct protocol *proto;
2006
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002007 newsrv = new_server(curproxy);
2008 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002009 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002010 err_code |= ERR_ALERT | ERR_ABORT;
2011 goto out;
2012 }
2013
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002014 if (srv_tmpl) {
2015 newsrv->tmpl_info.nb_low = tmpl_range_low;
2016 newsrv->tmpl_info.nb_high = tmpl_range_high;
2017 }
2018
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 /* the servers are linked backwards first */
2020 newsrv->next = curproxy->srv;
2021 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 newsrv->conf.file = strdup(file);
2023 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002024 /* Note: for a server template, its id is its prefix.
2025 * This is a temporary id which will be used for server allocations to come
2026 * after parsing.
2027 */
2028 if (srv)
2029 newsrv->id = strdup(args[1]);
2030 else
2031 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002032
2033 /* several ways to check the port component :
2034 * - IP => port=+0, relative (IPv4 only)
2035 * - IP: => port=+0, relative
2036 * - IP:N => port=N, absolute
2037 * - IP:+N => port=+N, relative
2038 * - IP:-N => port=-N, relative
2039 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002040 if (!parse_addr)
2041 goto skip_addr;
2042
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002043 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
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
2050 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002051 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002052 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002053 file, linenum, args[0], args[1]);
2054 err_code |= ERR_ALERT | ERR_FATAL;
2055 goto out;
2056 }
2057
2058 if (!port1 || !port2) {
2059 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002060 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002061 }
2062 else if (port1 != port2) {
2063 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002064 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 file, linenum, args[0], args[1], args[2]);
2066 err_code |= ERR_ALERT | ERR_FATAL;
2067 goto out;
2068 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002069
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002070 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002071 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002072 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002073 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002074 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2075 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2076 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002077 err_code |= ERR_ALERT | ERR_FATAL;
2078 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002079 }
2080 }
2081 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002082 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002083 file, linenum, newsrv->id);
2084 err_code |= ERR_ALERT | ERR_FATAL;
2085 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002086 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002087 }
2088
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002090 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002091
Olivier Houchard8da5f982017-08-04 18:35:36 +02002092 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002093 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002094 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002095 err_code |= ERR_ALERT | ERR_FATAL;
2096 goto out;
2097 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002098
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002099 cur_arg++;
2100 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002101 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002102 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002103 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002104 } else {
2105 newsrv = &curproxy->defsrv;
2106 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002107 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002108 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002109 }
2110
2111 while (*args[cur_arg]) {
Christopher Fauletcbba66c2020-04-06 14:26:30 +02002112 if (!strcmp(args[cur_arg], "init-addr")) {
Baptiste Assmann25938272016-09-21 20:26:16 +02002113 char *p, *end;
2114 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002115 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002116
2117 newsrv->init_addr_methods = 0;
2118 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2119
2120 for (p = args[cur_arg + 1]; *p; p = end) {
2121 /* cut on next comma */
2122 for (end = p; *end && *end != ','; end++);
2123 if (*end)
2124 *(end++) = 0;
2125
Willy Tarreau4310d362016-11-02 15:05:56 +01002126 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002127 if (!strcmp(p, "libc")) {
2128 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2129 }
2130 else if (!strcmp(p, "last")) {
2131 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2132 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002133 else if (!strcmp(p, "none")) {
2134 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2135 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002136 else if (str2ip2(p, &sa, 0)) {
2137 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002138 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002139 file, linenum, args[cur_arg], p);
2140 err_code |= ERR_ALERT | ERR_FATAL;
2141 goto out;
2142 }
2143 newsrv->init_addr = sa;
2144 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2145 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002146 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002147 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 +02002148 file, linenum, args[cur_arg], p);
2149 err_code |= ERR_ALERT | ERR_FATAL;
2150 goto out;
2151 }
2152 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002153 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002154 file, linenum, args[cur_arg], p);
2155 err_code |= ERR_ALERT | ERR_FATAL;
2156 goto out;
2157 }
2158 }
2159 cur_arg += 2;
2160 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002161 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002162 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002163 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2164 cur_arg += 2;
2165 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002166 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2167 char *p, *end;
2168
2169 for (p = args[cur_arg + 1]; *p; p = end) {
2170 /* cut on next comma */
2171 for (end = p; *end && *end != ','; end++);
2172 if (*end)
2173 *(end++) = 0;
2174
2175 if (!strcmp(p, "allow-dup-ip")) {
2176 newsrv->dns_opts.accept_duplicate_ip = 1;
2177 }
Daniel Corbettf8716912019-11-17 09:48:56 -05002178 else if (!strcmp(p, "ignore-weight")) {
2179 newsrv->dns_opts.ignore_weight = 1;
2180 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002181 else if (!strcmp(p, "prevent-dup-ip")) {
2182 newsrv->dns_opts.accept_duplicate_ip = 0;
2183 }
2184 else {
Daniel Corbettf8716912019-11-17 09:48:56 -05002185 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 +02002186 file, linenum, args[cur_arg], p);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190 }
2191
2192 cur_arg += 2;
2193 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002194 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2195 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002196 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002197 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002198 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002199 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002200 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002201 file, linenum, args[cur_arg]);
2202 err_code |= ERR_ALERT | ERR_FATAL;
2203 goto out;
2204 }
2205 cur_arg += 2;
2206 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002207 else if (!strcmp(args[cur_arg], "resolve-net")) {
2208 char *p, *e;
2209 unsigned char mask;
2210 struct dns_options *opt;
2211
2212 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002213 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002214 file, linenum, args[cur_arg]);
2215 err_code |= ERR_ALERT | ERR_FATAL;
2216 goto out;
2217 }
2218
2219 opt = &newsrv->dns_opts;
2220
2221 /* Split arguments by comma, and convert it from ipv4 or ipv6
2222 * string network in in_addr or in6_addr.
2223 */
2224 p = args[cur_arg + 1];
2225 e = p;
2226 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002227 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002228 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002229 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002230 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233 }
2234 /* look for end or comma. */
2235 while (*e != ',' && *e != '\0')
2236 e++;
2237 if (*e == ',') {
2238 *e = '\0';
2239 e++;
2240 }
2241 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2242 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2243 /* Try to convert input string from ipv4 or ipv6 network. */
2244 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2245 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2246 &mask)) {
2247 /* Try to convert input string from ipv6 network. */
2248 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2249 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2250 } else {
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05002251 /* All network conversions fail, return error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002252 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002253 file, linenum, args[cur_arg], p);
2254 err_code |= ERR_ALERT | ERR_FATAL;
2255 goto out;
2256 }
2257 opt->pref_net_nb++;
2258 p = e;
2259 }
2260
2261 cur_arg += 2;
2262 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002263 else if (!strcmp(args[cur_arg], "weight")) {
2264 int w;
2265 w = atol(args[cur_arg + 1]);
2266 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002267 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002268 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
2272 newsrv->uweight = newsrv->iweight = w;
2273 cur_arg += 2;
2274 }
2275 else if (!strcmp(args[cur_arg], "minconn")) {
2276 newsrv->minconn = atol(args[cur_arg + 1]);
2277 cur_arg += 2;
2278 }
2279 else if (!strcmp(args[cur_arg], "maxconn")) {
2280 newsrv->maxconn = atol(args[cur_arg + 1]);
2281 cur_arg += 2;
2282 }
2283 else if (!strcmp(args[cur_arg], "maxqueue")) {
2284 newsrv->maxqueue = atol(args[cur_arg + 1]);
2285 cur_arg += 2;
2286 }
2287 else if (!strcmp(args[cur_arg], "slowstart")) {
2288 /* slowstart is stored in seconds */
2289 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02002290
2291 if (err == PARSE_TIME_OVER) {
2292 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s> of server %s, maximum value is 2147483647 ms (~24.8 days).\n",
2293 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296 }
2297 else if (err == PARSE_TIME_UNDER) {
2298 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s> of server %s, minimum non-null value is 1 ms.\n",
2299 file, linenum, args[cur_arg+1], args[cur_arg], newsrv->id);
2300 err_code |= ERR_ALERT | ERR_FATAL;
2301 goto out;
2302 }
2303 else if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002304 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002305 file, linenum, *err, newsrv->id);
2306 err_code |= ERR_ALERT | ERR_FATAL;
2307 goto out;
2308 }
2309 newsrv->slowstart = (val + 999) / 1000;
2310 cur_arg += 2;
2311 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002312 else if (!strcmp(args[cur_arg], "on-error")) {
2313 if (!strcmp(args[cur_arg + 1], "fastinter"))
2314 newsrv->onerror = HANA_ONERR_FASTINTER;
2315 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2316 newsrv->onerror = HANA_ONERR_FAILCHK;
2317 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2318 newsrv->onerror = HANA_ONERR_SUDDTH;
2319 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2320 newsrv->onerror = HANA_ONERR_MARKDWN;
2321 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002322 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002323 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2324 file, linenum, args[cur_arg], args[cur_arg + 1]);
2325 err_code |= ERR_ALERT | ERR_FATAL;
2326 goto out;
2327 }
2328
2329 cur_arg += 2;
2330 }
2331 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2332 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2333 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2334 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002335 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002336 file, linenum, args[cur_arg], args[cur_arg + 1]);
2337 err_code |= ERR_ALERT | ERR_FATAL;
2338 goto out;
2339 }
2340
2341 cur_arg += 2;
2342 }
2343 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2344 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2345 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2346 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002347 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002348 file, linenum, args[cur_arg], args[cur_arg + 1]);
2349 err_code |= ERR_ALERT | ERR_FATAL;
2350 goto out;
2351 }
2352
2353 cur_arg += 2;
2354 }
2355 else if (!strcmp(args[cur_arg], "error-limit")) {
2356 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002357 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002358 file, linenum, args[cur_arg]);
2359 err_code |= ERR_ALERT | ERR_FATAL;
2360 goto out;
2361 }
2362
2363 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2364
2365 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002366 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002367 file, linenum, args[cur_arg]);
2368 err_code |= ERR_ALERT | ERR_FATAL;
2369 goto out;
2370 }
2371 cur_arg += 2;
2372 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002373 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002374 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002375 file, linenum, "usesrc", "source");
2376 err_code |= ERR_ALERT | ERR_FATAL;
2377 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002378 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002379 else {
2380 static int srv_dumped;
2381 struct srv_kw *kw;
2382 char *err;
2383
2384 kw = srv_find_kw(args[cur_arg]);
2385 if (kw) {
2386 char *err = NULL;
2387 int code;
2388
2389 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 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 +02002391 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002392 if (kw->skip != -1)
2393 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002394 err_code |= ERR_ALERT | ERR_FATAL;
2395 goto out;
2396 }
2397
2398 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002399 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002400 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002401 if (kw->skip != -1)
2402 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002403 err_code |= ERR_ALERT;
2404 continue;
2405 }
2406
2407 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2408 err_code |= code;
2409
2410 if (code) {
Gaetan Rivet10c4b4a2020-02-11 11:42:38 +01002411 display_parser_err(file, linenum, args, cur_arg, code, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002412 if (code & ERR_FATAL) {
2413 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002414 if (kw->skip != -1)
2415 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 goto out;
2417 }
2418 }
2419 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002420 if (kw->skip != -1)
2421 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002422 continue;
2423 }
2424
2425 err = NULL;
2426 if (!srv_dumped) {
2427 srv_dump_kws(&err);
2428 indent_msg(&err, 4);
2429 srv_dumped = 1;
2430 }
2431
Christopher Faulet767a84b2017-11-24 16:50:31 +01002432 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002433 file, linenum, args[0], args[1], args[cur_arg],
2434 err ? " Registered keywords :" : "", err ? err : "");
2435 free(err);
2436
2437 err_code |= ERR_ALERT | ERR_FATAL;
2438 goto out;
2439 }
2440 }
2441
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002442 if (!defsrv)
2443 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2444 if (err_code & ERR_FATAL)
2445 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002446 if (srv_tmpl)
2447 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002448 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002449 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002450 return 0;
2451
2452 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002453 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002454 free(errmsg);
2455 return err_code;
2456}
2457
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002458/* Returns a pointer to the first server matching either id <id>.
2459 * NULL is returned if no match is found.
2460 * the lookup is performed in the backend <bk>
2461 */
2462struct server *server_find_by_id(struct proxy *bk, int id)
2463{
2464 struct eb32_node *eb32;
2465 struct server *curserver;
2466
2467 if (!bk || (id ==0))
2468 return NULL;
2469
2470 /* <bk> has no backend capabilities, so it can't have a server */
2471 if (!(bk->cap & PR_CAP_BE))
2472 return NULL;
2473
2474 curserver = NULL;
2475
2476 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2477 if (eb32)
2478 curserver = container_of(eb32, struct server, conf.id);
2479
2480 return curserver;
2481}
2482
2483/* Returns a pointer to the first server matching either name <name>, or id
2484 * if <name> starts with a '#'. NULL is returned if no match is found.
2485 * the lookup is performed in the backend <bk>
2486 */
2487struct server *server_find_by_name(struct proxy *bk, const char *name)
2488{
2489 struct server *curserver;
2490
2491 if (!bk || !name)
2492 return NULL;
2493
2494 /* <bk> has no backend capabilities, so it can't have a server */
2495 if (!(bk->cap & PR_CAP_BE))
2496 return NULL;
2497
2498 curserver = NULL;
2499 if (*name == '#') {
2500 curserver = server_find_by_id(bk, atoi(name + 1));
2501 if (curserver)
2502 return curserver;
2503 }
2504 else {
2505 curserver = bk->srv;
2506
2507 while (curserver && (strcmp(curserver->id, name) != 0))
2508 curserver = curserver->next;
2509
2510 if (curserver)
2511 return curserver;
2512 }
2513
2514 return NULL;
2515}
2516
2517struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2518{
2519 struct server *byname;
2520 struct server *byid;
2521
2522 if (!name && !id)
2523 return NULL;
2524
2525 if (diff)
2526 *diff = 0;
2527
2528 byname = byid = NULL;
2529
2530 if (name) {
2531 byname = server_find_by_name(bk, name);
2532 if (byname && (!id || byname->puid == id))
2533 return byname;
2534 }
2535
2536 /* remaining possibilities :
2537 * - name not set
2538 * - name set but not found
2539 * - name found but ID doesn't match
2540 */
2541 if (id) {
2542 byid = server_find_by_id(bk, id);
2543 if (byid) {
2544 if (byname) {
2545 /* use id only if forced by configuration */
2546 if (byid->flags & SRV_F_FORCED_ID) {
2547 if (diff)
2548 *diff |= 2;
2549 return byid;
2550 }
2551 else {
2552 if (diff)
2553 *diff |= 1;
2554 return byname;
2555 }
2556 }
2557
2558 /* remaining possibilities:
2559 * - name not set
2560 * - name set but not found
2561 */
2562 if (name && diff)
2563 *diff |= 2;
2564 return byid;
2565 }
2566
2567 /* id bot found */
2568 if (byname) {
2569 if (diff)
2570 *diff |= 1;
2571 return byname;
2572 }
2573 }
2574
2575 return NULL;
2576}
2577
Willy Tarreau46b7f532018-08-21 11:54:26 +02002578/* Update a server state using the parameters available in the params list.
2579 *
2580 * Grabs the server lock during operation.
2581 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002582static void srv_update_state(struct server *srv, int version, char **params)
2583{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002584 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002585 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002586
2587 /* fields since version 1
2588 * and common to all other upcoming versions
2589 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002590 enum srv_state srv_op_state;
2591 enum srv_admin srv_admin_state;
2592 unsigned srv_uweight, srv_iweight;
2593 unsigned long srv_last_time_change;
2594 short srv_check_status;
2595 enum chk_result srv_check_result;
2596 int srv_check_health;
2597 int srv_check_state, srv_agent_state;
2598 int bk_f_forced_id;
2599 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002600 int fqdn_set_by_cli;
2601 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002602 const char *port_str;
2603 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002604 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002605
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002606 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002607 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002608 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002609 switch (version) {
2610 case 1:
2611 /*
2612 * now we can proceed with server's state update:
2613 * srv_addr: params[0]
2614 * srv_op_state: params[1]
2615 * srv_admin_state: params[2]
2616 * srv_uweight: params[3]
2617 * srv_iweight: params[4]
2618 * srv_last_time_change: params[5]
2619 * srv_check_status: params[6]
2620 * srv_check_result: params[7]
2621 * srv_check_health: params[8]
2622 * srv_check_state: params[9]
2623 * srv_agent_state: params[10]
2624 * bk_f_forced_id: params[11]
2625 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002626 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002627 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002628 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002629 */
2630
2631 /* validating srv_op_state */
2632 p = NULL;
2633 errno = 0;
2634 srv_op_state = strtol(params[1], &p, 10);
2635 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2636 (srv_op_state != SRV_ST_STOPPED &&
2637 srv_op_state != SRV_ST_STARTING &&
2638 srv_op_state != SRV_ST_RUNNING &&
2639 srv_op_state != SRV_ST_STOPPING)) {
2640 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2641 }
2642
2643 /* validating srv_admin_state */
2644 p = NULL;
2645 errno = 0;
2646 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002647 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002648
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002649 /* inherited statuses will be recomputed later.
2650 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2651 */
2652 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002653
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002654 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2655 (srv_admin_state != 0 &&
2656 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002657 srv_admin_state != SRV_ADMF_CMAINT &&
2658 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002659 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002660 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002661 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2662 }
2663
2664 /* validating srv_uweight */
2665 p = NULL;
2666 errno = 0;
2667 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002668 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002669 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2670
2671 /* validating srv_iweight */
2672 p = NULL;
2673 errno = 0;
2674 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002675 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002676 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2677
2678 /* validating srv_last_time_change */
2679 p = NULL;
2680 errno = 0;
2681 srv_last_time_change = strtol(params[5], &p, 10);
2682 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2683 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2684
2685 /* validating srv_check_status */
2686 p = NULL;
2687 errno = 0;
2688 srv_check_status = strtol(params[6], &p, 10);
2689 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2690 (srv_check_status >= HCHK_STATUS_SIZE))
2691 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2692
2693 /* validating srv_check_result */
2694 p = NULL;
2695 errno = 0;
2696 srv_check_result = strtol(params[7], &p, 10);
2697 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2698 (srv_check_result != CHK_RES_UNKNOWN &&
2699 srv_check_result != CHK_RES_NEUTRAL &&
2700 srv_check_result != CHK_RES_FAILED &&
2701 srv_check_result != CHK_RES_PASSED &&
2702 srv_check_result != CHK_RES_CONDPASS)) {
2703 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2704 }
2705
2706 /* validating srv_check_health */
2707 p = NULL;
2708 errno = 0;
2709 srv_check_health = strtol(params[8], &p, 10);
2710 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2711 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2712
2713 /* validating srv_check_state */
2714 p = NULL;
2715 errno = 0;
2716 srv_check_state = strtol(params[9], &p, 10);
2717 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2718 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2719 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2720
2721 /* validating srv_agent_state */
2722 p = NULL;
2723 errno = 0;
2724 srv_agent_state = strtol(params[10], &p, 10);
2725 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2726 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2727 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2728
2729 /* validating bk_f_forced_id */
2730 p = NULL;
2731 errno = 0;
2732 bk_f_forced_id = strtol(params[11], &p, 10);
2733 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2734 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2735
2736 /* validating srv_f_forced_id */
2737 p = NULL;
2738 errno = 0;
2739 srv_f_forced_id = strtol(params[12], &p, 10);
2740 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2741 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2742
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002743 /* validating srv_fqdn */
2744 fqdn = params[13];
2745 if (fqdn && *fqdn == '-')
2746 fqdn = NULL;
2747 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2748 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2749 fqdn = NULL;
2750 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002751
Frédéric Lécaille31694712017-08-01 08:47:19 +02002752 port_str = params[14];
2753 if (port_str) {
2754 port = strl2uic(port_str, strlen(port_str));
2755 if (port > USHRT_MAX) {
2756 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2757 port_str = NULL;
2758 }
2759 }
2760
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002761 /* SRV record
2762 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2763 */
2764 srvrecord = params[15];
2765 if (srvrecord && *srvrecord != '_')
2766 srvrecord = NULL;
2767
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002768 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002769 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002770 goto out;
2771
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002772 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002773 /* recover operational state and apply it to this server
2774 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01002775 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002776 switch (srv_op_state) {
2777 case SRV_ST_STOPPED:
2778 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002779 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002780 break;
2781 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002782 /* If rise == 1 there is no STARTING state, let's switch to
2783 * RUNNING
2784 */
2785 if (srv->check.rise == 1) {
2786 srv->check.health = srv->check.rise + srv->check.fall - 1;
2787 srv_set_running(srv, "", NULL);
2788 break;
2789 }
2790 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
2791 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02002792 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 break;
2794 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01002795 /* If fall == 1 there is no STOPPING state, let's switch to
2796 * STOPPED
2797 */
2798 if (srv->check.fall == 1) {
2799 srv->check.health = 0;
2800 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
2801 break;
2802 }
2803 if (srv->check.health < srv->check.rise ||
2804 srv->check.health > srv->check.rise + srv->check.fall - 2)
2805 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02002806 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002807 break;
2808 case SRV_ST_RUNNING:
2809 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002810 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002811 break;
2812 }
2813
2814 /* When applying server state, the following rules apply:
2815 * - in case of a configuration change, we apply the setting from the new
2816 * configuration, regardless of old running state
2817 * - if no configuration change, we apply old running state only if old running
2818 * state is different from new configuration state
2819 */
2820 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002821 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2822 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002823 srv_adm_set_maint(srv);
2824 else
2825 srv_adm_set_ready(srv);
2826 }
2827 /* configuration is the same, let's compate old running state and new conf state */
2828 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002829 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002830 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002831 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002832 srv_adm_set_ready(srv);
2833 }
2834 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002835 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002836 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002837 * (srv->iweight is the weight set up in configuration).
2838 * There are two possible reasons for FDRAIN to have been present :
2839 * - previous config weight was zero
2840 * - "set server b/s drain" was sent to the CLI
2841 *
2842 * In the first case, we simply want to drop this drain state
2843 * if the new weight is not zero anymore, meaning the administrator
2844 * has intentionally turned the weight back to a positive value to
2845 * enable the server again after an operation. In the second case,
2846 * the drain state was forced on the CLI regardless of the config's
2847 * weight so we don't want a change to the config weight to lose this
2848 * status. What this means is :
2849 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2850 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002851 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002852 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002853 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002854 }
2855
2856 srv->last_change = date.tv_sec - srv_last_time_change;
2857 srv->check.status = srv_check_status;
2858 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002859
2860 /* Only case we want to apply is removing ENABLED flag which could have been
2861 * done by the "disable health" command over the stats socket
2862 */
2863 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2864 (srv_check_state & CHK_ST_CONFIGURED) &&
2865 !(srv_check_state & CHK_ST_ENABLED))
2866 srv->check.state &= ~CHK_ST_ENABLED;
2867
2868 /* Only case we want to apply is removing ENABLED flag which could have been
2869 * done by the "disable agent" command over the stats socket
2870 */
2871 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2872 (srv_agent_state & CHK_ST_CONFIGURED) &&
2873 !(srv_agent_state & CHK_ST_ENABLED))
2874 srv->agent.state &= ~CHK_ST_ENABLED;
2875
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002876 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2877 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002879 * It means that a configuration file change has precedence over a unix socket change
2880 * for server's weight
2881 *
2882 * by default, HAProxy applies the following weight when parsing the configuration
2883 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002884 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002885 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002886 srv->uweight = srv_uweight;
2887 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02002888 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002889
Willy Tarreaue5a60682016-11-09 14:54:53 +01002890 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04002891 if (strcmp(params[0], "-"))
2892 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002893
2894 if (fqdn && srv->hostname) {
2895 if (!strcmp(srv->hostname, fqdn)) {
2896 /* Here we reset the 'set from stats socket FQDN' flag
2897 * to support such transitions:
2898 * Let's say initial FQDN value is foo1 (in configuration file).
2899 * - FQDN changed from stats socket, from foo1 to foo2 value,
2900 * - FQDN changed again from file configuration (with the same previous value
2901 set from stats socket, from foo1 to foo2 value),
2902 * - reload for any other reason than a FQDN modification,
2903 * the configuration file FQDN matches the fqdn server state file value.
2904 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08002905 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002906 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002907 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002908 }
2909 else {
2910 /* If the FDQN has been changed from stats socket,
2911 * apply fqdn state file value (which is the value set
2912 * from stats socket).
Baptiste Assmann13a92322019-06-07 09:40:55 +02002913 * Also ensure the runtime resolver will process this resolution.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002914 */
2915 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002916 srv_set_fqdn(srv, fqdn, 0);
Baptiste Assmann13a92322019-06-07 09:40:55 +02002917 srv->flags &= ~SRV_F_NO_RESOLUTION;
Emeric Brun52a91d32017-08-31 14:41:55 +02002918 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002919 }
2920 }
2921 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002922 /* If all the conditions below are validated, this means
2923 * we're evaluating a server managed by SRV resolution
2924 */
2925 else if (fqdn && !srv->hostname && srvrecord) {
2926 int res;
2927
2928 /* we can't apply previous state if SRV record has changed */
2929 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
2930 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);
2931 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2932 goto out;
2933 }
2934
2935 /* create or find a SRV resolution for this srv record */
2936 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
2937 srv->srvrq = new_dns_srvrq(srv, srvrecord);
2938 if (srv->srvrq == NULL) {
2939 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
2940 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2941 goto out;
2942 }
2943
2944 /* prepare DNS resolution for this server */
2945 res = srv_prepare_for_resolution(srv, fqdn);
2946 if (res == -1) {
2947 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
2948 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
2949 goto out;
2950 }
2951
2952 /* configure check.port accordingly */
2953 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2954 !(srv->flags & SRV_F_CHECKPORT))
2955 srv->check.port = port;
2956
2957 /* Unset SRV_F_MAPPORTS for SRV records.
2958 * SRV_F_MAPPORTS is unfortunately set by parse_server()
2959 * because no ports are provided in the configuration file.
2960 * This is because HAProxy will use the port found into the SRV record.
2961 */
2962 srv->flags &= ~SRV_F_MAPPORTS;
2963 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002964
Frédéric Lécaille31694712017-08-01 08:47:19 +02002965 if (port_str)
2966 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002967 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002968
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002969 break;
2970 default:
2971 chunk_appendf(msg, ", version '%d' not supported", version);
2972 }
2973
2974 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002975 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002976 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01002977 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002978 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002979 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002980}
2981
Baptiste Assmannda29fe22019-06-13 13:24:29 +02002982
2983/*
2984 * read next line from file <f> and return the server state version if one found.
2985 * If no version is found, then 0 is returned
2986 * Note that this should be the first read on <f>
2987 */
2988static int srv_state_get_version(FILE *f) {
2989 char buf[2];
2990 int ret;
2991
2992 /* first character of first line of the file must contain the version of the export */
2993 if (fgets(buf, 2, f) == NULL) {
2994 return 0;
2995 }
2996
2997 ret = atoi(buf);
2998 if ((ret < SRV_STATE_FILE_VERSION_MIN) ||
2999 (ret > SRV_STATE_FILE_VERSION_MAX))
3000 return 0;
3001
3002 return ret;
3003}
3004
3005
3006/*
3007 * parses server state line stored in <buf> and supposedly in version <version>.
3008 * Set <params> and <srv_params> accordingly.
3009 * In case of error, params[0] is set to NULL.
3010 */
3011static void srv_state_parse_line(char *buf, const int version, char **params, char **srv_params)
3012{
3013 int buflen, arg, srv_arg;
3014 char *cur, *end;
3015
3016 buflen = strlen(buf);
3017 cur = buf;
3018 end = cur + buflen;
3019
3020 /* we need at least one character */
3021 if (buflen == 0) {
3022 params[0] = NULL;
3023 return;
3024 }
3025
3026 /* ignore blank characters at the beginning of the line */
Willy Tarreau90807112020-02-25 08:16:33 +01003027 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003028 ++cur;
3029
3030 /* Ignore empty or commented lines */
3031 if (cur == end || *cur == '#') {
3032 params[0] = NULL;
3033 return;
3034 }
3035
3036 /* truncated lines */
3037 if (buf[buflen - 1] != '\n') {
3038 //ha_warning("server-state file '%s': truncated line\n", filepath);
3039 params[0] = NULL;
3040 return;
3041 }
3042
3043 /* Removes trailing '\n' */
3044 buf[buflen - 1] = '\0';
3045
3046 /* we're now ready to move the line into *srv_params[] */
3047 params[0] = cur;
3048 arg = 1;
3049 srv_arg = 0;
3050 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
Willy Tarreau90807112020-02-25 08:16:33 +01003051 if (isspace((unsigned char)*cur)) {
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003052 *cur = '\0';
3053 ++cur;
Willy Tarreau90807112020-02-25 08:16:33 +01003054 while (isspace((unsigned char)*cur))
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003055 ++cur;
3056 switch (version) {
3057 case 1:
3058 /*
3059 * srv_addr: params[4] => srv_params[0]
3060 * srv_op_state: params[5] => srv_params[1]
3061 * srv_admin_state: params[6] => srv_params[2]
3062 * srv_uweight: params[7] => srv_params[3]
3063 * srv_iweight: params[8] => srv_params[4]
3064 * srv_last_time_change: params[9] => srv_params[5]
3065 * srv_check_status: params[10] => srv_params[6]
3066 * srv_check_result: params[11] => srv_params[7]
3067 * srv_check_health: params[12] => srv_params[8]
3068 * srv_check_state: params[13] => srv_params[9]
3069 * srv_agent_state: params[14] => srv_params[10]
3070 * bk_f_forced_id: params[15] => srv_params[11]
3071 * srv_f_forced_id: params[16] => srv_params[12]
3072 * srv_fqdn: params[17] => srv_params[13]
3073 * srv_port: params[18] => srv_params[14]
3074 * srvrecord: params[19] => srv_params[15]
3075 */
3076 if (arg >= 4) {
3077 srv_params[srv_arg] = cur;
3078 ++srv_arg;
3079 }
3080 break;
3081 }
3082
3083 params[arg] = cur;
3084 ++arg;
3085 }
3086 else {
3087 ++cur;
3088 }
3089 }
3090
3091 /* if line is incomplete line, then ignore it.
3092 * otherwise, update useful flags */
3093 switch (version) {
3094 case 1:
3095 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1) {
3096 params[0] = NULL;
3097 return;
3098 }
3099 break;
3100 }
3101
3102 return;
3103}
3104
3105
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003106/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3107 * For each proxy, it does the following:
3108 * - opens its server state file (either one or local one)
3109 * - read whole file, line by line
3110 * - analyse each line to check if it matches our current backend:
3111 * - backend name matches
3112 * - backend id matches if id is forced and name doesn't match
3113 * - if the server pointed by the line is found, then state is applied
3114 *
3115 * If the running backend uuid or id differs from the state file, then HAProxy reports
3116 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003117 *
3118 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119 */
3120void apply_server_state(void)
3121{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003122 char mybuf[SRV_STATE_LINE_MAXLEN];
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003123 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3124 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003125 int version, global_file_version;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126 FILE *f;
3127 char *filepath;
3128 char globalfilepath[MAXPATHLEN + 1];
3129 char localfilepath[MAXPATHLEN + 1];
3130 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003131 struct proxy *curproxy, *bk;
3132 struct server *srv;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003133 char *line;
3134 char *bkname, *srvname;
3135 struct state_line *st;
3136 struct ebmb_node *node, *next_node;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003137
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003138
3139 global_file_version = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140 globalfilepathlen = 0;
3141 /* create the globalfilepath variable */
3142 if (global.server_state_file) {
3143 /* absolute path or no base directory provided */
3144 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3145 len = strlen(global.server_state_file);
3146 if (len > MAXPATHLEN) {
3147 globalfilepathlen = 0;
3148 goto globalfileerror;
3149 }
3150 memcpy(globalfilepath, global.server_state_file, len);
3151 globalfilepath[len] = '\0';
3152 globalfilepathlen = len;
3153 }
3154 else if (global.server_state_base) {
3155 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003156 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003157 globalfilepathlen = 0;
3158 goto globalfileerror;
3159 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003160 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003161 globalfilepath[len] = 0;
3162 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003163
3164 /* append a slash if needed */
3165 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3166 if (globalfilepathlen + 1 > MAXPATHLEN) {
3167 globalfilepathlen = 0;
3168 goto globalfileerror;
3169 }
3170 globalfilepath[globalfilepathlen++] = '/';
3171 }
3172
3173 len = strlen(global.server_state_file);
3174 if (globalfilepathlen + len > MAXPATHLEN) {
3175 globalfilepathlen = 0;
3176 goto globalfileerror;
3177 }
3178 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3179 globalfilepathlen += len;
3180 globalfilepath[globalfilepathlen++] = 0;
3181 }
3182 }
3183 globalfileerror:
3184 if (globalfilepathlen == 0)
3185 globalfilepath[0] = '\0';
3186
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003187 /* Load global server state in a tree */
3188 if (globalfilepathlen > 0) {
3189 errno = 0;
3190 f = fopen(globalfilepath, "r");
3191 if (errno)
3192 ha_warning("Can't open global server state file '%s': %s\n", globalfilepath, strerror(errno));
Vedran Furac5d486272019-10-16 14:49:38 +02003193 if (!f)
3194 goto out_load_server_state_in_tree;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003195
3196 global_file_version = srv_state_get_version(f);
3197 if (global_file_version == 0)
3198 goto out_load_server_state_in_tree;
3199
3200 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3201 line = NULL;
3202
3203 line = strdup(mybuf);
3204 if (line == NULL)
3205 continue;
3206
3207 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3208 if (params[0] == NULL)
Willy Tarreauca7a5af2019-12-20 17:26:27 +01003209 goto nextline;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003210
3211 /* bkname */
3212 bkname = params[1];
3213 /* srvname */
3214 srvname = params[3];
3215
3216 /* key */
3217 chunk_printf(&trash, "%s %s", bkname, srvname);
3218
3219 /* store line in tree */
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003220 st = calloc(1, sizeof(*st) + trash.data + 1);
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003221 if (st == NULL) {
3222 goto nextline;
3223 }
Willy Tarreau7d6a1fa2019-12-20 17:18:13 +01003224 memcpy(st->name_name.key, trash.area, trash.data + 1);
Willy Tarreaufd1aa012019-12-20 17:23:40 +01003225 if (ebst_insert(&state_file, &st->name_name) != &st->name_name) {
3226 /* this is a duplicate key, probably a hand-crafted file,
3227 * drop it!
3228 */
3229 goto nextline;
3230 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003231
3232 /* save line */
3233 st->line = line;
3234
3235 continue;
3236
3237 nextline:
3238 /* free up memory in case of error during the processing of the line */
3239 free(line);
3240 }
3241 }
3242 out_load_server_state_in_tree:
3243
3244 /* parse all proxies and load states form tree (global file) or from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003245 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003246 /* servers are only in backends */
3247 if (!(curproxy->cap & PR_CAP_BE))
3248 continue;
3249 fileopenerr = 0;
3250 filepath = NULL;
3251
3252 /* search server state file path and name */
3253 switch (curproxy->load_server_state_from_file) {
3254 /* read servers state from global file */
3255 case PR_SRV_STATE_FILE_GLOBAL:
3256 /* there was an error while generating global server state file path */
3257 if (globalfilepathlen == 0)
3258 continue;
3259 filepath = globalfilepath;
3260 fileopenerr = 1;
3261 break;
3262 /* this backend has its own file */
3263 case PR_SRV_STATE_FILE_LOCAL:
3264 localfilepathlen = 0;
3265 localfilepath[0] = '\0';
3266 len = 0;
3267 /* create the localfilepath variable */
3268 /* absolute path or no base directory provided */
3269 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3270 len = strlen(curproxy->server_state_file_name);
3271 if (len > MAXPATHLEN) {
3272 localfilepathlen = 0;
3273 goto localfileerror;
3274 }
3275 memcpy(localfilepath, curproxy->server_state_file_name, len);
3276 localfilepath[len] = '\0';
3277 localfilepathlen = len;
3278 }
3279 else if (global.server_state_base) {
3280 len = strlen(global.server_state_base);
3281 localfilepathlen += len;
3282
3283 if (localfilepathlen > MAXPATHLEN) {
3284 localfilepathlen = 0;
3285 goto localfileerror;
3286 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003287 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003288 localfilepath[localfilepathlen] = 0;
3289
3290 /* append a slash if needed */
3291 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3292 if (localfilepathlen + 1 > MAXPATHLEN) {
3293 localfilepathlen = 0;
3294 goto localfileerror;
3295 }
3296 localfilepath[localfilepathlen++] = '/';
3297 }
3298
3299 len = strlen(curproxy->server_state_file_name);
3300 if (localfilepathlen + len > MAXPATHLEN) {
3301 localfilepathlen = 0;
3302 goto localfileerror;
3303 }
3304 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3305 localfilepathlen += len;
3306 localfilepath[localfilepathlen++] = 0;
3307 }
3308 filepath = localfilepath;
3309 localfileerror:
3310 if (localfilepathlen == 0)
3311 localfilepath[0] = '\0';
3312
3313 break;
3314 case PR_SRV_STATE_FILE_NONE:
3315 default:
3316 continue;
3317 }
3318
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003319 /* when global file is used, we get data from the tree
3320 * Note that in such case we don't check backend name neither uuid.
3321 * Backend name can't be wrong since it's used as a key to retrieve the server state
3322 * line from the tree.
3323 */
3324 if (curproxy->load_server_state_from_file == PR_SRV_STATE_FILE_GLOBAL) {
3325 struct server *srv = curproxy->srv;
3326 while (srv) {
3327 struct ebmb_node *node;
3328 struct state_line *st;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003329
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003330 chunk_printf(&trash, "%s %s", curproxy->id, srv->id);
3331 node = ebst_lookup(&state_file, trash.area);
3332 if (!node)
3333 goto next;
Dragan Dosencf4fb032015-11-04 23:03:26 +01003334
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003335 st = container_of(node, struct state_line, name_name);
3336 memcpy(mybuf, st->line, strlen(st->line));
3337 mybuf[strlen(st->line)] = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003338
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003339 srv_state_parse_line(mybuf, global_file_version, params, srv_params);
3340 if (params[0] == NULL)
3341 goto next;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003342
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003343 srv_update_state(srv, global_file_version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003344
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003345 next:
3346 srv = srv->next;
3347 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003348
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003349 continue; /* next proxy in list */
3350 }
3351 else {
3352 /* load 'local' state file */
3353 errno = 0;
3354 f = fopen(filepath, "r");
3355 if (errno && fileopenerr)
3356 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3357 if (!f)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003358 continue;
3359
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003360 mybuf[0] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003361
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003362 /* first character of first line of the file must contain the version of the export */
3363 version = srv_state_get_version(f);
3364 if (version == 0) {
3365 ha_warning("Can't get version of the server state file '%s'\n", filepath);
3366 goto fileclose;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003367 }
3368
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003369 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3370 int bk_f_forced_id = 0;
3371 int check_id = 0;
3372 int check_name = 0;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003373
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003374 srv_state_parse_line(mybuf, version, params, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003375
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003376 if (params[0] == NULL) {
3377 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003378 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003379
3380 /* if line is incomplete line, then ignore it.
3381 * otherwise, update useful flags */
3382 switch (version) {
3383 case 1:
3384 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3385 check_id = (atoi(params[0]) == curproxy->uuid);
3386 check_name = (strcmp(curproxy->id, params[1]) == 0);
3387 break;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003388 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003389
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003390 bk = curproxy;
3391
3392 /* if backend can't be found, let's continue */
3393 if (!check_id && !check_name)
3394 continue;
3395 else if (!check_id && check_name) {
3396 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3397 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3398 }
3399 else if (check_id && !check_name) {
3400 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3401 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3402 /* if name doesn't match, we still want to update curproxy if the backend id
3403 * was forced in previous the previous configuration */
3404 if (!bk_f_forced_id)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003405 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003406 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003407
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003408 /* look for the server by its name: param[3] */
3409 srv = server_find_best_match(bk, params[3], 0, NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003410
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003411 if (!srv) {
3412 /* if no server found, then warning and continue with next line */
3413 ha_warning("can't find server '%s' in backend '%s'\n",
3414 params[3], params[1]);
3415 send_log(bk, LOG_NOTICE, "can't find server '%s' in backend '%s'\n",
3416 params[3], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417 continue;
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003418 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003419
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003420 /* now we can proceed with server's state update */
3421 srv_update_state(srv, version, srv_params);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003422 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003424fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003425 fclose(f);
3426 }
Baptiste Assmannda29fe22019-06-13 13:24:29 +02003427
3428 /* now free memory allocated for the tree */
3429 for (node = ebmb_first(&state_file), next_node = node ? ebmb_next(node) : NULL;
3430 node;
3431 node = next_node, next_node = node ? ebmb_next(node) : NULL) {
3432 st = container_of(node, struct state_line, name_name);
3433 ebmb_delete(&st->name_name);
3434 free(st->line);
3435 free(st);
3436 }
3437
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003438}
3439
Simon Horman7d09b9a2013-02-12 10:45:51 +09003440/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003441 * update a server's current IP address.
3442 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3443 * ip is in network format.
3444 * updater is a string which contains an information about the requester of the update.
3445 * updater is used if not NULL.
3446 *
3447 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003448 *
3449 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003450 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003451int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003452{
3453 /* generates a log line and a warning on stderr */
3454 if (1) {
3455 /* book enough space for both IPv4 and IPv6 */
3456 char oldip[INET6_ADDRSTRLEN];
3457 char newip[INET6_ADDRSTRLEN];
3458
3459 memset(oldip, '\0', INET6_ADDRSTRLEN);
3460 memset(newip, '\0', INET6_ADDRSTRLEN);
3461
3462 /* copy old IP address in a string */
3463 switch (s->addr.ss_family) {
3464 case AF_INET:
3465 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3466 break;
3467 case AF_INET6:
3468 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3469 break;
3470 };
3471
3472 /* copy new IP address in a string */
3473 switch (ip_sin_family) {
3474 case AF_INET:
3475 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3476 break;
3477 case AF_INET6:
3478 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3479 break;
3480 };
3481
3482 /* save log line into a buffer */
3483 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3484 s->proxy->id, s->id, oldip, newip, updater);
3485
3486 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003487 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003488
3489 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003490 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003491 }
3492
3493 /* save the new IP family */
3494 s->addr.ss_family = ip_sin_family;
3495 /* save the new IP address */
3496 switch (ip_sin_family) {
3497 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003498 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003499 break;
3500 case AF_INET6:
3501 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3502 break;
3503 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003504 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003505
3506 return 0;
3507}
3508
3509/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003510 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3511 *
3512 * Caller can pass its name through <updater> to get it integrated in the response message
3513 * returned by the function.
3514 *
3515 * The function first does the following, in that order:
3516 * - validates the new addr and/or port
3517 * - checks if an update is required (new IP or port is different than current ones)
3518 * - checks the update is allowed:
3519 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3520 * - allow all changes if no CHECKS are configured
3521 * - if CHECK is configured:
3522 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3523 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3524 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003525 *
3526 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003527 */
3528const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3529{
3530 struct sockaddr_storage sa;
3531 int ret, port_change_required;
3532 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003533 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003534 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003535 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003536
3537 msg = get_trash_chunk();
3538 chunk_reset(msg);
3539
3540 if (addr) {
3541 memset(&sa, 0, sizeof(struct sockaddr_storage));
3542 if (str2ip2(addr, &sa, 0) == NULL) {
3543 chunk_printf(msg, "Invalid addr '%s'", addr);
3544 goto out;
3545 }
3546
3547 /* changes are allowed on AF_INET* families only */
3548 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3549 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3550 goto out;
3551 }
3552
3553 /* collecting data currently setup */
3554 memset(current_addr, '\0', sizeof(current_addr));
3555 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3556 /* changes are allowed on AF_INET* families only */
3557 if ((ret != AF_INET) && (ret != AF_INET6)) {
3558 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3559 goto out;
3560 }
3561
3562 /* applying ADDR changes if required and allowed
3563 * ipcmp returns 0 when both ADDR are the same
3564 */
3565 if (ipcmp(&s->addr, &sa) == 0) {
3566 chunk_appendf(msg, "no need to change the addr");
3567 goto port;
3568 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003569 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003570 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003571
3572 /* we also need to update check's ADDR only if it uses the server's one */
3573 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003574 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003575 }
3576
3577 /* we also need to update agent ADDR only if it use the server's one */
3578 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003579 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003580 }
3581
3582 /* update report for caller */
3583 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3584 }
3585
3586 port:
3587 if (port) {
3588 char sign = '\0';
3589 char *endptr;
3590
3591 if (addr)
3592 chunk_appendf(msg, ", ");
3593
3594 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003595 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003596
3597 /* check if PORT change is required */
3598 port_change_required = 0;
3599
3600 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003601 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003602 new_port = strtol(port, &endptr, 10);
3603 if ((errno != 0) || (port == endptr)) {
3604 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3605 goto out;
3606 }
3607
3608 /* check if caller triggers a port mapped or offset */
3609 if (sign == '-' || (sign == '+')) {
3610 /* check if server currently uses port map */
3611 if (!(s->flags & SRV_F_MAPPORTS)) {
3612 /* switch from fixed port to port map mandatorily triggers
3613 * a port change */
3614 port_change_required = 1;
3615 /* check is configured
3616 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3617 * prevent PORT change if check doesn't have it's dedicated port while switching
3618 * to port mapping */
3619 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3620 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.");
3621 goto out;
3622 }
3623 }
3624 /* we're already using port maps */
3625 else {
3626 port_change_required = current_port != new_port;
3627 }
3628 }
3629 /* fixed port */
3630 else {
3631 port_change_required = current_port != new_port;
3632 }
3633
3634 /* applying PORT changes if required and update response message */
3635 if (port_change_required) {
3636 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003637 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003638 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003639
3640 /* prepare message */
3641 chunk_appendf(msg, "port changed from '");
3642 if (s->flags & SRV_F_MAPPORTS)
3643 chunk_appendf(msg, "+");
3644 chunk_appendf(msg, "%d' to '", current_port);
3645
3646 if (sign == '-') {
3647 s->flags |= SRV_F_MAPPORTS;
3648 chunk_appendf(msg, "%c", sign);
3649 /* just use for result output */
3650 new_port = -new_port;
3651 }
3652 else if (sign == '+') {
3653 s->flags |= SRV_F_MAPPORTS;
3654 chunk_appendf(msg, "%c", sign);
3655 }
3656 else {
3657 s->flags &= ~SRV_F_MAPPORTS;
3658 }
3659
3660 chunk_appendf(msg, "%d'", new_port);
3661
3662 /* we also need to update health checks port only if it uses server's realport */
3663 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3664 s->check.port = new_port;
3665 }
3666 }
3667 else {
3668 chunk_appendf(msg, "no need to change the port");
3669 }
3670 }
3671
3672out:
William Dauchy6318d332020-05-02 21:52:36 +02003673 if (changed) {
3674 /* force connection cleanup on the given server */
3675 srv_cleanup_connections(s);
Olivier Houchard4e694042017-03-14 20:01:29 +01003676 srv_set_dyncookie(s);
William Dauchy6318d332020-05-02 21:52:36 +02003677 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003678 if (updater)
3679 chunk_appendf(msg, " by '%s'", updater);
3680 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003681 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003682}
3683
3684
3685/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003686 * update server status based on result of name resolution
3687 * returns:
3688 * 0 if server status is updated
3689 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003690 *
3691 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003692 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003693int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003694{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003695 struct dns_resolvers *resolvers = s->resolvers;
3696 struct dns_resolution *resolution = s->dns_requester->resolution;
3697 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003698
3699 switch (resolution->status) {
3700 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003701 /* status when HAProxy has just (re)started.
3702 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003703 break;
3704
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003705 case RSLV_STATUS_VALID:
3706 /*
3707 * resume health checks
3708 * server will be turned back on if health check is safe
3709 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003710 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003711 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003712 return 1;
3713 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3714 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003715 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003716 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003717
Emeric Brun52a91d32017-08-31 14:41:55 +02003718 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003719 return 1;
3720 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3721 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3722 s->proxy->id, s->id);
3723
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003724 ha_warning("%s.\n", trash.area);
3725 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003726 return 0;
3727
3728 case RSLV_STATUS_NX:
3729 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003730 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3731 if (!tick_is_expired(exp, now_ms))
3732 break;
3733
3734 if (s->next_admin & SRV_ADMF_RMAINT)
3735 return 1;
3736 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3737 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003738
3739 case RSLV_STATUS_TIMEOUT:
3740 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003741 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3742 if (!tick_is_expired(exp, now_ms))
3743 break;
3744
3745 if (s->next_admin & SRV_ADMF_RMAINT)
3746 return 1;
3747 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3748 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003749
3750 case RSLV_STATUS_REFUSED:
3751 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003752 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3753 if (!tick_is_expired(exp, now_ms))
3754 break;
3755
3756 if (s->next_admin & SRV_ADMF_RMAINT)
3757 return 1;
3758 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3759 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003760
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003761 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003762 /* stop server if resolution failed for a long enough period */
3763 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3764 if (!tick_is_expired(exp, now_ms))
3765 break;
3766
3767 if (s->next_admin & SRV_ADMF_RMAINT)
3768 return 1;
3769 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3770 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003771 }
3772
3773 return 1;
3774}
3775
3776/*
3777 * Server Name Resolution valid response callback
3778 * It expects:
3779 * - <nameserver>: the name server which answered the valid response
3780 * - <response>: buffer containing a valid DNS response
3781 * - <response_len>: size of <response>
3782 * It performs the following actions:
3783 * - ignore response if current ip found and server family not met
3784 * - update with first new ip found if family is met and current IP is not found
3785 * returns:
3786 * 0 on error
3787 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003788 *
3789 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003790 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003791int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003793 struct server *s = NULL;
3794 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003795 void *serverip, *firstip;
3796 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003797 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003798 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003799 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003800
Christopher Faulet67957bd2017-09-27 11:00:59 +02003801 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003802 if (!s)
3803 return 1;
3804
Christopher Faulet67957bd2017-09-27 11:00:59 +02003805 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003806
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003807 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 firstip = NULL; /* pointer to the first valid response found */
3809 /* it will be used as the new IP if a change is required */
3810 firstip_sin_family = AF_UNSPEC;
3811 serverip = NULL; /* current server IP address */
3812
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813 /* initializing server IP pointer */
3814 server_sin_family = s->addr.ss_family;
3815 switch (server_sin_family) {
3816 case AF_INET:
3817 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3818 break;
3819
3820 case AF_INET6:
3821 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3822 break;
3823
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003824 case AF_UNSPEC:
3825 break;
3826
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003827 default:
3828 goto invalid;
3829 }
3830
Baptiste Assmann729c9012017-05-22 15:13:10 +02003831 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003832 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003833 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003834
3835 switch (ret) {
3836 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003837 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003838
3839 case DNS_UPD_SRVIP_NOT_FOUND:
3840 goto save_ip;
3841
3842 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003843 goto invalid;
3844
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003845 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003846 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003847 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003848
Baptiste Assmannfad03182015-10-28 02:03:32 +01003849 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003850 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003851 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003852 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003853
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003854 default:
3855 goto invalid;
3856
3857 }
3858
3859 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003860 if (nameserver) {
3861 nameserver->counters.update++;
3862 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003864 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003865 else
3866 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003867 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003868
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003869 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003870 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003871 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003872
3873 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003874 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003875 nameserver->counters.invalid++;
3876 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003877 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003878 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003879 return 0;
3880}
3881
3882/*
3883 * Server Name Resolution error management callback
3884 * returns:
3885 * 0 on error
3886 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003887 *
3888 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003889 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003890int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003891{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003892 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003893
Christopher Faulet67957bd2017-09-27 11:00:59 +02003894 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003895 if (!s)
3896 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003897 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003898 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003899 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003900 return 1;
3901}
3902
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003903/*
3904 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003905 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003906 * It returns a pointer to the first server found or NULL if <ip> is not yet
3907 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003908 *
3909 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003910 */
3911struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3912{
3913 struct server *tmpsrv;
3914 struct proxy *be;
3915
3916 if (!srv)
3917 return NULL;
3918
3919 be = srv->proxy;
3920 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003921 /* we found the current server is the same, ignore it */
3922 if (srv == tmpsrv)
3923 continue;
3924
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003925 /* We want to compare the IP in the record with the IP of the servers in the
3926 * same backend, only if:
3927 * * DNS resolution is enabled on the server
3928 * * the hostname used for the resolution by our server is the same than the
3929 * one used for the server found in the backend
3930 * * the server found in the backend is not our current server
3931 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003932 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003933 if ((tmpsrv->hostname_dn == NULL) ||
3934 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3935 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003936 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003937 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003938 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003939 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003940
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003941 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003942 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003943 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003944 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003945 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003946
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003947 /* At this point, we have 2 different servers using the same DNS hostname
3948 * for their respective resolution.
3949 */
3950 if (*ip_family == tmpsrv->addr.ss_family &&
3951 ((tmpsrv->addr.ss_family == AF_INET &&
3952 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3953 (tmpsrv->addr.ss_family == AF_INET6 &&
3954 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003955 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003956 return tmpsrv;
3957 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003958 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003959 }
3960
Emeric Brune9fd6b52017-11-02 17:20:39 +01003961
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003962 return NULL;
3963}
3964
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003965/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3966 * resolver. This is suited for initial address configuration. Returns 0 on
3967 * success otherwise a non-zero error code. In case of error, *err_code, if
3968 * not NULL, is filled up.
3969 */
3970int srv_set_addr_via_libc(struct server *srv, int *err_code)
3971{
3972 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003973 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003974 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003975 return 1;
3976 }
3977 return 0;
3978}
3979
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003980/* Set the server's FDQN (->hostname) from <hostname>.
3981 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003982 *
3983 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003984 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003985int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003986{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003987 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003988 char *hostname_dn;
3989 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003990
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003991 /* Note that the server lock is already held. */
3992 if (!srv->resolvers)
3993 return -1;
3994
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003995 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003996 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997 /* run time DNS resolution was not active for this server
3998 * and we can't enable it at run time for now.
3999 */
4000 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004001 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004002
4003 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004004 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004005 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004006 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4007 hostname_dn, trash.size);
4008 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004009 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004010
Christopher Faulet67957bd2017-09-27 11:00:59 +02004011 resolution = srv->dns_requester->resolution;
4012 if (resolution &&
4013 resolution->hostname_dn &&
4014 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004015 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004016
Christopher Faulet67957bd2017-09-27 11:00:59 +02004017 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004019 free(srv->hostname);
4020 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004021 srv->hostname = strdup(hostname);
4022 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004023 srv->hostname_dn_len = hostname_dn_len;
4024 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004025 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004026
Baptiste Assmann13a92322019-06-07 09:40:55 +02004027 if (srv->flags & SRV_F_NO_RESOLUTION)
4028 goto end;
4029
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004030 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004031 goto err;
4032
4033 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004034 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004035 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004036 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004037
4038 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004039 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004040 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004041 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004042}
4043
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004044/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4045 * from the state file. This is suited for initial address configuration.
4046 * Returns 0 on success otherwise a non-zero error code. In case of error,
4047 * *err_code, if not NULL, is filled up.
4048 */
4049static int srv_apply_lastaddr(struct server *srv, int *err_code)
4050{
4051 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4052 if (err_code)
4053 *err_code |= ERR_WARN;
4054 return 1;
4055 }
4056 return 0;
4057}
4058
Willy Tarreau25e51522016-11-04 15:10:17 +01004059/* returns 0 if no error, otherwise a combination of ERR_* flags */
4060static int srv_iterate_initaddr(struct server *srv)
4061{
4062 int return_code = 0;
4063 int err_code;
4064 unsigned int methods;
4065
4066 methods = srv->init_addr_methods;
4067 if (!methods) { // default to "last,libc"
4068 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4069 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4070 }
4071
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004072 /* "-dr" : always append "none" so that server addresses resolution
4073 * failures are silently ignored, this is convenient to validate some
4074 * configs out of their environment.
4075 */
4076 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4077 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4078
Willy Tarreau25e51522016-11-04 15:10:17 +01004079 while (methods) {
4080 err_code = 0;
4081 switch (srv_get_next_initaddr(&methods)) {
4082 case SRV_IADDR_LAST:
4083 if (!srv->lastaddr)
4084 continue;
4085 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004086 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004087 return_code |= err_code;
4088 break;
4089
4090 case SRV_IADDR_LIBC:
4091 if (!srv->hostname)
4092 continue;
4093 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004094 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004095 return_code |= err_code;
4096 break;
4097
Willy Tarreau37ebe122016-11-04 15:17:58 +01004098 case SRV_IADDR_NONE:
4099 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004100 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004101 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4102 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004103 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004104 return return_code;
4105
Willy Tarreau4310d362016-11-02 15:05:56 +01004106 case SRV_IADDR_IP:
4107 ipcpy(&srv->init_addr, &srv->addr);
4108 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004109 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4110 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004111 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004112 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004113
Willy Tarreau25e51522016-11-04 15:10:17 +01004114 default: /* unhandled method */
4115 break;
4116 }
4117 }
4118
4119 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004120 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004121 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4122 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004123 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004124 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004125 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4126 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004127
4128 return_code |= ERR_ALERT | ERR_FATAL;
4129 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004130out:
4131 srv_set_dyncookie(srv);
4132 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004133}
4134
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004135/*
4136 * This function parses all backends and all servers within each backend
4137 * and performs servers' addr resolution based on information provided by:
4138 * - configuration file
4139 * - server-state file (states provided by an 'old' haproxy process)
4140 *
4141 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4142 */
4143int srv_init_addr(void)
4144{
4145 struct proxy *curproxy;
4146 int return_code = 0;
4147
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004148 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004149 while (curproxy) {
4150 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004151
4152 /* servers are in backend only */
4153 if (!(curproxy->cap & PR_CAP_BE))
4154 goto srv_init_addr_next;
4155
Willy Tarreau25e51522016-11-04 15:10:17 +01004156 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004157 if (srv->hostname)
4158 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004159
4160 srv_init_addr_next:
4161 curproxy = curproxy->next;
4162 }
4163
4164 return return_code;
4165}
4166
Willy Tarreau46b7f532018-08-21 11:54:26 +02004167/*
4168 * Must be called with the server lock held.
4169 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004170const 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 +02004171{
4172
Willy Tarreau83061a82018-07-13 11:56:34 +02004173 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004174
4175 msg = get_trash_chunk();
4176 chunk_reset(msg);
4177
Olivier Houchard8da5f982017-08-04 18:35:36 +02004178 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004179 chunk_appendf(msg, "no need to change the FDQN");
4180 goto out;
4181 }
4182
4183 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4184 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4185 goto out;
4186 }
4187
4188 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4189 server->proxy->id, server->id, server->hostname, fqdn);
4190
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004191 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004192 chunk_reset(msg);
4193 chunk_appendf(msg, "could not update %s/%s FQDN",
4194 server->proxy->id, server->id);
4195 goto out;
4196 }
4197
4198 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004199 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004200
4201 out:
4202 if (updater)
4203 chunk_appendf(msg, " by '%s'", updater);
4204 chunk_appendf(msg, "\n");
4205
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004206 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004207}
4208
4209
Willy Tarreau21b069d2016-11-23 17:15:08 +01004210/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4211 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004212 * 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 +01004213 * used for CLI commands requiring a server name.
4214 * Important: the <arg> is modified to remove the '/'.
4215 */
4216struct server *cli_find_server(struct appctx *appctx, char *arg)
4217{
4218 struct proxy *px;
4219 struct server *sv;
4220 char *line;
4221
4222 /* split "backend/server" and make <line> point to server */
4223 for (line = arg; *line; line++)
4224 if (*line == '/') {
4225 *line++ = '\0';
4226 break;
4227 }
4228
4229 if (!*line || !*arg) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004230 cli_err(appctx, "Require 'backend/server'.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004231 return NULL;
4232 }
4233
4234 if (!get_backend_server(arg, line, &px, &sv)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004235 cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004236 return NULL;
4237 }
4238
4239 if (px->state == PR_STSTOPPED) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004240 cli_err(appctx, "Proxy is disabled.\n");
Willy Tarreau21b069d2016-11-23 17:15:08 +01004241 return NULL;
4242 }
4243
4244 return sv;
4245}
4246
William Lallemand222baf22016-11-19 02:00:33 +01004247
Willy Tarreau46b7f532018-08-21 11:54:26 +02004248/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004249static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004250{
4251 struct server *sv;
4252 const char *warning;
4253
4254 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4255 return 1;
4256
4257 sv = cli_find_server(appctx, args[2]);
4258 if (!sv)
4259 return 1;
4260
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004261 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004262
William Lallemand222baf22016-11-19 02:00:33 +01004263 if (strcmp(args[3], "weight") == 0) {
4264 warning = server_parse_weight_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004265 if (warning)
4266 cli_err(appctx, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004267 }
4268 else if (strcmp(args[3], "state") == 0) {
4269 if (strcmp(args[4], "ready") == 0)
4270 srv_adm_set_ready(sv);
4271 else if (strcmp(args[4], "drain") == 0)
4272 srv_adm_set_drain(sv);
4273 else if (strcmp(args[4], "maint") == 0)
4274 srv_adm_set_maint(sv);
Willy Tarreau9d008692019-08-09 11:21:01 +02004275 else
4276 cli_err(appctx, "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004277 }
4278 else if (strcmp(args[3], "health") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004279 if (sv->track)
4280 cli_err(appctx, "cannot change health on a tracking server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004281 else if (strcmp(args[4], "up") == 0) {
4282 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004283 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004284 }
4285 else if (strcmp(args[4], "stopping") == 0) {
4286 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004287 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004288 }
4289 else if (strcmp(args[4], "down") == 0) {
4290 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004291 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004292 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004293 else
4294 cli_err(appctx, "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004295 }
4296 else if (strcmp(args[3], "agent") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004297 if (!(sv->agent.state & CHK_ST_ENABLED))
4298 cli_err(appctx, "agent checks are not enabled on this server.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004299 else if (strcmp(args[4], "up") == 0) {
4300 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004301 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004302 }
4303 else if (strcmp(args[4], "down") == 0) {
4304 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004305 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004306 }
Willy Tarreau9d008692019-08-09 11:21:01 +02004307 else
4308 cli_err(appctx, "'set server <srv> agent' expects 'up' or 'down'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004309 }
Misiek2da082d2017-01-09 09:40:42 +01004310 else if (strcmp(args[3], "agent-addr") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004311 if (!(sv->agent.state & CHK_ST_ENABLED))
4312 cli_err(appctx, "agent checks are not enabled on this server.\n");
4313 else if (str2ip(args[4], &sv->agent.addr) == NULL)
4314 cli_err(appctx, "incorrect addr address given for agent.\n");
Misiek2da082d2017-01-09 09:40:42 +01004315 }
4316 else if (strcmp(args[3], "agent-send") == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004317 if (!(sv->agent.state & CHK_ST_ENABLED))
4318 cli_err(appctx, "agent checks are not enabled on this server.\n");
4319 else {
Christopher Faulet0ae3d1d2020-04-06 17:54:24 +02004320 if (!set_srv_agent_send(sv, args[4]))
Willy Tarreau9d008692019-08-09 11:21:01 +02004321 cli_err(appctx, "cannot allocate memory for new string.\n");
Misiek2da082d2017-01-09 09:40:42 +01004322 }
4323 }
William Lallemand222baf22016-11-19 02:00:33 +01004324 else if (strcmp(args[3], "check-port") == 0) {
4325 int i = 0;
4326 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004327 cli_err(appctx, "'set server <srv> check-port' expects an integer as argument.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004328 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004329 }
4330 if ((i < 0) || (i > 65535)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004331 cli_err(appctx, "provided port is not valid.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004332 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004333 }
4334 /* prevent the update of port to 0 if MAPPORTS are in use */
4335 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004336 cli_err(appctx, "can't unset 'port' since MAPPORTS is in use.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004337 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004338 }
4339 sv->check.port = i;
Willy Tarreau9d008692019-08-09 11:21:01 +02004340 cli_msg(appctx, LOG_NOTICE, "health check port updated.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004341 }
4342 else if (strcmp(args[3], "addr") == 0) {
4343 char *addr = NULL;
4344 char *port = NULL;
4345 if (strlen(args[4]) == 0) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004346 cli_err(appctx, "set server <b>/<s> addr requires an address and optionally a port.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004347 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004348 }
4349 else {
4350 addr = args[4];
4351 }
4352 if (strcmp(args[5], "port") == 0) {
4353 port = args[6];
4354 }
4355 warning = update_server_addr_port(sv, addr, port, "stats socket command");
Willy Tarreau9d008692019-08-09 11:21:01 +02004356 if (warning)
4357 cli_msg(appctx, LOG_WARNING, warning);
William Lallemand222baf22016-11-19 02:00:33 +01004358 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4359 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004360 else if (strcmp(args[3], "fqdn") == 0) {
4361 if (!*args[4]) {
Willy Tarreau9d008692019-08-09 11:21:01 +02004362 cli_err(appctx, "set server <b>/<s> fqdn requires a FQDN.\n");
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004363 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004364 }
Baptiste Assmann13a92322019-06-07 09:40:55 +02004365 /* ensure runtime resolver will process this new fqdn */
4366 if (sv->flags & SRV_F_NO_RESOLUTION) {
4367 sv->flags &= ~SRV_F_NO_RESOLUTION;
4368 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004369 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Willy Tarreau9d008692019-08-09 11:21:01 +02004370 if (warning)
4371 cli_msg(appctx, LOG_WARNING, warning);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004372 }
William Lallemand222baf22016-11-19 02:00:33 +01004373 else {
Willy Tarreau9d008692019-08-09 11:21:01 +02004374 cli_err(appctx,
4375 "'set server <srv>' only supports 'agent', 'health', 'state',"
4376 " 'weight', 'addr', 'fqdn' and 'check-port'.\n");
William Lallemand222baf22016-11-19 02:00:33 +01004377 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004378 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004379 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004380 return 1;
4381}
4382
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004383static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004384{
4385 struct stream_interface *si = appctx->owner;
4386 struct proxy *px;
4387 struct server *sv;
4388 char *line;
4389
4390
4391 /* split "backend/server" and make <line> point to server */
4392 for (line = args[2]; *line; line++)
4393 if (*line == '/') {
4394 *line++ = '\0';
4395 break;
4396 }
4397
Willy Tarreau9d008692019-08-09 11:21:01 +02004398 if (!*line)
4399 return cli_err(appctx, "Require 'backend/server'.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004400
Willy Tarreau9d008692019-08-09 11:21:01 +02004401 if (!get_backend_server(args[2], line, &px, &sv))
4402 return cli_err(appctx, px ? "No such server.\n" : "No such backend.\n");
William Lallemand6b160942016-11-22 12:34:35 +01004403
4404 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004405 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4406 sv->iweight);
4407 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004408 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004409 return 0;
4410 }
William Lallemand6b160942016-11-22 12:34:35 +01004411 return 1;
4412}
4413
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004414/* Parse a "set weight" command.
4415 *
4416 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004417 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004418static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004419{
4420 struct server *sv;
4421 const char *warning;
4422
4423 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4424 return 1;
4425
4426 sv = cli_find_server(appctx, args[2]);
4427 if (!sv)
4428 return 1;
4429
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004430 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4431
William Lallemand6b160942016-11-22 12:34:35 +01004432 warning = server_parse_weight_change_request(sv, args[3]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004433 if (warning)
4434 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004435
4436 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4437
William Lallemand6b160942016-11-22 12:34:35 +01004438 return 1;
4439}
4440
Willy Tarreau46b7f532018-08-21 11:54:26 +02004441/* parse a "set maxconn server" command. It always returns 1.
4442 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004443 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004444 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004445static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004446{
4447 struct server *sv;
4448 const char *warning;
4449
4450 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4451 return 1;
4452
4453 sv = cli_find_server(appctx, args[3]);
4454 if (!sv)
4455 return 1;
4456
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004457 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4458
Willy Tarreaub8026272016-11-23 11:26:56 +01004459 warning = server_parse_maxconn_change_request(sv, args[4]);
Willy Tarreau9d008692019-08-09 11:21:01 +02004460 if (warning)
4461 cli_err(appctx, warning);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004462
4463 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4464
Willy Tarreaub8026272016-11-23 11:26:56 +01004465 return 1;
4466}
William Lallemand6b160942016-11-22 12:34:35 +01004467
Willy Tarreau46b7f532018-08-21 11:54:26 +02004468/* parse a "disable agent" command. It always returns 1.
4469 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004470 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004471 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004472static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004473{
4474 struct server *sv;
4475
4476 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4477 return 1;
4478
4479 sv = cli_find_server(appctx, args[2]);
4480 if (!sv)
4481 return 1;
4482
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004483 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004484 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004485 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004486 return 1;
4487}
4488
Willy Tarreau46b7f532018-08-21 11:54:26 +02004489/* parse a "disable health" command. It always returns 1.
4490 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004491 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004492 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004493static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004494{
4495 struct server *sv;
4496
4497 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4498 return 1;
4499
4500 sv = cli_find_server(appctx, args[2]);
4501 if (!sv)
4502 return 1;
4503
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004504 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004505 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004506 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004507 return 1;
4508}
4509
Willy Tarreau46b7f532018-08-21 11:54:26 +02004510/* parse a "disable server" command. It always returns 1.
4511 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004512 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004513 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004514static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004515{
4516 struct server *sv;
4517
4518 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4519 return 1;
4520
4521 sv = cli_find_server(appctx, args[2]);
4522 if (!sv)
4523 return 1;
4524
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004525 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004526 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004527 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004528 return 1;
4529}
4530
Willy Tarreau46b7f532018-08-21 11:54:26 +02004531/* parse a "enable agent" command. It always returns 1.
4532 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004533 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004534 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004535static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004536{
4537 struct server *sv;
4538
4539 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4540 return 1;
4541
4542 sv = cli_find_server(appctx, args[2]);
4543 if (!sv)
4544 return 1;
4545
Willy Tarreau9d008692019-08-09 11:21:01 +02004546 if (!(sv->agent.state & CHK_ST_CONFIGURED))
4547 return cli_err(appctx, "Agent was not configured on this server, cannot enable.\n");
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004548
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004549 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004550 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004551 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004552 return 1;
4553}
4554
Willy Tarreau46b7f532018-08-21 11:54:26 +02004555/* parse a "enable health" command. It always returns 1.
4556 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004557 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004558 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004559static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004560{
4561 struct server *sv;
4562
4563 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4564 return 1;
4565
4566 sv = cli_find_server(appctx, args[2]);
4567 if (!sv)
4568 return 1;
4569
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004570 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004571 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004572 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004573 return 1;
4574}
4575
Willy Tarreau46b7f532018-08-21 11:54:26 +02004576/* parse a "enable server" command. It always returns 1.
4577 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004578 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004579 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004580static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004581{
4582 struct server *sv;
4583
4584 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4585 return 1;
4586
4587 sv = cli_find_server(appctx, args[2]);
4588 if (!sv)
4589 return 1;
4590
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004591 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004592 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004593 if (!(sv->flags & SRV_F_COOKIESET)
4594 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4595 sv->cookie)
4596 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004597 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004598 return 1;
4599}
4600
William Lallemand222baf22016-11-19 02:00:33 +01004601/* register cli keywords */
4602static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004603 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004604 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004605 { { "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 +01004606 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004607 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004608 { { "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 +01004609 { { "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 +01004610 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004611 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4612 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4613
William Lallemand222baf22016-11-19 02:00:33 +01004614 {{},}
4615}};
4616
Willy Tarreau0108d902018-11-25 19:14:37 +01004617INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004618
Emeric Brun64cc49c2017-10-03 14:46:45 +02004619/*
4620 * This function applies server's status changes, it is
4621 * is designed to be called asynchronously.
4622 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004623 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004624 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004625static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004626{
4627 struct check *check = &s->check;
4628 int xferred;
4629 struct proxy *px = s->proxy;
4630 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4631 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4632 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004633 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004634
Emeric Brun64cc49c2017-10-03 14:46:45 +02004635 /* If currently main is not set we try to apply pending state changes */
4636 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4637 int next_admin;
4638
4639 /* Backup next admin */
4640 next_admin = s->next_admin;
4641
4642 /* restore current admin state */
4643 s->next_admin = s->cur_admin;
4644
4645 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4646 s->last_change = now.tv_sec;
4647 if (s->proxy->lbprm.set_server_status_down)
4648 s->proxy->lbprm.set_server_status_down(s);
4649
4650 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4651 srv_shutdown_streams(s, SF_ERR_DOWN);
4652
4653 /* we might have streams queued on this server and waiting for
4654 * a connection. Those which are redispatchable will be queued
4655 * to another server or to the proxy itself.
4656 */
4657 xferred = pendconn_redistribute(s);
4658
4659 tmptrash = alloc_trash_chunk();
4660 if (tmptrash) {
4661 chunk_printf(tmptrash,
4662 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4663 s->proxy->id, s->id);
4664
Emeric Brun5a133512017-10-19 14:42:30 +02004665 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004666 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004667
4668 /* we don't send an alert if the server was previously paused */
4669 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004670 send_log(s->proxy, log_level, "%s.\n",
4671 tmptrash->area);
4672 send_email_alert(s, log_level, "%s",
4673 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004674 free_trash_chunk(tmptrash);
4675 tmptrash = NULL;
4676 }
4677 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4678 set_backend_down(s->proxy);
4679
4680 s->counters.down_trans++;
4681 }
4682 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4683 s->last_change = now.tv_sec;
4684 if (s->proxy->lbprm.set_server_status_down)
4685 s->proxy->lbprm.set_server_status_down(s);
4686
4687 /* we might have streams queued on this server and waiting for
4688 * a connection. Those which are redispatchable will be queued
4689 * to another server or to the proxy itself.
4690 */
4691 xferred = pendconn_redistribute(s);
4692
4693 tmptrash = alloc_trash_chunk();
4694 if (tmptrash) {
4695 chunk_printf(tmptrash,
4696 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4697 s->proxy->id, s->id);
4698
Emeric Brun5a133512017-10-19 14:42:30 +02004699 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004700
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004701 ha_warning("%s.\n", tmptrash->area);
4702 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4703 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004704 free_trash_chunk(tmptrash);
4705 tmptrash = NULL;
4706 }
4707
4708 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4709 set_backend_down(s->proxy);
4710 }
4711 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4712 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4713 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4714 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4715 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4716 s->proxy->last_change = now.tv_sec;
4717 }
4718
4719 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4720 s->down_time += now.tv_sec - s->last_change;
4721
4722 s->last_change = now.tv_sec;
4723 if (s->next_state == SRV_ST_STARTING)
4724 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4725
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004726 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004727 /* now propagate the status change to any LB algorithms */
4728 if (px->lbprm.update_server_eweight)
4729 px->lbprm.update_server_eweight(s);
4730 else if (srv_willbe_usable(s)) {
4731 if (px->lbprm.set_server_status_up)
4732 px->lbprm.set_server_status_up(s);
4733 }
4734 else {
4735 if (px->lbprm.set_server_status_down)
4736 px->lbprm.set_server_status_down(s);
4737 }
4738
4739 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4740 * and it's not a backup server and its effective weight is > 0,
4741 * then it can accept new connections, so we shut down all streams
4742 * on all backup servers.
4743 */
4744 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4745 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4746 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4747
4748 /* check if we can handle some connections queued at the proxy. We
4749 * will take as many as we can handle.
4750 */
4751 xferred = pendconn_grab_from_px(s);
4752
4753 tmptrash = alloc_trash_chunk();
4754 if (tmptrash) {
4755 chunk_printf(tmptrash,
4756 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4757 s->proxy->id, s->id);
4758
Emeric Brun5a133512017-10-19 14:42:30 +02004759 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004760 ha_warning("%s.\n", tmptrash->area);
4761 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4762 tmptrash->area);
4763 send_email_alert(s, LOG_NOTICE, "%s",
4764 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004765 free_trash_chunk(tmptrash);
4766 tmptrash = NULL;
4767 }
4768
4769 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4770 set_backend_down(s->proxy);
4771 }
4772 else if (s->cur_eweight != s->next_eweight) {
4773 /* now propagate the status change to any LB algorithms */
4774 if (px->lbprm.update_server_eweight)
4775 px->lbprm.update_server_eweight(s);
4776 else if (srv_willbe_usable(s)) {
4777 if (px->lbprm.set_server_status_up)
4778 px->lbprm.set_server_status_up(s);
4779 }
4780 else {
4781 if (px->lbprm.set_server_status_down)
4782 px->lbprm.set_server_status_down(s);
4783 }
4784
4785 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4786 set_backend_down(s->proxy);
4787 }
4788
4789 s->next_admin = next_admin;
4790 }
4791
Emeric Brun5a133512017-10-19 14:42:30 +02004792 /* reset operational state change */
4793 *s->op_st_chg.reason = 0;
4794 s->op_st_chg.status = s->op_st_chg.code = -1;
4795 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004796
4797 /* Now we try to apply pending admin changes */
4798
4799 /* Maintenance must also disable health checks */
4800 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4801 if (s->check.state & CHK_ST_ENABLED) {
4802 s->check.state |= CHK_ST_PAUSED;
4803 check->health = 0;
4804 }
4805
4806 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004807 tmptrash = alloc_trash_chunk();
4808 if (tmptrash) {
4809 chunk_printf(tmptrash,
4810 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4811 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4812 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004813
Olivier Houchard796a2b32017-10-24 17:42:47 +02004814 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004815
Olivier Houchard796a2b32017-10-24 17:42:47 +02004816 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004817 ha_warning("%s.\n", tmptrash->area);
4818 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4819 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004820 }
4821 free_trash_chunk(tmptrash);
4822 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004823 }
Emeric Brun8f298292017-12-06 16:47:17 +01004824 /* commit new admin status */
4825
4826 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004827 }
4828 else { /* server was still running */
4829 check->health = 0; /* failure */
4830 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004831
4832 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004833 if (s->proxy->lbprm.set_server_status_down)
4834 s->proxy->lbprm.set_server_status_down(s);
4835
Emeric Brun64cc49c2017-10-03 14:46:45 +02004836 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4837 srv_shutdown_streams(s, SF_ERR_DOWN);
4838
William Dauchy6318d332020-05-02 21:52:36 +02004839 /* force connection cleanup on the given server */
4840 srv_cleanup_connections(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004841 /* we might have streams queued on this server and waiting for
4842 * a connection. Those which are redispatchable will be queued
4843 * to another server or to the proxy itself.
4844 */
4845 xferred = pendconn_redistribute(s);
4846
4847 tmptrash = alloc_trash_chunk();
4848 if (tmptrash) {
4849 chunk_printf(tmptrash,
4850 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4851 s->flags & SRV_F_BACKUP ? "Backup " : "",
4852 s->proxy->id, s->id,
4853 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4854
4855 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4856
4857 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004858 ha_warning("%s.\n", tmptrash->area);
4859 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4860 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004861 }
4862 free_trash_chunk(tmptrash);
4863 tmptrash = NULL;
4864 }
4865 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4866 set_backend_down(s->proxy);
4867
4868 s->counters.down_trans++;
4869 }
4870 }
4871 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4872 /* OK here we're leaving maintenance, we have many things to check,
4873 * because the server might possibly be coming back up depending on
4874 * its state. In practice, leaving maintenance means that we should
4875 * immediately turn to UP (more or less the slowstart) under the
4876 * following conditions :
4877 * - server is neither checked nor tracked
4878 * - server tracks another server which is not checked
4879 * - server tracks another server which is already up
4880 * Which sums up as something simpler :
4881 * "either the tracking server is up or the server's checks are disabled
4882 * or up". Otherwise we only re-enable health checks. There's a special
4883 * case associated to the stopping state which can be inherited. Note
4884 * that the server might still be in drain mode, which is naturally dealt
4885 * with by the lower level functions.
4886 */
4887
4888 if (s->check.state & CHK_ST_ENABLED) {
4889 s->check.state &= ~CHK_ST_PAUSED;
4890 check->health = check->rise; /* start OK but check immediately */
4891 }
4892
4893 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4894 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4895 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4896 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4897 s->next_state = SRV_ST_STOPPING;
4898 }
4899 else {
4900 s->next_state = SRV_ST_STARTING;
4901 if (s->slowstart > 0)
4902 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4903 else
4904 s->next_state = SRV_ST_RUNNING;
4905 }
4906
4907 }
4908
4909 tmptrash = alloc_trash_chunk();
4910 if (tmptrash) {
4911 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4912 chunk_printf(tmptrash,
4913 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4914 s->flags & SRV_F_BACKUP ? "Backup " : "",
4915 s->proxy->id, s->id,
4916 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4917 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4918 }
4919 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4920 chunk_printf(tmptrash,
4921 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4922 s->flags & SRV_F_BACKUP ? "Backup " : "",
4923 s->proxy->id, s->id, s->hostname,
4924 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4925 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4926 }
4927 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4928 chunk_printf(tmptrash,
4929 "%sServer %s/%s is %s/%s (leaving maintenance)",
4930 s->flags & SRV_F_BACKUP ? "Backup " : "",
4931 s->proxy->id, s->id,
4932 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4933 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4934 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004935 ha_warning("%s.\n", tmptrash->area);
4936 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4937 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004938 free_trash_chunk(tmptrash);
4939 tmptrash = NULL;
4940 }
4941
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004942 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004943 /* now propagate the status change to any LB algorithms */
4944 if (px->lbprm.update_server_eweight)
4945 px->lbprm.update_server_eweight(s);
4946 else if (srv_willbe_usable(s)) {
4947 if (px->lbprm.set_server_status_up)
4948 px->lbprm.set_server_status_up(s);
4949 }
4950 else {
4951 if (px->lbprm.set_server_status_down)
4952 px->lbprm.set_server_status_down(s);
4953 }
4954
4955 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4956 set_backend_down(s->proxy);
4957
Willy Tarreau6a78e612018-08-07 10:14:53 +02004958 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4959 * and it's not a backup server and its effective weight is > 0,
4960 * then it can accept new connections, so we shut down all streams
4961 * on all backup servers.
4962 */
4963 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4964 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4965 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4966
4967 /* check if we can handle some connections queued at the proxy. We
4968 * will take as many as we can handle.
4969 */
4970 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004971 }
4972 else if (s->next_admin & SRV_ADMF_MAINT) {
4973 /* remaining in maintenance mode, let's inform precisely about the
4974 * situation.
4975 */
4976 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4977 tmptrash = alloc_trash_chunk();
4978 if (tmptrash) {
4979 chunk_printf(tmptrash,
4980 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4981 s->flags & SRV_F_BACKUP ? "Backup " : "",
4982 s->proxy->id, s->id);
4983
4984 if (s->track) /* normally it's mandatory here */
4985 chunk_appendf(tmptrash, " via %s/%s",
4986 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004987 ha_warning("%s.\n", tmptrash->area);
4988 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4989 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004990 free_trash_chunk(tmptrash);
4991 tmptrash = NULL;
4992 }
4993 }
4994 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4995 tmptrash = alloc_trash_chunk();
4996 if (tmptrash) {
4997 chunk_printf(tmptrash,
4998 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4999 s->flags & SRV_F_BACKUP ? "Backup " : "",
5000 s->proxy->id, s->id, s->hostname);
5001
5002 if (s->track) /* normally it's mandatory here */
5003 chunk_appendf(tmptrash, " via %s/%s",
5004 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005005 ha_warning("%s.\n", tmptrash->area);
5006 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5007 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005008 free_trash_chunk(tmptrash);
5009 tmptrash = NULL;
5010 }
5011 }
5012 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5013 tmptrash = alloc_trash_chunk();
5014 if (tmptrash) {
5015 chunk_printf(tmptrash,
5016 "%sServer %s/%s remains in forced maintenance",
5017 s->flags & SRV_F_BACKUP ? "Backup " : "",
5018 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005019 ha_warning("%s.\n", tmptrash->area);
5020 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5021 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005022 free_trash_chunk(tmptrash);
5023 tmptrash = NULL;
5024 }
5025 }
5026 /* don't report anything when leaving drain mode and remaining in maintenance */
5027
5028 s->cur_admin = s->next_admin;
5029 }
5030
5031 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5032 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5033 /* drain state is applied only if not yet in maint */
5034
5035 s->last_change = now.tv_sec;
5036 if (px->lbprm.set_server_status_down)
5037 px->lbprm.set_server_status_down(s);
5038
5039 /* we might have streams queued on this server and waiting for
5040 * a connection. Those which are redispatchable will be queued
5041 * to another server or to the proxy itself.
5042 */
5043 xferred = pendconn_redistribute(s);
5044
5045 tmptrash = alloc_trash_chunk();
5046 if (tmptrash) {
5047 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5048 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5049 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5050
5051 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5052
5053 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005054 ha_warning("%s.\n", tmptrash->area);
5055 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5056 tmptrash->area);
5057 send_email_alert(s, LOG_NOTICE, "%s",
5058 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005059 }
5060 free_trash_chunk(tmptrash);
5061 tmptrash = NULL;
5062 }
5063
5064 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5065 set_backend_down(s->proxy);
5066 }
5067 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5068 /* OK completely leaving drain mode */
5069 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5070 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5071 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5072 s->proxy->last_change = now.tv_sec;
5073 }
5074
5075 if (s->last_change < now.tv_sec) // ignore negative times
5076 s->down_time += now.tv_sec - s->last_change;
5077 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005078 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005079
5080 tmptrash = alloc_trash_chunk();
5081 if (tmptrash) {
5082 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5083 chunk_printf(tmptrash,
5084 "%sServer %s/%s is %s (leaving forced drain)",
5085 s->flags & SRV_F_BACKUP ? "Backup " : "",
5086 s->proxy->id, s->id,
5087 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5088 }
5089 else {
5090 chunk_printf(tmptrash,
5091 "%sServer %s/%s is %s (leaving drain)",
5092 s->flags & SRV_F_BACKUP ? "Backup " : "",
5093 s->proxy->id, s->id,
5094 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5095 if (s->track) /* normally it's mandatory here */
5096 chunk_appendf(tmptrash, " via %s/%s",
5097 s->track->proxy->id, s->track->id);
5098 }
5099
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005100 ha_warning("%s.\n", tmptrash->area);
5101 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5102 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005103 free_trash_chunk(tmptrash);
5104 tmptrash = NULL;
5105 }
5106
5107 /* now propagate the status change to any LB algorithms */
5108 if (px->lbprm.update_server_eweight)
5109 px->lbprm.update_server_eweight(s);
5110 else if (srv_willbe_usable(s)) {
5111 if (px->lbprm.set_server_status_up)
5112 px->lbprm.set_server_status_up(s);
5113 }
5114 else {
5115 if (px->lbprm.set_server_status_down)
5116 px->lbprm.set_server_status_down(s);
5117 }
5118 }
5119 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5120 /* remaining in drain mode after removing one of its flags */
5121
5122 tmptrash = alloc_trash_chunk();
5123 if (tmptrash) {
5124 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5125 chunk_printf(tmptrash,
5126 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5127 s->flags & SRV_F_BACKUP ? "Backup " : "",
5128 s->proxy->id, s->id);
5129
5130 if (s->track) /* normally it's mandatory here */
5131 chunk_appendf(tmptrash, " via %s/%s",
5132 s->track->proxy->id, s->track->id);
5133 }
5134 else {
5135 chunk_printf(tmptrash,
5136 "%sServer %s/%s remains in forced drain mode",
5137 s->flags & SRV_F_BACKUP ? "Backup " : "",
5138 s->proxy->id, s->id);
5139 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005140 ha_warning("%s.\n", tmptrash->area);
5141 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5142 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005143 free_trash_chunk(tmptrash);
5144 tmptrash = NULL;
5145 }
5146
5147 /* commit new admin status */
5148
5149 s->cur_admin = s->next_admin;
5150 }
5151 }
5152
5153 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005154 *s->adm_st_chg_cause = 0;
5155}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005156
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005157struct task *srv_cleanup_toremove_connections(struct task *task, void *context, unsigned short state)
5158{
5159 struct connection *conn;
5160
Olivier Houchard859dc802019-08-08 15:47:21 +02005161 while ((conn = MT_LIST_POP(&toremove_connections[tid],
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005162 struct connection *, list)) != NULL) {
Christopher Faulet73c12072019-04-08 11:23:22 +02005163 conn->mux->destroy(conn->ctx);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005164 }
5165
5166 return task;
5167}
5168
William Dauchy6318d332020-05-02 21:52:36 +02005169/* cleanup connections for a given server
5170 * might be useful when going on forced maintenance or live changing ip/port
5171 */
William Dauchy707ad322020-05-04 13:52:40 +02005172static void srv_cleanup_connections(struct server *srv)
William Dauchy6318d332020-05-02 21:52:36 +02005173{
5174 struct connection *conn;
5175 int did_remove;
5176 int i;
5177 int j;
5178
5179 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5180 for (i = 0; i < global.nbthread; i++) {
5181 did_remove = 0;
5182 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
5183 for (j = 0; j < srv->curr_idle_conns; j++) {
5184 conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5185 if (!conn)
5186 conn = MT_LIST_POP(&srv->safe_conns[i],
5187 struct connection *, list);
5188 if (!conn)
5189 break;
5190 did_remove = 1;
5191 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
5192 }
5193 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
5194 if (did_remove)
5195 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5196 }
5197 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5198}
5199
Willy Tarreau980855b2019-02-07 14:59:29 +01005200struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005201{
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005202 struct server *srv;
5203 struct eb32_node *eb;
5204 int i;
5205 unsigned int next_wakeup;
5206 int need_wakeup = 0;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005207
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005208 HA_SPIN_LOCK(OTHER_LOCK, &idle_conn_srv_lock);
5209 while (1) {
5210 int srv_is_empty = 1;
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005211 int exceed_conns;
5212 int to_kill;
5213 int curr_idle;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005214
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005215 eb = eb32_lookup_ge(&idle_conn_srv, now_ms - TIMER_LOOK_BACK);
5216 if (!eb) {
5217 /* we might have reached the end of the tree, typically because
5218 * <now_ms> is in the first half and we're first scanning the last
5219 * half. Let's loop back to the beginning of the tree now.
5220 */
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005221
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005222 eb = eb32_first(&idle_conn_srv);
5223 if (likely(!eb))
5224 break;
5225 }
5226 if (tick_is_lt(now_ms, eb->key)) {
5227 /* timer not expired yet, revisit it later */
5228 next_wakeup = eb->key;
5229 need_wakeup = 1;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005230 break;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005231 }
5232 srv = eb32_entry(eb, struct server, idle_node);
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005233
5234 /* Calculate how many idle connections we want to kill :
5235 * we want to remove half the difference between the total
5236 * of established connections (used or idle) and the max
5237 * number of used connections.
5238 */
5239 curr_idle = srv->curr_idle_conns;
5240 if (curr_idle == 0)
5241 goto remove;
5242 exceed_conns = srv->curr_used_conns + curr_idle -
5243 srv->max_used_conns;
5244 exceed_conns = to_kill = exceed_conns / 2 + (exceed_conns & 1);
5245 srv->max_used_conns = srv->curr_used_conns;
5246
5247 for (i = 0; i < global.nbthread && to_kill > 0; i++) {
5248 int max_conn;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005249 int j;
5250 int did_remove = 0;
5251
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005252 max_conn = (exceed_conns * srv->curr_idle_thr[i]) /
5253 curr_idle + 1;
Olivier Houchard4be71902019-07-11 15:49:00 +02005254 HA_SPIN_LOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005255 for (j = 0; j < max_conn; j++) {
Olivier Houcharddc2f2752020-02-13 19:12:07 +01005256 struct connection *conn = MT_LIST_POP(&srv->idle_conns[i], struct connection *, list);
5257 if (!conn)
5258 conn = MT_LIST_POP(&srv->safe_conns[i],
5259 struct connection *, list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005260 if (!conn)
5261 break;
5262 did_remove = 1;
Olivier Houchard859dc802019-08-08 15:47:21 +02005263 MT_LIST_ADDQ(&toremove_connections[i], (struct mt_list *)&conn->list);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005264 }
Olivier Houchard4be71902019-07-11 15:49:00 +02005265 HA_SPIN_UNLOCK(OTHER_LOCK, &toremove_lock[i]);
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005266 if (did_remove && max_conn < srv->curr_idle_thr[i])
5267 srv_is_empty = 0;
5268 if (did_remove)
5269 task_wakeup(idle_conn_cleanup[i], TASK_WOKEN_OTHER);
5270 }
Olivier Houchard079cb9a2020-03-30 00:23:57 +02005271remove:
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005272 eb32_delete(&srv->idle_node);
5273 if (!srv_is_empty) {
5274 /* There are still more idle connections, add the
5275 * server back in the tree.
5276 */
5277 srv->idle_node.key = tick_add(srv->pool_purge_delay,
5278 now_ms);
5279 eb32_insert(&idle_conn_srv, &srv->idle_node);
5280 }
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005281 }
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005282 HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conn_srv_lock);
5283
5284 if (need_wakeup)
5285 task->expire = next_wakeup;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005286 else
5287 task->expire = TICK_ETERNITY;
Olivier Houchard9ea5d362019-02-14 18:29:09 +01005288
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005289 return task;
5290}
Olivier Houchard88698d92019-04-16 19:07:22 +02005291
5292/* config parser for global "tune.pool-{low,high}-fd-ratio" */
5293static int cfg_parse_pool_fd_ratio(char **args, int section_type, struct proxy *curpx,
5294 struct proxy *defpx, const char *file, int line,
5295 char **err)
5296{
5297 int arg = -1;
5298
5299 if (too_many_args(1, args, err, NULL))
5300 return -1;
5301
5302 if (*(args[1]) != 0)
5303 arg = atoi(args[1]);
5304
5305 if (arg < 0 || arg > 100) {
5306 memprintf(err, "'%s' expects an integer argument between 0 and 100.", args[0]);
5307 return -1;
5308 }
5309
5310 if (args[0][10] == 'h')
5311 global.tune.pool_high_ratio = arg;
5312 else
5313 global.tune.pool_low_ratio = arg;
5314 return 0;
5315}
5316
5317/* config keyword parsers */
5318static struct cfg_kw_list cfg_kws = {ILH, {
5319 { CFG_GLOBAL, "tune.pool-high-fd-ratio", cfg_parse_pool_fd_ratio },
5320 { CFG_GLOBAL, "tune.pool-low-fd-ratio", cfg_parse_pool_fd_ratio },
5321 { 0, NULL, NULL }
5322}};
5323
5324INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
5325
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005326/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005327 * Local variables:
5328 * c-indent-level: 8
5329 * c-basic-offset: 8
5330 * End:
5331 */