blob: e6944c2e1cb3fa6ba1ba8949fba5e065cf742130 [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>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020035#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020036#include <proto/port_range.h>
37#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020038#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010039#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010042#include <proto/stream_interface.h>
43#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020045#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010046#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020047
Willy Tarreau3ff577e2018-08-02 11:48:52 +020048static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020049static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010051static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Simon Hormana3608442013-11-01 16:46:15 +090058int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020059{
Emeric Brun52a91d32017-08-31 14:41:55 +020060 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061 return s->down_time;
62
63 return now.tv_sec - s->last_change + s->down_time;
64}
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066int srv_lastsession(const struct server *s)
67{
68 if (s->counters.last_sess)
69 return now.tv_sec - s->counters.last_sess;
70
71 return -1;
72}
73
Simon Horman4a741432013-02-23 15:35:38 +090074int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Simon Horman4a741432013-02-23 15:35:38 +090076 const struct server *s = check->server;
77
Willy Tarreauff5ae352013-12-11 20:36:34 +010078 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090079 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085}
86
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010087/*
88 * Check that we did not get a hash collision.
89 * Unlikely, but it can happen.
90 */
91static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010092{
93 struct proxy *p = s->proxy;
94 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010095
96 for (tmpserv = p->srv; tmpserv != NULL;
97 tmpserv = tmpserv->next) {
98 if (tmpserv == s)
99 continue;
100 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
101 continue;
102 if (tmpserv->cookie &&
103 strcmp(tmpserv->cookie, s->cookie) == 0) {
104 ha_warning("We generated two equal cookies for two different servers.\n"
105 "Please change the secret key for '%s'.\n",
106 s->proxy->id);
107 }
108 }
109
110}
111
Willy Tarreau46b7f532018-08-21 11:54:26 +0200112/*
113 * Must be called with the server lock held.
114 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100115void srv_set_dyncookie(struct server *s)
116{
117 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100118 char *tmpbuf;
119 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100120 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100121 size_t buffer_len;
122 int addr_len;
123 int port;
124
125 if ((s->flags & SRV_F_COOKIESET) ||
126 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
127 s->proxy->dyncookie_key == NULL)
128 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100129 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100130
131 if (s->addr.ss_family != AF_INET &&
132 s->addr.ss_family != AF_INET6)
133 return;
134 /*
135 * Buffer to calculate the cookie value.
136 * The buffer contains the secret key + the server IP address
137 * + the TCP port.
138 */
139 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
140 /*
141 * The TCP port should use only 2 bytes, but is stored in
142 * an unsigned int in struct server, so let's use 4, to be
143 * on the safe side.
144 */
145 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200146 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100147 memcpy(tmpbuf, p->dyncookie_key, key_len);
148 memcpy(&(tmpbuf[key_len]),
149 s->addr.ss_family == AF_INET ?
150 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
151 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
152 addr_len);
153 /*
154 * Make sure it's the same across all the load balancers,
155 * no matter their endianness.
156 */
157 port = htonl(s->svc_port);
158 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
159 hash_value = XXH64(tmpbuf, buffer_len, 0);
160 memprintf(&s->cookie, "%016llx", hash_value);
161 if (!s->cookie)
162 return;
163 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100164
165 /* Don't bother checking if the dyncookie is duplicated if
166 * the server is marked as "disabled", maybe it doesn't have
167 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100168 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100169 if (!(s->next_admin & SRV_ADMF_FMAINT))
170 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100171}
172
Willy Tarreau21faa912012-10-10 08:27:36 +0200173/*
174 * Registers the server keyword list <kwl> as a list of valid keywords for next
175 * parsing sessions.
176 */
177void srv_register_keywords(struct srv_kw_list *kwl)
178{
179 LIST_ADDQ(&srv_keywords.list, &kwl->list);
180}
181
182/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
183 * keyword is found with a NULL ->parse() function, then an attempt is made to
184 * find one with a valid ->parse() function. This way it is possible to declare
185 * platform-dependant, known keywords as NULL, then only declare them as valid
186 * if some options are met. Note that if the requested keyword contains an
187 * opening parenthesis, everything from this point is ignored.
188 */
189struct srv_kw *srv_find_kw(const char *kw)
190{
191 int index;
192 const char *kwend;
193 struct srv_kw_list *kwl;
194 struct srv_kw *ret = NULL;
195
196 kwend = strchr(kw, '(');
197 if (!kwend)
198 kwend = kw + strlen(kw);
199
200 list_for_each_entry(kwl, &srv_keywords.list, list) {
201 for (index = 0; kwl->kw[index].kw != NULL; index++) {
202 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
203 kwl->kw[index].kw[kwend-kw] == 0) {
204 if (kwl->kw[index].parse)
205 return &kwl->kw[index]; /* found it !*/
206 else
207 ret = &kwl->kw[index]; /* may be OK */
208 }
209 }
210 }
211 return ret;
212}
213
214/* Dumps all registered "server" keywords to the <out> string pointer. The
215 * unsupported keywords are only dumped if their supported form was not
216 * found.
217 */
218void srv_dump_kws(char **out)
219{
220 struct srv_kw_list *kwl;
221 int index;
222
223 *out = NULL;
224 list_for_each_entry(kwl, &srv_keywords.list, list) {
225 for (index = 0; kwl->kw[index].kw != NULL; index++) {
226 if (kwl->kw[index].parse ||
227 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
228 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
229 kwl->scope,
230 kwl->kw[index].kw,
231 kwl->kw[index].skip ? " <arg>" : "",
232 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
233 kwl->kw[index].parse ? "" : " (not supported)");
234 }
235 }
236 }
237}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100238
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100239/* Parse the "addr" server keyword */
240static int srv_parse_addr(char **args, int *cur_arg,
241 struct proxy *curproxy, struct server *newsrv, char **err)
242{
243 char *errmsg, *arg;
244 struct sockaddr_storage *sk;
245 int port1, port2;
246 struct protocol *proto;
247
248 errmsg = NULL;
249 arg = args[*cur_arg + 1];
250
251 if (!*arg) {
252 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
253 goto err;
254 }
255
256 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
257 if (!sk) {
258 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
259 goto err;
260 }
261
262 proto = protocol_by_family(sk->ss_family);
263 if (!proto || !proto->connect) {
264 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
265 args[*cur_arg], arg);
266 goto err;
267 }
268
269 if (port1 != port2) {
270 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
271 args[*cur_arg], arg);
272 goto err;
273 }
274
275 newsrv->check.addr = newsrv->agent.addr = *sk;
276 newsrv->flags |= SRV_F_CHECKADDR;
277 newsrv->flags |= SRV_F_AGENTADDR;
278
279 return 0;
280
281 err:
282 free(errmsg);
283 return ERR_ALERT | ERR_FATAL;
284}
285
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100286/* Parse the "agent-check" server keyword */
287static int srv_parse_agent_check(char **args, int *cur_arg,
288 struct proxy *curproxy, struct server *newsrv, char **err)
289{
290 newsrv->do_agent = 1;
291 return 0;
292}
293
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100294/* Parse the "backup" server keyword */
295static int srv_parse_backup(char **args, int *cur_arg,
296 struct proxy *curproxy, struct server *newsrv, char **err)
297{
298 newsrv->flags |= SRV_F_BACKUP;
299 return 0;
300}
301
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100302/* Parse the "check" server keyword */
303static int srv_parse_check(char **args, int *cur_arg,
304 struct proxy *curproxy, struct server *newsrv, char **err)
305{
306 newsrv->do_check = 1;
307 return 0;
308}
309
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100310/* Parse the "check-send-proxy" server keyword */
311static int srv_parse_check_send_proxy(char **args, int *cur_arg,
312 struct proxy *curproxy, struct server *newsrv, char **err)
313{
314 newsrv->check.send_proxy = 1;
315 return 0;
316}
317
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100318/* Parse the "cookie" server keyword */
319static int srv_parse_cookie(char **args, int *cur_arg,
320 struct proxy *curproxy, struct server *newsrv, char **err)
321{
322 char *arg;
323
324 arg = args[*cur_arg + 1];
325 if (!*arg) {
326 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
327 return ERR_ALERT | ERR_FATAL;
328 }
329
330 free(newsrv->cookie);
331 newsrv->cookie = strdup(arg);
332 newsrv->cklen = strlen(arg);
333 newsrv->flags |= SRV_F_COOKIESET;
334 return 0;
335}
336
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100337/* Parse the "disabled" server keyword */
338static int srv_parse_disabled(char **args, int *cur_arg,
339 struct proxy *curproxy, struct server *newsrv, char **err)
340{
Emeric Brun52a91d32017-08-31 14:41:55 +0200341 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
342 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100343 newsrv->check.state |= CHK_ST_PAUSED;
344 newsrv->check.health = 0;
345 return 0;
346}
347
348/* Parse the "enabled" server keyword */
349static int srv_parse_enabled(char **args, int *cur_arg,
350 struct proxy *curproxy, struct server *newsrv, char **err)
351{
Emeric Brun52a91d32017-08-31 14:41:55 +0200352 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
353 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100354 newsrv->check.state &= ~CHK_ST_PAUSED;
355 newsrv->check.health = newsrv->check.rise;
356 return 0;
357}
358
Willy Tarreaudff55432012-10-10 17:51:05 +0200359/* parse the "id" server keyword */
360static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
361{
362 struct eb32_node *node;
363
364 if (!*args[*cur_arg + 1]) {
365 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
366 return ERR_ALERT | ERR_FATAL;
367 }
368
369 newsrv->puid = atol(args[*cur_arg + 1]);
370 newsrv->conf.id.key = newsrv->puid;
371
372 if (newsrv->puid <= 0) {
373 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
374 return ERR_ALERT | ERR_FATAL;
375 }
376
377 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
378 if (node) {
379 struct server *target = container_of(node, struct server, conf.id);
380 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
381 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
382 target->id);
383 return ERR_ALERT | ERR_FATAL;
384 }
385
386 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200387 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200388 return 0;
389}
390
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100391/* Parse the "namespace" server keyword */
392static int srv_parse_namespace(char **args, int *cur_arg,
393 struct proxy *curproxy, struct server *newsrv, char **err)
394{
395#ifdef CONFIG_HAP_NS
396 char *arg;
397
398 arg = args[*cur_arg + 1];
399 if (!*arg) {
400 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
401 return ERR_ALERT | ERR_FATAL;
402 }
403
404 if (!strcmp(arg, "*")) {
405 /* Use the namespace associated with the connection (if present). */
406 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
407 return 0;
408 }
409
410 /*
411 * As this parser may be called several times for the same 'default-server'
412 * object, or for a new 'server' instance deriving from a 'default-server'
413 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
414 */
415 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
416
417 newsrv->netns = netns_store_lookup(arg, strlen(arg));
418 if (!newsrv->netns)
419 newsrv->netns = netns_store_insert(arg);
420
421 if (!newsrv->netns) {
422 memprintf(err, "Cannot open namespace '%s'", arg);
423 return ERR_ALERT | ERR_FATAL;
424 }
425
426 return 0;
427#else
428 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
429 return ERR_ALERT | ERR_FATAL;
430#endif
431}
432
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100433/* Parse the "no-agent-check" server keyword */
434static int srv_parse_no_agent_check(char **args, int *cur_arg,
435 struct proxy *curproxy, struct server *newsrv, char **err)
436{
437 free_check(&newsrv->agent);
438 newsrv->agent.inter = 0;
439 newsrv->agent.port = 0;
440 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
441 newsrv->do_agent = 0;
442 return 0;
443}
444
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100445/* Parse the "no-backup" server keyword */
446static int srv_parse_no_backup(char **args, int *cur_arg,
447 struct proxy *curproxy, struct server *newsrv, char **err)
448{
449 newsrv->flags &= ~SRV_F_BACKUP;
450 return 0;
451}
452
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100453/* Parse the "no-check" server keyword */
454static int srv_parse_no_check(char **args, int *cur_arg,
455 struct proxy *curproxy, struct server *newsrv, char **err)
456{
457 free_check(&newsrv->check);
458 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
459 newsrv->do_check = 0;
460 return 0;
461}
462
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100463/* Parse the "no-check-send-proxy" server keyword */
464static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
465 struct proxy *curproxy, struct server *newsrv, char **err)
466{
467 newsrv->check.send_proxy = 0;
468 return 0;
469}
470
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100471/* Disable server PROXY protocol flags. */
472static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
473{
474 srv->pp_opts &= ~flags;
475 return 0;
476}
477
478/* Parse the "no-send-proxy" server keyword */
479static int srv_parse_no_send_proxy(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
482 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
483}
484
485/* Parse the "no-send-proxy-v2" server keyword */
486static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
487 struct proxy *curproxy, struct server *newsrv, char **err)
488{
489 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
490}
491
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100492/* Parse the "non-stick" server keyword */
493static int srv_parse_non_stick(char **args, int *cur_arg,
494 struct proxy *curproxy, struct server *newsrv, char **err)
495{
496 newsrv->flags |= SRV_F_NON_STICK;
497 return 0;
498}
499
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100500/* Enable server PROXY protocol flags. */
501static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
502{
503 srv->pp_opts |= flags;
504 return 0;
505}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200506/* parse the "proto" server keyword */
507static int srv_parse_proto(char **args, int *cur_arg,
508 struct proxy *px, struct server *newsrv, char **err)
509{
510 struct ist proto;
511
512 if (!*args[*cur_arg + 1]) {
513 memprintf(err, "'%s' : missing value", args[*cur_arg]);
514 return ERR_ALERT | ERR_FATAL;
515 }
516 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
517 newsrv->mux_proto = get_mux_proto(proto);
518 if (!newsrv->mux_proto) {
519 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
520 return ERR_ALERT | ERR_FATAL;
521 }
522 else if (!(newsrv->mux_proto->side & PROTO_SIDE_BE)) {
523 memprintf(err, "'%s' : MUX protocol '%s' cannot be used for outgoing connections",
524 args[*cur_arg], args[*cur_arg+1]);
525 return ERR_ALERT | ERR_FATAL;
526 }
527 return 0;
528}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100529
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100530/* parse the "proxy-v2-options" */
531static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
532 struct proxy *px, struct server *newsrv, char **err)
533{
534 char *p, *n;
535 for (p = args[*cur_arg+1]; p; p = n) {
536 n = strchr(p, ',');
537 if (n)
538 *n++ = '\0';
539 if (!strcmp(p, "ssl")) {
540 newsrv->pp_opts |= SRV_PP_V2_SSL;
541 } else if (!strcmp(p, "cert-cn")) {
542 newsrv->pp_opts |= SRV_PP_V2_SSL;
543 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100544 } else if (!strcmp(p, "cert-key")) {
545 newsrv->pp_opts |= SRV_PP_V2_SSL;
546 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
547 } else if (!strcmp(p, "cert-sig")) {
548 newsrv->pp_opts |= SRV_PP_V2_SSL;
549 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
550 } else if (!strcmp(p, "ssl-cipher")) {
551 newsrv->pp_opts |= SRV_PP_V2_SSL;
552 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100553 } else if (!strcmp(p, "authority")) {
554 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100555 } else if (!strcmp(p, "crc32c")) {
556 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100557 } else
558 goto fail;
559 }
560 return 0;
561 fail:
562 if (err)
563 memprintf(err, "'%s' : proxy v2 option not implemented", p);
564 return ERR_ALERT | ERR_FATAL;
565}
566
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100567/* Parse the "observe" server keyword */
568static int srv_parse_observe(char **args, int *cur_arg,
569 struct proxy *curproxy, struct server *newsrv, char **err)
570{
571 char *arg;
572
573 arg = args[*cur_arg + 1];
574 if (!*arg) {
575 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
576 return ERR_ALERT | ERR_FATAL;
577 }
578
579 if (!strcmp(arg, "none")) {
580 newsrv->observe = HANA_OBS_NONE;
581 }
582 else if (!strcmp(arg, "layer4")) {
583 newsrv->observe = HANA_OBS_LAYER4;
584 }
585 else if (!strcmp(arg, "layer7")) {
586 if (curproxy->mode != PR_MODE_HTTP) {
587 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
588 return ERR_ALERT;
589 }
590 newsrv->observe = HANA_OBS_LAYER7;
591 }
592 else {
593 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
594 "but got '%s'\n", args[*cur_arg], arg);
595 return ERR_ALERT | ERR_FATAL;
596 }
597
598 return 0;
599}
600
Frédéric Lécaille16186232017-03-14 16:42:49 +0100601/* Parse the "redir" server keyword */
602static int srv_parse_redir(char **args, int *cur_arg,
603 struct proxy *curproxy, struct server *newsrv, char **err)
604{
605 char *arg;
606
607 arg = args[*cur_arg + 1];
608 if (!*arg) {
609 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
610 return ERR_ALERT | ERR_FATAL;
611 }
612
613 free(newsrv->rdr_pfx);
614 newsrv->rdr_pfx = strdup(arg);
615 newsrv->rdr_len = strlen(arg);
616
617 return 0;
618}
619
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100620/* Parse the "send-proxy" server keyword */
621static int srv_parse_send_proxy(char **args, int *cur_arg,
622 struct proxy *curproxy, struct server *newsrv, char **err)
623{
624 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
625}
626
627/* Parse the "send-proxy-v2" server keyword */
628static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
629 struct proxy *curproxy, struct server *newsrv, char **err)
630{
631 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
632}
633
Frédéric Lécailledba97072017-03-17 15:33:50 +0100634
635/* Parse the "source" server keyword */
636static int srv_parse_source(char **args, int *cur_arg,
637 struct proxy *curproxy, struct server *newsrv, char **err)
638{
639 char *errmsg;
640 int port_low, port_high;
641 struct sockaddr_storage *sk;
642 struct protocol *proto;
643
644 errmsg = NULL;
645
646 if (!*args[*cur_arg + 1]) {
647 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
648 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
649 goto err;
650 }
651
652 /* 'sk' is statically allocated (no need to be freed). */
653 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
654 if (!sk) {
655 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
656 goto err;
657 }
658
659 proto = protocol_by_family(sk->ss_family);
660 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100661 ha_alert("'%s %s' : connect() not supported for this address family.\n",
662 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100663 goto err;
664 }
665
666 newsrv->conn_src.opts |= CO_SRC_BIND;
667 newsrv->conn_src.source_addr = *sk;
668
669 if (port_low != port_high) {
670 int i;
671
672 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100673 ha_alert("'%s' does not support port offsets (found '%s').\n",
674 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100675 goto err;
676 }
677
678 if (port_low <= 0 || port_low > 65535 ||
679 port_high <= 0 || port_high > 65535 ||
680 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100681 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 +0100682 goto err;
683 }
684 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
685 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
686 newsrv->conn_src.sport_range->ports[i] = port_low + i;
687 }
688
689 *cur_arg += 2;
690 while (*(args[*cur_arg])) {
691 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
692#if defined(CONFIG_HAP_TRANSPARENT)
693 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100694 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
695 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100696 goto err;
697 }
698 if (!strcmp(args[*cur_arg + 1], "client")) {
699 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
700 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
701 }
702 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
703 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
704 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
705 }
706 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
707 char *name, *end;
708
709 name = args[*cur_arg + 1] + 7;
710 while (isspace(*name))
711 name++;
712
713 end = name;
714 while (*end && !isspace(*end) && *end != ',' && *end != ')')
715 end++;
716
717 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
718 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
719 free(newsrv->conn_src.bind_hdr_name);
720 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
721 newsrv->conn_src.bind_hdr_len = end - name;
722 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
723 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
724 newsrv->conn_src.bind_hdr_occ = -1;
725
726 /* now look for an occurrence number */
727 while (isspace(*end))
728 end++;
729 if (*end == ',') {
730 end++;
731 name = end;
732 if (*end == '-')
733 end++;
734 while (isdigit((int)*end))
735 end++;
736 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
737 }
738
739 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100740 ha_alert("usesrc hdr_ip(name,num) does not support negative"
741 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100742 goto err;
743 }
744 }
745 else {
746 struct sockaddr_storage *sk;
747 int port1, port2;
748
749 /* 'sk' is statically allocated (no need to be freed). */
750 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
751 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100752 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 goto err;
754 }
755
756 proto = protocol_by_family(sk->ss_family);
757 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_alert("'%s %s' : connect() not supported for this address family.\n",
759 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762
763 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100764 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
765 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100766 goto err;
767 }
768 newsrv->conn_src.tproxy_addr = *sk;
769 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
770 }
771 global.last_checks |= LSTCHK_NETADM;
772 *cur_arg += 2;
773 continue;
774#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100775 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 +0100776 goto err;
777#endif /* defined(CONFIG_HAP_TRANSPARENT) */
778 } /* "usesrc" */
779
780 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
781#ifdef SO_BINDTODEVICE
782 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100784 goto err;
785 }
786 free(newsrv->conn_src.iface_name);
787 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
788 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
789 global.last_checks |= LSTCHK_NETADM;
790#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100792 goto err;
793#endif
794 *cur_arg += 2;
795 continue;
796 }
797 /* this keyword in not an option of "source" */
798 break;
799 } /* while */
800
801 return 0;
802
803 err:
804 free(errmsg);
805 return ERR_ALERT | ERR_FATAL;
806}
807
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100808/* Parse the "stick" server keyword */
809static int srv_parse_stick(char **args, int *cur_arg,
810 struct proxy *curproxy, struct server *newsrv, char **err)
811{
812 newsrv->flags &= ~SRV_F_NON_STICK;
813 return 0;
814}
815
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100816/* Parse the "track" server keyword */
817static int srv_parse_track(char **args, int *cur_arg,
818 struct proxy *curproxy, struct server *newsrv, char **err)
819{
820 char *arg;
821
822 arg = args[*cur_arg + 1];
823 if (!*arg) {
824 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
825 return ERR_ALERT | ERR_FATAL;
826 }
827
828 free(newsrv->trackit);
829 newsrv->trackit = strdup(arg);
830
831 return 0;
832}
833
Frédéric Lécailledba97072017-03-17 15:33:50 +0100834
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200835/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200837 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200838 *
839 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200840 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200841void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200842{
Willy Tarreau87b09662015-04-03 00:22:06 +0200843 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200844
Willy Tarreau87b09662015-04-03 00:22:06 +0200845 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
846 if (stream->srv_conn == srv)
847 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200848}
849
850/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200851 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200852 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200853 *
854 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200855 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200856void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200857{
858 struct server *srv;
859
860 for (srv = px->srv; srv != NULL; srv = srv->next)
861 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200862 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200863}
864
Willy Tarreaubda92272014-05-20 21:55:30 +0200865/* Appends some information to a message string related to a server going UP or
866 * DOWN. If both <forced> and <reason> are null and the server tracks another
867 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200868 * If <check> is non-null, an entire string describing the check result will be
869 * appended after a comma and a space (eg: to report some information from the
870 * check that changed the state). In the other case, the string will be built
871 * using the check results stored into the struct server if present.
872 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200873 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200874 *
875 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200876 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200877void srv_append_status(struct buffer *msg, struct server *s,
878 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200879{
Emeric Brun5a133512017-10-19 14:42:30 +0200880 short status = s->op_st_chg.status;
881 short code = s->op_st_chg.code;
882 long duration = s->op_st_chg.duration;
883 char *desc = s->op_st_chg.reason;
884
885 if (check) {
886 status = check->status;
887 code = check->code;
888 duration = check->duration;
889 desc = check->desc;
890 }
891
892 if (status != -1) {
893 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
894
895 if (status >= HCHK_STATUS_L57DATA)
896 chunk_appendf(msg, ", code: %d", code);
897
898 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200899 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200900
901 chunk_appendf(msg, ", info: \"");
902
903 chunk_initlen(&src, desc, 0, strlen(desc));
904 chunk_asciiencode(msg, &src, '"');
905
906 chunk_appendf(msg, "\"");
907 }
908
909 if (duration >= 0)
910 chunk_appendf(msg, ", check duration: %ldms", duration);
911 }
912 else if (desc && *desc) {
913 chunk_appendf(msg, ", %s", desc);
914 }
915 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200916 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200917 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200918
919 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200920 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200921 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
922 " %d sessions active, %d requeued, %d remaining in queue",
923 s->proxy->srv_act, s->proxy->srv_bck,
924 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
925 s->cur_sess, xferred, s->nbpend);
926 else
927 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
928 " %d sessions requeued, %d total in queue",
929 s->proxy->srv_act, s->proxy->srv_bck,
930 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
931 xferred, s->nbpend);
932 }
933}
934
Emeric Brun5a133512017-10-19 14:42:30 +0200935/* Marks server <s> down, regardless of its checks' statuses. The server is
936 * registered in a list to postpone the counting of the remaining servers on
937 * the proxy and transfers queued streams whenever possible to other servers at
938 * a sync point. Maintenance servers are ignored. It stores the <reason> if
939 * non-null as the reason for going down or the available data from the check
940 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200941 *
942 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200943 */
Emeric Brun5a133512017-10-19 14:42:30 +0200944void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200945{
946 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200947
Emeric Brun64cc49c2017-10-03 14:46:45 +0200948 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200949 return;
950
Emeric Brun52a91d32017-08-31 14:41:55 +0200951 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200952 *s->op_st_chg.reason = 0;
953 s->op_st_chg.status = -1;
954 if (reason) {
955 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
956 }
957 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100958 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200959 s->op_st_chg.code = check->code;
960 s->op_st_chg.status = check->status;
961 s->op_st_chg.duration = check->duration;
962 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200963
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200964 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200965 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200966
Emeric Brun9f0b4582017-10-23 14:39:51 +0200967 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100968 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200969 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100970 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200971 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200972}
973
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200974/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200975 * in maintenance. The server is registered in a list to postpone the counting
976 * of the remaining servers on the proxy and tries to grab requests from the
977 * proxy at a sync point. Maintenance servers are ignored. It stores the
978 * <reason> if non-null as the reason for going down or the available data
979 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200980 *
981 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200982 */
Emeric Brun5a133512017-10-19 14:42:30 +0200983void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200984{
985 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200986
Emeric Brun64cc49c2017-10-03 14:46:45 +0200987 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200988 return;
989
Emeric Brun52a91d32017-08-31 14:41:55 +0200990 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200991 return;
992
Emeric Brun52a91d32017-08-31 14:41:55 +0200993 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200994 *s->op_st_chg.reason = 0;
995 s->op_st_chg.status = -1;
996 if (reason) {
997 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
998 }
999 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001000 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001001 s->op_st_chg.code = check->code;
1002 s->op_st_chg.status = check->status;
1003 s->op_st_chg.duration = check->duration;
1004 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001005
Emeric Brun64cc49c2017-10-03 14:46:45 +02001006 if (s->slowstart <= 0)
1007 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001008
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001009 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001010 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001011
Emeric Brun9f0b4582017-10-23 14:39:51 +02001012 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001013 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001014 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001015 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001016 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001017}
1018
Willy Tarreau8eb77842014-05-21 13:54:57 +02001019/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001020 * isn't in maintenance. The server is registered in a list to postpone the
1021 * counting of the remaining servers on the proxy and tries to grab requests
1022 * from the proxy. Maintenance servers are ignored. It stores the
1023 * <reason> if non-null as the reason for going down or the available data
1024 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001025 * up. Note that it makes use of the trash to build the log strings, so <reason>
1026 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001027 *
1028 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001029 */
Emeric Brun5a133512017-10-19 14:42:30 +02001030void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001031{
1032 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001033
Emeric Brun64cc49c2017-10-03 14:46:45 +02001034 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001035 return;
1036
Emeric Brun52a91d32017-08-31 14:41:55 +02001037 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001038 return;
1039
Emeric Brun52a91d32017-08-31 14:41:55 +02001040 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001041 *s->op_st_chg.reason = 0;
1042 s->op_st_chg.status = -1;
1043 if (reason) {
1044 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1045 }
1046 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001047 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001048 s->op_st_chg.code = check->code;
1049 s->op_st_chg.status = check->status;
1050 s->op_st_chg.duration = check->duration;
1051 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001052
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001053 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001054 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001055
Emeric Brun9f0b4582017-10-23 14:39:51 +02001056 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001057 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001058 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001059 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001060 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001061}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001062
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001063/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1064 * enforce either maint mode or drain mode. It is not allowed to set more than
1065 * one flag at once. The equivalent "inherited" flag is propagated to all
1066 * tracking servers. Maintenance mode disables health checks (but not agent
1067 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001068 * is done. If <cause> is non-null, it will be displayed at the end of the log
1069 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001070 *
1071 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001073void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001074{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001075 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001076
1077 if (!mode)
1078 return;
1079
1080 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001081 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082 return;
1083
Emeric Brun52a91d32017-08-31 14:41:55 +02001084 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001085 if (cause)
1086 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1087
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001088 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001089 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001090
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001091 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001092 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1093 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001094 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001095
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001096 /* compute the inherited flag to propagate */
1097 if (mode & SRV_ADMF_MAINT)
1098 mode = SRV_ADMF_IMAINT;
1099 else if (mode & SRV_ADMF_DRAIN)
1100 mode = SRV_ADMF_IDRAIN;
1101
Emeric Brun9f0b4582017-10-23 14:39:51 +02001102 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001103 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001104 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001105 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001106 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001107}
1108
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001109/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1110 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1111 * than one flag at once. The equivalent "inherited" flag is propagated to all
1112 * tracking servers. Leaving maintenance mode re-enables health checks. When
1113 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001114 *
1115 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001116 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001117void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001118{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001119 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001120
1121 if (!mode)
1122 return;
1123
1124 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001125 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126 return;
1127
Emeric Brun52a91d32017-08-31 14:41:55 +02001128 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001130 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001131 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001132
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1135 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001136 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001137
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001138 if (mode & SRV_ADMF_MAINT)
1139 mode = SRV_ADMF_IMAINT;
1140 else if (mode & SRV_ADMF_DRAIN)
1141 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001142
Emeric Brun9f0b4582017-10-23 14:39:51 +02001143 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001144 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001145 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001146 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001147 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001148}
1149
Willy Tarreau757478e2016-11-03 19:22:19 +01001150/* principle: propagate maint and drain to tracking servers. This is useful
1151 * upon startup so that inherited states are correct.
1152 */
1153static void srv_propagate_admin_state(struct server *srv)
1154{
1155 struct server *srv2;
1156
1157 if (!srv->trackers)
1158 return;
1159
1160 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001161 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001162 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001163 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001164
Emeric Brun52a91d32017-08-31 14:41:55 +02001165 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001166 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001167 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001168 }
1169}
1170
1171/* Compute and propagate the admin states for all servers in proxy <px>.
1172 * Only servers *not* tracking another one are considered, because other
1173 * ones will be handled when the server they track is visited.
1174 */
1175void srv_compute_all_admin_states(struct proxy *px)
1176{
1177 struct server *srv;
1178
1179 for (srv = px->srv; srv; srv = srv->next) {
1180 if (srv->track)
1181 continue;
1182 srv_propagate_admin_state(srv);
1183 }
1184}
1185
Willy Tarreaudff55432012-10-10 17:51:05 +02001186/* Note: must not be declared <const> as its list will be overwritten.
1187 * Please take care of keeping this list alphabetically sorted, doing so helps
1188 * all code contributors.
1189 * Optional keywords are also declared with a NULL ->parse() function so that
1190 * the config parser can report an appropriate error when a known keyword was
1191 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001192 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001193 */
1194static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001195 { "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 +01001196 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001197 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001198 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001199 { "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 +01001200 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001201 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1202 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001203 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001204 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001205 { "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 +01001206 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001207 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001208 { "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 +01001209 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1210 { "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 +01001211 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001212 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001213 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001214 { "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 +01001215 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001216 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1217 { "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 +02001218 { "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 +01001219 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001220 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001221 { NULL, NULL, 0 },
1222}};
1223
1224__attribute__((constructor))
1225static void __listener_init(void)
1226{
1227 srv_register_keywords(&srv_kws);
1228}
1229
Willy Tarreau004e0452013-11-21 11:22:01 +01001230/* Recomputes the server's eweight based on its state, uweight, the current time,
1231 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001232 * state is automatically disabled if the time is elapsed. If <must_update> is
1233 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001234 *
1235 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001236 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001237void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001238{
1239 struct proxy *px = sv->proxy;
1240 unsigned w;
1241
1242 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1243 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001244 if (sv->next_state == SRV_ST_STARTING)
1245 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001246 }
1247
1248 /* We must take care of not pushing the server to full throttle during slow starts.
1249 * It must also start immediately, at least at the minimal step when leaving maintenance.
1250 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001251 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001252 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1253 else
1254 w = px->lbprm.wdiv;
1255
Emeric Brun52a91d32017-08-31 14:41:55 +02001256 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001257
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001258 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001259 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001260 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001261}
1262
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001264 * Parses weight_str and configures sv accordingly.
1265 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001266 *
1267 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001268 */
1269const char *server_parse_weight_change_request(struct server *sv,
1270 const char *weight_str)
1271{
1272 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001273 long int w;
1274 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001275
1276 px = sv->proxy;
1277
1278 /* if the weight is terminated with '%', it is set relative to
1279 * the initial weight, otherwise it is absolute.
1280 */
1281 if (!*weight_str)
1282 return "Require <weight> or <weight%>.\n";
1283
Simon Hormanb796afa2013-02-12 10:45:53 +09001284 w = strtol(weight_str, &end, 10);
1285 if (end == weight_str)
1286 return "Empty weight string empty or preceded by garbage";
1287 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001288 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001289 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001290 /* Avoid integer overflow */
1291 if (w > 25600)
1292 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001293 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001294 if (w > 256)
1295 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001296 }
1297 else if (w < 0 || w > 256)
1298 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001299 else if (end[0] != '\0')
1300 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001301
1302 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1303 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1304
1305 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001306 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001307
1308 return NULL;
1309}
1310
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001311/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001312 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1313 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001314 * Returns:
1315 * - error string on error
1316 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001317 *
1318 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001319 */
1320const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001321 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001322{
1323 unsigned char ip[INET6_ADDRSTRLEN];
1324
1325 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001326 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001327 return NULL;
1328 }
1329 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001330 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001331 return NULL;
1332 }
1333
1334 return "Could not understand IP address format.\n";
1335}
1336
Willy Tarreau46b7f532018-08-21 11:54:26 +02001337/*
1338 * Must be called with the server lock held.
1339 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001340const char *server_parse_maxconn_change_request(struct server *sv,
1341 const char *maxconn_str)
1342{
1343 long int v;
1344 char *end;
1345
1346 if (!*maxconn_str)
1347 return "Require <maxconn>.\n";
1348
1349 v = strtol(maxconn_str, &end, 10);
1350 if (end == maxconn_str)
1351 return "maxconn string empty or preceded by garbage";
1352 else if (end[0] != '\0')
1353 return "Trailing garbage in maxconn string";
1354
1355 if (sv->maxconn == sv->minconn) { // static maxconn
1356 sv->maxconn = sv->minconn = v;
1357 } else { // dynamic maxconn
1358 sv->maxconn = v;
1359 }
1360
1361 if (may_dequeue_tasks(sv, sv->proxy))
1362 process_srv_queue(sv);
1363
1364 return NULL;
1365}
1366
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001367#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001368static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1369 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001370{
1371 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001372 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001373 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001374 NULL,
1375 };
1376
1377 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001378 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001379
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001380 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1381}
1382
1383static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1384{
1385 struct sample_expr *expr;
1386
1387 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 +01001388 if (!expr) {
1389 memprintf(err, "error detected while parsing sni expression : %s", *err);
1390 return ERR_ALERT | ERR_FATAL;
1391 }
1392
1393 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1394 memprintf(err, "error detected while parsing sni expression : "
1395 " fetch method '%s' extracts information from '%s', "
1396 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001397 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001398 return ERR_ALERT | ERR_FATAL;
1399 }
1400
1401 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1402 release_sample_expr(newsrv->ssl_ctx.sni);
1403 newsrv->ssl_ctx.sni = expr;
1404
1405 return 0;
1406}
1407#endif
1408
1409static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1410{
1411 if (err && *err) {
1412 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001413 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 +01001414 }
1415 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001416 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1417 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418}
1419
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001420static void srv_conn_src_sport_range_cpy(struct server *srv,
1421 struct server *src)
1422{
1423 int range_sz;
1424
1425 range_sz = src->conn_src.sport_range->size;
1426 if (range_sz > 0) {
1427 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1428 if (srv->conn_src.sport_range != NULL) {
1429 int i;
1430
1431 for (i = 0; i < range_sz; i++) {
1432 srv->conn_src.sport_range->ports[i] =
1433 src->conn_src.sport_range->ports[i];
1434 }
1435 }
1436 }
1437}
1438
1439/*
1440 * Copy <src> server connection source settings to <srv> server everything needed.
1441 */
1442static void srv_conn_src_cpy(struct server *srv, struct server *src)
1443{
1444 srv->conn_src.opts = src->conn_src.opts;
1445 srv->conn_src.source_addr = src->conn_src.source_addr;
1446
1447 /* Source port range copy. */
1448 if (src->conn_src.sport_range != NULL)
1449 srv_conn_src_sport_range_cpy(srv, src);
1450
1451#ifdef CONFIG_HAP_TRANSPARENT
1452 if (src->conn_src.bind_hdr_name != NULL) {
1453 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1454 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1455 }
1456 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1457 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1458#endif
1459 if (src->conn_src.iface_name != NULL)
1460 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1461}
1462
1463/*
1464 * Copy <src> server SSL settings to <srv> server allocating
1465 * everything needed.
1466 */
1467#if defined(USE_OPENSSL)
1468static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1469{
1470 if (src->ssl_ctx.ca_file != NULL)
1471 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1472 if (src->ssl_ctx.crl_file != NULL)
1473 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1474 if (src->ssl_ctx.client_crt != NULL)
1475 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1476
1477 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1478
1479 if (src->ssl_ctx.verify_host != NULL)
1480 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1481 if (src->ssl_ctx.ciphers != NULL)
1482 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1483 if (src->sni_expr != NULL)
1484 srv->sni_expr = strdup(src->sni_expr);
1485}
1486#endif
1487
1488/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001489 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001490 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001491 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001492 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001493static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001494{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001495 char *hostname_dn;
1496 int hostname_len, hostname_dn_len;
1497
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001498 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001499 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001500
Christopher Faulet67957bd2017-09-27 11:00:59 +02001501 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001502 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001503 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1504 hostname_dn, trash.size);
1505 if (hostname_dn_len == -1)
1506 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001507
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001508
Christopher Faulet67957bd2017-09-27 11:00:59 +02001509 free(srv->hostname);
1510 free(srv->hostname_dn);
1511 srv->hostname = strdup(hostname);
1512 srv->hostname_dn = strdup(hostname_dn);
1513 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001514 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001515 goto err;
1516
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001517 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001518
1519 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520 free(srv->hostname); srv->hostname = NULL;
1521 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001522 return -1;
1523}
1524
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001525/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001526 * Copy <src> server settings to <srv> server allocating
1527 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001528 * This function is not supposed to be called at any time, but only
1529 * during server settings parsing or during server allocations from
1530 * a server template, and just after having calloc()'ed a new server.
1531 * So, <src> may only be a default server (when parsing server settings)
1532 * or a server template (during server allocations from a server template).
1533 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1534 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001535 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001536static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001537{
1538 /* Connection source settings copy */
1539 srv_conn_src_cpy(srv, src);
1540
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001541 if (srv_tmpl) {
1542 srv->addr = src->addr;
1543 srv->svc_port = src->svc_port;
1544 }
1545
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001546 srv->pp_opts = src->pp_opts;
1547 if (src->rdr_pfx != NULL) {
1548 srv->rdr_pfx = strdup(src->rdr_pfx);
1549 srv->rdr_len = src->rdr_len;
1550 }
1551 if (src->cookie != NULL) {
1552 srv->cookie = strdup(src->cookie);
1553 srv->cklen = src->cklen;
1554 }
1555 srv->use_ssl = src->use_ssl;
1556 srv->check.addr = srv->agent.addr = src->check.addr;
1557 srv->check.use_ssl = src->check.use_ssl;
1558 srv->check.port = src->check.port;
1559 /* Note: 'flags' field has potentially been already initialized. */
1560 srv->flags |= src->flags;
1561 srv->do_check = src->do_check;
1562 srv->do_agent = src->do_agent;
1563 if (srv->check.port)
1564 srv->flags |= SRV_F_CHECKPORT;
1565 srv->check.inter = src->check.inter;
1566 srv->check.fastinter = src->check.fastinter;
1567 srv->check.downinter = src->check.downinter;
1568 srv->agent.use_ssl = src->agent.use_ssl;
1569 srv->agent.port = src->agent.port;
1570 if (src->agent.send_string != NULL)
1571 srv->agent.send_string = strdup(src->agent.send_string);
1572 srv->agent.send_string_len = src->agent.send_string_len;
1573 srv->agent.inter = src->agent.inter;
1574 srv->agent.fastinter = src->agent.fastinter;
1575 srv->agent.downinter = src->agent.downinter;
1576 srv->maxqueue = src->maxqueue;
1577 srv->minconn = src->minconn;
1578 srv->maxconn = src->maxconn;
1579 srv->slowstart = src->slowstart;
1580 srv->observe = src->observe;
1581 srv->onerror = src->onerror;
1582 srv->onmarkeddown = src->onmarkeddown;
1583 srv->onmarkedup = src->onmarkedup;
1584 if (src->trackit != NULL)
1585 srv->trackit = strdup(src->trackit);
1586 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1587 srv->uweight = srv->iweight = src->iweight;
1588
1589 srv->check.send_proxy = src->check.send_proxy;
1590 /* health: up, but will fall down at first failure */
1591 srv->check.rise = srv->check.health = src->check.rise;
1592 srv->check.fall = src->check.fall;
1593
1594 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001595 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1596 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1597 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001598 srv->check.state |= CHK_ST_PAUSED;
1599 srv->check.health = 0;
1600 }
1601
1602 /* health: up but will fall down at first failure */
1603 srv->agent.rise = srv->agent.health = src->agent.rise;
1604 srv->agent.fall = src->agent.fall;
1605
1606 if (src->resolvers_id != NULL)
1607 srv->resolvers_id = strdup(src->resolvers_id);
1608 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001609 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001610 if (srv->dns_opts.family_prio == AF_UNSPEC)
1611 srv->dns_opts.family_prio = AF_INET6;
1612 memcpy(srv->dns_opts.pref_net,
1613 src->dns_opts.pref_net,
1614 sizeof srv->dns_opts.pref_net);
1615 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1616
1617 srv->init_addr_methods = src->init_addr_methods;
1618 srv->init_addr = src->init_addr;
1619#if defined(USE_OPENSSL)
1620 srv_ssl_settings_cpy(srv, src);
1621#endif
1622#ifdef TCP_USER_TIMEOUT
1623 srv->tcp_ut = src->tcp_ut;
1624#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001625 srv->mux_proto = src->mux_proto;
1626
Olivier Houchard8da5f982017-08-04 18:35:36 +02001627 if (srv_tmpl)
1628 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001629}
1630
1631static struct server *new_server(struct proxy *proxy)
1632{
1633 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001634 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001635
1636 srv = calloc(1, sizeof *srv);
1637 if (!srv)
1638 return NULL;
1639
1640 srv->obj_type = OBJ_TYPE_SERVER;
1641 srv->proxy = proxy;
1642 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001643 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001644
1645 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1646 goto free_srv;
1647 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1648 goto free_priv_conns;
1649 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1650 goto free_idle_conns;
1651
1652 for (i = 0; i < global.nbthread; i++) {
1653 LIST_INIT(&srv->priv_conns[i]);
1654 LIST_INIT(&srv->idle_conns[i]);
1655 LIST_INIT(&srv->safe_conns[i]);
1656 }
1657
Emeric Brun52a91d32017-08-31 14:41:55 +02001658 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001659 srv->last_change = now.tv_sec;
1660
1661 srv->check.status = HCHK_STATUS_INI;
1662 srv->check.server = srv;
1663 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1664
1665 srv->agent.status = HCHK_STATUS_INI;
1666 srv->agent.server = srv;
1667 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1668
1669 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001670
1671 free_idle_conns:
1672 free(srv->idle_conns);
1673 free_priv_conns:
1674 free(srv->priv_conns);
1675 free_srv:
1676 free(srv);
1677 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001678}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001679
1680/*
1681 * Validate <srv> server health-check settings.
1682 * Returns 0 if everything is OK, -1 if not.
1683 */
1684static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1685{
1686 struct tcpcheck_rule *r = NULL;
1687 struct list *l;
1688
1689 /*
1690 * We need at least a service port, a check port or the first tcp-check rule must
1691 * be a 'connect' one when checking an IPv4/IPv6 server.
1692 */
1693 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1694 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1695 return 0;
1696
1697 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1698 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001699 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1700 "Check has been disabled.\n",
1701 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001702 return -1;
1703 }
1704
1705 /* search the first action (connect / send / expect) in the list */
1706 l = &srv->proxy->tcpcheck_rules;
1707 list_for_each_entry(r, l, list) {
1708 if (r->action != TCPCHK_ACT_COMMENT)
1709 break;
1710 }
1711
1712 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001713 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1714 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1715 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001716 return -1;
1717 }
1718
1719 /* scan the tcp-check ruleset to ensure a port has been configured */
1720 l = &srv->proxy->tcpcheck_rules;
1721 list_for_each_entry(r, l, list) {
1722 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001723 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1724 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1725 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001726 return -1;
1727 }
1728 }
1729
1730 return 0;
1731}
1732
1733/*
1734 * Initialize <srv> health-check structure.
1735 * Returns the error string in case of memory allocation failure, NULL if not.
1736 */
1737static const char *do_health_check_init(struct server *srv, int check_type, int state)
1738{
1739 const char *ret;
1740
1741 if (!srv->do_check)
1742 return NULL;
1743
1744 ret = init_check(&srv->check, check_type);
1745 if (ret)
1746 return ret;
1747
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001748 srv->check.state |= state;
1749 global.maxsock++;
1750
1751 return NULL;
1752}
1753
1754static int server_health_check_init(const char *file, int linenum,
1755 struct server *srv, struct proxy *curproxy)
1756{
1757 const char *ret;
1758
1759 if (!srv->do_check)
1760 return 0;
1761
1762 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001763 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1764 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001765 return ERR_ALERT | ERR_FATAL;
1766 }
1767
1768 if (server_healthcheck_validate(file, linenum, srv) < 0)
1769 return ERR_ALERT | ERR_ABORT;
1770
1771 /* note: check type will be set during the config review phase */
1772 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1773 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001774 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001775 return ERR_ALERT | ERR_ABORT;
1776 }
1777
1778 return 0;
1779}
1780
1781/*
1782 * Initialize <srv> agent check structure.
1783 * Returns the error string in case of memory allocation failure, NULL if not.
1784 */
1785static const char *do_server_agent_check_init(struct server *srv, int state)
1786{
1787 const char *ret;
1788
1789 if (!srv->do_agent)
1790 return NULL;
1791
1792 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1793 if (ret)
1794 return ret;
1795
1796 if (!srv->agent.inter)
1797 srv->agent.inter = srv->check.inter;
1798
1799 srv->agent.state |= state;
1800 global.maxsock++;
1801
1802 return NULL;
1803}
1804
1805static int server_agent_check_init(const char *file, int linenum,
1806 struct server *srv, struct proxy *curproxy)
1807{
1808 const char *ret;
1809
1810 if (!srv->do_agent)
1811 return 0;
1812
1813 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001814 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 +02001815 file, linenum, srv->id);
1816 return ERR_ALERT | ERR_FATAL;
1817 }
1818
1819 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1820 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001821 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001822 return ERR_ALERT | ERR_ABORT;
1823 }
1824
1825 return 0;
1826}
1827
1828#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1829static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1830 struct server *srv, struct proxy *proxy)
1831{
1832 int ret;
1833 char *err = NULL;
1834
1835 if (!srv->sni_expr)
1836 return 0;
1837
1838 ret = server_parse_sni_expr(srv, proxy, &err);
1839 if (!ret)
1840 return 0;
1841
1842 display_parser_err(file, linenum, args, cur_arg, &err);
1843 free(err);
1844
1845 return ret;
1846}
1847#endif
1848
1849/*
1850 * Server initializations finalization.
1851 * Initialize health check, agent check and SNI expression if enabled.
1852 * Must not be called for a default server instance.
1853 */
1854static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1855 struct server *srv, struct proxy *px)
1856{
1857 int ret;
1858
1859 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1860 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1861 return ret;
1862 }
1863
1864#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1865 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1866 return ret;
1867#endif
1868
1869 if (srv->flags & SRV_F_BACKUP)
1870 px->srv_bck++;
1871 else
1872 px->srv_act++;
1873 srv_lb_commit_status(srv);
1874
1875 return 0;
1876}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001877
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001878/*
1879 * Parse as much as possible such a range string argument: low[-high]
1880 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1881 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1882 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1883 * Returns 0 if succeeded, -1 if not.
1884 */
1885static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1886{
1887 char *nb_high_arg;
1888
1889 *nb_high = 0;
1890 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001891 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001892
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001893 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001894 *nb_high_arg++ = '\0';
1895 *nb_high = atoi(nb_high_arg);
1896 }
1897 else {
1898 *nb_high += *nb_low;
1899 *nb_low = 1;
1900 }
1901
1902 if (*nb_low < 0 || *nb_high < *nb_low)
1903 return -1;
1904
1905 return 0;
1906}
1907
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001908static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1909{
1910 chunk_printf(&trash, "%s%d", prefix, nb);
1911 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001912 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001913}
1914
1915/*
1916 * Initialize as much as possible servers from <srv> server template.
1917 * Note that a server template is a special server with
1918 * a few different parameters than a server which has
1919 * been parsed mostly the same way as a server.
1920 * Returns the number of servers succesfully allocated,
1921 * 'srv' template included.
1922 */
1923static int server_template_init(struct server *srv, struct proxy *px)
1924{
1925 int i;
1926 struct server *newsrv;
1927
1928 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1929 int check_init_state;
1930 int agent_init_state;
1931
1932 newsrv = new_server(px);
1933 if (!newsrv)
1934 goto err;
1935
1936 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001937 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001938#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1939 if (newsrv->sni_expr) {
1940 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1941 if (!newsrv->ssl_ctx.sni)
1942 goto err;
1943 }
1944#endif
1945 /* Set this new server ID. */
1946 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1947
1948 /* Initial checks states. */
1949 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1950 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1951
1952 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1953 do_server_agent_check_init(newsrv, agent_init_state))
1954 goto err;
1955
1956 /* Linked backwards first. This will be restablished after parsing. */
1957 newsrv->next = px->srv;
1958 px->srv = newsrv;
1959 }
1960 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1961
1962 return i - srv->tmpl_info.nb_low;
1963
1964 err:
1965 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1966 if (newsrv) {
1967#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1968 release_sample_expr(newsrv->ssl_ctx.sni);
1969#endif
1970 free_check(&newsrv->agent);
1971 free_check(&newsrv->check);
1972 }
1973 free(newsrv);
1974 return i - srv->tmpl_info.nb_low;
1975}
1976
Willy Tarreau272adea2014-03-31 10:39:59 +02001977int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1978{
1979 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001980 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001981 char *errmsg = NULL;
1982 int err_code = 0;
1983 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001984 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001985
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001986 if (!strcmp(args[0], "server") ||
1987 !strcmp(args[0], "default-server") ||
1988 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001990 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001991 int srv = !defsrv && !strcmp(args[0], "server");
1992 int srv_tmpl = !defsrv && !srv;
1993 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001994
1995 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001996 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001997 err_code |= ERR_ALERT | ERR_FATAL;
1998 goto out;
1999 }
2000 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002001 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002002
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002003 /* There is no mandatory first arguments for default server. */
2004 if (srv) {
2005 if (!*args[2]) {
2006 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002007 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002008 file, linenum, args[0]);
2009 err_code |= ERR_ALERT | ERR_FATAL;
2010 goto out;
2011 }
2012
2013 err = invalid_char(args[1]);
2014 }
2015 else if (srv_tmpl) {
2016 if (!*args[3]) {
2017 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002018 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 +02002019 file, linenum, args[0]);
2020 err_code |= ERR_ALERT | ERR_FATAL;
2021 goto out;
2022 }
2023
2024 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002025 }
2026
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002027 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002028 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 +02002029 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002030 err_code |= ERR_ALERT | ERR_FATAL;
2031 goto out;
2032 }
2033
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002034 cur_arg = 2;
2035 if (srv_tmpl) {
2036 /* Parse server-template <nb | range> arg. */
2037 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002038 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002039 file, linenum, args[0], args[cur_arg]);
2040 err_code |= ERR_ALERT | ERR_FATAL;
2041 goto out;
2042 }
2043 cur_arg++;
2044 }
2045
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 if (!defsrv) {
2047 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002048 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002049 struct protocol *proto;
2050
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002051 newsrv = new_server(curproxy);
2052 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002053 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002054 err_code |= ERR_ALERT | ERR_ABORT;
2055 goto out;
2056 }
2057
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002058 if (srv_tmpl) {
2059 newsrv->tmpl_info.nb_low = tmpl_range_low;
2060 newsrv->tmpl_info.nb_high = tmpl_range_high;
2061 }
2062
Willy Tarreau272adea2014-03-31 10:39:59 +02002063 /* the servers are linked backwards first */
2064 newsrv->next = curproxy->srv;
2065 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002066 newsrv->conf.file = strdup(file);
2067 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002068 /* Note: for a server template, its id is its prefix.
2069 * This is a temporary id which will be used for server allocations to come
2070 * after parsing.
2071 */
2072 if (srv)
2073 newsrv->id = strdup(args[1]);
2074 else
2075 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002076
2077 /* several ways to check the port component :
2078 * - IP => port=+0, relative (IPv4 only)
2079 * - IP: => port=+0, relative
2080 * - IP:N => port=N, absolute
2081 * - IP:+N => port=+N, relative
2082 * - IP:-N => port=-N, relative
2083 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002084 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002085 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002086 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002087 err_code |= ERR_ALERT | ERR_FATAL;
2088 goto out;
2089 }
2090
2091 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002092 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002093 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 file, linenum, args[0], args[1]);
2095 err_code |= ERR_ALERT | ERR_FATAL;
2096 goto out;
2097 }
2098
2099 if (!port1 || !port2) {
2100 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002101 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002102 }
2103 else if (port1 != port2) {
2104 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002105 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 file, linenum, args[0], args[1], args[2]);
2107 err_code |= ERR_ALERT | ERR_FATAL;
2108 goto out;
2109 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002110
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002111 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002112 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002113 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002114 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002115 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2116 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2117 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002118 err_code |= ERR_ALERT | ERR_FATAL;
2119 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002120 }
2121 }
2122 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002123 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002124 file, linenum, newsrv->id);
2125 err_code |= ERR_ALERT | ERR_FATAL;
2126 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002127 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002128 }
2129
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002131 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002132
Olivier Houchard8da5f982017-08-04 18:35:36 +02002133 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002134 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002135 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002136 err_code |= ERR_ALERT | ERR_FATAL;
2137 goto out;
2138 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002139
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002140 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002141 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002142 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002143 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002144 } else {
2145 newsrv = &curproxy->defsrv;
2146 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002147 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002148 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 }
2150
2151 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002152 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002153 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2154 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002155 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002156 file, linenum, *err, newsrv->id);
2157 err_code |= ERR_ALERT | ERR_FATAL;
2158 goto out;
2159 }
2160 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002161 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002162 file, linenum, val, args[cur_arg], newsrv->id);
2163 err_code |= ERR_ALERT | ERR_FATAL;
2164 goto out;
2165 }
2166 newsrv->agent.inter = val;
2167 cur_arg += 2;
2168 }
Misiekea849332017-01-09 09:39:51 +01002169 else if (!strcmp(args[cur_arg], "agent-addr")) {
2170 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002171 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002172 goto out;
2173 }
2174
2175 cur_arg += 2;
2176 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002177 else if (!strcmp(args[cur_arg], "agent-port")) {
2178 global.maxsock++;
2179 newsrv->agent.port = atol(args[cur_arg + 1]);
2180 cur_arg += 2;
2181 }
James Brown55f9ff12015-10-21 18:19:05 -07002182 else if (!strcmp(args[cur_arg], "agent-send")) {
2183 global.maxsock++;
2184 free(newsrv->agent.send_string);
2185 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2186 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2187 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2188 cur_arg += 2;
2189 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002190 else if (!strcmp(args[cur_arg], "init-addr")) {
2191 char *p, *end;
2192 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002193 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002194
2195 newsrv->init_addr_methods = 0;
2196 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2197
2198 for (p = args[cur_arg + 1]; *p; p = end) {
2199 /* cut on next comma */
2200 for (end = p; *end && *end != ','; end++);
2201 if (*end)
2202 *(end++) = 0;
2203
Willy Tarreau4310d362016-11-02 15:05:56 +01002204 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002205 if (!strcmp(p, "libc")) {
2206 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2207 }
2208 else if (!strcmp(p, "last")) {
2209 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2210 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002211 else if (!strcmp(p, "none")) {
2212 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2213 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002214 else if (str2ip2(p, &sa, 0)) {
2215 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002216 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002217 file, linenum, args[cur_arg], p);
2218 err_code |= ERR_ALERT | ERR_FATAL;
2219 goto out;
2220 }
2221 newsrv->init_addr = sa;
2222 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2223 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002224 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002225 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 +02002226 file, linenum, args[cur_arg], p);
2227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002231 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002232 file, linenum, args[cur_arg], p);
2233 err_code |= ERR_ALERT | ERR_FATAL;
2234 goto out;
2235 }
2236 }
2237 cur_arg += 2;
2238 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002239 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002240 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002241 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2242 cur_arg += 2;
2243 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002244 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2245 char *p, *end;
2246
2247 for (p = args[cur_arg + 1]; *p; p = end) {
2248 /* cut on next comma */
2249 for (end = p; *end && *end != ','; end++);
2250 if (*end)
2251 *(end++) = 0;
2252
2253 if (!strcmp(p, "allow-dup-ip")) {
2254 newsrv->dns_opts.accept_duplicate_ip = 1;
2255 }
2256 else if (!strcmp(p, "prevent-dup-ip")) {
2257 newsrv->dns_opts.accept_duplicate_ip = 0;
2258 }
2259 else {
2260 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2261 file, linenum, args[cur_arg], p);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265 }
2266
2267 cur_arg += 2;
2268 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002269 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2270 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002271 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002272 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002273 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002274 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002276 file, linenum, args[cur_arg]);
2277 err_code |= ERR_ALERT | ERR_FATAL;
2278 goto out;
2279 }
2280 cur_arg += 2;
2281 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002282 else if (!strcmp(args[cur_arg], "resolve-net")) {
2283 char *p, *e;
2284 unsigned char mask;
2285 struct dns_options *opt;
2286
2287 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002288 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002289 file, linenum, args[cur_arg]);
2290 err_code |= ERR_ALERT | ERR_FATAL;
2291 goto out;
2292 }
2293
2294 opt = &newsrv->dns_opts;
2295
2296 /* Split arguments by comma, and convert it from ipv4 or ipv6
2297 * string network in in_addr or in6_addr.
2298 */
2299 p = args[cur_arg + 1];
2300 e = p;
2301 while (*p != '\0') {
2302 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002303 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002304 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002305 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2306 err_code |= ERR_ALERT | ERR_FATAL;
2307 goto out;
2308 }
2309 /* look for end or comma. */
2310 while (*e != ',' && *e != '\0')
2311 e++;
2312 if (*e == ',') {
2313 *e = '\0';
2314 e++;
2315 }
2316 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2317 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2318 /* Try to convert input string from ipv4 or ipv6 network. */
2319 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2320 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2321 &mask)) {
2322 /* Try to convert input string from ipv6 network. */
2323 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2324 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2325 } else {
2326 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002327 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002328 file, linenum, args[cur_arg], p);
2329 err_code |= ERR_ALERT | ERR_FATAL;
2330 goto out;
2331 }
2332 opt->pref_net_nb++;
2333 p = e;
2334 }
2335
2336 cur_arg += 2;
2337 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002338 else if (!strcmp(args[cur_arg], "rise")) {
2339 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002340 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002341 file, linenum, args[cur_arg]);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345
2346 newsrv->check.rise = atol(args[cur_arg + 1]);
2347 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002348 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002349 file, linenum, args[cur_arg]);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353
2354 if (newsrv->check.health)
2355 newsrv->check.health = newsrv->check.rise;
2356 cur_arg += 2;
2357 }
2358 else if (!strcmp(args[cur_arg], "fall")) {
2359 newsrv->check.fall = atol(args[cur_arg + 1]);
2360
2361 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002362 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002363 file, linenum, args[cur_arg]);
2364 err_code |= ERR_ALERT | ERR_FATAL;
2365 goto out;
2366 }
2367
2368 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002369 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 file, linenum, args[cur_arg]);
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373 }
2374
2375 cur_arg += 2;
2376 }
2377 else if (!strcmp(args[cur_arg], "inter")) {
2378 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2379 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002381 file, linenum, *err, newsrv->id);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002386 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002387 file, linenum, val, args[cur_arg], newsrv->id);
2388 err_code |= ERR_ALERT | ERR_FATAL;
2389 goto out;
2390 }
2391 newsrv->check.inter = val;
2392 cur_arg += 2;
2393 }
2394 else if (!strcmp(args[cur_arg], "fastinter")) {
2395 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2396 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002397 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002398 file, linenum, *err, newsrv->id);
2399 err_code |= ERR_ALERT | ERR_FATAL;
2400 goto out;
2401 }
2402 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002403 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002404 file, linenum, val, args[cur_arg], newsrv->id);
2405 err_code |= ERR_ALERT | ERR_FATAL;
2406 goto out;
2407 }
2408 newsrv->check.fastinter = val;
2409 cur_arg += 2;
2410 }
2411 else if (!strcmp(args[cur_arg], "downinter")) {
2412 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2413 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002414 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 file, linenum, *err, newsrv->id);
2416 err_code |= ERR_ALERT | ERR_FATAL;
2417 goto out;
2418 }
2419 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002420 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002421 file, linenum, val, args[cur_arg], newsrv->id);
2422 err_code |= ERR_ALERT | ERR_FATAL;
2423 goto out;
2424 }
2425 newsrv->check.downinter = val;
2426 cur_arg += 2;
2427 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002428 else if (!strcmp(args[cur_arg], "port")) {
2429 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002430 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002431 cur_arg += 2;
2432 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002433 else if (!strcmp(args[cur_arg], "weight")) {
2434 int w;
2435 w = atol(args[cur_arg + 1]);
2436 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002437 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002438 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2439 err_code |= ERR_ALERT | ERR_FATAL;
2440 goto out;
2441 }
2442 newsrv->uweight = newsrv->iweight = w;
2443 cur_arg += 2;
2444 }
2445 else if (!strcmp(args[cur_arg], "minconn")) {
2446 newsrv->minconn = atol(args[cur_arg + 1]);
2447 cur_arg += 2;
2448 }
2449 else if (!strcmp(args[cur_arg], "maxconn")) {
2450 newsrv->maxconn = atol(args[cur_arg + 1]);
2451 cur_arg += 2;
2452 }
2453 else if (!strcmp(args[cur_arg], "maxqueue")) {
2454 newsrv->maxqueue = atol(args[cur_arg + 1]);
2455 cur_arg += 2;
2456 }
2457 else if (!strcmp(args[cur_arg], "slowstart")) {
2458 /* slowstart is stored in seconds */
2459 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2460 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002461 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 file, linenum, *err, newsrv->id);
2463 err_code |= ERR_ALERT | ERR_FATAL;
2464 goto out;
2465 }
2466 newsrv->slowstart = (val + 999) / 1000;
2467 cur_arg += 2;
2468 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002469 else if (!strcmp(args[cur_arg], "on-error")) {
2470 if (!strcmp(args[cur_arg + 1], "fastinter"))
2471 newsrv->onerror = HANA_ONERR_FASTINTER;
2472 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2473 newsrv->onerror = HANA_ONERR_FAILCHK;
2474 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2475 newsrv->onerror = HANA_ONERR_SUDDTH;
2476 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2477 newsrv->onerror = HANA_ONERR_MARKDWN;
2478 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002479 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002480 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2481 file, linenum, args[cur_arg], args[cur_arg + 1]);
2482 err_code |= ERR_ALERT | ERR_FATAL;
2483 goto out;
2484 }
2485
2486 cur_arg += 2;
2487 }
2488 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2489 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2490 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2491 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002492 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002493 file, linenum, args[cur_arg], args[cur_arg + 1]);
2494 err_code |= ERR_ALERT | ERR_FATAL;
2495 goto out;
2496 }
2497
2498 cur_arg += 2;
2499 }
2500 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2501 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2502 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2503 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002504 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002505 file, linenum, args[cur_arg], args[cur_arg + 1]);
2506 err_code |= ERR_ALERT | ERR_FATAL;
2507 goto out;
2508 }
2509
2510 cur_arg += 2;
2511 }
2512 else if (!strcmp(args[cur_arg], "error-limit")) {
2513 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002514 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002515 file, linenum, args[cur_arg]);
2516 err_code |= ERR_ALERT | ERR_FATAL;
2517 goto out;
2518 }
2519
2520 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2521
2522 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002523 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002524 file, linenum, args[cur_arg]);
2525 err_code |= ERR_ALERT | ERR_FATAL;
2526 goto out;
2527 }
2528 cur_arg += 2;
2529 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002530 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002531 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002532 file, linenum, "usesrc", "source");
2533 err_code |= ERR_ALERT | ERR_FATAL;
2534 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002535 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 else {
2537 static int srv_dumped;
2538 struct srv_kw *kw;
2539 char *err;
2540
2541 kw = srv_find_kw(args[cur_arg]);
2542 if (kw) {
2543 char *err = NULL;
2544 int code;
2545
2546 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002547 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 +02002548 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002549 if (kw->skip != -1)
2550 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002551 err_code |= ERR_ALERT | ERR_FATAL;
2552 goto out;
2553 }
2554
2555 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002556 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002557 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002558 if (kw->skip != -1)
2559 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002560 err_code |= ERR_ALERT;
2561 continue;
2562 }
2563
2564 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2565 err_code |= code;
2566
2567 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002568 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002569 if (code & ERR_FATAL) {
2570 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002571 if (kw->skip != -1)
2572 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002573 goto out;
2574 }
2575 }
2576 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002577 if (kw->skip != -1)
2578 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002579 continue;
2580 }
2581
2582 err = NULL;
2583 if (!srv_dumped) {
2584 srv_dump_kws(&err);
2585 indent_msg(&err, 4);
2586 srv_dumped = 1;
2587 }
2588
Christopher Faulet767a84b2017-11-24 16:50:31 +01002589 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002590 file, linenum, args[0], args[1], args[cur_arg],
2591 err ? " Registered keywords :" : "", err ? err : "");
2592 free(err);
2593
2594 err_code |= ERR_ALERT | ERR_FATAL;
2595 goto out;
2596 }
2597 }
2598
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002599 if (!defsrv)
2600 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2601 if (err_code & ERR_FATAL)
2602 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002603 if (srv_tmpl)
2604 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002605 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002606 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002607 return 0;
2608
2609 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002610 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002611 free(errmsg);
2612 return err_code;
2613}
2614
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002615/* Returns a pointer to the first server matching either id <id>.
2616 * NULL is returned if no match is found.
2617 * the lookup is performed in the backend <bk>
2618 */
2619struct server *server_find_by_id(struct proxy *bk, int id)
2620{
2621 struct eb32_node *eb32;
2622 struct server *curserver;
2623
2624 if (!bk || (id ==0))
2625 return NULL;
2626
2627 /* <bk> has no backend capabilities, so it can't have a server */
2628 if (!(bk->cap & PR_CAP_BE))
2629 return NULL;
2630
2631 curserver = NULL;
2632
2633 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2634 if (eb32)
2635 curserver = container_of(eb32, struct server, conf.id);
2636
2637 return curserver;
2638}
2639
2640/* Returns a pointer to the first server matching either name <name>, or id
2641 * if <name> starts with a '#'. NULL is returned if no match is found.
2642 * the lookup is performed in the backend <bk>
2643 */
2644struct server *server_find_by_name(struct proxy *bk, const char *name)
2645{
2646 struct server *curserver;
2647
2648 if (!bk || !name)
2649 return NULL;
2650
2651 /* <bk> has no backend capabilities, so it can't have a server */
2652 if (!(bk->cap & PR_CAP_BE))
2653 return NULL;
2654
2655 curserver = NULL;
2656 if (*name == '#') {
2657 curserver = server_find_by_id(bk, atoi(name + 1));
2658 if (curserver)
2659 return curserver;
2660 }
2661 else {
2662 curserver = bk->srv;
2663
2664 while (curserver && (strcmp(curserver->id, name) != 0))
2665 curserver = curserver->next;
2666
2667 if (curserver)
2668 return curserver;
2669 }
2670
2671 return NULL;
2672}
2673
2674struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2675{
2676 struct server *byname;
2677 struct server *byid;
2678
2679 if (!name && !id)
2680 return NULL;
2681
2682 if (diff)
2683 *diff = 0;
2684
2685 byname = byid = NULL;
2686
2687 if (name) {
2688 byname = server_find_by_name(bk, name);
2689 if (byname && (!id || byname->puid == id))
2690 return byname;
2691 }
2692
2693 /* remaining possibilities :
2694 * - name not set
2695 * - name set but not found
2696 * - name found but ID doesn't match
2697 */
2698 if (id) {
2699 byid = server_find_by_id(bk, id);
2700 if (byid) {
2701 if (byname) {
2702 /* use id only if forced by configuration */
2703 if (byid->flags & SRV_F_FORCED_ID) {
2704 if (diff)
2705 *diff |= 2;
2706 return byid;
2707 }
2708 else {
2709 if (diff)
2710 *diff |= 1;
2711 return byname;
2712 }
2713 }
2714
2715 /* remaining possibilities:
2716 * - name not set
2717 * - name set but not found
2718 */
2719 if (name && diff)
2720 *diff |= 2;
2721 return byid;
2722 }
2723
2724 /* id bot found */
2725 if (byname) {
2726 if (diff)
2727 *diff |= 1;
2728 return byname;
2729 }
2730 }
2731
2732 return NULL;
2733}
2734
Willy Tarreau46b7f532018-08-21 11:54:26 +02002735/* Update a server state using the parameters available in the params list.
2736 *
2737 * Grabs the server lock during operation.
2738 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002739static void srv_update_state(struct server *srv, int version, char **params)
2740{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002741 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002742 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002743
2744 /* fields since version 1
2745 * and common to all other upcoming versions
2746 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002747 enum srv_state srv_op_state;
2748 enum srv_admin srv_admin_state;
2749 unsigned srv_uweight, srv_iweight;
2750 unsigned long srv_last_time_change;
2751 short srv_check_status;
2752 enum chk_result srv_check_result;
2753 int srv_check_health;
2754 int srv_check_state, srv_agent_state;
2755 int bk_f_forced_id;
2756 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002757 int fqdn_set_by_cli;
2758 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002759 const char *port_str;
2760 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002761 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002762
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002763 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002764 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002765 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002766 switch (version) {
2767 case 1:
2768 /*
2769 * now we can proceed with server's state update:
2770 * srv_addr: params[0]
2771 * srv_op_state: params[1]
2772 * srv_admin_state: params[2]
2773 * srv_uweight: params[3]
2774 * srv_iweight: params[4]
2775 * srv_last_time_change: params[5]
2776 * srv_check_status: params[6]
2777 * srv_check_result: params[7]
2778 * srv_check_health: params[8]
2779 * srv_check_state: params[9]
2780 * srv_agent_state: params[10]
2781 * bk_f_forced_id: params[11]
2782 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002783 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002784 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002785 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002786 */
2787
2788 /* validating srv_op_state */
2789 p = NULL;
2790 errno = 0;
2791 srv_op_state = strtol(params[1], &p, 10);
2792 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2793 (srv_op_state != SRV_ST_STOPPED &&
2794 srv_op_state != SRV_ST_STARTING &&
2795 srv_op_state != SRV_ST_RUNNING &&
2796 srv_op_state != SRV_ST_STOPPING)) {
2797 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2798 }
2799
2800 /* validating srv_admin_state */
2801 p = NULL;
2802 errno = 0;
2803 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002804 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002805
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002806 /* inherited statuses will be recomputed later.
2807 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2808 */
2809 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002810
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002811 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2812 (srv_admin_state != 0 &&
2813 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002814 srv_admin_state != SRV_ADMF_CMAINT &&
2815 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002816 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002817 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002818 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2819 }
2820
2821 /* validating srv_uweight */
2822 p = NULL;
2823 errno = 0;
2824 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002825 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002826 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2827
2828 /* validating srv_iweight */
2829 p = NULL;
2830 errno = 0;
2831 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002832 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002833 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2834
2835 /* validating srv_last_time_change */
2836 p = NULL;
2837 errno = 0;
2838 srv_last_time_change = strtol(params[5], &p, 10);
2839 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2840 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2841
2842 /* validating srv_check_status */
2843 p = NULL;
2844 errno = 0;
2845 srv_check_status = strtol(params[6], &p, 10);
2846 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2847 (srv_check_status >= HCHK_STATUS_SIZE))
2848 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2849
2850 /* validating srv_check_result */
2851 p = NULL;
2852 errno = 0;
2853 srv_check_result = strtol(params[7], &p, 10);
2854 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2855 (srv_check_result != CHK_RES_UNKNOWN &&
2856 srv_check_result != CHK_RES_NEUTRAL &&
2857 srv_check_result != CHK_RES_FAILED &&
2858 srv_check_result != CHK_RES_PASSED &&
2859 srv_check_result != CHK_RES_CONDPASS)) {
2860 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2861 }
2862
2863 /* validating srv_check_health */
2864 p = NULL;
2865 errno = 0;
2866 srv_check_health = strtol(params[8], &p, 10);
2867 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2868 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2869
2870 /* validating srv_check_state */
2871 p = NULL;
2872 errno = 0;
2873 srv_check_state = strtol(params[9], &p, 10);
2874 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2875 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2876 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2877
2878 /* validating srv_agent_state */
2879 p = NULL;
2880 errno = 0;
2881 srv_agent_state = strtol(params[10], &p, 10);
2882 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2883 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2884 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2885
2886 /* validating bk_f_forced_id */
2887 p = NULL;
2888 errno = 0;
2889 bk_f_forced_id = strtol(params[11], &p, 10);
2890 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2891 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2892
2893 /* validating srv_f_forced_id */
2894 p = NULL;
2895 errno = 0;
2896 srv_f_forced_id = strtol(params[12], &p, 10);
2897 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2898 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2899
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002900 /* validating srv_fqdn */
2901 fqdn = params[13];
2902 if (fqdn && *fqdn == '-')
2903 fqdn = NULL;
2904 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2905 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2906 fqdn = NULL;
2907 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002908
Frédéric Lécaille31694712017-08-01 08:47:19 +02002909 port_str = params[14];
2910 if (port_str) {
2911 port = strl2uic(port_str, strlen(port_str));
2912 if (port > USHRT_MAX) {
2913 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2914 port_str = NULL;
2915 }
2916 }
2917
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002918 /* SRV record
2919 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2920 */
2921 srvrecord = params[15];
2922 if (srvrecord && *srvrecord != '_')
2923 srvrecord = NULL;
2924
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002925 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002926 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002927 goto out;
2928
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002929 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002930 /* recover operational state and apply it to this server
2931 * and all servers tracking this one */
2932 switch (srv_op_state) {
2933 case SRV_ST_STOPPED:
2934 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002935 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002936 break;
2937 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002938 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002939 break;
2940 case SRV_ST_STOPPING:
2941 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002942 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002943 break;
2944 case SRV_ST_RUNNING:
2945 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002946 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002947 break;
2948 }
2949
2950 /* When applying server state, the following rules apply:
2951 * - in case of a configuration change, we apply the setting from the new
2952 * configuration, regardless of old running state
2953 * - if no configuration change, we apply old running state only if old running
2954 * state is different from new configuration state
2955 */
2956 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002957 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2958 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002959 srv_adm_set_maint(srv);
2960 else
2961 srv_adm_set_ready(srv);
2962 }
2963 /* configuration is the same, let's compate old running state and new conf state */
2964 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002965 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002966 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002967 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002968 srv_adm_set_ready(srv);
2969 }
2970 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002971 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002972 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002973 * (srv->iweight is the weight set up in configuration).
2974 * There are two possible reasons for FDRAIN to have been present :
2975 * - previous config weight was zero
2976 * - "set server b/s drain" was sent to the CLI
2977 *
2978 * In the first case, we simply want to drop this drain state
2979 * if the new weight is not zero anymore, meaning the administrator
2980 * has intentionally turned the weight back to a positive value to
2981 * enable the server again after an operation. In the second case,
2982 * the drain state was forced on the CLI regardless of the config's
2983 * weight so we don't want a change to the config weight to lose this
2984 * status. What this means is :
2985 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2986 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002987 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002988 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002989 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002990 }
2991
2992 srv->last_change = date.tv_sec - srv_last_time_change;
2993 srv->check.status = srv_check_status;
2994 srv->check.result = srv_check_result;
2995 srv->check.health = srv_check_health;
2996
2997 /* Only case we want to apply is removing ENABLED flag which could have been
2998 * done by the "disable health" command over the stats socket
2999 */
3000 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3001 (srv_check_state & CHK_ST_CONFIGURED) &&
3002 !(srv_check_state & CHK_ST_ENABLED))
3003 srv->check.state &= ~CHK_ST_ENABLED;
3004
3005 /* Only case we want to apply is removing ENABLED flag which could have been
3006 * done by the "disable agent" command over the stats socket
3007 */
3008 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3009 (srv_agent_state & CHK_ST_CONFIGURED) &&
3010 !(srv_agent_state & CHK_ST_ENABLED))
3011 srv->agent.state &= ~CHK_ST_ENABLED;
3012
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003013 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3014 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003015 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003016 * It means that a configuration file change has precedence over a unix socket change
3017 * for server's weight
3018 *
3019 * by default, HAProxy applies the following weight when parsing the configuration
3020 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003021 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003022 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003023 srv->uweight = srv_uweight;
3024 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003025 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003026
Willy Tarreaue5a60682016-11-09 14:54:53 +01003027 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003028 if (strcmp(params[0], "-"))
3029 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003030
3031 if (fqdn && srv->hostname) {
3032 if (!strcmp(srv->hostname, fqdn)) {
3033 /* Here we reset the 'set from stats socket FQDN' flag
3034 * to support such transitions:
3035 * Let's say initial FQDN value is foo1 (in configuration file).
3036 * - FQDN changed from stats socket, from foo1 to foo2 value,
3037 * - FQDN changed again from file configuration (with the same previous value
3038 set from stats socket, from foo1 to foo2 value),
3039 * - reload for any other reason than a FQDN modification,
3040 * the configuration file FQDN matches the fqdn server state file value.
3041 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3042 * any futher FQDN modification.
3043 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003044 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003045 }
3046 else {
3047 /* If the FDQN has been changed from stats socket,
3048 * apply fqdn state file value (which is the value set
3049 * from stats socket).
3050 */
3051 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003052 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003053 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003054 }
3055 }
3056 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003057 /* If all the conditions below are validated, this means
3058 * we're evaluating a server managed by SRV resolution
3059 */
3060 else if (fqdn && !srv->hostname && srvrecord) {
3061 int res;
3062
3063 /* we can't apply previous state if SRV record has changed */
3064 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3065 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);
3066 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3067 goto out;
3068 }
3069
3070 /* create or find a SRV resolution for this srv record */
3071 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3072 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3073 if (srv->srvrq == NULL) {
3074 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3075 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3076 goto out;
3077 }
3078
3079 /* prepare DNS resolution for this server */
3080 res = srv_prepare_for_resolution(srv, fqdn);
3081 if (res == -1) {
3082 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3083 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3084 goto out;
3085 }
3086
3087 /* configure check.port accordingly */
3088 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3089 !(srv->flags & SRV_F_CHECKPORT))
3090 srv->check.port = port;
3091
3092 /* Unset SRV_F_MAPPORTS for SRV records.
3093 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3094 * because no ports are provided in the configuration file.
3095 * This is because HAProxy will use the port found into the SRV record.
3096 */
3097 srv->flags &= ~SRV_F_MAPPORTS;
3098 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003099
Frédéric Lécaille31694712017-08-01 08:47:19 +02003100 if (port_str)
3101 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003102 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003103
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003104 break;
3105 default:
3106 chunk_appendf(msg, ", version '%d' not supported", version);
3107 }
3108
3109 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003110 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003111 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003112 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003113 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003114 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003115}
3116
3117/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3118 * For each proxy, it does the following:
3119 * - opens its server state file (either one or local one)
3120 * - read whole file, line by line
3121 * - analyse each line to check if it matches our current backend:
3122 * - backend name matches
3123 * - backend id matches if id is forced and name doesn't match
3124 * - if the server pointed by the line is found, then state is applied
3125 *
3126 * If the running backend uuid or id differs from the state file, then HAProxy reports
3127 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003128 *
3129 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003130 */
3131void apply_server_state(void)
3132{
3133 char *cur, *end;
3134 char mybuf[SRV_STATE_LINE_MAXLEN];
3135 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003136 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3137 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003138 int arg, srv_arg, version, diff;
3139 FILE *f;
3140 char *filepath;
3141 char globalfilepath[MAXPATHLEN + 1];
3142 char localfilepath[MAXPATHLEN + 1];
3143 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003144 struct proxy *curproxy, *bk;
3145 struct server *srv;
3146
3147 globalfilepathlen = 0;
3148 /* create the globalfilepath variable */
3149 if (global.server_state_file) {
3150 /* absolute path or no base directory provided */
3151 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3152 len = strlen(global.server_state_file);
3153 if (len > MAXPATHLEN) {
3154 globalfilepathlen = 0;
3155 goto globalfileerror;
3156 }
3157 memcpy(globalfilepath, global.server_state_file, len);
3158 globalfilepath[len] = '\0';
3159 globalfilepathlen = len;
3160 }
3161 else if (global.server_state_base) {
3162 len = strlen(global.server_state_base);
3163 globalfilepathlen += len;
3164
3165 if (globalfilepathlen > MAXPATHLEN) {
3166 globalfilepathlen = 0;
3167 goto globalfileerror;
3168 }
3169 strncpy(globalfilepath, global.server_state_base, len);
3170 globalfilepath[globalfilepathlen] = 0;
3171
3172 /* append a slash if needed */
3173 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3174 if (globalfilepathlen + 1 > MAXPATHLEN) {
3175 globalfilepathlen = 0;
3176 goto globalfileerror;
3177 }
3178 globalfilepath[globalfilepathlen++] = '/';
3179 }
3180
3181 len = strlen(global.server_state_file);
3182 if (globalfilepathlen + len > MAXPATHLEN) {
3183 globalfilepathlen = 0;
3184 goto globalfileerror;
3185 }
3186 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3187 globalfilepathlen += len;
3188 globalfilepath[globalfilepathlen++] = 0;
3189 }
3190 }
3191 globalfileerror:
3192 if (globalfilepathlen == 0)
3193 globalfilepath[0] = '\0';
3194
3195 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003196 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003197 /* servers are only in backends */
3198 if (!(curproxy->cap & PR_CAP_BE))
3199 continue;
3200 fileopenerr = 0;
3201 filepath = NULL;
3202
3203 /* search server state file path and name */
3204 switch (curproxy->load_server_state_from_file) {
3205 /* read servers state from global file */
3206 case PR_SRV_STATE_FILE_GLOBAL:
3207 /* there was an error while generating global server state file path */
3208 if (globalfilepathlen == 0)
3209 continue;
3210 filepath = globalfilepath;
3211 fileopenerr = 1;
3212 break;
3213 /* this backend has its own file */
3214 case PR_SRV_STATE_FILE_LOCAL:
3215 localfilepathlen = 0;
3216 localfilepath[0] = '\0';
3217 len = 0;
3218 /* create the localfilepath variable */
3219 /* absolute path or no base directory provided */
3220 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3221 len = strlen(curproxy->server_state_file_name);
3222 if (len > MAXPATHLEN) {
3223 localfilepathlen = 0;
3224 goto localfileerror;
3225 }
3226 memcpy(localfilepath, curproxy->server_state_file_name, len);
3227 localfilepath[len] = '\0';
3228 localfilepathlen = len;
3229 }
3230 else if (global.server_state_base) {
3231 len = strlen(global.server_state_base);
3232 localfilepathlen += len;
3233
3234 if (localfilepathlen > MAXPATHLEN) {
3235 localfilepathlen = 0;
3236 goto localfileerror;
3237 }
3238 strncpy(localfilepath, global.server_state_base, len);
3239 localfilepath[localfilepathlen] = 0;
3240
3241 /* append a slash if needed */
3242 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3243 if (localfilepathlen + 1 > MAXPATHLEN) {
3244 localfilepathlen = 0;
3245 goto localfileerror;
3246 }
3247 localfilepath[localfilepathlen++] = '/';
3248 }
3249
3250 len = strlen(curproxy->server_state_file_name);
3251 if (localfilepathlen + len > MAXPATHLEN) {
3252 localfilepathlen = 0;
3253 goto localfileerror;
3254 }
3255 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3256 localfilepathlen += len;
3257 localfilepath[localfilepathlen++] = 0;
3258 }
3259 filepath = localfilepath;
3260 localfileerror:
3261 if (localfilepathlen == 0)
3262 localfilepath[0] = '\0';
3263
3264 break;
3265 case PR_SRV_STATE_FILE_NONE:
3266 default:
3267 continue;
3268 }
3269
3270 /* preload global state file */
3271 errno = 0;
3272 f = fopen(filepath, "r");
3273 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003274 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003275 if (!f)
3276 continue;
3277
3278 mybuf[0] = '\0';
3279 mybuflen = 0;
3280 version = 0;
3281
3282 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003283 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003284 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003285 goto fileclose;
3286 }
3287
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003288 cur = mybuf;
3289 version = atoi(cur);
3290 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3291 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003292 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003293
3294 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3295 int bk_f_forced_id = 0;
3296 int check_id = 0;
3297 int check_name = 0;
3298
3299 mybuflen = strlen(mybuf);
3300 cur = mybuf;
3301 end = cur + mybuflen;
3302
3303 bk = NULL;
3304 srv = NULL;
3305
3306 /* we need at least one character */
3307 if (mybuflen == 0)
3308 continue;
3309
3310 /* ignore blank characters at the beginning of the line */
3311 while (isspace(*cur))
3312 ++cur;
3313
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003314 /* Ignore empty or commented lines */
3315 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003316 continue;
3317
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003318 /* truncated lines */
3319 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003320 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003321 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003322 }
3323
3324 /* Removes trailing '\n' */
3325 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003326
3327 /* we're now ready to move the line into *srv_params[] */
3328 params[0] = cur;
3329 arg = 1;
3330 srv_arg = 0;
3331 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3332 if (isspace(*cur)) {
3333 *cur = '\0';
3334 ++cur;
3335 while (isspace(*cur))
3336 ++cur;
3337 switch (version) {
3338 case 1:
3339 /*
3340 * srv_addr: params[4] => srv_params[0]
3341 * srv_op_state: params[5] => srv_params[1]
3342 * srv_admin_state: params[6] => srv_params[2]
3343 * srv_uweight: params[7] => srv_params[3]
3344 * srv_iweight: params[8] => srv_params[4]
3345 * srv_last_time_change: params[9] => srv_params[5]
3346 * srv_check_status: params[10] => srv_params[6]
3347 * srv_check_result: params[11] => srv_params[7]
3348 * srv_check_health: params[12] => srv_params[8]
3349 * srv_check_state: params[13] => srv_params[9]
3350 * srv_agent_state: params[14] => srv_params[10]
3351 * bk_f_forced_id: params[15] => srv_params[11]
3352 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003353 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003354 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003355 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003356 */
3357 if (arg >= 4) {
3358 srv_params[srv_arg] = cur;
3359 ++srv_arg;
3360 }
3361 break;
3362 }
3363
3364 params[arg] = cur;
3365 ++arg;
3366 }
3367 else {
3368 ++cur;
3369 }
3370 }
3371
3372 /* if line is incomplete line, then ignore it.
3373 * otherwise, update useful flags */
3374 switch (version) {
3375 case 1:
3376 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3377 continue;
3378 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3379 check_id = (atoi(params[0]) == curproxy->uuid);
3380 check_name = (strcmp(curproxy->id, params[1]) == 0);
3381 break;
3382 }
3383
3384 diff = 0;
3385 bk = curproxy;
3386
3387 /* if backend can't be found, let's continue */
3388 if (!check_id && !check_name)
3389 continue;
3390 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003391 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 +02003392 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3393 }
3394 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003395 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 +02003396 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3397 /* if name doesn't match, we still want to update curproxy if the backend id
3398 * was forced in previous the previous configuration */
3399 if (!bk_f_forced_id)
3400 continue;
3401 }
3402
3403 /* look for the server by its id: param[2] */
3404 /* else look for the server by its name: param[3] */
3405 diff = 0;
3406 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3407
3408 if (!srv) {
3409 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003410 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3411 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003412 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3413 params[3], params[2], params[0], params[1]);
3414 continue;
3415 }
3416 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003417 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 +02003418 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 +02003419 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003420 }
3421 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003422 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 +02003423 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 +02003424 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003425 }
3426
3427 /* now we can proceed with server's state update */
3428 srv_update_state(srv, version, srv_params);
3429 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003430fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003431 fclose(f);
3432 }
3433}
3434
Simon Horman7d09b9a2013-02-12 10:45:51 +09003435/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003436 * update a server's current IP address.
3437 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3438 * ip is in network format.
3439 * updater is a string which contains an information about the requester of the update.
3440 * updater is used if not NULL.
3441 *
3442 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003443 *
3444 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003445 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003446int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003447{
3448 /* generates a log line and a warning on stderr */
3449 if (1) {
3450 /* book enough space for both IPv4 and IPv6 */
3451 char oldip[INET6_ADDRSTRLEN];
3452 char newip[INET6_ADDRSTRLEN];
3453
3454 memset(oldip, '\0', INET6_ADDRSTRLEN);
3455 memset(newip, '\0', INET6_ADDRSTRLEN);
3456
3457 /* copy old IP address in a string */
3458 switch (s->addr.ss_family) {
3459 case AF_INET:
3460 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3461 break;
3462 case AF_INET6:
3463 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3464 break;
3465 };
3466
3467 /* copy new IP address in a string */
3468 switch (ip_sin_family) {
3469 case AF_INET:
3470 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3471 break;
3472 case AF_INET6:
3473 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3474 break;
3475 };
3476
3477 /* save log line into a buffer */
3478 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3479 s->proxy->id, s->id, oldip, newip, updater);
3480
3481 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003482 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003483
3484 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003485 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003486 }
3487
3488 /* save the new IP family */
3489 s->addr.ss_family = ip_sin_family;
3490 /* save the new IP address */
3491 switch (ip_sin_family) {
3492 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003493 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003494 break;
3495 case AF_INET6:
3496 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3497 break;
3498 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003499 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003500
3501 return 0;
3502}
3503
3504/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003505 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3506 *
3507 * Caller can pass its name through <updater> to get it integrated in the response message
3508 * returned by the function.
3509 *
3510 * The function first does the following, in that order:
3511 * - validates the new addr and/or port
3512 * - checks if an update is required (new IP or port is different than current ones)
3513 * - checks the update is allowed:
3514 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3515 * - allow all changes if no CHECKS are configured
3516 * - if CHECK is configured:
3517 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3518 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3519 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003520 *
3521 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003522 */
3523const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3524{
3525 struct sockaddr_storage sa;
3526 int ret, port_change_required;
3527 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003528 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003529 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003530 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003531
3532 msg = get_trash_chunk();
3533 chunk_reset(msg);
3534
3535 if (addr) {
3536 memset(&sa, 0, sizeof(struct sockaddr_storage));
3537 if (str2ip2(addr, &sa, 0) == NULL) {
3538 chunk_printf(msg, "Invalid addr '%s'", addr);
3539 goto out;
3540 }
3541
3542 /* changes are allowed on AF_INET* families only */
3543 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3544 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3545 goto out;
3546 }
3547
3548 /* collecting data currently setup */
3549 memset(current_addr, '\0', sizeof(current_addr));
3550 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3551 /* changes are allowed on AF_INET* families only */
3552 if ((ret != AF_INET) && (ret != AF_INET6)) {
3553 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3554 goto out;
3555 }
3556
3557 /* applying ADDR changes if required and allowed
3558 * ipcmp returns 0 when both ADDR are the same
3559 */
3560 if (ipcmp(&s->addr, &sa) == 0) {
3561 chunk_appendf(msg, "no need to change the addr");
3562 goto port;
3563 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003564 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003565 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003566
3567 /* we also need to update check's ADDR only if it uses the server's one */
3568 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003569 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003570 }
3571
3572 /* we also need to update agent ADDR only if it use the server's one */
3573 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003574 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003575 }
3576
3577 /* update report for caller */
3578 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3579 }
3580
3581 port:
3582 if (port) {
3583 char sign = '\0';
3584 char *endptr;
3585
3586 if (addr)
3587 chunk_appendf(msg, ", ");
3588
3589 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003590 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003591
3592 /* check if PORT change is required */
3593 port_change_required = 0;
3594
3595 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003596 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003597 new_port = strtol(port, &endptr, 10);
3598 if ((errno != 0) || (port == endptr)) {
3599 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3600 goto out;
3601 }
3602
3603 /* check if caller triggers a port mapped or offset */
3604 if (sign == '-' || (sign == '+')) {
3605 /* check if server currently uses port map */
3606 if (!(s->flags & SRV_F_MAPPORTS)) {
3607 /* switch from fixed port to port map mandatorily triggers
3608 * a port change */
3609 port_change_required = 1;
3610 /* check is configured
3611 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3612 * prevent PORT change if check doesn't have it's dedicated port while switching
3613 * to port mapping */
3614 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3615 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.");
3616 goto out;
3617 }
3618 }
3619 /* we're already using port maps */
3620 else {
3621 port_change_required = current_port != new_port;
3622 }
3623 }
3624 /* fixed port */
3625 else {
3626 port_change_required = current_port != new_port;
3627 }
3628
3629 /* applying PORT changes if required and update response message */
3630 if (port_change_required) {
3631 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003632 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003633 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003634
3635 /* prepare message */
3636 chunk_appendf(msg, "port changed from '");
3637 if (s->flags & SRV_F_MAPPORTS)
3638 chunk_appendf(msg, "+");
3639 chunk_appendf(msg, "%d' to '", current_port);
3640
3641 if (sign == '-') {
3642 s->flags |= SRV_F_MAPPORTS;
3643 chunk_appendf(msg, "%c", sign);
3644 /* just use for result output */
3645 new_port = -new_port;
3646 }
3647 else if (sign == '+') {
3648 s->flags |= SRV_F_MAPPORTS;
3649 chunk_appendf(msg, "%c", sign);
3650 }
3651 else {
3652 s->flags &= ~SRV_F_MAPPORTS;
3653 }
3654
3655 chunk_appendf(msg, "%d'", new_port);
3656
3657 /* we also need to update health checks port only if it uses server's realport */
3658 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3659 s->check.port = new_port;
3660 }
3661 }
3662 else {
3663 chunk_appendf(msg, "no need to change the port");
3664 }
3665 }
3666
3667out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003668 if (changed)
3669 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003670 if (updater)
3671 chunk_appendf(msg, " by '%s'", updater);
3672 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003673 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003674}
3675
3676
3677/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003678 * update server status based on result of name resolution
3679 * returns:
3680 * 0 if server status is updated
3681 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003682 *
3683 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003684 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003685int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003686{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003687 struct dns_resolvers *resolvers = s->resolvers;
3688 struct dns_resolution *resolution = s->dns_requester->resolution;
3689 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003690
3691 switch (resolution->status) {
3692 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003693 /* status when HAProxy has just (re)started.
3694 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003695 break;
3696
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003697 case RSLV_STATUS_VALID:
3698 /*
3699 * resume health checks
3700 * server will be turned back on if health check is safe
3701 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003702 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003703 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003704 return 1;
3705 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3706 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003707 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003708 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003709
Emeric Brun52a91d32017-08-31 14:41:55 +02003710 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003711 return 1;
3712 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3713 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3714 s->proxy->id, s->id);
3715
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003716 ha_warning("%s.\n", trash.area);
3717 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003718 return 0;
3719
3720 case RSLV_STATUS_NX:
3721 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003722 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3723 if (!tick_is_expired(exp, now_ms))
3724 break;
3725
3726 if (s->next_admin & SRV_ADMF_RMAINT)
3727 return 1;
3728 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3729 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003730
3731 case RSLV_STATUS_TIMEOUT:
3732 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003733 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3734 if (!tick_is_expired(exp, now_ms))
3735 break;
3736
3737 if (s->next_admin & SRV_ADMF_RMAINT)
3738 return 1;
3739 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3740 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003741
3742 case RSLV_STATUS_REFUSED:
3743 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003744 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3745 if (!tick_is_expired(exp, now_ms))
3746 break;
3747
3748 if (s->next_admin & SRV_ADMF_RMAINT)
3749 return 1;
3750 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3751 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003752
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003753 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003754 /* stop server if resolution failed for a long enough period */
3755 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3756 if (!tick_is_expired(exp, now_ms))
3757 break;
3758
3759 if (s->next_admin & SRV_ADMF_RMAINT)
3760 return 1;
3761 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3762 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003763 }
3764
3765 return 1;
3766}
3767
3768/*
3769 * Server Name Resolution valid response callback
3770 * It expects:
3771 * - <nameserver>: the name server which answered the valid response
3772 * - <response>: buffer containing a valid DNS response
3773 * - <response_len>: size of <response>
3774 * It performs the following actions:
3775 * - ignore response if current ip found and server family not met
3776 * - update with first new ip found if family is met and current IP is not found
3777 * returns:
3778 * 0 on error
3779 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003780 *
3781 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003782 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003783int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003784{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003785 struct server *s = NULL;
3786 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787 void *serverip, *firstip;
3788 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003789 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003790 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003791 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792
Christopher Faulet67957bd2017-09-27 11:00:59 +02003793 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003794 if (!s)
3795 return 1;
3796
Christopher Faulet67957bd2017-09-27 11:00:59 +02003797 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003798
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003799 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003800 firstip = NULL; /* pointer to the first valid response found */
3801 /* it will be used as the new IP if a change is required */
3802 firstip_sin_family = AF_UNSPEC;
3803 serverip = NULL; /* current server IP address */
3804
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003805 /* initializing server IP pointer */
3806 server_sin_family = s->addr.ss_family;
3807 switch (server_sin_family) {
3808 case AF_INET:
3809 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3810 break;
3811
3812 case AF_INET6:
3813 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3814 break;
3815
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003816 case AF_UNSPEC:
3817 break;
3818
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003819 default:
3820 goto invalid;
3821 }
3822
Baptiste Assmann729c9012017-05-22 15:13:10 +02003823 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003824 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003825 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003826
3827 switch (ret) {
3828 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003829 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003830
3831 case DNS_UPD_SRVIP_NOT_FOUND:
3832 goto save_ip;
3833
3834 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003835 goto invalid;
3836
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003837 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003838 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003839 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003840
Baptiste Assmannfad03182015-10-28 02:03:32 +01003841 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003842 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003843 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003844 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003845
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003846 default:
3847 goto invalid;
3848
3849 }
3850
3851 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003852 if (nameserver) {
3853 nameserver->counters.update++;
3854 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003855 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003856 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003857 else
3858 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003859 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003860
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003861 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003862 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003864
3865 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003866 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003867 nameserver->counters.invalid++;
3868 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003869 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003870 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003871 return 0;
3872}
3873
3874/*
3875 * Server Name Resolution error management callback
3876 * returns:
3877 * 0 on error
3878 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003879 *
3880 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003881 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003882int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003883{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003884 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003885
Christopher Faulet67957bd2017-09-27 11:00:59 +02003886 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003887 if (!s)
3888 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003889 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003890 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003891 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003892 return 1;
3893}
3894
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003895/*
3896 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003897 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003898 * It returns a pointer to the first server found or NULL if <ip> is not yet
3899 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003900 *
3901 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003902 */
3903struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3904{
3905 struct server *tmpsrv;
3906 struct proxy *be;
3907
3908 if (!srv)
3909 return NULL;
3910
3911 be = srv->proxy;
3912 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003913 /* we found the current server is the same, ignore it */
3914 if (srv == tmpsrv)
3915 continue;
3916
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003917 /* We want to compare the IP in the record with the IP of the servers in the
3918 * same backend, only if:
3919 * * DNS resolution is enabled on the server
3920 * * the hostname used for the resolution by our server is the same than the
3921 * one used for the server found in the backend
3922 * * the server found in the backend is not our current server
3923 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003924 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003925 if ((tmpsrv->hostname_dn == NULL) ||
3926 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3927 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003928 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003929 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003930 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003931 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003932
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003933 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003934 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003935 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003936 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003937 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003938
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003939 /* At this point, we have 2 different servers using the same DNS hostname
3940 * for their respective resolution.
3941 */
3942 if (*ip_family == tmpsrv->addr.ss_family &&
3943 ((tmpsrv->addr.ss_family == AF_INET &&
3944 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3945 (tmpsrv->addr.ss_family == AF_INET6 &&
3946 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003947 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003948 return tmpsrv;
3949 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003950 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003951 }
3952
Emeric Brune9fd6b52017-11-02 17:20:39 +01003953
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003954 return NULL;
3955}
3956
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003957/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3958 * resolver. This is suited for initial address configuration. Returns 0 on
3959 * success otherwise a non-zero error code. In case of error, *err_code, if
3960 * not NULL, is filled up.
3961 */
3962int srv_set_addr_via_libc(struct server *srv, int *err_code)
3963{
3964 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003965 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003966 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003967 return 1;
3968 }
3969 return 0;
3970}
3971
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003972/* Set the server's FDQN (->hostname) from <hostname>.
3973 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003974 *
3975 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003976 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003977int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003978{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003979 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003980 char *hostname_dn;
3981 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003982
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003983 /* Note that the server lock is already held. */
3984 if (!srv->resolvers)
3985 return -1;
3986
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003987 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003988 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003989 /* run time DNS resolution was not active for this server
3990 * and we can't enable it at run time for now.
3991 */
3992 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003993 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003994
3995 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003996 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003997 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003998 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3999 hostname_dn, trash.size);
4000 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004001 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004002
Christopher Faulet67957bd2017-09-27 11:00:59 +02004003 resolution = srv->dns_requester->resolution;
4004 if (resolution &&
4005 resolution->hostname_dn &&
4006 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004007 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004008
Christopher Faulet67957bd2017-09-27 11:00:59 +02004009 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004010
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004011 free(srv->hostname);
4012 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004013 srv->hostname = strdup(hostname);
4014 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004015 srv->hostname_dn_len = hostname_dn_len;
4016 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004017 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004019 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004020 goto err;
4021
4022 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004023 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004024 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004025 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004026
4027 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004028 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004029 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004030 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004031}
4032
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004033/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4034 * from the state file. This is suited for initial address configuration.
4035 * Returns 0 on success otherwise a non-zero error code. In case of error,
4036 * *err_code, if not NULL, is filled up.
4037 */
4038static int srv_apply_lastaddr(struct server *srv, int *err_code)
4039{
4040 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4041 if (err_code)
4042 *err_code |= ERR_WARN;
4043 return 1;
4044 }
4045 return 0;
4046}
4047
Willy Tarreau25e51522016-11-04 15:10:17 +01004048/* returns 0 if no error, otherwise a combination of ERR_* flags */
4049static int srv_iterate_initaddr(struct server *srv)
4050{
4051 int return_code = 0;
4052 int err_code;
4053 unsigned int methods;
4054
4055 methods = srv->init_addr_methods;
4056 if (!methods) { // default to "last,libc"
4057 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4058 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4059 }
4060
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004061 /* "-dr" : always append "none" so that server addresses resolution
4062 * failures are silently ignored, this is convenient to validate some
4063 * configs out of their environment.
4064 */
4065 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4066 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4067
Willy Tarreau25e51522016-11-04 15:10:17 +01004068 while (methods) {
4069 err_code = 0;
4070 switch (srv_get_next_initaddr(&methods)) {
4071 case SRV_IADDR_LAST:
4072 if (!srv->lastaddr)
4073 continue;
4074 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004075 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004076 return_code |= err_code;
4077 break;
4078
4079 case SRV_IADDR_LIBC:
4080 if (!srv->hostname)
4081 continue;
4082 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004083 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004084 return_code |= err_code;
4085 break;
4086
Willy Tarreau37ebe122016-11-04 15:17:58 +01004087 case SRV_IADDR_NONE:
4088 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004089 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004090 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4091 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004092 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004093 return return_code;
4094
Willy Tarreau4310d362016-11-02 15:05:56 +01004095 case SRV_IADDR_IP:
4096 ipcpy(&srv->init_addr, &srv->addr);
4097 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004098 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4099 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004100 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004101 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004102
Willy Tarreau25e51522016-11-04 15:10:17 +01004103 default: /* unhandled method */
4104 break;
4105 }
4106 }
4107
4108 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004109 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004110 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4111 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004112 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004113 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004114 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4115 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004116
4117 return_code |= ERR_ALERT | ERR_FATAL;
4118 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004119out:
4120 srv_set_dyncookie(srv);
4121 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004122}
4123
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004124/*
4125 * This function parses all backends and all servers within each backend
4126 * and performs servers' addr resolution based on information provided by:
4127 * - configuration file
4128 * - server-state file (states provided by an 'old' haproxy process)
4129 *
4130 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4131 */
4132int srv_init_addr(void)
4133{
4134 struct proxy *curproxy;
4135 int return_code = 0;
4136
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004137 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004138 while (curproxy) {
4139 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004140
4141 /* servers are in backend only */
4142 if (!(curproxy->cap & PR_CAP_BE))
4143 goto srv_init_addr_next;
4144
Willy Tarreau25e51522016-11-04 15:10:17 +01004145 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004146 if (srv->hostname)
4147 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004148
4149 srv_init_addr_next:
4150 curproxy = curproxy->next;
4151 }
4152
4153 return return_code;
4154}
4155
Willy Tarreau46b7f532018-08-21 11:54:26 +02004156/*
4157 * Must be called with the server lock held.
4158 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004159const 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 +02004160{
4161
Willy Tarreau83061a82018-07-13 11:56:34 +02004162 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004163
4164 msg = get_trash_chunk();
4165 chunk_reset(msg);
4166
Olivier Houchard8da5f982017-08-04 18:35:36 +02004167 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004168 chunk_appendf(msg, "no need to change the FDQN");
4169 goto out;
4170 }
4171
4172 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4173 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4174 goto out;
4175 }
4176
4177 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4178 server->proxy->id, server->id, server->hostname, fqdn);
4179
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004180 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004181 chunk_reset(msg);
4182 chunk_appendf(msg, "could not update %s/%s FQDN",
4183 server->proxy->id, server->id);
4184 goto out;
4185 }
4186
4187 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004188 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004189
4190 out:
4191 if (updater)
4192 chunk_appendf(msg, " by '%s'", updater);
4193 chunk_appendf(msg, "\n");
4194
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004195 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004196}
4197
4198
Willy Tarreau21b069d2016-11-23 17:15:08 +01004199/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4200 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004201 * 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 +01004202 * used for CLI commands requiring a server name.
4203 * Important: the <arg> is modified to remove the '/'.
4204 */
4205struct server *cli_find_server(struct appctx *appctx, char *arg)
4206{
4207 struct proxy *px;
4208 struct server *sv;
4209 char *line;
4210
4211 /* split "backend/server" and make <line> point to server */
4212 for (line = arg; *line; line++)
4213 if (*line == '/') {
4214 *line++ = '\0';
4215 break;
4216 }
4217
4218 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004219 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004220 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004221 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004222 return NULL;
4223 }
4224
4225 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004226 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004227 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004228 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004229 return NULL;
4230 }
4231
4232 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004233 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004234 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004235 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004236 return NULL;
4237 }
4238
4239 return sv;
4240}
4241
William Lallemand222baf22016-11-19 02:00:33 +01004242
Willy Tarreau46b7f532018-08-21 11:54:26 +02004243/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004244static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004245{
4246 struct server *sv;
4247 const char *warning;
4248
4249 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4250 return 1;
4251
4252 sv = cli_find_server(appctx, args[2]);
4253 if (!sv)
4254 return 1;
4255
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004256 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004257
William Lallemand222baf22016-11-19 02:00:33 +01004258 if (strcmp(args[3], "weight") == 0) {
4259 warning = server_parse_weight_change_request(sv, args[4]);
4260 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004261 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004262 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004263 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004264 }
4265 }
4266 else if (strcmp(args[3], "state") == 0) {
4267 if (strcmp(args[4], "ready") == 0)
4268 srv_adm_set_ready(sv);
4269 else if (strcmp(args[4], "drain") == 0)
4270 srv_adm_set_drain(sv);
4271 else if (strcmp(args[4], "maint") == 0)
4272 srv_adm_set_maint(sv);
4273 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004274 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004275 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004276 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004277 }
4278 }
4279 else if (strcmp(args[3], "health") == 0) {
4280 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004281 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004282 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004283 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004284 }
4285 else if (strcmp(args[4], "up") == 0) {
4286 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004287 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004288 }
4289 else if (strcmp(args[4], "stopping") == 0) {
4290 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004291 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004292 }
4293 else if (strcmp(args[4], "down") == 0) {
4294 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004295 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004296 }
4297 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004298 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004299 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004300 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004301 }
4302 }
4303 else if (strcmp(args[3], "agent") == 0) {
4304 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004305 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004306 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004307 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004308 }
4309 else if (strcmp(args[4], "up") == 0) {
4310 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004311 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004312 }
4313 else if (strcmp(args[4], "down") == 0) {
4314 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004315 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004316 }
4317 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004318 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004319 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004320 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004321 }
4322 }
Misiek2da082d2017-01-09 09:40:42 +01004323 else if (strcmp(args[3], "agent-addr") == 0) {
4324 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004325 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004326 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4327 appctx->st0 = CLI_ST_PRINT;
4328 } else {
4329 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004330 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004331 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4332 appctx->st0 = CLI_ST_PRINT;
4333 }
4334 }
4335 }
4336 else if (strcmp(args[3], "agent-send") == 0) {
4337 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004338 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004339 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4340 appctx->st0 = CLI_ST_PRINT;
4341 } else {
4342 char *nss = strdup(args[4]);
4343 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004344 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004345 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4346 appctx->st0 = CLI_ST_PRINT;
4347 } else {
4348 free(sv->agent.send_string);
4349 sv->agent.send_string = nss;
4350 sv->agent.send_string_len = strlen(args[4]);
4351 }
4352 }
4353 }
William Lallemand222baf22016-11-19 02:00:33 +01004354 else if (strcmp(args[3], "check-port") == 0) {
4355 int i = 0;
4356 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004357 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004358 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004359 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004360 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004361 }
4362 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004363 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004364 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004365 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004366 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004367 }
4368 /* prevent the update of port to 0 if MAPPORTS are in use */
4369 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004370 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004371 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004372 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004373 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004374 }
4375 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004376 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004377 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004378 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004379 }
4380 else if (strcmp(args[3], "addr") == 0) {
4381 char *addr = NULL;
4382 char *port = NULL;
4383 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004384 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004385 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004386 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004387 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004388 }
4389 else {
4390 addr = args[4];
4391 }
4392 if (strcmp(args[5], "port") == 0) {
4393 port = args[6];
4394 }
4395 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4396 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004397 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004398 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004399 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004400 }
4401 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4402 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004403 else if (strcmp(args[3], "fqdn") == 0) {
4404 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004405 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004406 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4407 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004408 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004409 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004410 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004411 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004412 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004413 appctx->ctx.cli.msg = warning;
4414 appctx->st0 = CLI_ST_PRINT;
4415 }
4416 }
William Lallemand222baf22016-11-19 02:00:33 +01004417 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004418 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004419 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 +01004420 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004421 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004422 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004423 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004424 return 1;
4425}
4426
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004427static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004428{
4429 struct stream_interface *si = appctx->owner;
4430 struct proxy *px;
4431 struct server *sv;
4432 char *line;
4433
4434
4435 /* split "backend/server" and make <line> point to server */
4436 for (line = args[2]; *line; line++)
4437 if (*line == '/') {
4438 *line++ = '\0';
4439 break;
4440 }
4441
4442 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004443 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004444 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004445 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004446 return 1;
4447 }
4448
4449 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004450 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004451 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004452 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004453 return 1;
4454 }
4455
4456 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004457 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4458 sv->iweight);
4459 if (ci_putstr(si_ic(si), trash.area) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004460 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004461 return 0;
4462 }
William Lallemand6b160942016-11-22 12:34:35 +01004463 return 1;
4464}
4465
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004466/* Parse a "set weight" command.
4467 *
4468 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004469 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004470static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004471{
4472 struct server *sv;
4473 const char *warning;
4474
4475 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4476 return 1;
4477
4478 sv = cli_find_server(appctx, args[2]);
4479 if (!sv)
4480 return 1;
4481
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004482 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4483
William Lallemand6b160942016-11-22 12:34:35 +01004484 warning = server_parse_weight_change_request(sv, args[3]);
4485 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004486 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004487 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004488 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004489 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004490
4491 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4492
William Lallemand6b160942016-11-22 12:34:35 +01004493 return 1;
4494}
4495
Willy Tarreau46b7f532018-08-21 11:54:26 +02004496/* parse a "set maxconn server" command. It always returns 1.
4497 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004498 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004499 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004500static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004501{
4502 struct server *sv;
4503 const char *warning;
4504
4505 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4506 return 1;
4507
4508 sv = cli_find_server(appctx, args[3]);
4509 if (!sv)
4510 return 1;
4511
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004512 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4513
Willy Tarreaub8026272016-11-23 11:26:56 +01004514 warning = server_parse_maxconn_change_request(sv, args[4]);
4515 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004516 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004517 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004518 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004519 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004520
4521 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4522
Willy Tarreaub8026272016-11-23 11:26:56 +01004523 return 1;
4524}
William Lallemand6b160942016-11-22 12:34:35 +01004525
Willy Tarreau46b7f532018-08-21 11:54:26 +02004526/* parse a "disable agent" command. It always returns 1.
4527 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004528 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004529 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004530static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004531{
4532 struct server *sv;
4533
4534 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4535 return 1;
4536
4537 sv = cli_find_server(appctx, args[2]);
4538 if (!sv)
4539 return 1;
4540
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004541 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004542 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004543 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004544 return 1;
4545}
4546
Willy Tarreau46b7f532018-08-21 11:54:26 +02004547/* parse a "disable health" command. It always returns 1.
4548 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004549 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004550 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004551static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004552{
4553 struct server *sv;
4554
4555 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4556 return 1;
4557
4558 sv = cli_find_server(appctx, args[2]);
4559 if (!sv)
4560 return 1;
4561
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004562 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004563 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004564 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004565 return 1;
4566}
4567
Willy Tarreau46b7f532018-08-21 11:54:26 +02004568/* parse a "disable server" command. It always returns 1.
4569 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004570 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004572static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004573{
4574 struct server *sv;
4575
4576 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4577 return 1;
4578
4579 sv = cli_find_server(appctx, args[2]);
4580 if (!sv)
4581 return 1;
4582
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004583 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004584 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004585 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004586 return 1;
4587}
4588
Willy Tarreau46b7f532018-08-21 11:54:26 +02004589/* parse a "enable agent" command. It always returns 1.
4590 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004591 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004593static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004594{
4595 struct server *sv;
4596
4597 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4598 return 1;
4599
4600 sv = cli_find_server(appctx, args[2]);
4601 if (!sv)
4602 return 1;
4603
4604 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004605 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004606 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004607 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004608 return 1;
4609 }
4610
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004611 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004612 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004613 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004614 return 1;
4615}
4616
Willy Tarreau46b7f532018-08-21 11:54:26 +02004617/* parse a "enable health" 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_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +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 Tarreau2c04eda2016-11-24 12:51:04 +01004633 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004634 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004635 return 1;
4636}
4637
Willy Tarreau46b7f532018-08-21 11:54:26 +02004638/* parse a "enable server" 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_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +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 Tarreauffb4d582016-11-24 12:47:00 +01004654 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004655 if (!(sv->flags & SRV_F_COOKIESET)
4656 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4657 sv->cookie)
4658 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004659 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004660 return 1;
4661}
4662
William Lallemand222baf22016-11-19 02:00:33 +01004663/* register cli keywords */
4664static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004665 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004666 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004667 { { "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 +01004668 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004669 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004670 { { "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 +01004671 { { "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 +01004672 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004673 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4674 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4675
William Lallemand222baf22016-11-19 02:00:33 +01004676 {{},}
4677}};
4678
4679__attribute__((constructor))
4680static void __server_init(void)
4681{
4682 cli_register_kw(&cli_kws);
4683}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004684
Emeric Brun64cc49c2017-10-03 14:46:45 +02004685/*
4686 * This function applies server's status changes, it is
4687 * is designed to be called asynchronously.
4688 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004689 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004690 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004691static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004692{
4693 struct check *check = &s->check;
4694 int xferred;
4695 struct proxy *px = s->proxy;
4696 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4697 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4698 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004699 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004700
Emeric Brun64cc49c2017-10-03 14:46:45 +02004701 /* If currently main is not set we try to apply pending state changes */
4702 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4703 int next_admin;
4704
4705 /* Backup next admin */
4706 next_admin = s->next_admin;
4707
4708 /* restore current admin state */
4709 s->next_admin = s->cur_admin;
4710
4711 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4712 s->last_change = now.tv_sec;
4713 if (s->proxy->lbprm.set_server_status_down)
4714 s->proxy->lbprm.set_server_status_down(s);
4715
4716 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4717 srv_shutdown_streams(s, SF_ERR_DOWN);
4718
4719 /* we might have streams queued on this server and waiting for
4720 * a connection. Those which are redispatchable will be queued
4721 * to another server or to the proxy itself.
4722 */
4723 xferred = pendconn_redistribute(s);
4724
4725 tmptrash = alloc_trash_chunk();
4726 if (tmptrash) {
4727 chunk_printf(tmptrash,
4728 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4729 s->proxy->id, s->id);
4730
Emeric Brun5a133512017-10-19 14:42:30 +02004731 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004732 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004733
4734 /* we don't send an alert if the server was previously paused */
4735 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004736 send_log(s->proxy, log_level, "%s.\n",
4737 tmptrash->area);
4738 send_email_alert(s, log_level, "%s",
4739 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004740 free_trash_chunk(tmptrash);
4741 tmptrash = NULL;
4742 }
4743 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4744 set_backend_down(s->proxy);
4745
4746 s->counters.down_trans++;
4747 }
4748 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4749 s->last_change = now.tv_sec;
4750 if (s->proxy->lbprm.set_server_status_down)
4751 s->proxy->lbprm.set_server_status_down(s);
4752
4753 /* we might have streams queued on this server and waiting for
4754 * a connection. Those which are redispatchable will be queued
4755 * to another server or to the proxy itself.
4756 */
4757 xferred = pendconn_redistribute(s);
4758
4759 tmptrash = alloc_trash_chunk();
4760 if (tmptrash) {
4761 chunk_printf(tmptrash,
4762 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4763 s->proxy->id, s->id);
4764
Emeric Brun5a133512017-10-19 14:42:30 +02004765 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004766
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004767 ha_warning("%s.\n", tmptrash->area);
4768 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4769 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004770 free_trash_chunk(tmptrash);
4771 tmptrash = NULL;
4772 }
4773
4774 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4775 set_backend_down(s->proxy);
4776 }
4777 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4778 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4779 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4780 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4781 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4782 s->proxy->last_change = now.tv_sec;
4783 }
4784
4785 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4786 s->down_time += now.tv_sec - s->last_change;
4787
4788 s->last_change = now.tv_sec;
4789 if (s->next_state == SRV_ST_STARTING)
4790 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4791
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004792 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004793 /* now propagate the status change to any LB algorithms */
4794 if (px->lbprm.update_server_eweight)
4795 px->lbprm.update_server_eweight(s);
4796 else if (srv_willbe_usable(s)) {
4797 if (px->lbprm.set_server_status_up)
4798 px->lbprm.set_server_status_up(s);
4799 }
4800 else {
4801 if (px->lbprm.set_server_status_down)
4802 px->lbprm.set_server_status_down(s);
4803 }
4804
4805 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4806 * and it's not a backup server and its effective weight is > 0,
4807 * then it can accept new connections, so we shut down all streams
4808 * on all backup servers.
4809 */
4810 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4811 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4812 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4813
4814 /* check if we can handle some connections queued at the proxy. We
4815 * will take as many as we can handle.
4816 */
4817 xferred = pendconn_grab_from_px(s);
4818
4819 tmptrash = alloc_trash_chunk();
4820 if (tmptrash) {
4821 chunk_printf(tmptrash,
4822 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4823 s->proxy->id, s->id);
4824
Emeric Brun5a133512017-10-19 14:42:30 +02004825 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004826 ha_warning("%s.\n", tmptrash->area);
4827 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4828 tmptrash->area);
4829 send_email_alert(s, LOG_NOTICE, "%s",
4830 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004831 free_trash_chunk(tmptrash);
4832 tmptrash = NULL;
4833 }
4834
4835 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4836 set_backend_down(s->proxy);
4837 }
4838 else if (s->cur_eweight != s->next_eweight) {
4839 /* now propagate the status change to any LB algorithms */
4840 if (px->lbprm.update_server_eweight)
4841 px->lbprm.update_server_eweight(s);
4842 else if (srv_willbe_usable(s)) {
4843 if (px->lbprm.set_server_status_up)
4844 px->lbprm.set_server_status_up(s);
4845 }
4846 else {
4847 if (px->lbprm.set_server_status_down)
4848 px->lbprm.set_server_status_down(s);
4849 }
4850
4851 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4852 set_backend_down(s->proxy);
4853 }
4854
4855 s->next_admin = next_admin;
4856 }
4857
Emeric Brun5a133512017-10-19 14:42:30 +02004858 /* reset operational state change */
4859 *s->op_st_chg.reason = 0;
4860 s->op_st_chg.status = s->op_st_chg.code = -1;
4861 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004862
4863 /* Now we try to apply pending admin changes */
4864
4865 /* Maintenance must also disable health checks */
4866 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4867 if (s->check.state & CHK_ST_ENABLED) {
4868 s->check.state |= CHK_ST_PAUSED;
4869 check->health = 0;
4870 }
4871
4872 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004873 tmptrash = alloc_trash_chunk();
4874 if (tmptrash) {
4875 chunk_printf(tmptrash,
4876 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4877 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4878 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004879
Olivier Houchard796a2b32017-10-24 17:42:47 +02004880 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004881
Olivier Houchard796a2b32017-10-24 17:42:47 +02004882 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004883 ha_warning("%s.\n", tmptrash->area);
4884 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4885 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004886 }
4887 free_trash_chunk(tmptrash);
4888 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004889 }
Emeric Brun8f298292017-12-06 16:47:17 +01004890 /* commit new admin status */
4891
4892 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004893 }
4894 else { /* server was still running */
4895 check->health = 0; /* failure */
4896 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004897
4898 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004899 if (s->proxy->lbprm.set_server_status_down)
4900 s->proxy->lbprm.set_server_status_down(s);
4901
Emeric Brun64cc49c2017-10-03 14:46:45 +02004902 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4903 srv_shutdown_streams(s, SF_ERR_DOWN);
4904
4905 /* we might have streams queued on this server and waiting for
4906 * a connection. Those which are redispatchable will be queued
4907 * to another server or to the proxy itself.
4908 */
4909 xferred = pendconn_redistribute(s);
4910
4911 tmptrash = alloc_trash_chunk();
4912 if (tmptrash) {
4913 chunk_printf(tmptrash,
4914 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4915 s->flags & SRV_F_BACKUP ? "Backup " : "",
4916 s->proxy->id, s->id,
4917 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4918
4919 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4920
4921 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004922 ha_warning("%s.\n", tmptrash->area);
4923 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4924 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004925 }
4926 free_trash_chunk(tmptrash);
4927 tmptrash = NULL;
4928 }
4929 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4930 set_backend_down(s->proxy);
4931
4932 s->counters.down_trans++;
4933 }
4934 }
4935 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4936 /* OK here we're leaving maintenance, we have many things to check,
4937 * because the server might possibly be coming back up depending on
4938 * its state. In practice, leaving maintenance means that we should
4939 * immediately turn to UP (more or less the slowstart) under the
4940 * following conditions :
4941 * - server is neither checked nor tracked
4942 * - server tracks another server which is not checked
4943 * - server tracks another server which is already up
4944 * Which sums up as something simpler :
4945 * "either the tracking server is up or the server's checks are disabled
4946 * or up". Otherwise we only re-enable health checks. There's a special
4947 * case associated to the stopping state which can be inherited. Note
4948 * that the server might still be in drain mode, which is naturally dealt
4949 * with by the lower level functions.
4950 */
4951
4952 if (s->check.state & CHK_ST_ENABLED) {
4953 s->check.state &= ~CHK_ST_PAUSED;
4954 check->health = check->rise; /* start OK but check immediately */
4955 }
4956
4957 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4958 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4959 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4960 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4961 s->next_state = SRV_ST_STOPPING;
4962 }
4963 else {
4964 s->next_state = SRV_ST_STARTING;
4965 if (s->slowstart > 0)
4966 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4967 else
4968 s->next_state = SRV_ST_RUNNING;
4969 }
4970
4971 }
4972
4973 tmptrash = alloc_trash_chunk();
4974 if (tmptrash) {
4975 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4976 chunk_printf(tmptrash,
4977 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4978 s->flags & SRV_F_BACKUP ? "Backup " : "",
4979 s->proxy->id, s->id,
4980 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4981 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4982 }
4983 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4984 chunk_printf(tmptrash,
4985 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4986 s->flags & SRV_F_BACKUP ? "Backup " : "",
4987 s->proxy->id, s->id, s->hostname,
4988 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4989 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4990 }
4991 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4992 chunk_printf(tmptrash,
4993 "%sServer %s/%s is %s/%s (leaving maintenance)",
4994 s->flags & SRV_F_BACKUP ? "Backup " : "",
4995 s->proxy->id, s->id,
4996 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4997 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4998 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004999 ha_warning("%s.\n", tmptrash->area);
5000 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5001 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005002 free_trash_chunk(tmptrash);
5003 tmptrash = NULL;
5004 }
5005
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005006 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005007 /* now propagate the status change to any LB algorithms */
5008 if (px->lbprm.update_server_eweight)
5009 px->lbprm.update_server_eweight(s);
5010 else if (srv_willbe_usable(s)) {
5011 if (px->lbprm.set_server_status_up)
5012 px->lbprm.set_server_status_up(s);
5013 }
5014 else {
5015 if (px->lbprm.set_server_status_down)
5016 px->lbprm.set_server_status_down(s);
5017 }
5018
5019 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5020 set_backend_down(s->proxy);
5021
Willy Tarreau6a78e612018-08-07 10:14:53 +02005022 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5023 * and it's not a backup server and its effective weight is > 0,
5024 * then it can accept new connections, so we shut down all streams
5025 * on all backup servers.
5026 */
5027 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5028 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5029 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5030
5031 /* check if we can handle some connections queued at the proxy. We
5032 * will take as many as we can handle.
5033 */
5034 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005035 }
5036 else if (s->next_admin & SRV_ADMF_MAINT) {
5037 /* remaining in maintenance mode, let's inform precisely about the
5038 * situation.
5039 */
5040 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5041 tmptrash = alloc_trash_chunk();
5042 if (tmptrash) {
5043 chunk_printf(tmptrash,
5044 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5045 s->flags & SRV_F_BACKUP ? "Backup " : "",
5046 s->proxy->id, s->id);
5047
5048 if (s->track) /* normally it's mandatory here */
5049 chunk_appendf(tmptrash, " via %s/%s",
5050 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005051 ha_warning("%s.\n", tmptrash->area);
5052 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5053 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005054 free_trash_chunk(tmptrash);
5055 tmptrash = NULL;
5056 }
5057 }
5058 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5059 tmptrash = alloc_trash_chunk();
5060 if (tmptrash) {
5061 chunk_printf(tmptrash,
5062 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5063 s->flags & SRV_F_BACKUP ? "Backup " : "",
5064 s->proxy->id, s->id, s->hostname);
5065
5066 if (s->track) /* normally it's mandatory here */
5067 chunk_appendf(tmptrash, " via %s/%s",
5068 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005069 ha_warning("%s.\n", tmptrash->area);
5070 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5071 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005072 free_trash_chunk(tmptrash);
5073 tmptrash = NULL;
5074 }
5075 }
5076 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5077 tmptrash = alloc_trash_chunk();
5078 if (tmptrash) {
5079 chunk_printf(tmptrash,
5080 "%sServer %s/%s remains in forced maintenance",
5081 s->flags & SRV_F_BACKUP ? "Backup " : "",
5082 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005083 ha_warning("%s.\n", tmptrash->area);
5084 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5085 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005086 free_trash_chunk(tmptrash);
5087 tmptrash = NULL;
5088 }
5089 }
5090 /* don't report anything when leaving drain mode and remaining in maintenance */
5091
5092 s->cur_admin = s->next_admin;
5093 }
5094
5095 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5096 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5097 /* drain state is applied only if not yet in maint */
5098
5099 s->last_change = now.tv_sec;
5100 if (px->lbprm.set_server_status_down)
5101 px->lbprm.set_server_status_down(s);
5102
5103 /* we might have streams queued on this server and waiting for
5104 * a connection. Those which are redispatchable will be queued
5105 * to another server or to the proxy itself.
5106 */
5107 xferred = pendconn_redistribute(s);
5108
5109 tmptrash = alloc_trash_chunk();
5110 if (tmptrash) {
5111 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5112 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5113 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5114
5115 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5116
5117 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005118 ha_warning("%s.\n", tmptrash->area);
5119 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5120 tmptrash->area);
5121 send_email_alert(s, LOG_NOTICE, "%s",
5122 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005123 }
5124 free_trash_chunk(tmptrash);
5125 tmptrash = NULL;
5126 }
5127
5128 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5129 set_backend_down(s->proxy);
5130 }
5131 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5132 /* OK completely leaving drain mode */
5133 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5134 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5135 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5136 s->proxy->last_change = now.tv_sec;
5137 }
5138
5139 if (s->last_change < now.tv_sec) // ignore negative times
5140 s->down_time += now.tv_sec - s->last_change;
5141 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005142 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005143
5144 tmptrash = alloc_trash_chunk();
5145 if (tmptrash) {
5146 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5147 chunk_printf(tmptrash,
5148 "%sServer %s/%s is %s (leaving forced drain)",
5149 s->flags & SRV_F_BACKUP ? "Backup " : "",
5150 s->proxy->id, s->id,
5151 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5152 }
5153 else {
5154 chunk_printf(tmptrash,
5155 "%sServer %s/%s is %s (leaving drain)",
5156 s->flags & SRV_F_BACKUP ? "Backup " : "",
5157 s->proxy->id, s->id,
5158 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5159 if (s->track) /* normally it's mandatory here */
5160 chunk_appendf(tmptrash, " via %s/%s",
5161 s->track->proxy->id, s->track->id);
5162 }
5163
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005164 ha_warning("%s.\n", tmptrash->area);
5165 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5166 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005167 free_trash_chunk(tmptrash);
5168 tmptrash = NULL;
5169 }
5170
5171 /* now propagate the status change to any LB algorithms */
5172 if (px->lbprm.update_server_eweight)
5173 px->lbprm.update_server_eweight(s);
5174 else if (srv_willbe_usable(s)) {
5175 if (px->lbprm.set_server_status_up)
5176 px->lbprm.set_server_status_up(s);
5177 }
5178 else {
5179 if (px->lbprm.set_server_status_down)
5180 px->lbprm.set_server_status_down(s);
5181 }
5182 }
5183 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5184 /* remaining in drain mode after removing one of its flags */
5185
5186 tmptrash = alloc_trash_chunk();
5187 if (tmptrash) {
5188 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5189 chunk_printf(tmptrash,
5190 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5191 s->flags & SRV_F_BACKUP ? "Backup " : "",
5192 s->proxy->id, s->id);
5193
5194 if (s->track) /* normally it's mandatory here */
5195 chunk_appendf(tmptrash, " via %s/%s",
5196 s->track->proxy->id, s->track->id);
5197 }
5198 else {
5199 chunk_printf(tmptrash,
5200 "%sServer %s/%s remains in forced drain mode",
5201 s->flags & SRV_F_BACKUP ? "Backup " : "",
5202 s->proxy->id, s->id);
5203 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005204 ha_warning("%s.\n", tmptrash->area);
5205 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5206 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005207 free_trash_chunk(tmptrash);
5208 tmptrash = NULL;
5209 }
5210
5211 /* commit new admin status */
5212
5213 s->cur_admin = s->next_admin;
5214 }
5215 }
5216
5217 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005218 *s->adm_st_chg_cause = 0;
5219}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005220
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005221/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005222 * Local variables:
5223 * c-indent-level: 8
5224 * c-basic-offset: 8
5225 * End:
5226 */