blob: 4068255e5c918f6e73406265f6ed6fdc2bfc4bc1 [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>
Willy Tarreau272adea2014-03-31 10:39:59 +020028#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010029#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020030#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010031#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020032
William Lallemand222baf22016-11-19 02:00:33 +010033#include <proto/applet.h>
34#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090035#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020036#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020037#include <proto/port_range.h>
38#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020039#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010040#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020041#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020042#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010043#include <proto/stream_interface.h>
44#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020045#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020046#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010047#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020048
Willy Tarreau3ff577e2018-08-02 11:48:52 +020049static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020050static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010051static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010052static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau21faa912012-10-10 08:27:36 +020054/* List head of all known server keywords */
55static struct srv_kw_list srv_keywords = {
56 .list = LIST_HEAD_INIT(srv_keywords.list)
57};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020058
Simon Hormana3608442013-11-01 16:46:15 +090059int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020060{
Emeric Brun52a91d32017-08-31 14:41:55 +020061 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062 return s->down_time;
63
64 return now.tv_sec - s->last_change + s->down_time;
65}
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050067int srv_lastsession(const struct server *s)
68{
69 if (s->counters.last_sess)
70 return now.tv_sec - s->counters.last_sess;
71
72 return -1;
73}
74
Simon Horman4a741432013-02-23 15:35:38 +090075int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020076{
Simon Horman4a741432013-02-23 15:35:38 +090077 const struct server *s = check->server;
78
Willy Tarreauff5ae352013-12-11 20:36:34 +010079 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090080 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010081
Emeric Brun52a91d32017-08-31 14:41:55 +020082 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090083 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010084
Simon Horman4a741432013-02-23 15:35:38 +090085 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010086}
87
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010088/*
89 * Check that we did not get a hash collision.
90 * Unlikely, but it can happen.
91 */
92static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010093{
94 struct proxy *p = s->proxy;
95 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010096
97 for (tmpserv = p->srv; tmpserv != NULL;
98 tmpserv = tmpserv->next) {
99 if (tmpserv == s)
100 continue;
101 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
102 continue;
103 if (tmpserv->cookie &&
104 strcmp(tmpserv->cookie, s->cookie) == 0) {
105 ha_warning("We generated two equal cookies for two different servers.\n"
106 "Please change the secret key for '%s'.\n",
107 s->proxy->id);
108 }
109 }
110
111}
112
Willy Tarreau46b7f532018-08-21 11:54:26 +0200113/*
114 * Must be called with the server lock held.
115 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100116void srv_set_dyncookie(struct server *s)
117{
118 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100119 char *tmpbuf;
120 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100121 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100122 size_t buffer_len;
123 int addr_len;
124 int port;
125
126 if ((s->flags & SRV_F_COOKIESET) ||
127 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
128 s->proxy->dyncookie_key == NULL)
129 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100130 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100131
132 if (s->addr.ss_family != AF_INET &&
133 s->addr.ss_family != AF_INET6)
134 return;
135 /*
136 * Buffer to calculate the cookie value.
137 * The buffer contains the secret key + the server IP address
138 * + the TCP port.
139 */
140 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
141 /*
142 * The TCP port should use only 2 bytes, but is stored in
143 * an unsigned int in struct server, so let's use 4, to be
144 * on the safe side.
145 */
146 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200147 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100148 memcpy(tmpbuf, p->dyncookie_key, key_len);
149 memcpy(&(tmpbuf[key_len]),
150 s->addr.ss_family == AF_INET ?
151 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
152 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
153 addr_len);
154 /*
155 * Make sure it's the same across all the load balancers,
156 * no matter their endianness.
157 */
158 port = htonl(s->svc_port);
159 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
160 hash_value = XXH64(tmpbuf, buffer_len, 0);
161 memprintf(&s->cookie, "%016llx", hash_value);
162 if (!s->cookie)
163 return;
164 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100165
166 /* Don't bother checking if the dyncookie is duplicated if
167 * the server is marked as "disabled", maybe it doesn't have
168 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100169 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100170 if (!(s->next_admin & SRV_ADMF_FMAINT))
171 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100172}
173
Willy Tarreau21faa912012-10-10 08:27:36 +0200174/*
175 * Registers the server keyword list <kwl> as a list of valid keywords for next
176 * parsing sessions.
177 */
178void srv_register_keywords(struct srv_kw_list *kwl)
179{
180 LIST_ADDQ(&srv_keywords.list, &kwl->list);
181}
182
183/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
184 * keyword is found with a NULL ->parse() function, then an attempt is made to
185 * find one with a valid ->parse() function. This way it is possible to declare
186 * platform-dependant, known keywords as NULL, then only declare them as valid
187 * if some options are met. Note that if the requested keyword contains an
188 * opening parenthesis, everything from this point is ignored.
189 */
190struct srv_kw *srv_find_kw(const char *kw)
191{
192 int index;
193 const char *kwend;
194 struct srv_kw_list *kwl;
195 struct srv_kw *ret = NULL;
196
197 kwend = strchr(kw, '(');
198 if (!kwend)
199 kwend = kw + strlen(kw);
200
201 list_for_each_entry(kwl, &srv_keywords.list, list) {
202 for (index = 0; kwl->kw[index].kw != NULL; index++) {
203 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
204 kwl->kw[index].kw[kwend-kw] == 0) {
205 if (kwl->kw[index].parse)
206 return &kwl->kw[index]; /* found it !*/
207 else
208 ret = &kwl->kw[index]; /* may be OK */
209 }
210 }
211 }
212 return ret;
213}
214
215/* Dumps all registered "server" keywords to the <out> string pointer. The
216 * unsupported keywords are only dumped if their supported form was not
217 * found.
218 */
219void srv_dump_kws(char **out)
220{
221 struct srv_kw_list *kwl;
222 int index;
223
224 *out = NULL;
225 list_for_each_entry(kwl, &srv_keywords.list, list) {
226 for (index = 0; kwl->kw[index].kw != NULL; index++) {
227 if (kwl->kw[index].parse ||
228 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
229 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
230 kwl->scope,
231 kwl->kw[index].kw,
232 kwl->kw[index].skip ? " <arg>" : "",
233 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
234 kwl->kw[index].parse ? "" : " (not supported)");
235 }
236 }
237 }
238}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100239
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100240/* Parse the "addr" server keyword */
241static int srv_parse_addr(char **args, int *cur_arg,
242 struct proxy *curproxy, struct server *newsrv, char **err)
243{
244 char *errmsg, *arg;
245 struct sockaddr_storage *sk;
246 int port1, port2;
247 struct protocol *proto;
248
249 errmsg = NULL;
250 arg = args[*cur_arg + 1];
251
252 if (!*arg) {
253 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
254 goto err;
255 }
256
257 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
258 if (!sk) {
259 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
260 goto err;
261 }
262
263 proto = protocol_by_family(sk->ss_family);
264 if (!proto || !proto->connect) {
265 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
266 args[*cur_arg], arg);
267 goto err;
268 }
269
270 if (port1 != port2) {
271 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
272 args[*cur_arg], arg);
273 goto err;
274 }
275
276 newsrv->check.addr = newsrv->agent.addr = *sk;
277 newsrv->flags |= SRV_F_CHECKADDR;
278 newsrv->flags |= SRV_F_AGENTADDR;
279
280 return 0;
281
282 err:
283 free(errmsg);
284 return ERR_ALERT | ERR_FATAL;
285}
286
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100287/* Parse the "agent-check" server keyword */
288static int srv_parse_agent_check(char **args, int *cur_arg,
289 struct proxy *curproxy, struct server *newsrv, char **err)
290{
291 newsrv->do_agent = 1;
292 return 0;
293}
294
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100295/* Parse the "backup" server keyword */
296static int srv_parse_backup(char **args, int *cur_arg,
297 struct proxy *curproxy, struct server *newsrv, char **err)
298{
299 newsrv->flags |= SRV_F_BACKUP;
300 return 0;
301}
302
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100303/* Parse the "check" server keyword */
304static int srv_parse_check(char **args, int *cur_arg,
305 struct proxy *curproxy, struct server *newsrv, char **err)
306{
307 newsrv->do_check = 1;
308 return 0;
309}
310
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100311/* Parse the "check-send-proxy" server keyword */
312static int srv_parse_check_send_proxy(char **args, int *cur_arg,
313 struct proxy *curproxy, struct server *newsrv, char **err)
314{
315 newsrv->check.send_proxy = 1;
316 return 0;
317}
318
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100319/* Parse the "cookie" server keyword */
320static int srv_parse_cookie(char **args, int *cur_arg,
321 struct proxy *curproxy, struct server *newsrv, char **err)
322{
323 char *arg;
324
325 arg = args[*cur_arg + 1];
326 if (!*arg) {
327 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
328 return ERR_ALERT | ERR_FATAL;
329 }
330
331 free(newsrv->cookie);
332 newsrv->cookie = strdup(arg);
333 newsrv->cklen = strlen(arg);
334 newsrv->flags |= SRV_F_COOKIESET;
335 return 0;
336}
337
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100338/* Parse the "disabled" server keyword */
339static int srv_parse_disabled(char **args, int *cur_arg,
340 struct proxy *curproxy, struct server *newsrv, char **err)
341{
Emeric Brun52a91d32017-08-31 14:41:55 +0200342 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
343 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100344 newsrv->check.state |= CHK_ST_PAUSED;
345 newsrv->check.health = 0;
346 return 0;
347}
348
349/* Parse the "enabled" server keyword */
350static int srv_parse_enabled(char **args, int *cur_arg,
351 struct proxy *curproxy, struct server *newsrv, char **err)
352{
Emeric Brun52a91d32017-08-31 14:41:55 +0200353 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
354 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100355 newsrv->check.state &= ~CHK_ST_PAUSED;
356 newsrv->check.health = newsrv->check.rise;
357 return 0;
358}
359
Willy Tarreau9c538e02019-01-23 10:21:49 +0100360static int srv_parse_max_reuse(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
361{
362 char *arg;
363
364 arg = args[*cur_arg + 1];
365 if (!*arg) {
366 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
367 return ERR_ALERT | ERR_FATAL;
368 }
369 newsrv->max_reuse = atoi(arg);
370
371 return 0;
372}
373
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100374static 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 +0100375{
376 const char *res;
377 char *arg;
378 unsigned int time;
379
380 arg = args[*cur_arg + 1];
381 if (!*arg) {
382 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
383 return ERR_ALERT | ERR_FATAL;
384 }
385 res = parse_time_err(arg, &time, TIME_UNIT_MS);
386 if (res) {
387 memprintf(err, "unexpected character '%c' in argument to <%s>.\n",
388 *res, args[*cur_arg]);
389 return ERR_ALERT | ERR_FATAL;
390 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +0100391 newsrv->pool_purge_delay = time;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +0100392
393 return 0;
394}
395
Olivier Houchard006e3102018-12-10 18:30:32 +0100396static int srv_parse_pool_max_conn(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
397{
398 char *arg;
399
400 arg = args[*cur_arg + 1];
401 if (!*arg) {
402 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
403 return ERR_ALERT | ERR_FATAL;
404 }
Willy Tarreaucb923d52019-01-23 10:39:27 +0100405
Olivier Houchard006e3102018-12-10 18:30:32 +0100406 newsrv->max_idle_conns = atoi(arg);
Willy Tarreaucb923d52019-01-23 10:39:27 +0100407 if ((int)newsrv->max_idle_conns < -1) {
408 memprintf(err, "'%s' must be >= -1", args[*cur_arg]);
409 return ERR_ALERT | ERR_FATAL;
410 }
Olivier Houchard006e3102018-12-10 18:30:32 +0100411
412 return 0;
413}
414
Willy Tarreaudff55432012-10-10 17:51:05 +0200415/* parse the "id" server keyword */
416static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
417{
418 struct eb32_node *node;
419
420 if (!*args[*cur_arg + 1]) {
421 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
422 return ERR_ALERT | ERR_FATAL;
423 }
424
425 newsrv->puid = atol(args[*cur_arg + 1]);
426 newsrv->conf.id.key = newsrv->puid;
427
428 if (newsrv->puid <= 0) {
429 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
430 return ERR_ALERT | ERR_FATAL;
431 }
432
433 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
434 if (node) {
435 struct server *target = container_of(node, struct server, conf.id);
436 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
437 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
438 target->id);
439 return ERR_ALERT | ERR_FATAL;
440 }
441
442 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200443 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200444 return 0;
445}
446
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100447/* Parse the "namespace" server keyword */
448static int srv_parse_namespace(char **args, int *cur_arg,
449 struct proxy *curproxy, struct server *newsrv, char **err)
450{
451#ifdef CONFIG_HAP_NS
452 char *arg;
453
454 arg = args[*cur_arg + 1];
455 if (!*arg) {
456 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
457 return ERR_ALERT | ERR_FATAL;
458 }
459
460 if (!strcmp(arg, "*")) {
461 /* Use the namespace associated with the connection (if present). */
462 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
463 return 0;
464 }
465
466 /*
467 * As this parser may be called several times for the same 'default-server'
468 * object, or for a new 'server' instance deriving from a 'default-server'
469 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
470 */
471 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
472
473 newsrv->netns = netns_store_lookup(arg, strlen(arg));
474 if (!newsrv->netns)
475 newsrv->netns = netns_store_insert(arg);
476
477 if (!newsrv->netns) {
478 memprintf(err, "Cannot open namespace '%s'", arg);
479 return ERR_ALERT | ERR_FATAL;
480 }
481
482 return 0;
483#else
484 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
485 return ERR_ALERT | ERR_FATAL;
486#endif
487}
488
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100489/* Parse the "no-agent-check" server keyword */
490static int srv_parse_no_agent_check(char **args, int *cur_arg,
491 struct proxy *curproxy, struct server *newsrv, char **err)
492{
493 free_check(&newsrv->agent);
494 newsrv->agent.inter = 0;
495 newsrv->agent.port = 0;
496 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
497 newsrv->do_agent = 0;
498 return 0;
499}
500
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100501/* Parse the "no-backup" server keyword */
502static int srv_parse_no_backup(char **args, int *cur_arg,
503 struct proxy *curproxy, struct server *newsrv, char **err)
504{
505 newsrv->flags &= ~SRV_F_BACKUP;
506 return 0;
507}
508
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100509/* Parse the "no-check" server keyword */
510static int srv_parse_no_check(char **args, int *cur_arg,
511 struct proxy *curproxy, struct server *newsrv, char **err)
512{
513 free_check(&newsrv->check);
514 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
515 newsrv->do_check = 0;
516 return 0;
517}
518
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100519/* Parse the "no-check-send-proxy" server keyword */
520static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
521 struct proxy *curproxy, struct server *newsrv, char **err)
522{
523 newsrv->check.send_proxy = 0;
524 return 0;
525}
526
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100527/* Disable server PROXY protocol flags. */
528static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
529{
530 srv->pp_opts &= ~flags;
531 return 0;
532}
533
534/* Parse the "no-send-proxy" server keyword */
535static int srv_parse_no_send_proxy(char **args, int *cur_arg,
536 struct proxy *curproxy, struct server *newsrv, char **err)
537{
538 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
539}
540
541/* Parse the "no-send-proxy-v2" server keyword */
542static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
546}
547
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100548/* Parse the "non-stick" server keyword */
549static int srv_parse_non_stick(char **args, int *cur_arg,
550 struct proxy *curproxy, struct server *newsrv, char **err)
551{
552 newsrv->flags |= SRV_F_NON_STICK;
553 return 0;
554}
555
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100556/* Enable server PROXY protocol flags. */
557static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
558{
559 srv->pp_opts |= flags;
560 return 0;
561}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200562/* parse the "proto" server keyword */
563static int srv_parse_proto(char **args, int *cur_arg,
564 struct proxy *px, struct server *newsrv, char **err)
565{
566 struct ist proto;
567
568 if (!*args[*cur_arg + 1]) {
569 memprintf(err, "'%s' : missing value", args[*cur_arg]);
570 return ERR_ALERT | ERR_FATAL;
571 }
572 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
573 newsrv->mux_proto = get_mux_proto(proto);
574 if (!newsrv->mux_proto) {
575 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
576 return ERR_ALERT | ERR_FATAL;
577 }
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200578 return 0;
579}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100580
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100581/* parse the "proxy-v2-options" */
582static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
583 struct proxy *px, struct server *newsrv, char **err)
584{
585 char *p, *n;
586 for (p = args[*cur_arg+1]; p; p = n) {
587 n = strchr(p, ',');
588 if (n)
589 *n++ = '\0';
590 if (!strcmp(p, "ssl")) {
591 newsrv->pp_opts |= SRV_PP_V2_SSL;
592 } else if (!strcmp(p, "cert-cn")) {
593 newsrv->pp_opts |= SRV_PP_V2_SSL;
594 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100595 } else if (!strcmp(p, "cert-key")) {
596 newsrv->pp_opts |= SRV_PP_V2_SSL;
597 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
598 } else if (!strcmp(p, "cert-sig")) {
599 newsrv->pp_opts |= SRV_PP_V2_SSL;
600 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
601 } else if (!strcmp(p, "ssl-cipher")) {
602 newsrv->pp_opts |= SRV_PP_V2_SSL;
603 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100604 } else if (!strcmp(p, "authority")) {
605 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100606 } else if (!strcmp(p, "crc32c")) {
607 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100608 } else
609 goto fail;
610 }
611 return 0;
612 fail:
613 if (err)
614 memprintf(err, "'%s' : proxy v2 option not implemented", p);
615 return ERR_ALERT | ERR_FATAL;
616}
617
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100618/* Parse the "observe" server keyword */
619static int srv_parse_observe(char **args, int *cur_arg,
620 struct proxy *curproxy, struct server *newsrv, char **err)
621{
622 char *arg;
623
624 arg = args[*cur_arg + 1];
625 if (!*arg) {
626 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
627 return ERR_ALERT | ERR_FATAL;
628 }
629
630 if (!strcmp(arg, "none")) {
631 newsrv->observe = HANA_OBS_NONE;
632 }
633 else if (!strcmp(arg, "layer4")) {
634 newsrv->observe = HANA_OBS_LAYER4;
635 }
636 else if (!strcmp(arg, "layer7")) {
637 if (curproxy->mode != PR_MODE_HTTP) {
638 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
639 return ERR_ALERT;
640 }
641 newsrv->observe = HANA_OBS_LAYER7;
642 }
643 else {
644 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
645 "but got '%s'\n", args[*cur_arg], arg);
646 return ERR_ALERT | ERR_FATAL;
647 }
648
649 return 0;
650}
651
Frédéric Lécaille16186232017-03-14 16:42:49 +0100652/* Parse the "redir" server keyword */
653static int srv_parse_redir(char **args, int *cur_arg,
654 struct proxy *curproxy, struct server *newsrv, char **err)
655{
656 char *arg;
657
658 arg = args[*cur_arg + 1];
659 if (!*arg) {
660 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
661 return ERR_ALERT | ERR_FATAL;
662 }
663
664 free(newsrv->rdr_pfx);
665 newsrv->rdr_pfx = strdup(arg);
666 newsrv->rdr_len = strlen(arg);
667
668 return 0;
669}
670
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100671/* Parse the "send-proxy" server keyword */
672static int srv_parse_send_proxy(char **args, int *cur_arg,
673 struct proxy *curproxy, struct server *newsrv, char **err)
674{
675 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
676}
677
678/* Parse the "send-proxy-v2" server keyword */
679static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
680 struct proxy *curproxy, struct server *newsrv, char **err)
681{
682 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
683}
684
Frédéric Lécailledba97072017-03-17 15:33:50 +0100685
686/* Parse the "source" server keyword */
687static int srv_parse_source(char **args, int *cur_arg,
688 struct proxy *curproxy, struct server *newsrv, char **err)
689{
690 char *errmsg;
691 int port_low, port_high;
692 struct sockaddr_storage *sk;
693 struct protocol *proto;
694
695 errmsg = NULL;
696
697 if (!*args[*cur_arg + 1]) {
698 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
699 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
700 goto err;
701 }
702
703 /* 'sk' is statically allocated (no need to be freed). */
704 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
705 if (!sk) {
706 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
707 goto err;
708 }
709
710 proto = protocol_by_family(sk->ss_family);
711 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100712 ha_alert("'%s %s' : connect() not supported for this address family.\n",
713 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100714 goto err;
715 }
716
717 newsrv->conn_src.opts |= CO_SRC_BIND;
718 newsrv->conn_src.source_addr = *sk;
719
720 if (port_low != port_high) {
721 int i;
722
723 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100724 ha_alert("'%s' does not support port offsets (found '%s').\n",
725 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100726 goto err;
727 }
728
729 if (port_low <= 0 || port_low > 65535 ||
730 port_high <= 0 || port_high > 65535 ||
731 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100732 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 +0100733 goto err;
734 }
735 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
736 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
737 newsrv->conn_src.sport_range->ports[i] = port_low + i;
738 }
739
740 *cur_arg += 2;
741 while (*(args[*cur_arg])) {
742 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
743#if defined(CONFIG_HAP_TRANSPARENT)
744 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100745 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
746 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100747 goto err;
748 }
749 if (!strcmp(args[*cur_arg + 1], "client")) {
750 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
751 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
752 }
753 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
754 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
755 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
756 }
757 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
758 char *name, *end;
759
760 name = args[*cur_arg + 1] + 7;
761 while (isspace(*name))
762 name++;
763
764 end = name;
765 while (*end && !isspace(*end) && *end != ',' && *end != ')')
766 end++;
767
768 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
769 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
770 free(newsrv->conn_src.bind_hdr_name);
771 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
772 newsrv->conn_src.bind_hdr_len = end - name;
773 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
774 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
775 newsrv->conn_src.bind_hdr_occ = -1;
776
777 /* now look for an occurrence number */
778 while (isspace(*end))
779 end++;
780 if (*end == ',') {
781 end++;
782 name = end;
783 if (*end == '-')
784 end++;
785 while (isdigit((int)*end))
786 end++;
787 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
788 }
789
790 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("usesrc hdr_ip(name,num) does not support negative"
792 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100793 goto err;
794 }
795 }
796 else {
797 struct sockaddr_storage *sk;
798 int port1, port2;
799
800 /* 'sk' is statically allocated (no need to be freed). */
801 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
802 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100803 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100804 goto err;
805 }
806
807 proto = protocol_by_family(sk->ss_family);
808 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100809 ha_alert("'%s %s' : connect() not supported for this address family.\n",
810 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100811 goto err;
812 }
813
814 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100815 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
816 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100817 goto err;
818 }
819 newsrv->conn_src.tproxy_addr = *sk;
820 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
821 }
822 global.last_checks |= LSTCHK_NETADM;
823 *cur_arg += 2;
824 continue;
825#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100826 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 +0100827 goto err;
828#endif /* defined(CONFIG_HAP_TRANSPARENT) */
829 } /* "usesrc" */
830
831 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
832#ifdef SO_BINDTODEVICE
833 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100834 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100835 goto err;
836 }
837 free(newsrv->conn_src.iface_name);
838 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
839 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
840 global.last_checks |= LSTCHK_NETADM;
841#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100842 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100843 goto err;
844#endif
845 *cur_arg += 2;
846 continue;
847 }
848 /* this keyword in not an option of "source" */
849 break;
850 } /* while */
851
852 return 0;
853
854 err:
855 free(errmsg);
856 return ERR_ALERT | ERR_FATAL;
857}
858
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100859/* Parse the "stick" server keyword */
860static int srv_parse_stick(char **args, int *cur_arg,
861 struct proxy *curproxy, struct server *newsrv, char **err)
862{
863 newsrv->flags &= ~SRV_F_NON_STICK;
864 return 0;
865}
866
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100867/* Parse the "track" server keyword */
868static int srv_parse_track(char **args, int *cur_arg,
869 struct proxy *curproxy, struct server *newsrv, char **err)
870{
871 char *arg;
872
873 arg = args[*cur_arg + 1];
874 if (!*arg) {
875 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
876 return ERR_ALERT | ERR_FATAL;
877 }
878
879 free(newsrv->trackit);
880 newsrv->trackit = strdup(arg);
881
882 return 0;
883}
884
Frédéric Lécailledba97072017-03-17 15:33:50 +0100885
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200886/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200887 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200888 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200889 *
890 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200891 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200892void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200893{
Willy Tarreau87b09662015-04-03 00:22:06 +0200894 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200895
Willy Tarreau87b09662015-04-03 00:22:06 +0200896 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
897 if (stream->srv_conn == srv)
898 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200899}
900
901/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200902 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200903 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200904 *
905 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200906 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200907void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200908{
909 struct server *srv;
910
911 for (srv = px->srv; srv != NULL; srv = srv->next)
912 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200913 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200914}
915
Willy Tarreaubda92272014-05-20 21:55:30 +0200916/* Appends some information to a message string related to a server going UP or
917 * DOWN. If both <forced> and <reason> are null and the server tracks another
918 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200919 * If <check> is non-null, an entire string describing the check result will be
920 * appended after a comma and a space (eg: to report some information from the
921 * check that changed the state). In the other case, the string will be built
922 * using the check results stored into the struct server if present.
923 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200924 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200925 *
926 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200927 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200928void srv_append_status(struct buffer *msg, struct server *s,
929 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200930{
Emeric Brun5a133512017-10-19 14:42:30 +0200931 short status = s->op_st_chg.status;
932 short code = s->op_st_chg.code;
933 long duration = s->op_st_chg.duration;
934 char *desc = s->op_st_chg.reason;
935
936 if (check) {
937 status = check->status;
938 code = check->code;
939 duration = check->duration;
940 desc = check->desc;
941 }
942
943 if (status != -1) {
944 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
945
946 if (status >= HCHK_STATUS_L57DATA)
947 chunk_appendf(msg, ", code: %d", code);
948
949 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200950 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200951
952 chunk_appendf(msg, ", info: \"");
953
954 chunk_initlen(&src, desc, 0, strlen(desc));
955 chunk_asciiencode(msg, &src, '"');
956
957 chunk_appendf(msg, "\"");
958 }
959
960 if (duration >= 0)
961 chunk_appendf(msg, ", check duration: %ldms", duration);
962 }
963 else if (desc && *desc) {
964 chunk_appendf(msg, ", %s", desc);
965 }
966 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200967 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200968 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200969
970 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200971 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200972 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
973 " %d sessions active, %d requeued, %d remaining in queue",
974 s->proxy->srv_act, s->proxy->srv_bck,
975 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
976 s->cur_sess, xferred, s->nbpend);
977 else
978 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
979 " %d sessions requeued, %d total in queue",
980 s->proxy->srv_act, s->proxy->srv_bck,
981 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
982 xferred, s->nbpend);
983 }
984}
985
Emeric Brun5a133512017-10-19 14:42:30 +0200986/* Marks server <s> down, regardless of its checks' statuses. The server is
987 * registered in a list to postpone the counting of the remaining servers on
988 * the proxy and transfers queued streams whenever possible to other servers at
989 * a sync point. Maintenance servers are ignored. It stores the <reason> if
990 * non-null as the reason for going down or the available data from the check
991 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200992 *
993 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200994 */
Emeric Brun5a133512017-10-19 14:42:30 +0200995void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200996{
997 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200998
Emeric Brun64cc49c2017-10-03 14:46:45 +0200999 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001000 return;
1001
Emeric Brun52a91d32017-08-31 14:41:55 +02001002 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +02001003 *s->op_st_chg.reason = 0;
1004 s->op_st_chg.status = -1;
1005 if (reason) {
1006 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1007 }
1008 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001009 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001010 s->op_st_chg.code = check->code;
1011 s->op_st_chg.status = check->status;
1012 s->op_st_chg.duration = check->duration;
1013 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001014
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001015 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001016 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001017
Emeric Brun9f0b4582017-10-23 14:39:51 +02001018 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001019 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001020 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001021 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001022 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +02001023}
1024
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001025/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +02001026 * in maintenance. The server is registered in a list to postpone the counting
1027 * of the remaining servers on the proxy and tries to grab requests from the
1028 * proxy at a sync point. Maintenance servers are ignored. It stores the
1029 * <reason> if non-null as the reason for going down or the available data
1030 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001031 *
1032 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001033 */
Emeric Brun5a133512017-10-19 14:42:30 +02001034void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001035{
1036 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001037
Emeric Brun64cc49c2017-10-03 14:46:45 +02001038 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001039 return;
1040
Emeric Brun52a91d32017-08-31 14:41:55 +02001041 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001042 return;
1043
Emeric Brun52a91d32017-08-31 14:41:55 +02001044 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +02001045 *s->op_st_chg.reason = 0;
1046 s->op_st_chg.status = -1;
1047 if (reason) {
1048 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1049 }
1050 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001051 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001052 s->op_st_chg.code = check->code;
1053 s->op_st_chg.status = check->status;
1054 s->op_st_chg.duration = check->duration;
1055 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001056
Emeric Brun64cc49c2017-10-03 14:46:45 +02001057 if (s->slowstart <= 0)
1058 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001059
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001060 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001061 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001062
Emeric Brun9f0b4582017-10-23 14:39:51 +02001063 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001064 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001065 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001066 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001067 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001068}
1069
Willy Tarreau8eb77842014-05-21 13:54:57 +02001070/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001071 * isn't in maintenance. The server is registered in a list to postpone the
1072 * counting of the remaining servers on the proxy and tries to grab requests
1073 * from the proxy. Maintenance servers are ignored. It stores the
1074 * <reason> if non-null as the reason for going down or the available data
1075 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001076 * up. Note that it makes use of the trash to build the log strings, so <reason>
1077 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001078 *
1079 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001080 */
Emeric Brun5a133512017-10-19 14:42:30 +02001081void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001082{
1083 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001084
Emeric Brun64cc49c2017-10-03 14:46:45 +02001085 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001086 return;
1087
Emeric Brun52a91d32017-08-31 14:41:55 +02001088 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001089 return;
1090
Emeric Brun52a91d32017-08-31 14:41:55 +02001091 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001092 *s->op_st_chg.reason = 0;
1093 s->op_st_chg.status = -1;
1094 if (reason) {
1095 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1096 }
1097 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001098 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001099 s->op_st_chg.code = check->code;
1100 s->op_st_chg.status = check->status;
1101 s->op_st_chg.duration = check->duration;
1102 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001103
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001104 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001105 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001106
Emeric Brun9f0b4582017-10-23 14:39:51 +02001107 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001108 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001109 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001110 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001111 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001112}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001113
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001114/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1115 * enforce either maint mode or drain mode. It is not allowed to set more than
1116 * one flag at once. The equivalent "inherited" flag is propagated to all
1117 * tracking servers. Maintenance mode disables health checks (but not agent
1118 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001119 * is done. If <cause> is non-null, it will be displayed at the end of the log
1120 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001121 *
1122 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001123 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001124void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001125{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001127
1128 if (!mode)
1129 return;
1130
1131 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001132 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001133 return;
1134
Emeric Brun52a91d32017-08-31 14:41:55 +02001135 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001136 if (cause)
1137 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1138
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001139 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001140 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001141
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001142 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001143 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1144 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001145 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001146
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001147 /* compute the inherited flag to propagate */
1148 if (mode & SRV_ADMF_MAINT)
1149 mode = SRV_ADMF_IMAINT;
1150 else if (mode & SRV_ADMF_DRAIN)
1151 mode = SRV_ADMF_IDRAIN;
1152
Emeric Brun9f0b4582017-10-23 14:39:51 +02001153 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001154 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001155 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001156 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001157 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001158}
1159
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001160/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1161 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1162 * than one flag at once. The equivalent "inherited" flag is propagated to all
1163 * tracking servers. Leaving maintenance mode re-enables health checks. When
1164 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001165 *
1166 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001167 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001168void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001169{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001170 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001171
1172 if (!mode)
1173 return;
1174
1175 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001176 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001177 return;
1178
Emeric Brun52a91d32017-08-31 14:41:55 +02001179 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001180
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001181 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001182 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001183
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001184 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001185 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1186 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001187 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001188
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001189 if (mode & SRV_ADMF_MAINT)
1190 mode = SRV_ADMF_IMAINT;
1191 else if (mode & SRV_ADMF_DRAIN)
1192 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001193
Emeric Brun9f0b4582017-10-23 14:39:51 +02001194 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001195 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001196 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001197 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001198 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001199}
1200
Willy Tarreau757478e2016-11-03 19:22:19 +01001201/* principle: propagate maint and drain to tracking servers. This is useful
1202 * upon startup so that inherited states are correct.
1203 */
1204static void srv_propagate_admin_state(struct server *srv)
1205{
1206 struct server *srv2;
1207
1208 if (!srv->trackers)
1209 return;
1210
1211 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001212 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001213 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001214 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001215
Emeric Brun52a91d32017-08-31 14:41:55 +02001216 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001217 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001218 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001219 }
1220}
1221
1222/* Compute and propagate the admin states for all servers in proxy <px>.
1223 * Only servers *not* tracking another one are considered, because other
1224 * ones will be handled when the server they track is visited.
1225 */
1226void srv_compute_all_admin_states(struct proxy *px)
1227{
1228 struct server *srv;
1229
1230 for (srv = px->srv; srv; srv = srv->next) {
1231 if (srv->track)
1232 continue;
1233 srv_propagate_admin_state(srv);
1234 }
1235}
1236
Willy Tarreaudff55432012-10-10 17:51:05 +02001237/* Note: must not be declared <const> as its list will be overwritten.
1238 * Please take care of keeping this list alphabetically sorted, doing so helps
1239 * all code contributors.
1240 * Optional keywords are also declared with a NULL ->parse() function so that
1241 * the config parser can report an appropriate error when a known keyword was
1242 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001243 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001244 */
1245static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001246 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001247 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001248 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001249 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001250 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001251 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001252 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1253 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001254 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Willy Tarreau9c538e02019-01-23 10:21:49 +01001255 { "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 +01001256 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001257 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001258 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001259 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001260 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001261 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1262 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001263 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001264 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Olivier Houchard006e3102018-12-10 18:30:32 +01001265 { "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 +01001266 { "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 +02001267 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001268 { "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 +01001269 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001270 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1271 { "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 +02001272 { "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 +01001273 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001274 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001275 { NULL, NULL, 0 },
1276}};
1277
Willy Tarreau0108d902018-11-25 19:14:37 +01001278INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws);
Willy Tarreaudff55432012-10-10 17:51:05 +02001279
Willy Tarreau004e0452013-11-21 11:22:01 +01001280/* Recomputes the server's eweight based on its state, uweight, the current time,
1281 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001282 * state is automatically disabled if the time is elapsed. If <must_update> is
1283 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001284 *
1285 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001286 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001287void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001288{
1289 struct proxy *px = sv->proxy;
1290 unsigned w;
1291
1292 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1293 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001294 if (sv->next_state == SRV_ST_STARTING)
1295 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001296 }
1297
1298 /* We must take care of not pushing the server to full throttle during slow starts.
1299 * It must also start immediately, at least at the minimal step when leaving maintenance.
1300 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001301 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001302 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1303 else
1304 w = px->lbprm.wdiv;
1305
Emeric Brun52a91d32017-08-31 14:41:55 +02001306 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001307
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001308 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001309 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001310 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001311}
1312
Willy Tarreaubaaee002006-06-26 02:48:02 +02001313/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001314 * Parses weight_str and configures sv accordingly.
1315 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001316 *
1317 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001318 */
1319const char *server_parse_weight_change_request(struct server *sv,
1320 const char *weight_str)
1321{
1322 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001323 long int w;
1324 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001325
1326 px = sv->proxy;
1327
1328 /* if the weight is terminated with '%', it is set relative to
1329 * the initial weight, otherwise it is absolute.
1330 */
1331 if (!*weight_str)
1332 return "Require <weight> or <weight%>.\n";
1333
Simon Hormanb796afa2013-02-12 10:45:53 +09001334 w = strtol(weight_str, &end, 10);
1335 if (end == weight_str)
1336 return "Empty weight string empty or preceded by garbage";
1337 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001338 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001339 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001340 /* Avoid integer overflow */
1341 if (w > 25600)
1342 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001343 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001344 if (w > 256)
1345 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001346 }
1347 else if (w < 0 || w > 256)
1348 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001349 else if (end[0] != '\0')
1350 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001351
1352 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1353 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1354
1355 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001356 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001357
1358 return NULL;
1359}
1360
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001361/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001362 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1363 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001364 * Returns:
1365 * - error string on error
1366 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001367 *
1368 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001369 */
1370const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001371 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001372{
1373 unsigned char ip[INET6_ADDRSTRLEN];
1374
1375 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001376 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001377 return NULL;
1378 }
1379 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001380 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001381 return NULL;
1382 }
1383
1384 return "Could not understand IP address format.\n";
1385}
1386
Willy Tarreau46b7f532018-08-21 11:54:26 +02001387/*
1388 * Must be called with the server lock held.
1389 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001390const char *server_parse_maxconn_change_request(struct server *sv,
1391 const char *maxconn_str)
1392{
1393 long int v;
1394 char *end;
1395
1396 if (!*maxconn_str)
1397 return "Require <maxconn>.\n";
1398
1399 v = strtol(maxconn_str, &end, 10);
1400 if (end == maxconn_str)
1401 return "maxconn string empty or preceded by garbage";
1402 else if (end[0] != '\0')
1403 return "Trailing garbage in maxconn string";
1404
1405 if (sv->maxconn == sv->minconn) { // static maxconn
1406 sv->maxconn = sv->minconn = v;
1407 } else { // dynamic maxconn
1408 sv->maxconn = v;
1409 }
1410
1411 if (may_dequeue_tasks(sv, sv->proxy))
1412 process_srv_queue(sv);
1413
1414 return NULL;
1415}
1416
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001417#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001418static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1419 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001420{
1421 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001422 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001423 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001424 NULL,
1425 };
1426
1427 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001428 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001429
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001430 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1431}
1432
1433static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1434{
1435 struct sample_expr *expr;
1436
1437 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 +01001438 if (!expr) {
1439 memprintf(err, "error detected while parsing sni expression : %s", *err);
1440 return ERR_ALERT | ERR_FATAL;
1441 }
1442
1443 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1444 memprintf(err, "error detected while parsing sni expression : "
1445 " fetch method '%s' extracts information from '%s', "
1446 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001447 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001448 return ERR_ALERT | ERR_FATAL;
1449 }
1450
1451 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1452 release_sample_expr(newsrv->ssl_ctx.sni);
1453 newsrv->ssl_ctx.sni = expr;
1454
1455 return 0;
1456}
1457#endif
1458
1459static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1460{
1461 if (err && *err) {
1462 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001463 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001464 }
1465 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001466 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1467 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001468}
1469
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001470static void srv_conn_src_sport_range_cpy(struct server *srv,
1471 struct server *src)
1472{
1473 int range_sz;
1474
1475 range_sz = src->conn_src.sport_range->size;
1476 if (range_sz > 0) {
1477 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1478 if (srv->conn_src.sport_range != NULL) {
1479 int i;
1480
1481 for (i = 0; i < range_sz; i++) {
1482 srv->conn_src.sport_range->ports[i] =
1483 src->conn_src.sport_range->ports[i];
1484 }
1485 }
1486 }
1487}
1488
1489/*
1490 * Copy <src> server connection source settings to <srv> server everything needed.
1491 */
1492static void srv_conn_src_cpy(struct server *srv, struct server *src)
1493{
1494 srv->conn_src.opts = src->conn_src.opts;
1495 srv->conn_src.source_addr = src->conn_src.source_addr;
1496
1497 /* Source port range copy. */
1498 if (src->conn_src.sport_range != NULL)
1499 srv_conn_src_sport_range_cpy(srv, src);
1500
1501#ifdef CONFIG_HAP_TRANSPARENT
1502 if (src->conn_src.bind_hdr_name != NULL) {
1503 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1504 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1505 }
1506 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1507 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1508#endif
1509 if (src->conn_src.iface_name != NULL)
1510 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1511}
1512
1513/*
1514 * Copy <src> server SSL settings to <srv> server allocating
1515 * everything needed.
1516 */
1517#if defined(USE_OPENSSL)
1518static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1519{
1520 if (src->ssl_ctx.ca_file != NULL)
1521 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1522 if (src->ssl_ctx.crl_file != NULL)
1523 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1524 if (src->ssl_ctx.client_crt != NULL)
1525 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1526
1527 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1528
1529 if (src->ssl_ctx.verify_host != NULL)
1530 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1531 if (src->ssl_ctx.ciphers != NULL)
1532 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001533#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1534 if (src->ssl_ctx.ciphersuites != NULL)
1535 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1536#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001537 if (src->sni_expr != NULL)
1538 srv->sni_expr = strdup(src->sni_expr);
Olivier Houchardc7566002018-11-20 23:33:50 +01001539
1540#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1541 if (src->ssl_ctx.alpn_str) {
1542 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1543 if (srv->ssl_ctx.alpn_str) {
1544 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1545 src->ssl_ctx.alpn_len);
1546 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1547 }
1548 }
1549#endif
1550#ifdef OPENSSL_NPN_NEGOTIATED
1551 if (src->ssl_ctx.npn_str) {
1552 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1553 if (srv->ssl_ctx.npn_str) {
1554 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1555 src->ssl_ctx.npn_len);
1556 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1557 }
1558 }
1559#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001560}
1561#endif
1562
1563/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001564 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001565 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001566 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001567 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001568static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001569{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001570 char *hostname_dn;
1571 int hostname_len, hostname_dn_len;
1572
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001573 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001574 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001575
Christopher Faulet67957bd2017-09-27 11:00:59 +02001576 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001577 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001578 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1579 hostname_dn, trash.size);
1580 if (hostname_dn_len == -1)
1581 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001582
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001583
Christopher Faulet67957bd2017-09-27 11:00:59 +02001584 free(srv->hostname);
1585 free(srv->hostname_dn);
1586 srv->hostname = strdup(hostname);
1587 srv->hostname_dn = strdup(hostname_dn);
1588 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001589 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001590 goto err;
1591
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001592 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001593
1594 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001595 free(srv->hostname); srv->hostname = NULL;
1596 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001597 return -1;
1598}
1599
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001600/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001601 * Copy <src> server settings to <srv> server allocating
1602 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001603 * This function is not supposed to be called at any time, but only
1604 * during server settings parsing or during server allocations from
1605 * a server template, and just after having calloc()'ed a new server.
1606 * So, <src> may only be a default server (when parsing server settings)
1607 * or a server template (during server allocations from a server template).
1608 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1609 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001610 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001611static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001612{
1613 /* Connection source settings copy */
1614 srv_conn_src_cpy(srv, src);
1615
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001616 if (srv_tmpl) {
1617 srv->addr = src->addr;
1618 srv->svc_port = src->svc_port;
1619 }
1620
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001621 srv->pp_opts = src->pp_opts;
1622 if (src->rdr_pfx != NULL) {
1623 srv->rdr_pfx = strdup(src->rdr_pfx);
1624 srv->rdr_len = src->rdr_len;
1625 }
1626 if (src->cookie != NULL) {
1627 srv->cookie = strdup(src->cookie);
1628 srv->cklen = src->cklen;
1629 }
1630 srv->use_ssl = src->use_ssl;
1631 srv->check.addr = srv->agent.addr = src->check.addr;
1632 srv->check.use_ssl = src->check.use_ssl;
1633 srv->check.port = src->check.port;
Olivier Houchard21944012018-12-21 19:42:01 +01001634 srv->check.sni = src->check.sni;
Olivier Houchard92150142018-12-21 19:47:01 +01001635 srv->check.alpn_str = src->check.alpn_str;
1636 srv->check.alpn_len = srv->check.alpn_len;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001637 /* Note: 'flags' field has potentially been already initialized. */
1638 srv->flags |= src->flags;
1639 srv->do_check = src->do_check;
1640 srv->do_agent = src->do_agent;
1641 if (srv->check.port)
1642 srv->flags |= SRV_F_CHECKPORT;
1643 srv->check.inter = src->check.inter;
1644 srv->check.fastinter = src->check.fastinter;
1645 srv->check.downinter = src->check.downinter;
1646 srv->agent.use_ssl = src->agent.use_ssl;
1647 srv->agent.port = src->agent.port;
1648 if (src->agent.send_string != NULL)
1649 srv->agent.send_string = strdup(src->agent.send_string);
1650 srv->agent.send_string_len = src->agent.send_string_len;
1651 srv->agent.inter = src->agent.inter;
1652 srv->agent.fastinter = src->agent.fastinter;
1653 srv->agent.downinter = src->agent.downinter;
1654 srv->maxqueue = src->maxqueue;
1655 srv->minconn = src->minconn;
1656 srv->maxconn = src->maxconn;
1657 srv->slowstart = src->slowstart;
1658 srv->observe = src->observe;
1659 srv->onerror = src->onerror;
1660 srv->onmarkeddown = src->onmarkeddown;
1661 srv->onmarkedup = src->onmarkedup;
1662 if (src->trackit != NULL)
1663 srv->trackit = strdup(src->trackit);
1664 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1665 srv->uweight = srv->iweight = src->iweight;
1666
1667 srv->check.send_proxy = src->check.send_proxy;
1668 /* health: up, but will fall down at first failure */
1669 srv->check.rise = srv->check.health = src->check.rise;
1670 srv->check.fall = src->check.fall;
1671
1672 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001673 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1674 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1675 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001676 srv->check.state |= CHK_ST_PAUSED;
1677 srv->check.health = 0;
1678 }
1679
1680 /* health: up but will fall down at first failure */
1681 srv->agent.rise = srv->agent.health = src->agent.rise;
1682 srv->agent.fall = src->agent.fall;
1683
1684 if (src->resolvers_id != NULL)
1685 srv->resolvers_id = strdup(src->resolvers_id);
1686 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001687 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001688 if (srv->dns_opts.family_prio == AF_UNSPEC)
1689 srv->dns_opts.family_prio = AF_INET6;
1690 memcpy(srv->dns_opts.pref_net,
1691 src->dns_opts.pref_net,
1692 sizeof srv->dns_opts.pref_net);
1693 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1694
1695 srv->init_addr_methods = src->init_addr_methods;
1696 srv->init_addr = src->init_addr;
1697#if defined(USE_OPENSSL)
1698 srv_ssl_settings_cpy(srv, src);
1699#endif
1700#ifdef TCP_USER_TIMEOUT
1701 srv->tcp_ut = src->tcp_ut;
1702#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001703 srv->mux_proto = src->mux_proto;
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001704 srv->pool_purge_delay = src->pool_purge_delay;
Olivier Houchard006e3102018-12-10 18:30:32 +01001705 srv->max_idle_conns = src->max_idle_conns;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001706 srv->max_reuse = src->max_reuse;
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001707
Olivier Houchard8da5f982017-08-04 18:35:36 +02001708 if (srv_tmpl)
1709 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001710}
1711
William Lallemand313bfd12018-10-26 14:47:32 +02001712struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001713{
1714 struct server *srv;
1715
1716 srv = calloc(1, sizeof *srv);
1717 if (!srv)
1718 return NULL;
1719
1720 srv->obj_type = OBJ_TYPE_SERVER;
1721 srv->proxy = proxy;
1722 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001723 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001724
Emeric Brun52a91d32017-08-31 14:41:55 +02001725 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001726 srv->last_change = now.tv_sec;
1727
1728 srv->check.status = HCHK_STATUS_INI;
1729 srv->check.server = srv;
Olivier Houchardc98aa1f2019-01-11 18:17:17 +01001730 srv->check.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001731 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1732
1733 srv->agent.status = HCHK_STATUS_INI;
1734 srv->agent.server = srv;
Willy Tarreau1ba32032019-01-21 07:48:26 +01001735 srv->agent.proxy = proxy;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001736 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1737
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01001738 srv->pool_purge_delay = 1000;
Olivier Houchard006e3102018-12-10 18:30:32 +01001739 srv->max_idle_conns = -1;
Willy Tarreau9c538e02019-01-23 10:21:49 +01001740 srv->max_reuse = -1;
Olivier Houchard006e3102018-12-10 18:30:32 +01001741
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001742 return srv;
1743}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001744
1745/*
1746 * Validate <srv> server health-check settings.
1747 * Returns 0 if everything is OK, -1 if not.
1748 */
1749static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1750{
1751 struct tcpcheck_rule *r = NULL;
1752 struct list *l;
1753
1754 /*
1755 * We need at least a service port, a check port or the first tcp-check rule must
1756 * be a 'connect' one when checking an IPv4/IPv6 server.
1757 */
1758 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1759 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1760 return 0;
1761
1762 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1763 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001764 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1765 "Check has been disabled.\n",
1766 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001767 return -1;
1768 }
1769
1770 /* search the first action (connect / send / expect) in the list */
1771 l = &srv->proxy->tcpcheck_rules;
1772 list_for_each_entry(r, l, list) {
1773 if (r->action != TCPCHK_ACT_COMMENT)
1774 break;
1775 }
1776
1777 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001778 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1779 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1780 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001781 return -1;
1782 }
1783
1784 /* scan the tcp-check ruleset to ensure a port has been configured */
1785 l = &srv->proxy->tcpcheck_rules;
1786 list_for_each_entry(r, l, list) {
1787 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001788 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1789 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1790 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001791 return -1;
1792 }
1793 }
1794
1795 return 0;
1796}
1797
1798/*
1799 * Initialize <srv> health-check structure.
1800 * Returns the error string in case of memory allocation failure, NULL if not.
1801 */
1802static const char *do_health_check_init(struct server *srv, int check_type, int state)
1803{
1804 const char *ret;
1805
1806 if (!srv->do_check)
1807 return NULL;
1808
1809 ret = init_check(&srv->check, check_type);
1810 if (ret)
1811 return ret;
1812
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001813 srv->check.state |= state;
1814 global.maxsock++;
1815
1816 return NULL;
1817}
1818
1819static int server_health_check_init(const char *file, int linenum,
1820 struct server *srv, struct proxy *curproxy)
1821{
1822 const char *ret;
1823
1824 if (!srv->do_check)
1825 return 0;
1826
1827 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001828 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1829 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001830 return ERR_ALERT | ERR_FATAL;
1831 }
1832
1833 if (server_healthcheck_validate(file, linenum, srv) < 0)
1834 return ERR_ALERT | ERR_ABORT;
1835
1836 /* note: check type will be set during the config review phase */
1837 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1838 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001839 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001840 return ERR_ALERT | ERR_ABORT;
1841 }
1842
1843 return 0;
1844}
1845
1846/*
1847 * Initialize <srv> agent check structure.
1848 * Returns the error string in case of memory allocation failure, NULL if not.
1849 */
1850static const char *do_server_agent_check_init(struct server *srv, int state)
1851{
1852 const char *ret;
1853
1854 if (!srv->do_agent)
1855 return NULL;
1856
1857 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1858 if (ret)
1859 return ret;
1860
1861 if (!srv->agent.inter)
1862 srv->agent.inter = srv->check.inter;
1863
1864 srv->agent.state |= state;
1865 global.maxsock++;
1866
1867 return NULL;
1868}
1869
1870static int server_agent_check_init(const char *file, int linenum,
1871 struct server *srv, struct proxy *curproxy)
1872{
1873 const char *ret;
1874
1875 if (!srv->do_agent)
1876 return 0;
1877
1878 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001879 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001880 file, linenum, srv->id);
1881 return ERR_ALERT | ERR_FATAL;
1882 }
1883
1884 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1885 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001886 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001887 return ERR_ALERT | ERR_ABORT;
1888 }
1889
1890 return 0;
1891}
1892
1893#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1894static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1895 struct server *srv, struct proxy *proxy)
1896{
1897 int ret;
1898 char *err = NULL;
1899
1900 if (!srv->sni_expr)
1901 return 0;
1902
1903 ret = server_parse_sni_expr(srv, proxy, &err);
1904 if (!ret)
1905 return 0;
1906
1907 display_parser_err(file, linenum, args, cur_arg, &err);
1908 free(err);
1909
1910 return ret;
1911}
1912#endif
1913
1914/*
1915 * Server initializations finalization.
1916 * Initialize health check, agent check and SNI expression if enabled.
1917 * Must not be called for a default server instance.
1918 */
1919static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1920 struct server *srv, struct proxy *px)
1921{
1922 int ret;
1923
1924 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1925 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1926 return ret;
1927 }
1928
1929#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1930 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1931 return ret;
1932#endif
1933
1934 if (srv->flags & SRV_F_BACKUP)
1935 px->srv_bck++;
1936 else
1937 px->srv_act++;
1938 srv_lb_commit_status(srv);
1939
1940 return 0;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01001941err:
1942 return ERR_ALERT | ERR_FATAL;
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001943}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001944
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001945/*
1946 * Parse as much as possible such a range string argument: low[-high]
1947 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1948 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1949 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1950 * Returns 0 if succeeded, -1 if not.
1951 */
1952static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1953{
1954 char *nb_high_arg;
1955
1956 *nb_high = 0;
1957 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001958 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001959
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001960 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001961 *nb_high_arg++ = '\0';
1962 *nb_high = atoi(nb_high_arg);
1963 }
1964 else {
1965 *nb_high += *nb_low;
1966 *nb_low = 1;
1967 }
1968
1969 if (*nb_low < 0 || *nb_high < *nb_low)
1970 return -1;
1971
1972 return 0;
1973}
1974
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001975static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1976{
1977 chunk_printf(&trash, "%s%d", prefix, nb);
1978 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001979 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001980}
1981
1982/*
1983 * Initialize as much as possible servers from <srv> server template.
1984 * Note that a server template is a special server with
1985 * a few different parameters than a server which has
1986 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001987 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001988 * 'srv' template included.
1989 */
1990static int server_template_init(struct server *srv, struct proxy *px)
1991{
1992 int i;
1993 struct server *newsrv;
1994
1995 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1996 int check_init_state;
1997 int agent_init_state;
1998
1999 newsrv = new_server(px);
2000 if (!newsrv)
2001 goto err;
2002
2003 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02002004 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002005#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2006 if (newsrv->sni_expr) {
2007 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
2008 if (!newsrv->ssl_ctx.sni)
2009 goto err;
2010 }
2011#endif
2012 /* Set this new server ID. */
2013 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
2014
2015 /* Initial checks states. */
2016 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
2017 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
2018
2019 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
2020 do_server_agent_check_init(newsrv, agent_init_state))
2021 goto err;
2022
2023 /* Linked backwards first. This will be restablished after parsing. */
2024 newsrv->next = px->srv;
2025 px->srv = newsrv;
2026 }
2027 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2028
2029 return i - srv->tmpl_info.nb_low;
2030
2031 err:
2032 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
2033 if (newsrv) {
2034#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
2035 release_sample_expr(newsrv->ssl_ctx.sni);
2036#endif
2037 free_check(&newsrv->agent);
2038 free_check(&newsrv->check);
2039 }
2040 free(newsrv);
2041 return i - srv->tmpl_info.nb_low;
2042}
2043
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002044int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy, int parse_addr)
Willy Tarreau272adea2014-03-31 10:39:59 +02002045{
2046 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002047 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002048 char *errmsg = NULL;
2049 int err_code = 0;
2050 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002051 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002052
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002053 if (!strcmp(args[0], "server") ||
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002054 !strcmp(args[0], "peer") ||
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002055 !strcmp(args[0], "default-server") ||
2056 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002057 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002058 int defsrv = (*args[0] == 'd');
Frédéric Lécaillec06b5d42018-04-26 10:06:41 +02002059 int srv = !defsrv && (*args[0] == 'p' || !strcmp(args[0], "server"));
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002060 int srv_tmpl = !defsrv && !srv;
2061 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002062
2063 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002064 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 err_code |= ERR_ALERT | ERR_FATAL;
2066 goto out;
2067 }
2068 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002069 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002070
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002071 /* There is no mandatory first arguments for default server. */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002072 if (srv && parse_addr) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002073 if (!*args[2]) {
2074 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002075 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002076 file, linenum, args[0]);
2077 err_code |= ERR_ALERT | ERR_FATAL;
2078 goto out;
2079 }
2080
2081 err = invalid_char(args[1]);
2082 }
2083 else if (srv_tmpl) {
2084 if (!*args[3]) {
2085 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002086 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 +02002087 file, linenum, args[0]);
2088 err_code |= ERR_ALERT | ERR_FATAL;
2089 goto out;
2090 }
2091
2092 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002093 }
2094
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002095 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002096 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 +02002097 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002098 err_code |= ERR_ALERT | ERR_FATAL;
2099 goto out;
2100 }
2101
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002102 cur_arg = 2;
2103 if (srv_tmpl) {
2104 /* Parse server-template <nb | range> arg. */
2105 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002106 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002107 file, linenum, args[0], args[cur_arg]);
2108 err_code |= ERR_ALERT | ERR_FATAL;
2109 goto out;
2110 }
2111 cur_arg++;
2112 }
2113
Willy Tarreau272adea2014-03-31 10:39:59 +02002114 if (!defsrv) {
2115 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002116 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002117 struct protocol *proto;
2118
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002119 newsrv = new_server(curproxy);
2120 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002121 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002122 err_code |= ERR_ALERT | ERR_ABORT;
2123 goto out;
2124 }
2125
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002126 if (srv_tmpl) {
2127 newsrv->tmpl_info.nb_low = tmpl_range_low;
2128 newsrv->tmpl_info.nb_high = tmpl_range_high;
2129 }
2130
Willy Tarreau272adea2014-03-31 10:39:59 +02002131 /* the servers are linked backwards first */
2132 newsrv->next = curproxy->srv;
2133 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002134 newsrv->conf.file = strdup(file);
2135 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002136 /* Note: for a server template, its id is its prefix.
2137 * This is a temporary id which will be used for server allocations to come
2138 * after parsing.
2139 */
2140 if (srv)
2141 newsrv->id = strdup(args[1]);
2142 else
2143 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002144
2145 /* several ways to check the port component :
2146 * - IP => port=+0, relative (IPv4 only)
2147 * - IP: => port=+0, relative
2148 * - IP:N => port=N, absolute
2149 * - IP:+N => port=+N, relative
2150 * - IP:-N => port=-N, relative
2151 */
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002152 if (!parse_addr)
2153 goto skip_addr;
2154
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002155 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002156 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002157 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002158 err_code |= ERR_ALERT | ERR_FATAL;
2159 goto out;
2160 }
2161
2162 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002163 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002164 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002165 file, linenum, args[0], args[1]);
2166 err_code |= ERR_ALERT | ERR_FATAL;
2167 goto out;
2168 }
2169
2170 if (!port1 || !port2) {
2171 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002172 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002173 }
2174 else if (port1 != port2) {
2175 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002176 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002177 file, linenum, args[0], args[1], args[2]);
2178 err_code |= ERR_ALERT | ERR_FATAL;
2179 goto out;
2180 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002181
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002182 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002183 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002184 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002185 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002186 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2187 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2188 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002189 err_code |= ERR_ALERT | ERR_FATAL;
2190 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002191 }
2192 }
2193 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002194 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002195 file, linenum, newsrv->id);
2196 err_code |= ERR_ALERT | ERR_FATAL;
2197 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002198 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002199 }
2200
Willy Tarreau272adea2014-03-31 10:39:59 +02002201 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002202 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002203
Olivier Houchard8da5f982017-08-04 18:35:36 +02002204 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002205 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002206 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002210
Frédéric Lécaille355b2032019-01-11 14:06:12 +01002211 cur_arg++;
2212 skip_addr:
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002213 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002214 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002215 HA_SPIN_INIT(&newsrv->lock);
Willy Tarreau272adea2014-03-31 10:39:59 +02002216 } else {
2217 newsrv = &curproxy->defsrv;
2218 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002219 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002220 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002221 }
2222
2223 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002224 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002225 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2226 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002227 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002228 file, linenum, *err, newsrv->id);
2229 err_code |= ERR_ALERT | ERR_FATAL;
2230 goto out;
2231 }
2232 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002233 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002234 file, linenum, val, args[cur_arg], newsrv->id);
2235 err_code |= ERR_ALERT | ERR_FATAL;
2236 goto out;
2237 }
2238 newsrv->agent.inter = val;
2239 cur_arg += 2;
2240 }
Misiekea849332017-01-09 09:39:51 +01002241 else if (!strcmp(args[cur_arg], "agent-addr")) {
2242 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002243 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002244 goto out;
2245 }
2246
2247 cur_arg += 2;
2248 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002249 else if (!strcmp(args[cur_arg], "agent-port")) {
2250 global.maxsock++;
2251 newsrv->agent.port = atol(args[cur_arg + 1]);
2252 cur_arg += 2;
2253 }
James Brown55f9ff12015-10-21 18:19:05 -07002254 else if (!strcmp(args[cur_arg], "agent-send")) {
2255 global.maxsock++;
2256 free(newsrv->agent.send_string);
2257 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2258 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2259 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2260 cur_arg += 2;
2261 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002262 else if (!strcmp(args[cur_arg], "init-addr")) {
2263 char *p, *end;
2264 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002265 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002266
2267 newsrv->init_addr_methods = 0;
2268 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2269
2270 for (p = args[cur_arg + 1]; *p; p = end) {
2271 /* cut on next comma */
2272 for (end = p; *end && *end != ','; end++);
2273 if (*end)
2274 *(end++) = 0;
2275
Willy Tarreau4310d362016-11-02 15:05:56 +01002276 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002277 if (!strcmp(p, "libc")) {
2278 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2279 }
2280 else if (!strcmp(p, "last")) {
2281 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2282 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002283 else if (!strcmp(p, "none")) {
2284 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2285 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002286 else if (str2ip2(p, &sa, 0)) {
2287 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002288 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002289 file, linenum, args[cur_arg], p);
2290 err_code |= ERR_ALERT | ERR_FATAL;
2291 goto out;
2292 }
2293 newsrv->init_addr = sa;
2294 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2295 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002296 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002297 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 +02002298 file, linenum, args[cur_arg], p);
2299 err_code |= ERR_ALERT | ERR_FATAL;
2300 goto out;
2301 }
2302 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002303 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002304 file, linenum, args[cur_arg], p);
2305 err_code |= ERR_ALERT | ERR_FATAL;
2306 goto out;
2307 }
2308 }
2309 cur_arg += 2;
2310 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002311 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002312 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002313 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2314 cur_arg += 2;
2315 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002316 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2317 char *p, *end;
2318
2319 for (p = args[cur_arg + 1]; *p; p = end) {
2320 /* cut on next comma */
2321 for (end = p; *end && *end != ','; end++);
2322 if (*end)
2323 *(end++) = 0;
2324
2325 if (!strcmp(p, "allow-dup-ip")) {
2326 newsrv->dns_opts.accept_duplicate_ip = 1;
2327 }
2328 else if (!strcmp(p, "prevent-dup-ip")) {
2329 newsrv->dns_opts.accept_duplicate_ip = 0;
2330 }
2331 else {
2332 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2333 file, linenum, args[cur_arg], p);
2334 err_code |= ERR_ALERT | ERR_FATAL;
2335 goto out;
2336 }
2337 }
2338
2339 cur_arg += 2;
2340 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002341 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2342 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002343 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002344 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002345 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002346 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002347 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002348 file, linenum, args[cur_arg]);
2349 err_code |= ERR_ALERT | ERR_FATAL;
2350 goto out;
2351 }
2352 cur_arg += 2;
2353 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002354 else if (!strcmp(args[cur_arg], "resolve-net")) {
2355 char *p, *e;
2356 unsigned char mask;
2357 struct dns_options *opt;
2358
2359 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002360 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002361 file, linenum, args[cur_arg]);
2362 err_code |= ERR_ALERT | ERR_FATAL;
2363 goto out;
2364 }
2365
2366 opt = &newsrv->dns_opts;
2367
2368 /* Split arguments by comma, and convert it from ipv4 or ipv6
2369 * string network in in_addr or in6_addr.
2370 */
2371 p = args[cur_arg + 1];
2372 e = p;
2373 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002374 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002375 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002376 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002377 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2378 err_code |= ERR_ALERT | ERR_FATAL;
2379 goto out;
2380 }
2381 /* look for end or comma. */
2382 while (*e != ',' && *e != '\0')
2383 e++;
2384 if (*e == ',') {
2385 *e = '\0';
2386 e++;
2387 }
2388 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2389 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2390 /* Try to convert input string from ipv4 or ipv6 network. */
2391 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2392 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2393 &mask)) {
2394 /* Try to convert input string from ipv6 network. */
2395 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2396 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2397 } else {
2398 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002399 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002400 file, linenum, args[cur_arg], p);
2401 err_code |= ERR_ALERT | ERR_FATAL;
2402 goto out;
2403 }
2404 opt->pref_net_nb++;
2405 p = e;
2406 }
2407
2408 cur_arg += 2;
2409 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002410 else if (!strcmp(args[cur_arg], "rise")) {
2411 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002412 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002413 file, linenum, args[cur_arg]);
2414 err_code |= ERR_ALERT | ERR_FATAL;
2415 goto out;
2416 }
2417
2418 newsrv->check.rise = atol(args[cur_arg + 1]);
2419 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002420 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002421 file, linenum, args[cur_arg]);
2422 err_code |= ERR_ALERT | ERR_FATAL;
2423 goto out;
2424 }
2425
2426 if (newsrv->check.health)
2427 newsrv->check.health = newsrv->check.rise;
2428 cur_arg += 2;
2429 }
2430 else if (!strcmp(args[cur_arg], "fall")) {
2431 newsrv->check.fall = atol(args[cur_arg + 1]);
2432
2433 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002434 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002435 file, linenum, args[cur_arg]);
2436 err_code |= ERR_ALERT | ERR_FATAL;
2437 goto out;
2438 }
2439
2440 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002441 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002442 file, linenum, args[cur_arg]);
2443 err_code |= ERR_ALERT | ERR_FATAL;
2444 goto out;
2445 }
2446
2447 cur_arg += 2;
2448 }
2449 else if (!strcmp(args[cur_arg], "inter")) {
2450 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2451 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002452 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 file, linenum, *err, newsrv->id);
2454 err_code |= ERR_ALERT | ERR_FATAL;
2455 goto out;
2456 }
2457 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002458 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002459 file, linenum, val, args[cur_arg], newsrv->id);
2460 err_code |= ERR_ALERT | ERR_FATAL;
2461 goto out;
2462 }
2463 newsrv->check.inter = val;
2464 cur_arg += 2;
2465 }
2466 else if (!strcmp(args[cur_arg], "fastinter")) {
2467 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2468 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002469 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002470 file, linenum, *err, newsrv->id);
2471 err_code |= ERR_ALERT | ERR_FATAL;
2472 goto out;
2473 }
2474 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002475 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002476 file, linenum, val, args[cur_arg], newsrv->id);
2477 err_code |= ERR_ALERT | ERR_FATAL;
2478 goto out;
2479 }
2480 newsrv->check.fastinter = val;
2481 cur_arg += 2;
2482 }
2483 else if (!strcmp(args[cur_arg], "downinter")) {
2484 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2485 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002486 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002487 file, linenum, *err, newsrv->id);
2488 err_code |= ERR_ALERT | ERR_FATAL;
2489 goto out;
2490 }
2491 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002492 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002493 file, linenum, val, args[cur_arg], newsrv->id);
2494 err_code |= ERR_ALERT | ERR_FATAL;
2495 goto out;
2496 }
2497 newsrv->check.downinter = val;
2498 cur_arg += 2;
2499 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002500 else if (!strcmp(args[cur_arg], "port")) {
2501 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002502 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002503 cur_arg += 2;
2504 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002505 else if (!strcmp(args[cur_arg], "weight")) {
2506 int w;
2507 w = atol(args[cur_arg + 1]);
2508 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002509 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002510 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2511 err_code |= ERR_ALERT | ERR_FATAL;
2512 goto out;
2513 }
2514 newsrv->uweight = newsrv->iweight = w;
2515 cur_arg += 2;
2516 }
2517 else if (!strcmp(args[cur_arg], "minconn")) {
2518 newsrv->minconn = atol(args[cur_arg + 1]);
2519 cur_arg += 2;
2520 }
2521 else if (!strcmp(args[cur_arg], "maxconn")) {
2522 newsrv->maxconn = atol(args[cur_arg + 1]);
2523 cur_arg += 2;
2524 }
2525 else if (!strcmp(args[cur_arg], "maxqueue")) {
2526 newsrv->maxqueue = atol(args[cur_arg + 1]);
2527 cur_arg += 2;
2528 }
2529 else if (!strcmp(args[cur_arg], "slowstart")) {
2530 /* slowstart is stored in seconds */
2531 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2532 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002533 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002534 file, linenum, *err, newsrv->id);
2535 err_code |= ERR_ALERT | ERR_FATAL;
2536 goto out;
2537 }
2538 newsrv->slowstart = (val + 999) / 1000;
2539 cur_arg += 2;
2540 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002541 else if (!strcmp(args[cur_arg], "on-error")) {
2542 if (!strcmp(args[cur_arg + 1], "fastinter"))
2543 newsrv->onerror = HANA_ONERR_FASTINTER;
2544 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2545 newsrv->onerror = HANA_ONERR_FAILCHK;
2546 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2547 newsrv->onerror = HANA_ONERR_SUDDTH;
2548 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2549 newsrv->onerror = HANA_ONERR_MARKDWN;
2550 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002551 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002552 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2553 file, linenum, args[cur_arg], args[cur_arg + 1]);
2554 err_code |= ERR_ALERT | ERR_FATAL;
2555 goto out;
2556 }
2557
2558 cur_arg += 2;
2559 }
2560 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2561 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2562 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2563 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002564 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002565 file, linenum, args[cur_arg], args[cur_arg + 1]);
2566 err_code |= ERR_ALERT | ERR_FATAL;
2567 goto out;
2568 }
2569
2570 cur_arg += 2;
2571 }
2572 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2573 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2574 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2575 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002576 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002577 file, linenum, args[cur_arg], args[cur_arg + 1]);
2578 err_code |= ERR_ALERT | ERR_FATAL;
2579 goto out;
2580 }
2581
2582 cur_arg += 2;
2583 }
2584 else if (!strcmp(args[cur_arg], "error-limit")) {
2585 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002586 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002587 file, linenum, args[cur_arg]);
2588 err_code |= ERR_ALERT | ERR_FATAL;
2589 goto out;
2590 }
2591
2592 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2593
2594 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002595 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002596 file, linenum, args[cur_arg]);
2597 err_code |= ERR_ALERT | ERR_FATAL;
2598 goto out;
2599 }
2600 cur_arg += 2;
2601 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002602 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002603 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002604 file, linenum, "usesrc", "source");
2605 err_code |= ERR_ALERT | ERR_FATAL;
2606 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002607 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002608 else {
2609 static int srv_dumped;
2610 struct srv_kw *kw;
2611 char *err;
2612
2613 kw = srv_find_kw(args[cur_arg]);
2614 if (kw) {
2615 char *err = NULL;
2616 int code;
2617
2618 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002619 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 +02002620 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002621 if (kw->skip != -1)
2622 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002623 err_code |= ERR_ALERT | ERR_FATAL;
2624 goto out;
2625 }
2626
2627 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002628 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002629 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002630 if (kw->skip != -1)
2631 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002632 err_code |= ERR_ALERT;
2633 continue;
2634 }
2635
2636 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2637 err_code |= code;
2638
2639 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002640 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002641 if (code & ERR_FATAL) {
2642 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002643 if (kw->skip != -1)
2644 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002645 goto out;
2646 }
2647 }
2648 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002649 if (kw->skip != -1)
2650 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002651 continue;
2652 }
2653
2654 err = NULL;
2655 if (!srv_dumped) {
2656 srv_dump_kws(&err);
2657 indent_msg(&err, 4);
2658 srv_dumped = 1;
2659 }
2660
Christopher Faulet767a84b2017-11-24 16:50:31 +01002661 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002662 file, linenum, args[0], args[1], args[cur_arg],
2663 err ? " Registered keywords :" : "", err ? err : "");
2664 free(err);
2665
2666 err_code |= ERR_ALERT | ERR_FATAL;
2667 goto out;
2668 }
2669 }
2670
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002671 if (!defsrv)
2672 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2673 if (err_code & ERR_FATAL)
2674 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002675 if (srv_tmpl)
2676 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002677 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002678 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002679 return 0;
2680
2681 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002682 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002683 free(errmsg);
2684 return err_code;
2685}
2686
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002687/* Returns a pointer to the first server matching either id <id>.
2688 * NULL is returned if no match is found.
2689 * the lookup is performed in the backend <bk>
2690 */
2691struct server *server_find_by_id(struct proxy *bk, int id)
2692{
2693 struct eb32_node *eb32;
2694 struct server *curserver;
2695
2696 if (!bk || (id ==0))
2697 return NULL;
2698
2699 /* <bk> has no backend capabilities, so it can't have a server */
2700 if (!(bk->cap & PR_CAP_BE))
2701 return NULL;
2702
2703 curserver = NULL;
2704
2705 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2706 if (eb32)
2707 curserver = container_of(eb32, struct server, conf.id);
2708
2709 return curserver;
2710}
2711
2712/* Returns a pointer to the first server matching either name <name>, or id
2713 * if <name> starts with a '#'. NULL is returned if no match is found.
2714 * the lookup is performed in the backend <bk>
2715 */
2716struct server *server_find_by_name(struct proxy *bk, const char *name)
2717{
2718 struct server *curserver;
2719
2720 if (!bk || !name)
2721 return NULL;
2722
2723 /* <bk> has no backend capabilities, so it can't have a server */
2724 if (!(bk->cap & PR_CAP_BE))
2725 return NULL;
2726
2727 curserver = NULL;
2728 if (*name == '#') {
2729 curserver = server_find_by_id(bk, atoi(name + 1));
2730 if (curserver)
2731 return curserver;
2732 }
2733 else {
2734 curserver = bk->srv;
2735
2736 while (curserver && (strcmp(curserver->id, name) != 0))
2737 curserver = curserver->next;
2738
2739 if (curserver)
2740 return curserver;
2741 }
2742
2743 return NULL;
2744}
2745
2746struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2747{
2748 struct server *byname;
2749 struct server *byid;
2750
2751 if (!name && !id)
2752 return NULL;
2753
2754 if (diff)
2755 *diff = 0;
2756
2757 byname = byid = NULL;
2758
2759 if (name) {
2760 byname = server_find_by_name(bk, name);
2761 if (byname && (!id || byname->puid == id))
2762 return byname;
2763 }
2764
2765 /* remaining possibilities :
2766 * - name not set
2767 * - name set but not found
2768 * - name found but ID doesn't match
2769 */
2770 if (id) {
2771 byid = server_find_by_id(bk, id);
2772 if (byid) {
2773 if (byname) {
2774 /* use id only if forced by configuration */
2775 if (byid->flags & SRV_F_FORCED_ID) {
2776 if (diff)
2777 *diff |= 2;
2778 return byid;
2779 }
2780 else {
2781 if (diff)
2782 *diff |= 1;
2783 return byname;
2784 }
2785 }
2786
2787 /* remaining possibilities:
2788 * - name not set
2789 * - name set but not found
2790 */
2791 if (name && diff)
2792 *diff |= 2;
2793 return byid;
2794 }
2795
2796 /* id bot found */
2797 if (byname) {
2798 if (diff)
2799 *diff |= 1;
2800 return byname;
2801 }
2802 }
2803
2804 return NULL;
2805}
2806
Willy Tarreau46b7f532018-08-21 11:54:26 +02002807/* Update a server state using the parameters available in the params list.
2808 *
2809 * Grabs the server lock during operation.
2810 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002811static void srv_update_state(struct server *srv, int version, char **params)
2812{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002813 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002814 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002815
2816 /* fields since version 1
2817 * and common to all other upcoming versions
2818 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002819 enum srv_state srv_op_state;
2820 enum srv_admin srv_admin_state;
2821 unsigned srv_uweight, srv_iweight;
2822 unsigned long srv_last_time_change;
2823 short srv_check_status;
2824 enum chk_result srv_check_result;
2825 int srv_check_health;
2826 int srv_check_state, srv_agent_state;
2827 int bk_f_forced_id;
2828 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002829 int fqdn_set_by_cli;
2830 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002831 const char *port_str;
2832 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002833 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002834
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002835 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002836 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002837 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002838 switch (version) {
2839 case 1:
2840 /*
2841 * now we can proceed with server's state update:
2842 * srv_addr: params[0]
2843 * srv_op_state: params[1]
2844 * srv_admin_state: params[2]
2845 * srv_uweight: params[3]
2846 * srv_iweight: params[4]
2847 * srv_last_time_change: params[5]
2848 * srv_check_status: params[6]
2849 * srv_check_result: params[7]
2850 * srv_check_health: params[8]
2851 * srv_check_state: params[9]
2852 * srv_agent_state: params[10]
2853 * bk_f_forced_id: params[11]
2854 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002855 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002856 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002857 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002858 */
2859
2860 /* validating srv_op_state */
2861 p = NULL;
2862 errno = 0;
2863 srv_op_state = strtol(params[1], &p, 10);
2864 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2865 (srv_op_state != SRV_ST_STOPPED &&
2866 srv_op_state != SRV_ST_STARTING &&
2867 srv_op_state != SRV_ST_RUNNING &&
2868 srv_op_state != SRV_ST_STOPPING)) {
2869 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2870 }
2871
2872 /* validating srv_admin_state */
2873 p = NULL;
2874 errno = 0;
2875 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002876 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002877
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002878 /* inherited statuses will be recomputed later.
2879 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2880 */
2881 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002882
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002883 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2884 (srv_admin_state != 0 &&
2885 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002886 srv_admin_state != SRV_ADMF_CMAINT &&
2887 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002888 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002889 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002890 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2891 }
2892
2893 /* validating srv_uweight */
2894 p = NULL;
2895 errno = 0;
2896 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002897 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002898 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2899
2900 /* validating srv_iweight */
2901 p = NULL;
2902 errno = 0;
2903 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002904 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2906
2907 /* validating srv_last_time_change */
2908 p = NULL;
2909 errno = 0;
2910 srv_last_time_change = strtol(params[5], &p, 10);
2911 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2912 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2913
2914 /* validating srv_check_status */
2915 p = NULL;
2916 errno = 0;
2917 srv_check_status = strtol(params[6], &p, 10);
2918 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2919 (srv_check_status >= HCHK_STATUS_SIZE))
2920 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2921
2922 /* validating srv_check_result */
2923 p = NULL;
2924 errno = 0;
2925 srv_check_result = strtol(params[7], &p, 10);
2926 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2927 (srv_check_result != CHK_RES_UNKNOWN &&
2928 srv_check_result != CHK_RES_NEUTRAL &&
2929 srv_check_result != CHK_RES_FAILED &&
2930 srv_check_result != CHK_RES_PASSED &&
2931 srv_check_result != CHK_RES_CONDPASS)) {
2932 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2933 }
2934
2935 /* validating srv_check_health */
2936 p = NULL;
2937 errno = 0;
2938 srv_check_health = strtol(params[8], &p, 10);
2939 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2940 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2941
2942 /* validating srv_check_state */
2943 p = NULL;
2944 errno = 0;
2945 srv_check_state = strtol(params[9], &p, 10);
2946 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2947 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2948 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2949
2950 /* validating srv_agent_state */
2951 p = NULL;
2952 errno = 0;
2953 srv_agent_state = strtol(params[10], &p, 10);
2954 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2955 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2956 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2957
2958 /* validating bk_f_forced_id */
2959 p = NULL;
2960 errno = 0;
2961 bk_f_forced_id = strtol(params[11], &p, 10);
2962 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2963 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2964
2965 /* validating srv_f_forced_id */
2966 p = NULL;
2967 errno = 0;
2968 srv_f_forced_id = strtol(params[12], &p, 10);
2969 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2970 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2971
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002972 /* validating srv_fqdn */
2973 fqdn = params[13];
2974 if (fqdn && *fqdn == '-')
2975 fqdn = NULL;
2976 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2977 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2978 fqdn = NULL;
2979 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002980
Frédéric Lécaille31694712017-08-01 08:47:19 +02002981 port_str = params[14];
2982 if (port_str) {
2983 port = strl2uic(port_str, strlen(port_str));
2984 if (port > USHRT_MAX) {
2985 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2986 port_str = NULL;
2987 }
2988 }
2989
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002990 /* SRV record
2991 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2992 */
2993 srvrecord = params[15];
2994 if (srvrecord && *srvrecord != '_')
2995 srvrecord = NULL;
2996
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002997 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002998 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002999 goto out;
3000
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003001 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003002 /* recover operational state and apply it to this server
3003 * and all servers tracking this one */
Jérôme Magninf57afa42019-01-20 11:27:40 +01003004 srv->check.health = srv_check_health;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003005 switch (srv_op_state) {
3006 case SRV_ST_STOPPED:
3007 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02003008 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003009 break;
3010 case SRV_ST_STARTING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003011 /* If rise == 1 there is no STARTING state, let's switch to
3012 * RUNNING
3013 */
3014 if (srv->check.rise == 1) {
3015 srv->check.health = srv->check.rise + srv->check.fall - 1;
3016 srv_set_running(srv, "", NULL);
3017 break;
3018 }
3019 if (srv->check.health < 1 || srv->check.health >= srv->check.rise)
3020 srv->check.health = srv->check.rise - 1;
Emeric Brun52a91d32017-08-31 14:41:55 +02003021 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003022 break;
3023 case SRV_ST_STOPPING:
Jérôme Magninf57afa42019-01-20 11:27:40 +01003024 /* If fall == 1 there is no STOPPING state, let's switch to
3025 * STOPPED
3026 */
3027 if (srv->check.fall == 1) {
3028 srv->check.health = 0;
3029 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
3030 break;
3031 }
3032 if (srv->check.health < srv->check.rise ||
3033 srv->check.health > srv->check.rise + srv->check.fall - 2)
3034 srv->check.health = srv->check.rise;
Emeric Brun5a133512017-10-19 14:42:30 +02003035 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003036 break;
3037 case SRV_ST_RUNNING:
3038 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02003039 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003040 break;
3041 }
3042
3043 /* When applying server state, the following rules apply:
3044 * - in case of a configuration change, we apply the setting from the new
3045 * configuration, regardless of old running state
3046 * - if no configuration change, we apply old running state only if old running
3047 * state is different from new configuration state
3048 */
3049 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02003050 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
3051 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003052 srv_adm_set_maint(srv);
3053 else
3054 srv_adm_set_ready(srv);
3055 }
3056 /* configuration is the same, let's compate old running state and new conf state */
3057 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02003058 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003059 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02003060 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003061 srv_adm_set_ready(srv);
3062 }
3063 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02003064 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003065 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01003066 * (srv->iweight is the weight set up in configuration).
3067 * There are two possible reasons for FDRAIN to have been present :
3068 * - previous config weight was zero
3069 * - "set server b/s drain" was sent to the CLI
3070 *
3071 * In the first case, we simply want to drop this drain state
3072 * if the new weight is not zero anymore, meaning the administrator
3073 * has intentionally turned the weight back to a positive value to
3074 * enable the server again after an operation. In the second case,
3075 * the drain state was forced on the CLI regardless of the config's
3076 * weight so we don't want a change to the config weight to lose this
3077 * status. What this means is :
3078 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3079 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003080 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003081 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003082 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003083 }
3084
3085 srv->last_change = date.tv_sec - srv_last_time_change;
3086 srv->check.status = srv_check_status;
3087 srv->check.result = srv_check_result;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003088
3089 /* Only case we want to apply is removing ENABLED flag which could have been
3090 * done by the "disable health" command over the stats socket
3091 */
3092 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3093 (srv_check_state & CHK_ST_CONFIGURED) &&
3094 !(srv_check_state & CHK_ST_ENABLED))
3095 srv->check.state &= ~CHK_ST_ENABLED;
3096
3097 /* Only case we want to apply is removing ENABLED flag which could have been
3098 * done by the "disable agent" command over the stats socket
3099 */
3100 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3101 (srv_agent_state & CHK_ST_CONFIGURED) &&
3102 !(srv_agent_state & CHK_ST_ENABLED))
3103 srv->agent.state &= ~CHK_ST_ENABLED;
3104
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003105 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3106 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003107 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003108 * It means that a configuration file change has precedence over a unix socket change
3109 * for server's weight
3110 *
3111 * by default, HAProxy applies the following weight when parsing the configuration
3112 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003113 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003114 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003115 srv->uweight = srv_uweight;
3116 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003117 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003118
Willy Tarreaue5a60682016-11-09 14:54:53 +01003119 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003120 if (strcmp(params[0], "-"))
3121 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003122
3123 if (fqdn && srv->hostname) {
3124 if (!strcmp(srv->hostname, fqdn)) {
3125 /* Here we reset the 'set from stats socket FQDN' flag
3126 * to support such transitions:
3127 * Let's say initial FQDN value is foo1 (in configuration file).
3128 * - FQDN changed from stats socket, from foo1 to foo2 value,
3129 * - FQDN changed again from file configuration (with the same previous value
3130 set from stats socket, from foo1 to foo2 value),
3131 * - reload for any other reason than a FQDN modification,
3132 * the configuration file FQDN matches the fqdn server state file value.
3133 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003134 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003135 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003136 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003137 }
3138 else {
3139 /* If the FDQN has been changed from stats socket,
3140 * apply fqdn state file value (which is the value set
3141 * from stats socket).
3142 */
3143 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003144 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003145 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003146 }
3147 }
3148 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003149 /* If all the conditions below are validated, this means
3150 * we're evaluating a server managed by SRV resolution
3151 */
3152 else if (fqdn && !srv->hostname && srvrecord) {
3153 int res;
3154
3155 /* we can't apply previous state if SRV record has changed */
3156 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3157 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);
3158 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3159 goto out;
3160 }
3161
3162 /* create or find a SRV resolution for this srv record */
3163 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3164 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3165 if (srv->srvrq == NULL) {
3166 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3167 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3168 goto out;
3169 }
3170
3171 /* prepare DNS resolution for this server */
3172 res = srv_prepare_for_resolution(srv, fqdn);
3173 if (res == -1) {
3174 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3175 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3176 goto out;
3177 }
3178
3179 /* configure check.port accordingly */
3180 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3181 !(srv->flags & SRV_F_CHECKPORT))
3182 srv->check.port = port;
3183
3184 /* Unset SRV_F_MAPPORTS for SRV records.
3185 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3186 * because no ports are provided in the configuration file.
3187 * This is because HAProxy will use the port found into the SRV record.
3188 */
3189 srv->flags &= ~SRV_F_MAPPORTS;
3190 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003191
Frédéric Lécaille31694712017-08-01 08:47:19 +02003192 if (port_str)
3193 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003194 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003195
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003196 break;
3197 default:
3198 chunk_appendf(msg, ", version '%d' not supported", version);
3199 }
3200
3201 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003202 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003203 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003204 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003205 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003206 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003207}
3208
3209/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3210 * For each proxy, it does the following:
3211 * - opens its server state file (either one or local one)
3212 * - read whole file, line by line
3213 * - analyse each line to check if it matches our current backend:
3214 * - backend name matches
3215 * - backend id matches if id is forced and name doesn't match
3216 * - if the server pointed by the line is found, then state is applied
3217 *
3218 * If the running backend uuid or id differs from the state file, then HAProxy reports
3219 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003220 *
3221 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003222 */
3223void apply_server_state(void)
3224{
3225 char *cur, *end;
3226 char mybuf[SRV_STATE_LINE_MAXLEN];
3227 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003228 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3229 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003230 int arg, srv_arg, version, diff;
3231 FILE *f;
3232 char *filepath;
3233 char globalfilepath[MAXPATHLEN + 1];
3234 char localfilepath[MAXPATHLEN + 1];
3235 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003236 struct proxy *curproxy, *bk;
3237 struct server *srv;
3238
3239 globalfilepathlen = 0;
3240 /* create the globalfilepath variable */
3241 if (global.server_state_file) {
3242 /* absolute path or no base directory provided */
3243 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3244 len = strlen(global.server_state_file);
3245 if (len > MAXPATHLEN) {
3246 globalfilepathlen = 0;
3247 goto globalfileerror;
3248 }
3249 memcpy(globalfilepath, global.server_state_file, len);
3250 globalfilepath[len] = '\0';
3251 globalfilepathlen = len;
3252 }
3253 else if (global.server_state_base) {
3254 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003255 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003256 globalfilepathlen = 0;
3257 goto globalfileerror;
3258 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003259 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003260 globalfilepath[len] = 0;
3261 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003262
3263 /* append a slash if needed */
3264 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3265 if (globalfilepathlen + 1 > MAXPATHLEN) {
3266 globalfilepathlen = 0;
3267 goto globalfileerror;
3268 }
3269 globalfilepath[globalfilepathlen++] = '/';
3270 }
3271
3272 len = strlen(global.server_state_file);
3273 if (globalfilepathlen + len > MAXPATHLEN) {
3274 globalfilepathlen = 0;
3275 goto globalfileerror;
3276 }
3277 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3278 globalfilepathlen += len;
3279 globalfilepath[globalfilepathlen++] = 0;
3280 }
3281 }
3282 globalfileerror:
3283 if (globalfilepathlen == 0)
3284 globalfilepath[0] = '\0';
3285
3286 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003287 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003288 /* servers are only in backends */
3289 if (!(curproxy->cap & PR_CAP_BE))
3290 continue;
3291 fileopenerr = 0;
3292 filepath = NULL;
3293
3294 /* search server state file path and name */
3295 switch (curproxy->load_server_state_from_file) {
3296 /* read servers state from global file */
3297 case PR_SRV_STATE_FILE_GLOBAL:
3298 /* there was an error while generating global server state file path */
3299 if (globalfilepathlen == 0)
3300 continue;
3301 filepath = globalfilepath;
3302 fileopenerr = 1;
3303 break;
3304 /* this backend has its own file */
3305 case PR_SRV_STATE_FILE_LOCAL:
3306 localfilepathlen = 0;
3307 localfilepath[0] = '\0';
3308 len = 0;
3309 /* create the localfilepath variable */
3310 /* absolute path or no base directory provided */
3311 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3312 len = strlen(curproxy->server_state_file_name);
3313 if (len > MAXPATHLEN) {
3314 localfilepathlen = 0;
3315 goto localfileerror;
3316 }
3317 memcpy(localfilepath, curproxy->server_state_file_name, len);
3318 localfilepath[len] = '\0';
3319 localfilepathlen = len;
3320 }
3321 else if (global.server_state_base) {
3322 len = strlen(global.server_state_base);
3323 localfilepathlen += len;
3324
3325 if (localfilepathlen > MAXPATHLEN) {
3326 localfilepathlen = 0;
3327 goto localfileerror;
3328 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003329 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003330 localfilepath[localfilepathlen] = 0;
3331
3332 /* append a slash if needed */
3333 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3334 if (localfilepathlen + 1 > MAXPATHLEN) {
3335 localfilepathlen = 0;
3336 goto localfileerror;
3337 }
3338 localfilepath[localfilepathlen++] = '/';
3339 }
3340
3341 len = strlen(curproxy->server_state_file_name);
3342 if (localfilepathlen + len > MAXPATHLEN) {
3343 localfilepathlen = 0;
3344 goto localfileerror;
3345 }
3346 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3347 localfilepathlen += len;
3348 localfilepath[localfilepathlen++] = 0;
3349 }
3350 filepath = localfilepath;
3351 localfileerror:
3352 if (localfilepathlen == 0)
3353 localfilepath[0] = '\0';
3354
3355 break;
3356 case PR_SRV_STATE_FILE_NONE:
3357 default:
3358 continue;
3359 }
3360
3361 /* preload global state file */
3362 errno = 0;
3363 f = fopen(filepath, "r");
3364 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003365 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003366 if (!f)
3367 continue;
3368
3369 mybuf[0] = '\0';
3370 mybuflen = 0;
3371 version = 0;
3372
3373 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003374 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003375 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003376 goto fileclose;
3377 }
3378
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003379 cur = mybuf;
3380 version = atoi(cur);
3381 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3382 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003383 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003384
3385 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3386 int bk_f_forced_id = 0;
3387 int check_id = 0;
3388 int check_name = 0;
3389
3390 mybuflen = strlen(mybuf);
3391 cur = mybuf;
3392 end = cur + mybuflen;
3393
3394 bk = NULL;
3395 srv = NULL;
3396
3397 /* we need at least one character */
3398 if (mybuflen == 0)
3399 continue;
3400
3401 /* ignore blank characters at the beginning of the line */
3402 while (isspace(*cur))
3403 ++cur;
3404
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003405 /* Ignore empty or commented lines */
3406 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003407 continue;
3408
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003409 /* truncated lines */
3410 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003411 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003412 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003413 }
3414
3415 /* Removes trailing '\n' */
3416 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003417
3418 /* we're now ready to move the line into *srv_params[] */
3419 params[0] = cur;
3420 arg = 1;
3421 srv_arg = 0;
3422 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3423 if (isspace(*cur)) {
3424 *cur = '\0';
3425 ++cur;
3426 while (isspace(*cur))
3427 ++cur;
3428 switch (version) {
3429 case 1:
3430 /*
3431 * srv_addr: params[4] => srv_params[0]
3432 * srv_op_state: params[5] => srv_params[1]
3433 * srv_admin_state: params[6] => srv_params[2]
3434 * srv_uweight: params[7] => srv_params[3]
3435 * srv_iweight: params[8] => srv_params[4]
3436 * srv_last_time_change: params[9] => srv_params[5]
3437 * srv_check_status: params[10] => srv_params[6]
3438 * srv_check_result: params[11] => srv_params[7]
3439 * srv_check_health: params[12] => srv_params[8]
3440 * srv_check_state: params[13] => srv_params[9]
3441 * srv_agent_state: params[14] => srv_params[10]
3442 * bk_f_forced_id: params[15] => srv_params[11]
3443 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003444 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003445 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003446 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003447 */
3448 if (arg >= 4) {
3449 srv_params[srv_arg] = cur;
3450 ++srv_arg;
3451 }
3452 break;
3453 }
3454
3455 params[arg] = cur;
3456 ++arg;
3457 }
3458 else {
3459 ++cur;
3460 }
3461 }
3462
3463 /* if line is incomplete line, then ignore it.
3464 * otherwise, update useful flags */
3465 switch (version) {
3466 case 1:
3467 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3468 continue;
3469 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3470 check_id = (atoi(params[0]) == curproxy->uuid);
3471 check_name = (strcmp(curproxy->id, params[1]) == 0);
3472 break;
3473 }
3474
3475 diff = 0;
3476 bk = curproxy;
3477
3478 /* if backend can't be found, let's continue */
3479 if (!check_id && !check_name)
3480 continue;
3481 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003482 ha_warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003483 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3484 }
3485 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003486 ha_warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003487 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3488 /* if name doesn't match, we still want to update curproxy if the backend id
3489 * was forced in previous the previous configuration */
3490 if (!bk_f_forced_id)
3491 continue;
3492 }
3493
3494 /* look for the server by its id: param[2] */
3495 /* else look for the server by its name: param[3] */
3496 diff = 0;
3497 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3498
3499 if (!srv) {
3500 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003501 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3502 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003503 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3504 params[3], params[2], params[0], params[1]);
3505 continue;
3506 }
3507 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003508 ha_warning("In backend '%s' (id: '%d'): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003509 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server ID mismatch: from server state file: '%s', from running config %d\n", bk->id, bk->uuid, params[2], srv->puid);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003510 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003511 }
3512 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003513 ha_warning("In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003514 send_log(bk, LOG_NOTICE, "In backend '%s' (id: %d): server name mismatch: from server state file: '%s', from running config '%s'\n", bk->id, bk->uuid, params[3], srv->id);
Frédéric Lécaille0bedb8a2017-06-15 14:09:10 +02003515 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003516 }
3517
3518 /* now we can proceed with server's state update */
3519 srv_update_state(srv, version, srv_params);
3520 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003521fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003522 fclose(f);
3523 }
3524}
3525
Simon Horman7d09b9a2013-02-12 10:45:51 +09003526/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003527 * update a server's current IP address.
3528 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3529 * ip is in network format.
3530 * updater is a string which contains an information about the requester of the update.
3531 * updater is used if not NULL.
3532 *
3533 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003534 *
3535 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003536 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003537int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003538{
3539 /* generates a log line and a warning on stderr */
3540 if (1) {
3541 /* book enough space for both IPv4 and IPv6 */
3542 char oldip[INET6_ADDRSTRLEN];
3543 char newip[INET6_ADDRSTRLEN];
3544
3545 memset(oldip, '\0', INET6_ADDRSTRLEN);
3546 memset(newip, '\0', INET6_ADDRSTRLEN);
3547
3548 /* copy old IP address in a string */
3549 switch (s->addr.ss_family) {
3550 case AF_INET:
3551 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3552 break;
3553 case AF_INET6:
3554 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3555 break;
3556 };
3557
3558 /* copy new IP address in a string */
3559 switch (ip_sin_family) {
3560 case AF_INET:
3561 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3562 break;
3563 case AF_INET6:
3564 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3565 break;
3566 };
3567
3568 /* save log line into a buffer */
3569 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3570 s->proxy->id, s->id, oldip, newip, updater);
3571
3572 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003573 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003574
3575 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003576 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003577 }
3578
3579 /* save the new IP family */
3580 s->addr.ss_family = ip_sin_family;
3581 /* save the new IP address */
3582 switch (ip_sin_family) {
3583 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003584 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003585 break;
3586 case AF_INET6:
3587 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3588 break;
3589 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003590 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003591
3592 return 0;
3593}
3594
3595/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003596 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3597 *
3598 * Caller can pass its name through <updater> to get it integrated in the response message
3599 * returned by the function.
3600 *
3601 * The function first does the following, in that order:
3602 * - validates the new addr and/or port
3603 * - checks if an update is required (new IP or port is different than current ones)
3604 * - checks the update is allowed:
3605 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3606 * - allow all changes if no CHECKS are configured
3607 * - if CHECK is configured:
3608 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3609 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3610 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003611 *
3612 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003613 */
3614const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3615{
3616 struct sockaddr_storage sa;
3617 int ret, port_change_required;
3618 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003619 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003620 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003621 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003622
3623 msg = get_trash_chunk();
3624 chunk_reset(msg);
3625
3626 if (addr) {
3627 memset(&sa, 0, sizeof(struct sockaddr_storage));
3628 if (str2ip2(addr, &sa, 0) == NULL) {
3629 chunk_printf(msg, "Invalid addr '%s'", addr);
3630 goto out;
3631 }
3632
3633 /* changes are allowed on AF_INET* families only */
3634 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3635 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3636 goto out;
3637 }
3638
3639 /* collecting data currently setup */
3640 memset(current_addr, '\0', sizeof(current_addr));
3641 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3642 /* changes are allowed on AF_INET* families only */
3643 if ((ret != AF_INET) && (ret != AF_INET6)) {
3644 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3645 goto out;
3646 }
3647
3648 /* applying ADDR changes if required and allowed
3649 * ipcmp returns 0 when both ADDR are the same
3650 */
3651 if (ipcmp(&s->addr, &sa) == 0) {
3652 chunk_appendf(msg, "no need to change the addr");
3653 goto port;
3654 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003655 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003656 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003657
3658 /* we also need to update check's ADDR only if it uses the server's one */
3659 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003660 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003661 }
3662
3663 /* we also need to update agent ADDR only if it use the server's one */
3664 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003665 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003666 }
3667
3668 /* update report for caller */
3669 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3670 }
3671
3672 port:
3673 if (port) {
3674 char sign = '\0';
3675 char *endptr;
3676
3677 if (addr)
3678 chunk_appendf(msg, ", ");
3679
3680 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003681 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003682
3683 /* check if PORT change is required */
3684 port_change_required = 0;
3685
3686 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003687 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003688 new_port = strtol(port, &endptr, 10);
3689 if ((errno != 0) || (port == endptr)) {
3690 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3691 goto out;
3692 }
3693
3694 /* check if caller triggers a port mapped or offset */
3695 if (sign == '-' || (sign == '+')) {
3696 /* check if server currently uses port map */
3697 if (!(s->flags & SRV_F_MAPPORTS)) {
3698 /* switch from fixed port to port map mandatorily triggers
3699 * a port change */
3700 port_change_required = 1;
3701 /* check is configured
3702 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3703 * prevent PORT change if check doesn't have it's dedicated port while switching
3704 * to port mapping */
3705 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3706 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.");
3707 goto out;
3708 }
3709 }
3710 /* we're already using port maps */
3711 else {
3712 port_change_required = current_port != new_port;
3713 }
3714 }
3715 /* fixed port */
3716 else {
3717 port_change_required = current_port != new_port;
3718 }
3719
3720 /* applying PORT changes if required and update response message */
3721 if (port_change_required) {
3722 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003723 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003724 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003725
3726 /* prepare message */
3727 chunk_appendf(msg, "port changed from '");
3728 if (s->flags & SRV_F_MAPPORTS)
3729 chunk_appendf(msg, "+");
3730 chunk_appendf(msg, "%d' to '", current_port);
3731
3732 if (sign == '-') {
3733 s->flags |= SRV_F_MAPPORTS;
3734 chunk_appendf(msg, "%c", sign);
3735 /* just use for result output */
3736 new_port = -new_port;
3737 }
3738 else if (sign == '+') {
3739 s->flags |= SRV_F_MAPPORTS;
3740 chunk_appendf(msg, "%c", sign);
3741 }
3742 else {
3743 s->flags &= ~SRV_F_MAPPORTS;
3744 }
3745
3746 chunk_appendf(msg, "%d'", new_port);
3747
3748 /* we also need to update health checks port only if it uses server's realport */
3749 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3750 s->check.port = new_port;
3751 }
3752 }
3753 else {
3754 chunk_appendf(msg, "no need to change the port");
3755 }
3756 }
3757
3758out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003759 if (changed)
3760 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003761 if (updater)
3762 chunk_appendf(msg, " by '%s'", updater);
3763 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003764 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003765}
3766
3767
3768/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003769 * update server status based on result of name resolution
3770 * returns:
3771 * 0 if server status is updated
3772 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003773 *
3774 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003775 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003776int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003777{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003778 struct dns_resolvers *resolvers = s->resolvers;
3779 struct dns_resolution *resolution = s->dns_requester->resolution;
3780 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003781
3782 switch (resolution->status) {
3783 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003784 /* status when HAProxy has just (re)started.
3785 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003786 break;
3787
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003788 case RSLV_STATUS_VALID:
3789 /*
3790 * resume health checks
3791 * server will be turned back on if health check is safe
3792 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003793 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003794 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003795 return 1;
3796 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3797 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003798 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003799 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003800
Emeric Brun52a91d32017-08-31 14:41:55 +02003801 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003802 return 1;
3803 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3804 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3805 s->proxy->id, s->id);
3806
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003807 ha_warning("%s.\n", trash.area);
3808 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003809 return 0;
3810
3811 case RSLV_STATUS_NX:
3812 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003813 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3814 if (!tick_is_expired(exp, now_ms))
3815 break;
3816
3817 if (s->next_admin & SRV_ADMF_RMAINT)
3818 return 1;
3819 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3820 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003821
3822 case RSLV_STATUS_TIMEOUT:
3823 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003824 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3825 if (!tick_is_expired(exp, now_ms))
3826 break;
3827
3828 if (s->next_admin & SRV_ADMF_RMAINT)
3829 return 1;
3830 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3831 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003832
3833 case RSLV_STATUS_REFUSED:
3834 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003835 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3836 if (!tick_is_expired(exp, now_ms))
3837 break;
3838
3839 if (s->next_admin & SRV_ADMF_RMAINT)
3840 return 1;
3841 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3842 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003843
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003844 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003845 /* stop server if resolution failed for a long enough period */
3846 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3847 if (!tick_is_expired(exp, now_ms))
3848 break;
3849
3850 if (s->next_admin & SRV_ADMF_RMAINT)
3851 return 1;
3852 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3853 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003854 }
3855
3856 return 1;
3857}
3858
3859/*
3860 * Server Name Resolution valid response callback
3861 * It expects:
3862 * - <nameserver>: the name server which answered the valid response
3863 * - <response>: buffer containing a valid DNS response
3864 * - <response_len>: size of <response>
3865 * It performs the following actions:
3866 * - ignore response if current ip found and server family not met
3867 * - update with first new ip found if family is met and current IP is not found
3868 * returns:
3869 * 0 on error
3870 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003871 *
3872 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003873 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003874int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003875{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003876 struct server *s = NULL;
3877 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003878 void *serverip, *firstip;
3879 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003880 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003881 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003882 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003883
Christopher Faulet67957bd2017-09-27 11:00:59 +02003884 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885 if (!s)
3886 return 1;
3887
Christopher Faulet67957bd2017-09-27 11:00:59 +02003888 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003889
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003890 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003891 firstip = NULL; /* pointer to the first valid response found */
3892 /* it will be used as the new IP if a change is required */
3893 firstip_sin_family = AF_UNSPEC;
3894 serverip = NULL; /* current server IP address */
3895
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003896 /* initializing server IP pointer */
3897 server_sin_family = s->addr.ss_family;
3898 switch (server_sin_family) {
3899 case AF_INET:
3900 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3901 break;
3902
3903 case AF_INET6:
3904 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3905 break;
3906
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003907 case AF_UNSPEC:
3908 break;
3909
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003910 default:
3911 goto invalid;
3912 }
3913
Baptiste Assmann729c9012017-05-22 15:13:10 +02003914 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003915 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003916 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003917
3918 switch (ret) {
3919 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003920 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003921
3922 case DNS_UPD_SRVIP_NOT_FOUND:
3923 goto save_ip;
3924
3925 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003926 goto invalid;
3927
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003928 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003929 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003930 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003931
Baptiste Assmannfad03182015-10-28 02:03:32 +01003932 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003933 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003934 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003935 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003936
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003937 default:
3938 goto invalid;
3939
3940 }
3941
3942 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003943 if (nameserver) {
3944 nameserver->counters.update++;
3945 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003946 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003947 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003948 else
3949 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003950 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003951
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003952 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003953 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003954 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003955
3956 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003957 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003958 nameserver->counters.invalid++;
3959 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003960 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003961 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003962 return 0;
3963}
3964
3965/*
3966 * Server Name Resolution error management callback
3967 * returns:
3968 * 0 on error
3969 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003970 *
3971 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003972 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003973int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003974{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003975 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003976
Christopher Faulet67957bd2017-09-27 11:00:59 +02003977 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003978 if (!s)
3979 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003980 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003981 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003982 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003983 return 1;
3984}
3985
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003986/*
3987 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003988 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003989 * It returns a pointer to the first server found or NULL if <ip> is not yet
3990 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003991 *
3992 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003993 */
3994struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3995{
3996 struct server *tmpsrv;
3997 struct proxy *be;
3998
3999 if (!srv)
4000 return NULL;
4001
4002 be = srv->proxy;
4003 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01004004 /* we found the current server is the same, ignore it */
4005 if (srv == tmpsrv)
4006 continue;
4007
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004008 /* We want to compare the IP in the record with the IP of the servers in the
4009 * same backend, only if:
4010 * * DNS resolution is enabled on the server
4011 * * the hostname used for the resolution by our server is the same than the
4012 * one used for the server found in the backend
4013 * * the server found in the backend is not our current server
4014 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004015 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004016 if ((tmpsrv->hostname_dn == NULL) ||
4017 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
4018 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01004019 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004020 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004021 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004022 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004023
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004024 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01004025 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004026 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004027 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01004028 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02004029
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004030 /* At this point, we have 2 different servers using the same DNS hostname
4031 * for their respective resolution.
4032 */
4033 if (*ip_family == tmpsrv->addr.ss_family &&
4034 ((tmpsrv->addr.ss_family == AF_INET &&
4035 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
4036 (tmpsrv->addr.ss_family == AF_INET6 &&
4037 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004038 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004039 return tmpsrv;
4040 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004041 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004042 }
4043
Emeric Brune9fd6b52017-11-02 17:20:39 +01004044
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02004045 return NULL;
4046}
4047
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004048/* Sets the server's address (srv->addr) from srv->hostname using the libc's
4049 * resolver. This is suited for initial address configuration. Returns 0 on
4050 * success otherwise a non-zero error code. In case of error, *err_code, if
4051 * not NULL, is filled up.
4052 */
4053int srv_set_addr_via_libc(struct server *srv, int *err_code)
4054{
4055 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004056 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01004057 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004058 return 1;
4059 }
4060 return 0;
4061}
4062
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004063/* Set the server's FDQN (->hostname) from <hostname>.
4064 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004065 *
4066 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004067 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004068int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004069{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004070 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004071 char *hostname_dn;
4072 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004073
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004074 /* Note that the server lock is already held. */
4075 if (!srv->resolvers)
4076 return -1;
4077
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004078 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004079 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004080 /* run time DNS resolution was not active for this server
4081 * and we can't enable it at run time for now.
4082 */
4083 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004084 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004085
4086 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004087 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004088 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004089 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4090 hostname_dn, trash.size);
4091 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004092 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004093
Christopher Faulet67957bd2017-09-27 11:00:59 +02004094 resolution = srv->dns_requester->resolution;
4095 if (resolution &&
4096 resolution->hostname_dn &&
4097 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004098 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004099
Christopher Faulet67957bd2017-09-27 11:00:59 +02004100 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004101
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004102 free(srv->hostname);
4103 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004104 srv->hostname = strdup(hostname);
4105 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004106 srv->hostname_dn_len = hostname_dn_len;
4107 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004108 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004109
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004110 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004111 goto err;
4112
4113 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004114 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004115 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004116 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004117
4118 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004119 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004120 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004121 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004122}
4123
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004124/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4125 * from the state file. This is suited for initial address configuration.
4126 * Returns 0 on success otherwise a non-zero error code. In case of error,
4127 * *err_code, if not NULL, is filled up.
4128 */
4129static int srv_apply_lastaddr(struct server *srv, int *err_code)
4130{
4131 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4132 if (err_code)
4133 *err_code |= ERR_WARN;
4134 return 1;
4135 }
4136 return 0;
4137}
4138
Willy Tarreau25e51522016-11-04 15:10:17 +01004139/* returns 0 if no error, otherwise a combination of ERR_* flags */
4140static int srv_iterate_initaddr(struct server *srv)
4141{
4142 int return_code = 0;
4143 int err_code;
4144 unsigned int methods;
4145
4146 methods = srv->init_addr_methods;
4147 if (!methods) { // default to "last,libc"
4148 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4149 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4150 }
4151
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004152 /* "-dr" : always append "none" so that server addresses resolution
4153 * failures are silently ignored, this is convenient to validate some
4154 * configs out of their environment.
4155 */
4156 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4157 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4158
Willy Tarreau25e51522016-11-04 15:10:17 +01004159 while (methods) {
4160 err_code = 0;
4161 switch (srv_get_next_initaddr(&methods)) {
4162 case SRV_IADDR_LAST:
4163 if (!srv->lastaddr)
4164 continue;
4165 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004166 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004167 return_code |= err_code;
4168 break;
4169
4170 case SRV_IADDR_LIBC:
4171 if (!srv->hostname)
4172 continue;
4173 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004174 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004175 return_code |= err_code;
4176 break;
4177
Willy Tarreau37ebe122016-11-04 15:17:58 +01004178 case SRV_IADDR_NONE:
4179 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004180 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004181 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4182 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004183 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004184 return return_code;
4185
Willy Tarreau4310d362016-11-02 15:05:56 +01004186 case SRV_IADDR_IP:
4187 ipcpy(&srv->init_addr, &srv->addr);
4188 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004189 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4190 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004191 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004192 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004193
Willy Tarreau25e51522016-11-04 15:10:17 +01004194 default: /* unhandled method */
4195 break;
4196 }
4197 }
4198
4199 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004200 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004201 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4202 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004203 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004204 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004205 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4206 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004207
4208 return_code |= ERR_ALERT | ERR_FATAL;
4209 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004210out:
4211 srv_set_dyncookie(srv);
4212 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004213}
4214
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004215/*
4216 * This function parses all backends and all servers within each backend
4217 * and performs servers' addr resolution based on information provided by:
4218 * - configuration file
4219 * - server-state file (states provided by an 'old' haproxy process)
4220 *
4221 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4222 */
4223int srv_init_addr(void)
4224{
4225 struct proxy *curproxy;
4226 int return_code = 0;
4227
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004228 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004229 while (curproxy) {
4230 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004231
4232 /* servers are in backend only */
4233 if (!(curproxy->cap & PR_CAP_BE))
4234 goto srv_init_addr_next;
4235
Willy Tarreau25e51522016-11-04 15:10:17 +01004236 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004237 if (srv->hostname)
4238 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004239
4240 srv_init_addr_next:
4241 curproxy = curproxy->next;
4242 }
4243
4244 return return_code;
4245}
4246
Willy Tarreau46b7f532018-08-21 11:54:26 +02004247/*
4248 * Must be called with the server lock held.
4249 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004250const 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 +02004251{
4252
Willy Tarreau83061a82018-07-13 11:56:34 +02004253 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004254
4255 msg = get_trash_chunk();
4256 chunk_reset(msg);
4257
Olivier Houchard8da5f982017-08-04 18:35:36 +02004258 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004259 chunk_appendf(msg, "no need to change the FDQN");
4260 goto out;
4261 }
4262
4263 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4264 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4265 goto out;
4266 }
4267
4268 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4269 server->proxy->id, server->id, server->hostname, fqdn);
4270
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004271 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004272 chunk_reset(msg);
4273 chunk_appendf(msg, "could not update %s/%s FQDN",
4274 server->proxy->id, server->id);
4275 goto out;
4276 }
4277
4278 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004279 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004280
4281 out:
4282 if (updater)
4283 chunk_appendf(msg, " by '%s'", updater);
4284 chunk_appendf(msg, "\n");
4285
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004286 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004287}
4288
4289
Willy Tarreau21b069d2016-11-23 17:15:08 +01004290/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4291 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004292 * 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 +01004293 * used for CLI commands requiring a server name.
4294 * Important: the <arg> is modified to remove the '/'.
4295 */
4296struct server *cli_find_server(struct appctx *appctx, char *arg)
4297{
4298 struct proxy *px;
4299 struct server *sv;
4300 char *line;
4301
4302 /* split "backend/server" and make <line> point to server */
4303 for (line = arg; *line; line++)
4304 if (*line == '/') {
4305 *line++ = '\0';
4306 break;
4307 }
4308
4309 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004310 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004311 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004312 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004313 return NULL;
4314 }
4315
4316 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004317 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004318 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004319 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004320 return NULL;
4321 }
4322
4323 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004324 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004325 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004326 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004327 return NULL;
4328 }
4329
4330 return sv;
4331}
4332
William Lallemand222baf22016-11-19 02:00:33 +01004333
Willy Tarreau46b7f532018-08-21 11:54:26 +02004334/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004335static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004336{
4337 struct server *sv;
4338 const char *warning;
4339
4340 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4341 return 1;
4342
4343 sv = cli_find_server(appctx, args[2]);
4344 if (!sv)
4345 return 1;
4346
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004347 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004348
William Lallemand222baf22016-11-19 02:00:33 +01004349 if (strcmp(args[3], "weight") == 0) {
4350 warning = server_parse_weight_change_request(sv, args[4]);
4351 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004352 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004353 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004354 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004355 }
4356 }
4357 else if (strcmp(args[3], "state") == 0) {
4358 if (strcmp(args[4], "ready") == 0)
4359 srv_adm_set_ready(sv);
4360 else if (strcmp(args[4], "drain") == 0)
4361 srv_adm_set_drain(sv);
4362 else if (strcmp(args[4], "maint") == 0)
4363 srv_adm_set_maint(sv);
4364 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004365 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004366 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004367 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004368 }
4369 }
4370 else if (strcmp(args[3], "health") == 0) {
4371 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004372 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004373 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004374 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004375 }
4376 else if (strcmp(args[4], "up") == 0) {
4377 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004378 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004379 }
4380 else if (strcmp(args[4], "stopping") == 0) {
4381 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004382 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004383 }
4384 else if (strcmp(args[4], "down") == 0) {
4385 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004386 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004387 }
4388 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004389 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004390 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004391 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004392 }
4393 }
4394 else if (strcmp(args[3], "agent") == 0) {
4395 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004396 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004397 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004398 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004399 }
4400 else if (strcmp(args[4], "up") == 0) {
4401 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004402 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004403 }
4404 else if (strcmp(args[4], "down") == 0) {
4405 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004406 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004407 }
4408 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004409 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004410 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004411 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004412 }
4413 }
Misiek2da082d2017-01-09 09:40:42 +01004414 else if (strcmp(args[3], "agent-addr") == 0) {
4415 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004416 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004417 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4418 appctx->st0 = CLI_ST_PRINT;
4419 } else {
4420 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004421 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004422 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4423 appctx->st0 = CLI_ST_PRINT;
4424 }
4425 }
4426 }
4427 else if (strcmp(args[3], "agent-send") == 0) {
4428 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004429 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004430 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4431 appctx->st0 = CLI_ST_PRINT;
4432 } else {
4433 char *nss = strdup(args[4]);
4434 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004435 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004436 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4437 appctx->st0 = CLI_ST_PRINT;
4438 } else {
4439 free(sv->agent.send_string);
4440 sv->agent.send_string = nss;
4441 sv->agent.send_string_len = strlen(args[4]);
4442 }
4443 }
4444 }
William Lallemand222baf22016-11-19 02:00:33 +01004445 else if (strcmp(args[3], "check-port") == 0) {
4446 int i = 0;
4447 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004448 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004449 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004450 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004451 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004452 }
4453 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004454 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004455 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004456 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004457 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004458 }
4459 /* prevent the update of port to 0 if MAPPORTS are in use */
4460 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004461 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004462 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004463 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004464 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004465 }
4466 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004467 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004468 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004469 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004470 }
4471 else if (strcmp(args[3], "addr") == 0) {
4472 char *addr = NULL;
4473 char *port = NULL;
4474 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004475 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004476 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004477 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004478 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004479 }
4480 else {
4481 addr = args[4];
4482 }
4483 if (strcmp(args[5], "port") == 0) {
4484 port = args[6];
4485 }
4486 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4487 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004488 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004489 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004490 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004491 }
4492 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4493 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004494 else if (strcmp(args[3], "fqdn") == 0) {
4495 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004496 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004497 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4498 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004499 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004500 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004501 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004502 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004503 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004504 appctx->ctx.cli.msg = warning;
4505 appctx->st0 = CLI_ST_PRINT;
4506 }
4507 }
William Lallemand222baf22016-11-19 02:00:33 +01004508 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004509 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004510 appctx->ctx.cli.msg = "'set server <srv>' only supports 'agent', 'health', 'state', 'weight', 'addr', 'fqdn' and 'check-port'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004511 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004512 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004513 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004514 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004515 return 1;
4516}
4517
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004518static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004519{
4520 struct stream_interface *si = appctx->owner;
4521 struct proxy *px;
4522 struct server *sv;
4523 char *line;
4524
4525
4526 /* split "backend/server" and make <line> point to server */
4527 for (line = args[2]; *line; line++)
4528 if (*line == '/') {
4529 *line++ = '\0';
4530 break;
4531 }
4532
4533 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004534 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004535 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004536 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004537 return 1;
4538 }
4539
4540 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004541 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004542 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004543 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004544 return 1;
4545 }
4546
4547 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004548 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4549 sv->iweight);
4550 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004551 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004552 return 0;
4553 }
William Lallemand6b160942016-11-22 12:34:35 +01004554 return 1;
4555}
4556
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004557/* Parse a "set weight" command.
4558 *
4559 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004560 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004561static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004562{
4563 struct server *sv;
4564 const char *warning;
4565
4566 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4567 return 1;
4568
4569 sv = cli_find_server(appctx, args[2]);
4570 if (!sv)
4571 return 1;
4572
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004573 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4574
William Lallemand6b160942016-11-22 12:34:35 +01004575 warning = server_parse_weight_change_request(sv, args[3]);
4576 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004577 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004578 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004579 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004580 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004581
4582 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4583
William Lallemand6b160942016-11-22 12:34:35 +01004584 return 1;
4585}
4586
Willy Tarreau46b7f532018-08-21 11:54:26 +02004587/* parse a "set maxconn server" command. It always returns 1.
4588 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004589 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004590 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004591static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004592{
4593 struct server *sv;
4594 const char *warning;
4595
4596 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4597 return 1;
4598
4599 sv = cli_find_server(appctx, args[3]);
4600 if (!sv)
4601 return 1;
4602
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004603 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4604
Willy Tarreaub8026272016-11-23 11:26:56 +01004605 warning = server_parse_maxconn_change_request(sv, args[4]);
4606 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004607 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004608 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004609 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004610 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004611
4612 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4613
Willy Tarreaub8026272016-11-23 11:26:56 +01004614 return 1;
4615}
William Lallemand6b160942016-11-22 12:34:35 +01004616
Willy Tarreau46b7f532018-08-21 11:54:26 +02004617/* parse a "disable agent" command. It always returns 1.
4618 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004619 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004620 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004621static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004622{
4623 struct server *sv;
4624
4625 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4626 return 1;
4627
4628 sv = cli_find_server(appctx, args[2]);
4629 if (!sv)
4630 return 1;
4631
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004632 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004633 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004634 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004635 return 1;
4636}
4637
Willy Tarreau46b7f532018-08-21 11:54:26 +02004638/* parse a "disable health" command. It always returns 1.
4639 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004640 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004641 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004642static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004643{
4644 struct server *sv;
4645
4646 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4647 return 1;
4648
4649 sv = cli_find_server(appctx, args[2]);
4650 if (!sv)
4651 return 1;
4652
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004653 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004654 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004655 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004656 return 1;
4657}
4658
Willy Tarreau46b7f532018-08-21 11:54:26 +02004659/* parse a "disable server" command. It always returns 1.
4660 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004661 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004662 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004663static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004664{
4665 struct server *sv;
4666
4667 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4668 return 1;
4669
4670 sv = cli_find_server(appctx, args[2]);
4671 if (!sv)
4672 return 1;
4673
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004674 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004675 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004676 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004677 return 1;
4678}
4679
Willy Tarreau46b7f532018-08-21 11:54:26 +02004680/* parse a "enable agent" command. It always returns 1.
4681 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004682 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004683 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004684static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004685{
4686 struct server *sv;
4687
4688 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4689 return 1;
4690
4691 sv = cli_find_server(appctx, args[2]);
4692 if (!sv)
4693 return 1;
4694
4695 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004696 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004697 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004698 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004699 return 1;
4700 }
4701
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004702 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004703 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004704 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004705 return 1;
4706}
4707
Willy Tarreau46b7f532018-08-21 11:54:26 +02004708/* parse a "enable health" command. It always returns 1.
4709 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004710 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004711 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004712static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004713{
4714 struct server *sv;
4715
4716 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4717 return 1;
4718
4719 sv = cli_find_server(appctx, args[2]);
4720 if (!sv)
4721 return 1;
4722
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004723 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004724 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004725 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004726 return 1;
4727}
4728
Willy Tarreau46b7f532018-08-21 11:54:26 +02004729/* parse a "enable server" command. It always returns 1.
4730 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004731 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004732 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004733static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004734{
4735 struct server *sv;
4736
4737 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4738 return 1;
4739
4740 sv = cli_find_server(appctx, args[2]);
4741 if (!sv)
4742 return 1;
4743
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004744 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004745 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004746 if (!(sv->flags & SRV_F_COOKIESET)
4747 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4748 sv->cookie)
4749 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004750 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004751 return 1;
4752}
4753
William Lallemand222baf22016-11-19 02:00:33 +01004754/* register cli keywords */
4755static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004756 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004757 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004758 { { "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 +01004759 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004760 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004761 { { "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 +01004762 { { "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 +01004763 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004764 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4765 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4766
William Lallemand222baf22016-11-19 02:00:33 +01004767 {{},}
4768}};
4769
Willy Tarreau0108d902018-11-25 19:14:37 +01004770INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004771
Emeric Brun64cc49c2017-10-03 14:46:45 +02004772/*
4773 * This function applies server's status changes, it is
4774 * is designed to be called asynchronously.
4775 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004776 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004777 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004778static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004779{
4780 struct check *check = &s->check;
4781 int xferred;
4782 struct proxy *px = s->proxy;
4783 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4784 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4785 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004786 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004787
Emeric Brun64cc49c2017-10-03 14:46:45 +02004788 /* If currently main is not set we try to apply pending state changes */
4789 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4790 int next_admin;
4791
4792 /* Backup next admin */
4793 next_admin = s->next_admin;
4794
4795 /* restore current admin state */
4796 s->next_admin = s->cur_admin;
4797
4798 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4799 s->last_change = now.tv_sec;
4800 if (s->proxy->lbprm.set_server_status_down)
4801 s->proxy->lbprm.set_server_status_down(s);
4802
4803 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4804 srv_shutdown_streams(s, SF_ERR_DOWN);
4805
4806 /* we might have streams queued on this server and waiting for
4807 * a connection. Those which are redispatchable will be queued
4808 * to another server or to the proxy itself.
4809 */
4810 xferred = pendconn_redistribute(s);
4811
4812 tmptrash = alloc_trash_chunk();
4813 if (tmptrash) {
4814 chunk_printf(tmptrash,
4815 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4816 s->proxy->id, s->id);
4817
Emeric Brun5a133512017-10-19 14:42:30 +02004818 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004819 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004820
4821 /* we don't send an alert if the server was previously paused */
4822 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004823 send_log(s->proxy, log_level, "%s.\n",
4824 tmptrash->area);
4825 send_email_alert(s, log_level, "%s",
4826 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004827 free_trash_chunk(tmptrash);
4828 tmptrash = NULL;
4829 }
4830 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4831 set_backend_down(s->proxy);
4832
4833 s->counters.down_trans++;
4834 }
4835 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4836 s->last_change = now.tv_sec;
4837 if (s->proxy->lbprm.set_server_status_down)
4838 s->proxy->lbprm.set_server_status_down(s);
4839
4840 /* we might have streams queued on this server and waiting for
4841 * a connection. Those which are redispatchable will be queued
4842 * to another server or to the proxy itself.
4843 */
4844 xferred = pendconn_redistribute(s);
4845
4846 tmptrash = alloc_trash_chunk();
4847 if (tmptrash) {
4848 chunk_printf(tmptrash,
4849 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4850 s->proxy->id, s->id);
4851
Emeric Brun5a133512017-10-19 14:42:30 +02004852 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004853
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004854 ha_warning("%s.\n", tmptrash->area);
4855 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4856 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004857 free_trash_chunk(tmptrash);
4858 tmptrash = NULL;
4859 }
4860
4861 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4862 set_backend_down(s->proxy);
4863 }
4864 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4865 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4866 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4867 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4868 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4869 s->proxy->last_change = now.tv_sec;
4870 }
4871
4872 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4873 s->down_time += now.tv_sec - s->last_change;
4874
4875 s->last_change = now.tv_sec;
4876 if (s->next_state == SRV_ST_STARTING)
4877 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4878
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004879 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004880 /* now propagate the status change to any LB algorithms */
4881 if (px->lbprm.update_server_eweight)
4882 px->lbprm.update_server_eweight(s);
4883 else if (srv_willbe_usable(s)) {
4884 if (px->lbprm.set_server_status_up)
4885 px->lbprm.set_server_status_up(s);
4886 }
4887 else {
4888 if (px->lbprm.set_server_status_down)
4889 px->lbprm.set_server_status_down(s);
4890 }
4891
4892 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4893 * and it's not a backup server and its effective weight is > 0,
4894 * then it can accept new connections, so we shut down all streams
4895 * on all backup servers.
4896 */
4897 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4898 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4899 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4900
4901 /* check if we can handle some connections queued at the proxy. We
4902 * will take as many as we can handle.
4903 */
4904 xferred = pendconn_grab_from_px(s);
4905
4906 tmptrash = alloc_trash_chunk();
4907 if (tmptrash) {
4908 chunk_printf(tmptrash,
4909 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4910 s->proxy->id, s->id);
4911
Emeric Brun5a133512017-10-19 14:42:30 +02004912 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004913 ha_warning("%s.\n", tmptrash->area);
4914 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4915 tmptrash->area);
4916 send_email_alert(s, LOG_NOTICE, "%s",
4917 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004918 free_trash_chunk(tmptrash);
4919 tmptrash = NULL;
4920 }
4921
4922 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4923 set_backend_down(s->proxy);
4924 }
4925 else if (s->cur_eweight != s->next_eweight) {
4926 /* now propagate the status change to any LB algorithms */
4927 if (px->lbprm.update_server_eweight)
4928 px->lbprm.update_server_eweight(s);
4929 else if (srv_willbe_usable(s)) {
4930 if (px->lbprm.set_server_status_up)
4931 px->lbprm.set_server_status_up(s);
4932 }
4933 else {
4934 if (px->lbprm.set_server_status_down)
4935 px->lbprm.set_server_status_down(s);
4936 }
4937
4938 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4939 set_backend_down(s->proxy);
4940 }
4941
4942 s->next_admin = next_admin;
4943 }
4944
Emeric Brun5a133512017-10-19 14:42:30 +02004945 /* reset operational state change */
4946 *s->op_st_chg.reason = 0;
4947 s->op_st_chg.status = s->op_st_chg.code = -1;
4948 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004949
4950 /* Now we try to apply pending admin changes */
4951
4952 /* Maintenance must also disable health checks */
4953 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4954 if (s->check.state & CHK_ST_ENABLED) {
4955 s->check.state |= CHK_ST_PAUSED;
4956 check->health = 0;
4957 }
4958
4959 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004960 tmptrash = alloc_trash_chunk();
4961 if (tmptrash) {
4962 chunk_printf(tmptrash,
4963 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4964 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4965 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004966
Olivier Houchard796a2b32017-10-24 17:42:47 +02004967 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004968
Olivier Houchard796a2b32017-10-24 17:42:47 +02004969 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004970 ha_warning("%s.\n", tmptrash->area);
4971 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4972 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004973 }
4974 free_trash_chunk(tmptrash);
4975 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004976 }
Emeric Brun8f298292017-12-06 16:47:17 +01004977 /* commit new admin status */
4978
4979 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004980 }
4981 else { /* server was still running */
4982 check->health = 0; /* failure */
4983 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004984
4985 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004986 if (s->proxy->lbprm.set_server_status_down)
4987 s->proxy->lbprm.set_server_status_down(s);
4988
Emeric Brun64cc49c2017-10-03 14:46:45 +02004989 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4990 srv_shutdown_streams(s, SF_ERR_DOWN);
4991
4992 /* we might have streams queued on this server and waiting for
4993 * a connection. Those which are redispatchable will be queued
4994 * to another server or to the proxy itself.
4995 */
4996 xferred = pendconn_redistribute(s);
4997
4998 tmptrash = alloc_trash_chunk();
4999 if (tmptrash) {
5000 chunk_printf(tmptrash,
5001 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
5002 s->flags & SRV_F_BACKUP ? "Backup " : "",
5003 s->proxy->id, s->id,
5004 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5005
5006 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
5007
5008 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005009 ha_warning("%s.\n", tmptrash->area);
5010 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
5011 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005012 }
5013 free_trash_chunk(tmptrash);
5014 tmptrash = NULL;
5015 }
5016 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5017 set_backend_down(s->proxy);
5018
5019 s->counters.down_trans++;
5020 }
5021 }
5022 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
5023 /* OK here we're leaving maintenance, we have many things to check,
5024 * because the server might possibly be coming back up depending on
5025 * its state. In practice, leaving maintenance means that we should
5026 * immediately turn to UP (more or less the slowstart) under the
5027 * following conditions :
5028 * - server is neither checked nor tracked
5029 * - server tracks another server which is not checked
5030 * - server tracks another server which is already up
5031 * Which sums up as something simpler :
5032 * "either the tracking server is up or the server's checks are disabled
5033 * or up". Otherwise we only re-enable health checks. There's a special
5034 * case associated to the stopping state which can be inherited. Note
5035 * that the server might still be in drain mode, which is naturally dealt
5036 * with by the lower level functions.
5037 */
5038
5039 if (s->check.state & CHK_ST_ENABLED) {
5040 s->check.state &= ~CHK_ST_PAUSED;
5041 check->health = check->rise; /* start OK but check immediately */
5042 }
5043
5044 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
5045 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
5046 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
5047 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
5048 s->next_state = SRV_ST_STOPPING;
5049 }
5050 else {
5051 s->next_state = SRV_ST_STARTING;
5052 if (s->slowstart > 0)
5053 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
5054 else
5055 s->next_state = SRV_ST_RUNNING;
5056 }
5057
5058 }
5059
5060 tmptrash = alloc_trash_chunk();
5061 if (tmptrash) {
5062 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5063 chunk_printf(tmptrash,
5064 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5065 s->flags & SRV_F_BACKUP ? "Backup " : "",
5066 s->proxy->id, s->id,
5067 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5068 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5069 }
5070 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5071 chunk_printf(tmptrash,
5072 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5073 s->flags & SRV_F_BACKUP ? "Backup " : "",
5074 s->proxy->id, s->id, s->hostname,
5075 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5076 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5077 }
5078 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5079 chunk_printf(tmptrash,
5080 "%sServer %s/%s is %s/%s (leaving maintenance)",
5081 s->flags & SRV_F_BACKUP ? "Backup " : "",
5082 s->proxy->id, s->id,
5083 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5084 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5085 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005086 ha_warning("%s.\n", tmptrash->area);
5087 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5088 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005089 free_trash_chunk(tmptrash);
5090 tmptrash = NULL;
5091 }
5092
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005093 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005094 /* now propagate the status change to any LB algorithms */
5095 if (px->lbprm.update_server_eweight)
5096 px->lbprm.update_server_eweight(s);
5097 else if (srv_willbe_usable(s)) {
5098 if (px->lbprm.set_server_status_up)
5099 px->lbprm.set_server_status_up(s);
5100 }
5101 else {
5102 if (px->lbprm.set_server_status_down)
5103 px->lbprm.set_server_status_down(s);
5104 }
5105
5106 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5107 set_backend_down(s->proxy);
5108
Willy Tarreau6a78e612018-08-07 10:14:53 +02005109 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5110 * and it's not a backup server and its effective weight is > 0,
5111 * then it can accept new connections, so we shut down all streams
5112 * on all backup servers.
5113 */
5114 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5115 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5116 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5117
5118 /* check if we can handle some connections queued at the proxy. We
5119 * will take as many as we can handle.
5120 */
5121 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005122 }
5123 else if (s->next_admin & SRV_ADMF_MAINT) {
5124 /* remaining in maintenance mode, let's inform precisely about the
5125 * situation.
5126 */
5127 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5128 tmptrash = alloc_trash_chunk();
5129 if (tmptrash) {
5130 chunk_printf(tmptrash,
5131 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5132 s->flags & SRV_F_BACKUP ? "Backup " : "",
5133 s->proxy->id, s->id);
5134
5135 if (s->track) /* normally it's mandatory here */
5136 chunk_appendf(tmptrash, " via %s/%s",
5137 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005138 ha_warning("%s.\n", tmptrash->area);
5139 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5140 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005141 free_trash_chunk(tmptrash);
5142 tmptrash = NULL;
5143 }
5144 }
5145 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5146 tmptrash = alloc_trash_chunk();
5147 if (tmptrash) {
5148 chunk_printf(tmptrash,
5149 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5150 s->flags & SRV_F_BACKUP ? "Backup " : "",
5151 s->proxy->id, s->id, s->hostname);
5152
5153 if (s->track) /* normally it's mandatory here */
5154 chunk_appendf(tmptrash, " via %s/%s",
5155 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005156 ha_warning("%s.\n", tmptrash->area);
5157 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5158 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005159 free_trash_chunk(tmptrash);
5160 tmptrash = NULL;
5161 }
5162 }
5163 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5164 tmptrash = alloc_trash_chunk();
5165 if (tmptrash) {
5166 chunk_printf(tmptrash,
5167 "%sServer %s/%s remains in forced maintenance",
5168 s->flags & SRV_F_BACKUP ? "Backup " : "",
5169 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005170 ha_warning("%s.\n", tmptrash->area);
5171 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5172 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005173 free_trash_chunk(tmptrash);
5174 tmptrash = NULL;
5175 }
5176 }
5177 /* don't report anything when leaving drain mode and remaining in maintenance */
5178
5179 s->cur_admin = s->next_admin;
5180 }
5181
5182 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5183 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5184 /* drain state is applied only if not yet in maint */
5185
5186 s->last_change = now.tv_sec;
5187 if (px->lbprm.set_server_status_down)
5188 px->lbprm.set_server_status_down(s);
5189
5190 /* we might have streams queued on this server and waiting for
5191 * a connection. Those which are redispatchable will be queued
5192 * to another server or to the proxy itself.
5193 */
5194 xferred = pendconn_redistribute(s);
5195
5196 tmptrash = alloc_trash_chunk();
5197 if (tmptrash) {
5198 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5199 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5200 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5201
5202 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5203
5204 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005205 ha_warning("%s.\n", tmptrash->area);
5206 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5207 tmptrash->area);
5208 send_email_alert(s, LOG_NOTICE, "%s",
5209 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005210 }
5211 free_trash_chunk(tmptrash);
5212 tmptrash = NULL;
5213 }
5214
5215 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5216 set_backend_down(s->proxy);
5217 }
5218 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5219 /* OK completely leaving drain mode */
5220 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5221 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5222 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5223 s->proxy->last_change = now.tv_sec;
5224 }
5225
5226 if (s->last_change < now.tv_sec) // ignore negative times
5227 s->down_time += now.tv_sec - s->last_change;
5228 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005229 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005230
5231 tmptrash = alloc_trash_chunk();
5232 if (tmptrash) {
5233 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5234 chunk_printf(tmptrash,
5235 "%sServer %s/%s is %s (leaving forced drain)",
5236 s->flags & SRV_F_BACKUP ? "Backup " : "",
5237 s->proxy->id, s->id,
5238 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5239 }
5240 else {
5241 chunk_printf(tmptrash,
5242 "%sServer %s/%s is %s (leaving drain)",
5243 s->flags & SRV_F_BACKUP ? "Backup " : "",
5244 s->proxy->id, s->id,
5245 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5246 if (s->track) /* normally it's mandatory here */
5247 chunk_appendf(tmptrash, " via %s/%s",
5248 s->track->proxy->id, s->track->id);
5249 }
5250
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005251 ha_warning("%s.\n", tmptrash->area);
5252 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5253 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005254 free_trash_chunk(tmptrash);
5255 tmptrash = NULL;
5256 }
5257
5258 /* now propagate the status change to any LB algorithms */
5259 if (px->lbprm.update_server_eweight)
5260 px->lbprm.update_server_eweight(s);
5261 else if (srv_willbe_usable(s)) {
5262 if (px->lbprm.set_server_status_up)
5263 px->lbprm.set_server_status_up(s);
5264 }
5265 else {
5266 if (px->lbprm.set_server_status_down)
5267 px->lbprm.set_server_status_down(s);
5268 }
5269 }
5270 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5271 /* remaining in drain mode after removing one of its flags */
5272
5273 tmptrash = alloc_trash_chunk();
5274 if (tmptrash) {
5275 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5276 chunk_printf(tmptrash,
5277 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5278 s->flags & SRV_F_BACKUP ? "Backup " : "",
5279 s->proxy->id, s->id);
5280
5281 if (s->track) /* normally it's mandatory here */
5282 chunk_appendf(tmptrash, " via %s/%s",
5283 s->track->proxy->id, s->track->id);
5284 }
5285 else {
5286 chunk_printf(tmptrash,
5287 "%sServer %s/%s remains in forced drain mode",
5288 s->flags & SRV_F_BACKUP ? "Backup " : "",
5289 s->proxy->id, s->id);
5290 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005291 ha_warning("%s.\n", tmptrash->area);
5292 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5293 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005294 free_trash_chunk(tmptrash);
5295 tmptrash = NULL;
5296 }
5297
5298 /* commit new admin status */
5299
5300 s->cur_admin = s->next_admin;
5301 }
5302 }
5303
5304 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005305 *s->adm_st_chg_cause = 0;
5306}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005307
Willy Tarreau980855b2019-02-07 14:59:29 +01005308struct task *srv_cleanup_idle_connections(struct task *task, void *context, unsigned short state)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005309{
5310 struct server *srv = context;
5311 struct connection *conn, *conn_back;
Olivier Houchardf1314812019-02-18 16:41:17 +01005312 unsigned int to_destroy = srv->curr_idle_thr[tid] / 2 + (srv->curr_idle_thr[tid] & 1);
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005313 unsigned int i = 0;
5314
5315
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005316
5317 list_for_each_entry_safe(conn, conn_back, &srv->idle_orphan_conns[tid], list) {
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005318 if (i == to_destroy)
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005319 break;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005320 conn->mux->destroy(conn);
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005321 i++;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005322 }
Olivier Houchardb7b3faa2018-12-14 18:15:36 +01005323 if (!LIST_ISEMPTY(&srv->idle_orphan_conns[tid]))
5324 task_schedule(task, tick_add(now_ms, srv->pool_purge_delay));
5325 else
5326 task->expire = TICK_ETERNITY;
Olivier Houchard0c18a6f2018-12-02 14:11:41 +01005327 return task;
5328}
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005329/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005330 * Local variables:
5331 * c-indent-level: 8
5332 * c-basic-offset: 8
5333 * End:
5334 */