blob: 8d0ae742021bcab8607fc9c6c429e92894a02924 [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);
Dirkjan Bussink415150f2018-09-14 11:14:21 +02001483#if (OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL && !defined LIBRESSL_VERSION_NUMBER)
1484 if (src->ssl_ctx.ciphersuites != NULL)
1485 srv->ssl_ctx.ciphersuites = strdup(src->ssl_ctx.ciphersuites);
1486#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001487 if (src->sni_expr != NULL)
1488 srv->sni_expr = strdup(src->sni_expr);
1489}
1490#endif
1491
1492/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001493 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001494 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001495 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001496 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001497static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001498{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001499 char *hostname_dn;
1500 int hostname_len, hostname_dn_len;
1501
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001502 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001503 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001504
Christopher Faulet67957bd2017-09-27 11:00:59 +02001505 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001506 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001507 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1508 hostname_dn, trash.size);
1509 if (hostname_dn_len == -1)
1510 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001511
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001512
Christopher Faulet67957bd2017-09-27 11:00:59 +02001513 free(srv->hostname);
1514 free(srv->hostname_dn);
1515 srv->hostname = strdup(hostname);
1516 srv->hostname_dn = strdup(hostname_dn);
1517 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001518 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001519 goto err;
1520
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001521 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001522
1523 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001524 free(srv->hostname); srv->hostname = NULL;
1525 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001526 return -1;
1527}
1528
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001529/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001530 * Copy <src> server settings to <srv> server allocating
1531 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001532 * This function is not supposed to be called at any time, but only
1533 * during server settings parsing or during server allocations from
1534 * a server template, and just after having calloc()'ed a new server.
1535 * So, <src> may only be a default server (when parsing server settings)
1536 * or a server template (during server allocations from a server template).
1537 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1538 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001539 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001540static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001541{
1542 /* Connection source settings copy */
1543 srv_conn_src_cpy(srv, src);
1544
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001545 if (srv_tmpl) {
1546 srv->addr = src->addr;
1547 srv->svc_port = src->svc_port;
1548 }
1549
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001550 srv->pp_opts = src->pp_opts;
1551 if (src->rdr_pfx != NULL) {
1552 srv->rdr_pfx = strdup(src->rdr_pfx);
1553 srv->rdr_len = src->rdr_len;
1554 }
1555 if (src->cookie != NULL) {
1556 srv->cookie = strdup(src->cookie);
1557 srv->cklen = src->cklen;
1558 }
1559 srv->use_ssl = src->use_ssl;
1560 srv->check.addr = srv->agent.addr = src->check.addr;
1561 srv->check.use_ssl = src->check.use_ssl;
1562 srv->check.port = src->check.port;
1563 /* Note: 'flags' field has potentially been already initialized. */
1564 srv->flags |= src->flags;
1565 srv->do_check = src->do_check;
1566 srv->do_agent = src->do_agent;
1567 if (srv->check.port)
1568 srv->flags |= SRV_F_CHECKPORT;
1569 srv->check.inter = src->check.inter;
1570 srv->check.fastinter = src->check.fastinter;
1571 srv->check.downinter = src->check.downinter;
1572 srv->agent.use_ssl = src->agent.use_ssl;
1573 srv->agent.port = src->agent.port;
1574 if (src->agent.send_string != NULL)
1575 srv->agent.send_string = strdup(src->agent.send_string);
1576 srv->agent.send_string_len = src->agent.send_string_len;
1577 srv->agent.inter = src->agent.inter;
1578 srv->agent.fastinter = src->agent.fastinter;
1579 srv->agent.downinter = src->agent.downinter;
1580 srv->maxqueue = src->maxqueue;
1581 srv->minconn = src->minconn;
1582 srv->maxconn = src->maxconn;
1583 srv->slowstart = src->slowstart;
1584 srv->observe = src->observe;
1585 srv->onerror = src->onerror;
1586 srv->onmarkeddown = src->onmarkeddown;
1587 srv->onmarkedup = src->onmarkedup;
1588 if (src->trackit != NULL)
1589 srv->trackit = strdup(src->trackit);
1590 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1591 srv->uweight = srv->iweight = src->iweight;
1592
1593 srv->check.send_proxy = src->check.send_proxy;
1594 /* health: up, but will fall down at first failure */
1595 srv->check.rise = srv->check.health = src->check.rise;
1596 srv->check.fall = src->check.fall;
1597
1598 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001599 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1600 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1601 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001602 srv->check.state |= CHK_ST_PAUSED;
1603 srv->check.health = 0;
1604 }
1605
1606 /* health: up but will fall down at first failure */
1607 srv->agent.rise = srv->agent.health = src->agent.rise;
1608 srv->agent.fall = src->agent.fall;
1609
1610 if (src->resolvers_id != NULL)
1611 srv->resolvers_id = strdup(src->resolvers_id);
1612 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001613 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001614 if (srv->dns_opts.family_prio == AF_UNSPEC)
1615 srv->dns_opts.family_prio = AF_INET6;
1616 memcpy(srv->dns_opts.pref_net,
1617 src->dns_opts.pref_net,
1618 sizeof srv->dns_opts.pref_net);
1619 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1620
1621 srv->init_addr_methods = src->init_addr_methods;
1622 srv->init_addr = src->init_addr;
1623#if defined(USE_OPENSSL)
1624 srv_ssl_settings_cpy(srv, src);
1625#endif
1626#ifdef TCP_USER_TIMEOUT
1627 srv->tcp_ut = src->tcp_ut;
1628#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001629 srv->mux_proto = src->mux_proto;
1630
Olivier Houchard8da5f982017-08-04 18:35:36 +02001631 if (srv_tmpl)
1632 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001633}
1634
William Lallemand313bfd12018-10-26 14:47:32 +02001635struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001636{
1637 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001638 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001639
1640 srv = calloc(1, sizeof *srv);
1641 if (!srv)
1642 return NULL;
1643
1644 srv->obj_type = OBJ_TYPE_SERVER;
1645 srv->proxy = proxy;
1646 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001647 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001648
1649 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1650 goto free_srv;
1651 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1652 goto free_priv_conns;
1653 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1654 goto free_idle_conns;
1655
1656 for (i = 0; i < global.nbthread; i++) {
1657 LIST_INIT(&srv->priv_conns[i]);
1658 LIST_INIT(&srv->idle_conns[i]);
1659 LIST_INIT(&srv->safe_conns[i]);
1660 }
1661
Emeric Brun52a91d32017-08-31 14:41:55 +02001662 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001663 srv->last_change = now.tv_sec;
1664
1665 srv->check.status = HCHK_STATUS_INI;
1666 srv->check.server = srv;
1667 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1668
1669 srv->agent.status = HCHK_STATUS_INI;
1670 srv->agent.server = srv;
1671 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1672
1673 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001674
1675 free_idle_conns:
1676 free(srv->idle_conns);
1677 free_priv_conns:
1678 free(srv->priv_conns);
1679 free_srv:
1680 free(srv);
1681 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001682}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001683
1684/*
1685 * Validate <srv> server health-check settings.
1686 * Returns 0 if everything is OK, -1 if not.
1687 */
1688static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1689{
1690 struct tcpcheck_rule *r = NULL;
1691 struct list *l;
1692
1693 /*
1694 * We need at least a service port, a check port or the first tcp-check rule must
1695 * be a 'connect' one when checking an IPv4/IPv6 server.
1696 */
1697 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1698 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1699 return 0;
1700
1701 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1702 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001703 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1704 "Check has been disabled.\n",
1705 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001706 return -1;
1707 }
1708
1709 /* search the first action (connect / send / expect) in the list */
1710 l = &srv->proxy->tcpcheck_rules;
1711 list_for_each_entry(r, l, list) {
1712 if (r->action != TCPCHK_ACT_COMMENT)
1713 break;
1714 }
1715
1716 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001717 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1718 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1719 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001720 return -1;
1721 }
1722
1723 /* scan the tcp-check ruleset to ensure a port has been configured */
1724 l = &srv->proxy->tcpcheck_rules;
1725 list_for_each_entry(r, l, list) {
1726 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001727 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1728 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1729 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001730 return -1;
1731 }
1732 }
1733
1734 return 0;
1735}
1736
1737/*
1738 * Initialize <srv> health-check structure.
1739 * Returns the error string in case of memory allocation failure, NULL if not.
1740 */
1741static const char *do_health_check_init(struct server *srv, int check_type, int state)
1742{
1743 const char *ret;
1744
1745 if (!srv->do_check)
1746 return NULL;
1747
1748 ret = init_check(&srv->check, check_type);
1749 if (ret)
1750 return ret;
1751
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001752 srv->check.state |= state;
1753 global.maxsock++;
1754
1755 return NULL;
1756}
1757
1758static int server_health_check_init(const char *file, int linenum,
1759 struct server *srv, struct proxy *curproxy)
1760{
1761 const char *ret;
1762
1763 if (!srv->do_check)
1764 return 0;
1765
1766 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001767 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1768 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001769 return ERR_ALERT | ERR_FATAL;
1770 }
1771
1772 if (server_healthcheck_validate(file, linenum, srv) < 0)
1773 return ERR_ALERT | ERR_ABORT;
1774
1775 /* note: check type will be set during the config review phase */
1776 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1777 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001778 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001779 return ERR_ALERT | ERR_ABORT;
1780 }
1781
1782 return 0;
1783}
1784
1785/*
1786 * Initialize <srv> agent check structure.
1787 * Returns the error string in case of memory allocation failure, NULL if not.
1788 */
1789static const char *do_server_agent_check_init(struct server *srv, int state)
1790{
1791 const char *ret;
1792
1793 if (!srv->do_agent)
1794 return NULL;
1795
1796 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1797 if (ret)
1798 return ret;
1799
1800 if (!srv->agent.inter)
1801 srv->agent.inter = srv->check.inter;
1802
1803 srv->agent.state |= state;
1804 global.maxsock++;
1805
1806 return NULL;
1807}
1808
1809static int server_agent_check_init(const char *file, int linenum,
1810 struct server *srv, struct proxy *curproxy)
1811{
1812 const char *ret;
1813
1814 if (!srv->do_agent)
1815 return 0;
1816
1817 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001818 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 +02001819 file, linenum, srv->id);
1820 return ERR_ALERT | ERR_FATAL;
1821 }
1822
1823 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1824 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001825 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001826 return ERR_ALERT | ERR_ABORT;
1827 }
1828
1829 return 0;
1830}
1831
1832#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1833static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1834 struct server *srv, struct proxy *proxy)
1835{
1836 int ret;
1837 char *err = NULL;
1838
1839 if (!srv->sni_expr)
1840 return 0;
1841
1842 ret = server_parse_sni_expr(srv, proxy, &err);
1843 if (!ret)
1844 return 0;
1845
1846 display_parser_err(file, linenum, args, cur_arg, &err);
1847 free(err);
1848
1849 return ret;
1850}
1851#endif
1852
1853/*
1854 * Server initializations finalization.
1855 * Initialize health check, agent check and SNI expression if enabled.
1856 * Must not be called for a default server instance.
1857 */
1858static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1859 struct server *srv, struct proxy *px)
1860{
1861 int ret;
1862
1863 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1864 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1865 return ret;
1866 }
1867
1868#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1869 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1870 return ret;
1871#endif
1872
1873 if (srv->flags & SRV_F_BACKUP)
1874 px->srv_bck++;
1875 else
1876 px->srv_act++;
1877 srv_lb_commit_status(srv);
1878
1879 return 0;
1880}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001881
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001882/*
1883 * Parse as much as possible such a range string argument: low[-high]
1884 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1885 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1886 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1887 * Returns 0 if succeeded, -1 if not.
1888 */
1889static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1890{
1891 char *nb_high_arg;
1892
1893 *nb_high = 0;
1894 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001895 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001896
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001897 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001898 *nb_high_arg++ = '\0';
1899 *nb_high = atoi(nb_high_arg);
1900 }
1901 else {
1902 *nb_high += *nb_low;
1903 *nb_low = 1;
1904 }
1905
1906 if (*nb_low < 0 || *nb_high < *nb_low)
1907 return -1;
1908
1909 return 0;
1910}
1911
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001912static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1913{
1914 chunk_printf(&trash, "%s%d", prefix, nb);
1915 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001916 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001917}
1918
1919/*
1920 * Initialize as much as possible servers from <srv> server template.
1921 * Note that a server template is a special server with
1922 * a few different parameters than a server which has
1923 * been parsed mostly the same way as a server.
1924 * Returns the number of servers succesfully allocated,
1925 * 'srv' template included.
1926 */
1927static int server_template_init(struct server *srv, struct proxy *px)
1928{
1929 int i;
1930 struct server *newsrv;
1931
1932 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1933 int check_init_state;
1934 int agent_init_state;
1935
1936 newsrv = new_server(px);
1937 if (!newsrv)
1938 goto err;
1939
1940 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001941 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001942#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1943 if (newsrv->sni_expr) {
1944 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1945 if (!newsrv->ssl_ctx.sni)
1946 goto err;
1947 }
1948#endif
1949 /* Set this new server ID. */
1950 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1951
1952 /* Initial checks states. */
1953 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1954 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1955
1956 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1957 do_server_agent_check_init(newsrv, agent_init_state))
1958 goto err;
1959
1960 /* Linked backwards first. This will be restablished after parsing. */
1961 newsrv->next = px->srv;
1962 px->srv = newsrv;
1963 }
1964 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1965
1966 return i - srv->tmpl_info.nb_low;
1967
1968 err:
1969 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1970 if (newsrv) {
1971#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1972 release_sample_expr(newsrv->ssl_ctx.sni);
1973#endif
1974 free_check(&newsrv->agent);
1975 free_check(&newsrv->check);
1976 }
1977 free(newsrv);
1978 return i - srv->tmpl_info.nb_low;
1979}
1980
Willy Tarreau272adea2014-03-31 10:39:59 +02001981int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1982{
1983 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001984 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 char *errmsg = NULL;
1986 int err_code = 0;
1987 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001988 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001989
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001990 if (!strcmp(args[0], "server") ||
1991 !strcmp(args[0], "default-server") ||
1992 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001993 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001994 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001995 int srv = !defsrv && !strcmp(args[0], "server");
1996 int srv_tmpl = !defsrv && !srv;
1997 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001998
1999 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002000 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002001 err_code |= ERR_ALERT | ERR_FATAL;
2002 goto out;
2003 }
2004 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002005 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002006
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002007 /* There is no mandatory first arguments for default server. */
2008 if (srv) {
2009 if (!*args[2]) {
2010 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002011 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002012 file, linenum, args[0]);
2013 err_code |= ERR_ALERT | ERR_FATAL;
2014 goto out;
2015 }
2016
2017 err = invalid_char(args[1]);
2018 }
2019 else if (srv_tmpl) {
2020 if (!*args[3]) {
2021 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002022 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 +02002023 file, linenum, args[0]);
2024 err_code |= ERR_ALERT | ERR_FATAL;
2025 goto out;
2026 }
2027
2028 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002029 }
2030
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002031 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002032 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 +02002033 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002034 err_code |= ERR_ALERT | ERR_FATAL;
2035 goto out;
2036 }
2037
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002038 cur_arg = 2;
2039 if (srv_tmpl) {
2040 /* Parse server-template <nb | range> arg. */
2041 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002042 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002043 file, linenum, args[0], args[cur_arg]);
2044 err_code |= ERR_ALERT | ERR_FATAL;
2045 goto out;
2046 }
2047 cur_arg++;
2048 }
2049
Willy Tarreau272adea2014-03-31 10:39:59 +02002050 if (!defsrv) {
2051 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002052 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002053 struct protocol *proto;
2054
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002055 newsrv = new_server(curproxy);
2056 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002057 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002058 err_code |= ERR_ALERT | ERR_ABORT;
2059 goto out;
2060 }
2061
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002062 if (srv_tmpl) {
2063 newsrv->tmpl_info.nb_low = tmpl_range_low;
2064 newsrv->tmpl_info.nb_high = tmpl_range_high;
2065 }
2066
Willy Tarreau272adea2014-03-31 10:39:59 +02002067 /* the servers are linked backwards first */
2068 newsrv->next = curproxy->srv;
2069 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002070 newsrv->conf.file = strdup(file);
2071 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002072 /* Note: for a server template, its id is its prefix.
2073 * This is a temporary id which will be used for server allocations to come
2074 * after parsing.
2075 */
2076 if (srv)
2077 newsrv->id = strdup(args[1]);
2078 else
2079 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002080
2081 /* several ways to check the port component :
2082 * - IP => port=+0, relative (IPv4 only)
2083 * - IP: => port=+0, relative
2084 * - IP:N => port=N, absolute
2085 * - IP:+N => port=+N, relative
2086 * - IP:-N => port=-N, relative
2087 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002088 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002089 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002090 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002091 err_code |= ERR_ALERT | ERR_FATAL;
2092 goto out;
2093 }
2094
2095 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002096 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002097 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002098 file, linenum, args[0], args[1]);
2099 err_code |= ERR_ALERT | ERR_FATAL;
2100 goto out;
2101 }
2102
2103 if (!port1 || !port2) {
2104 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002105 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 }
2107 else if (port1 != port2) {
2108 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002109 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002110 file, linenum, args[0], args[1], args[2]);
2111 err_code |= ERR_ALERT | ERR_FATAL;
2112 goto out;
2113 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002114
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002115 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002116 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002117 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002118 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002119 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2120 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2121 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002122 err_code |= ERR_ALERT | ERR_FATAL;
2123 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002124 }
2125 }
2126 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002127 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002128 file, linenum, newsrv->id);
2129 err_code |= ERR_ALERT | ERR_FATAL;
2130 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002131 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002132 }
2133
Willy Tarreau272adea2014-03-31 10:39:59 +02002134 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002135 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002136
Olivier Houchard8da5f982017-08-04 18:35:36 +02002137 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002138 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002139 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002140 err_code |= ERR_ALERT | ERR_FATAL;
2141 goto out;
2142 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002143
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002144 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002145 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002146 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002147 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002148 } else {
2149 newsrv = &curproxy->defsrv;
2150 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002151 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002152 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002153 }
2154
2155 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002156 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002157 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2158 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002159 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002160 file, linenum, *err, newsrv->id);
2161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
2163 }
2164 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002165 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002166 file, linenum, val, args[cur_arg], newsrv->id);
2167 err_code |= ERR_ALERT | ERR_FATAL;
2168 goto out;
2169 }
2170 newsrv->agent.inter = val;
2171 cur_arg += 2;
2172 }
Misiekea849332017-01-09 09:39:51 +01002173 else if (!strcmp(args[cur_arg], "agent-addr")) {
2174 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002175 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002176 goto out;
2177 }
2178
2179 cur_arg += 2;
2180 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002181 else if (!strcmp(args[cur_arg], "agent-port")) {
2182 global.maxsock++;
2183 newsrv->agent.port = atol(args[cur_arg + 1]);
2184 cur_arg += 2;
2185 }
James Brown55f9ff12015-10-21 18:19:05 -07002186 else if (!strcmp(args[cur_arg], "agent-send")) {
2187 global.maxsock++;
2188 free(newsrv->agent.send_string);
2189 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2190 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2191 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2192 cur_arg += 2;
2193 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002194 else if (!strcmp(args[cur_arg], "init-addr")) {
2195 char *p, *end;
2196 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002197 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002198
2199 newsrv->init_addr_methods = 0;
2200 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2201
2202 for (p = args[cur_arg + 1]; *p; p = end) {
2203 /* cut on next comma */
2204 for (end = p; *end && *end != ','; end++);
2205 if (*end)
2206 *(end++) = 0;
2207
Willy Tarreau4310d362016-11-02 15:05:56 +01002208 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002209 if (!strcmp(p, "libc")) {
2210 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2211 }
2212 else if (!strcmp(p, "last")) {
2213 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2214 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002215 else if (!strcmp(p, "none")) {
2216 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2217 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002218 else if (str2ip2(p, &sa, 0)) {
2219 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002220 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002221 file, linenum, args[cur_arg], p);
2222 err_code |= ERR_ALERT | ERR_FATAL;
2223 goto out;
2224 }
2225 newsrv->init_addr = sa;
2226 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2227 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002228 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002229 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 +02002230 file, linenum, args[cur_arg], p);
2231 err_code |= ERR_ALERT | ERR_FATAL;
2232 goto out;
2233 }
2234 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002235 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002236 file, linenum, args[cur_arg], p);
2237 err_code |= ERR_ALERT | ERR_FATAL;
2238 goto out;
2239 }
2240 }
2241 cur_arg += 2;
2242 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002243 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002244 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002245 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2246 cur_arg += 2;
2247 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002248 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2249 char *p, *end;
2250
2251 for (p = args[cur_arg + 1]; *p; p = end) {
2252 /* cut on next comma */
2253 for (end = p; *end && *end != ','; end++);
2254 if (*end)
2255 *(end++) = 0;
2256
2257 if (!strcmp(p, "allow-dup-ip")) {
2258 newsrv->dns_opts.accept_duplicate_ip = 1;
2259 }
2260 else if (!strcmp(p, "prevent-dup-ip")) {
2261 newsrv->dns_opts.accept_duplicate_ip = 0;
2262 }
2263 else {
2264 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2265 file, linenum, args[cur_arg], p);
2266 err_code |= ERR_ALERT | ERR_FATAL;
2267 goto out;
2268 }
2269 }
2270
2271 cur_arg += 2;
2272 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002273 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2274 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002275 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002276 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002277 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002278 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002279 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002280 file, linenum, args[cur_arg]);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284 cur_arg += 2;
2285 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002286 else if (!strcmp(args[cur_arg], "resolve-net")) {
2287 char *p, *e;
2288 unsigned char mask;
2289 struct dns_options *opt;
2290
2291 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002292 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002293 file, linenum, args[cur_arg]);
2294 err_code |= ERR_ALERT | ERR_FATAL;
2295 goto out;
2296 }
2297
2298 opt = &newsrv->dns_opts;
2299
2300 /* Split arguments by comma, and convert it from ipv4 or ipv6
2301 * string network in in_addr or in6_addr.
2302 */
2303 p = args[cur_arg + 1];
2304 e = p;
2305 while (*p != '\0') {
2306 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002307 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002308 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002309 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2310 err_code |= ERR_ALERT | ERR_FATAL;
2311 goto out;
2312 }
2313 /* look for end or comma. */
2314 while (*e != ',' && *e != '\0')
2315 e++;
2316 if (*e == ',') {
2317 *e = '\0';
2318 e++;
2319 }
2320 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2321 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2322 /* Try to convert input string from ipv4 or ipv6 network. */
2323 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2324 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2325 &mask)) {
2326 /* Try to convert input string from ipv6 network. */
2327 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2328 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2329 } else {
2330 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002331 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002332 file, linenum, args[cur_arg], p);
2333 err_code |= ERR_ALERT | ERR_FATAL;
2334 goto out;
2335 }
2336 opt->pref_net_nb++;
2337 p = e;
2338 }
2339
2340 cur_arg += 2;
2341 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002342 else if (!strcmp(args[cur_arg], "rise")) {
2343 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002344 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002345 file, linenum, args[cur_arg]);
2346 err_code |= ERR_ALERT | ERR_FATAL;
2347 goto out;
2348 }
2349
2350 newsrv->check.rise = atol(args[cur_arg + 1]);
2351 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002352 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002353 file, linenum, args[cur_arg]);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357
2358 if (newsrv->check.health)
2359 newsrv->check.health = newsrv->check.rise;
2360 cur_arg += 2;
2361 }
2362 else if (!strcmp(args[cur_arg], "fall")) {
2363 newsrv->check.fall = atol(args[cur_arg + 1]);
2364
2365 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002366 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002367 file, linenum, args[cur_arg]);
2368 err_code |= ERR_ALERT | ERR_FATAL;
2369 goto out;
2370 }
2371
2372 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002373 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002374 file, linenum, args[cur_arg]);
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
2378
2379 cur_arg += 2;
2380 }
2381 else if (!strcmp(args[cur_arg], "inter")) {
2382 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2383 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002384 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002385 file, linenum, *err, newsrv->id);
2386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002390 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002391 file, linenum, val, args[cur_arg], newsrv->id);
2392 err_code |= ERR_ALERT | ERR_FATAL;
2393 goto out;
2394 }
2395 newsrv->check.inter = val;
2396 cur_arg += 2;
2397 }
2398 else if (!strcmp(args[cur_arg], "fastinter")) {
2399 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2400 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002401 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002402 file, linenum, *err, newsrv->id);
2403 err_code |= ERR_ALERT | ERR_FATAL;
2404 goto out;
2405 }
2406 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002407 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 file, linenum, val, args[cur_arg], newsrv->id);
2409 err_code |= ERR_ALERT | ERR_FATAL;
2410 goto out;
2411 }
2412 newsrv->check.fastinter = val;
2413 cur_arg += 2;
2414 }
2415 else if (!strcmp(args[cur_arg], "downinter")) {
2416 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2417 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002418 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 file, linenum, *err, newsrv->id);
2420 err_code |= ERR_ALERT | ERR_FATAL;
2421 goto out;
2422 }
2423 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002424 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002425 file, linenum, val, args[cur_arg], newsrv->id);
2426 err_code |= ERR_ALERT | ERR_FATAL;
2427 goto out;
2428 }
2429 newsrv->check.downinter = val;
2430 cur_arg += 2;
2431 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002432 else if (!strcmp(args[cur_arg], "port")) {
2433 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002434 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002435 cur_arg += 2;
2436 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002437 else if (!strcmp(args[cur_arg], "weight")) {
2438 int w;
2439 w = atol(args[cur_arg + 1]);
2440 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002441 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002442 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2443 err_code |= ERR_ALERT | ERR_FATAL;
2444 goto out;
2445 }
2446 newsrv->uweight = newsrv->iweight = w;
2447 cur_arg += 2;
2448 }
2449 else if (!strcmp(args[cur_arg], "minconn")) {
2450 newsrv->minconn = atol(args[cur_arg + 1]);
2451 cur_arg += 2;
2452 }
2453 else if (!strcmp(args[cur_arg], "maxconn")) {
2454 newsrv->maxconn = atol(args[cur_arg + 1]);
2455 cur_arg += 2;
2456 }
2457 else if (!strcmp(args[cur_arg], "maxqueue")) {
2458 newsrv->maxqueue = atol(args[cur_arg + 1]);
2459 cur_arg += 2;
2460 }
2461 else if (!strcmp(args[cur_arg], "slowstart")) {
2462 /* slowstart is stored in seconds */
2463 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2464 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002465 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002466 file, linenum, *err, newsrv->id);
2467 err_code |= ERR_ALERT | ERR_FATAL;
2468 goto out;
2469 }
2470 newsrv->slowstart = (val + 999) / 1000;
2471 cur_arg += 2;
2472 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002473 else if (!strcmp(args[cur_arg], "on-error")) {
2474 if (!strcmp(args[cur_arg + 1], "fastinter"))
2475 newsrv->onerror = HANA_ONERR_FASTINTER;
2476 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2477 newsrv->onerror = HANA_ONERR_FAILCHK;
2478 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2479 newsrv->onerror = HANA_ONERR_SUDDTH;
2480 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2481 newsrv->onerror = HANA_ONERR_MARKDWN;
2482 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002483 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002484 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2485 file, linenum, args[cur_arg], args[cur_arg + 1]);
2486 err_code |= ERR_ALERT | ERR_FATAL;
2487 goto out;
2488 }
2489
2490 cur_arg += 2;
2491 }
2492 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2493 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2494 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2495 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002496 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002497 file, linenum, args[cur_arg], args[cur_arg + 1]);
2498 err_code |= ERR_ALERT | ERR_FATAL;
2499 goto out;
2500 }
2501
2502 cur_arg += 2;
2503 }
2504 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2505 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2506 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2507 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002508 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002509 file, linenum, args[cur_arg], args[cur_arg + 1]);
2510 err_code |= ERR_ALERT | ERR_FATAL;
2511 goto out;
2512 }
2513
2514 cur_arg += 2;
2515 }
2516 else if (!strcmp(args[cur_arg], "error-limit")) {
2517 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002518 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002519 file, linenum, args[cur_arg]);
2520 err_code |= ERR_ALERT | ERR_FATAL;
2521 goto out;
2522 }
2523
2524 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2525
2526 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002527 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002528 file, linenum, args[cur_arg]);
2529 err_code |= ERR_ALERT | ERR_FATAL;
2530 goto out;
2531 }
2532 cur_arg += 2;
2533 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002534 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002535 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 file, linenum, "usesrc", "source");
2537 err_code |= ERR_ALERT | ERR_FATAL;
2538 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002539 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002540 else {
2541 static int srv_dumped;
2542 struct srv_kw *kw;
2543 char *err;
2544
2545 kw = srv_find_kw(args[cur_arg]);
2546 if (kw) {
2547 char *err = NULL;
2548 int code;
2549
2550 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002551 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 +02002552 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002553 if (kw->skip != -1)
2554 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002555 err_code |= ERR_ALERT | ERR_FATAL;
2556 goto out;
2557 }
2558
2559 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002560 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002561 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002562 if (kw->skip != -1)
2563 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002564 err_code |= ERR_ALERT;
2565 continue;
2566 }
2567
2568 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2569 err_code |= code;
2570
2571 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002572 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002573 if (code & ERR_FATAL) {
2574 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002575 if (kw->skip != -1)
2576 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002577 goto out;
2578 }
2579 }
2580 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002581 if (kw->skip != -1)
2582 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002583 continue;
2584 }
2585
2586 err = NULL;
2587 if (!srv_dumped) {
2588 srv_dump_kws(&err);
2589 indent_msg(&err, 4);
2590 srv_dumped = 1;
2591 }
2592
Christopher Faulet767a84b2017-11-24 16:50:31 +01002593 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002594 file, linenum, args[0], args[1], args[cur_arg],
2595 err ? " Registered keywords :" : "", err ? err : "");
2596 free(err);
2597
2598 err_code |= ERR_ALERT | ERR_FATAL;
2599 goto out;
2600 }
2601 }
2602
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002603 if (!defsrv)
2604 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2605 if (err_code & ERR_FATAL)
2606 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002607 if (srv_tmpl)
2608 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002609 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002610 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002611 return 0;
2612
2613 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002614 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002615 free(errmsg);
2616 return err_code;
2617}
2618
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002619/* Returns a pointer to the first server matching either id <id>.
2620 * NULL is returned if no match is found.
2621 * the lookup is performed in the backend <bk>
2622 */
2623struct server *server_find_by_id(struct proxy *bk, int id)
2624{
2625 struct eb32_node *eb32;
2626 struct server *curserver;
2627
2628 if (!bk || (id ==0))
2629 return NULL;
2630
2631 /* <bk> has no backend capabilities, so it can't have a server */
2632 if (!(bk->cap & PR_CAP_BE))
2633 return NULL;
2634
2635 curserver = NULL;
2636
2637 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2638 if (eb32)
2639 curserver = container_of(eb32, struct server, conf.id);
2640
2641 return curserver;
2642}
2643
2644/* Returns a pointer to the first server matching either name <name>, or id
2645 * if <name> starts with a '#'. NULL is returned if no match is found.
2646 * the lookup is performed in the backend <bk>
2647 */
2648struct server *server_find_by_name(struct proxy *bk, const char *name)
2649{
2650 struct server *curserver;
2651
2652 if (!bk || !name)
2653 return NULL;
2654
2655 /* <bk> has no backend capabilities, so it can't have a server */
2656 if (!(bk->cap & PR_CAP_BE))
2657 return NULL;
2658
2659 curserver = NULL;
2660 if (*name == '#') {
2661 curserver = server_find_by_id(bk, atoi(name + 1));
2662 if (curserver)
2663 return curserver;
2664 }
2665 else {
2666 curserver = bk->srv;
2667
2668 while (curserver && (strcmp(curserver->id, name) != 0))
2669 curserver = curserver->next;
2670
2671 if (curserver)
2672 return curserver;
2673 }
2674
2675 return NULL;
2676}
2677
2678struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2679{
2680 struct server *byname;
2681 struct server *byid;
2682
2683 if (!name && !id)
2684 return NULL;
2685
2686 if (diff)
2687 *diff = 0;
2688
2689 byname = byid = NULL;
2690
2691 if (name) {
2692 byname = server_find_by_name(bk, name);
2693 if (byname && (!id || byname->puid == id))
2694 return byname;
2695 }
2696
2697 /* remaining possibilities :
2698 * - name not set
2699 * - name set but not found
2700 * - name found but ID doesn't match
2701 */
2702 if (id) {
2703 byid = server_find_by_id(bk, id);
2704 if (byid) {
2705 if (byname) {
2706 /* use id only if forced by configuration */
2707 if (byid->flags & SRV_F_FORCED_ID) {
2708 if (diff)
2709 *diff |= 2;
2710 return byid;
2711 }
2712 else {
2713 if (diff)
2714 *diff |= 1;
2715 return byname;
2716 }
2717 }
2718
2719 /* remaining possibilities:
2720 * - name not set
2721 * - name set but not found
2722 */
2723 if (name && diff)
2724 *diff |= 2;
2725 return byid;
2726 }
2727
2728 /* id bot found */
2729 if (byname) {
2730 if (diff)
2731 *diff |= 1;
2732 return byname;
2733 }
2734 }
2735
2736 return NULL;
2737}
2738
Willy Tarreau46b7f532018-08-21 11:54:26 +02002739/* Update a server state using the parameters available in the params list.
2740 *
2741 * Grabs the server lock during operation.
2742 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002743static void srv_update_state(struct server *srv, int version, char **params)
2744{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002745 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002746 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002747
2748 /* fields since version 1
2749 * and common to all other upcoming versions
2750 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002751 enum srv_state srv_op_state;
2752 enum srv_admin srv_admin_state;
2753 unsigned srv_uweight, srv_iweight;
2754 unsigned long srv_last_time_change;
2755 short srv_check_status;
2756 enum chk_result srv_check_result;
2757 int srv_check_health;
2758 int srv_check_state, srv_agent_state;
2759 int bk_f_forced_id;
2760 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002761 int fqdn_set_by_cli;
2762 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002763 const char *port_str;
2764 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002765 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002766
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002767 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002768 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002769 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002770 switch (version) {
2771 case 1:
2772 /*
2773 * now we can proceed with server's state update:
2774 * srv_addr: params[0]
2775 * srv_op_state: params[1]
2776 * srv_admin_state: params[2]
2777 * srv_uweight: params[3]
2778 * srv_iweight: params[4]
2779 * srv_last_time_change: params[5]
2780 * srv_check_status: params[6]
2781 * srv_check_result: params[7]
2782 * srv_check_health: params[8]
2783 * srv_check_state: params[9]
2784 * srv_agent_state: params[10]
2785 * bk_f_forced_id: params[11]
2786 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002787 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002788 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002789 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002790 */
2791
2792 /* validating srv_op_state */
2793 p = NULL;
2794 errno = 0;
2795 srv_op_state = strtol(params[1], &p, 10);
2796 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2797 (srv_op_state != SRV_ST_STOPPED &&
2798 srv_op_state != SRV_ST_STARTING &&
2799 srv_op_state != SRV_ST_RUNNING &&
2800 srv_op_state != SRV_ST_STOPPING)) {
2801 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2802 }
2803
2804 /* validating srv_admin_state */
2805 p = NULL;
2806 errno = 0;
2807 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002808 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002809
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002810 /* inherited statuses will be recomputed later.
2811 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2812 */
2813 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002814
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002815 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2816 (srv_admin_state != 0 &&
2817 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002818 srv_admin_state != SRV_ADMF_CMAINT &&
2819 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002820 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002821 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2823 }
2824
2825 /* validating srv_uweight */
2826 p = NULL;
2827 errno = 0;
2828 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002829 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002830 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2831
2832 /* validating srv_iweight */
2833 p = NULL;
2834 errno = 0;
2835 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002836 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002837 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2838
2839 /* validating srv_last_time_change */
2840 p = NULL;
2841 errno = 0;
2842 srv_last_time_change = strtol(params[5], &p, 10);
2843 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2844 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2845
2846 /* validating srv_check_status */
2847 p = NULL;
2848 errno = 0;
2849 srv_check_status = strtol(params[6], &p, 10);
2850 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2851 (srv_check_status >= HCHK_STATUS_SIZE))
2852 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2853
2854 /* validating srv_check_result */
2855 p = NULL;
2856 errno = 0;
2857 srv_check_result = strtol(params[7], &p, 10);
2858 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2859 (srv_check_result != CHK_RES_UNKNOWN &&
2860 srv_check_result != CHK_RES_NEUTRAL &&
2861 srv_check_result != CHK_RES_FAILED &&
2862 srv_check_result != CHK_RES_PASSED &&
2863 srv_check_result != CHK_RES_CONDPASS)) {
2864 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2865 }
2866
2867 /* validating srv_check_health */
2868 p = NULL;
2869 errno = 0;
2870 srv_check_health = strtol(params[8], &p, 10);
2871 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2872 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2873
2874 /* validating srv_check_state */
2875 p = NULL;
2876 errno = 0;
2877 srv_check_state = strtol(params[9], &p, 10);
2878 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2879 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2880 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2881
2882 /* validating srv_agent_state */
2883 p = NULL;
2884 errno = 0;
2885 srv_agent_state = strtol(params[10], &p, 10);
2886 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2887 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2888 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2889
2890 /* validating bk_f_forced_id */
2891 p = NULL;
2892 errno = 0;
2893 bk_f_forced_id = strtol(params[11], &p, 10);
2894 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2895 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2896
2897 /* validating srv_f_forced_id */
2898 p = NULL;
2899 errno = 0;
2900 srv_f_forced_id = strtol(params[12], &p, 10);
2901 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2902 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2903
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002904 /* validating srv_fqdn */
2905 fqdn = params[13];
2906 if (fqdn && *fqdn == '-')
2907 fqdn = NULL;
2908 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2909 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2910 fqdn = NULL;
2911 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002912
Frédéric Lécaille31694712017-08-01 08:47:19 +02002913 port_str = params[14];
2914 if (port_str) {
2915 port = strl2uic(port_str, strlen(port_str));
2916 if (port > USHRT_MAX) {
2917 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2918 port_str = NULL;
2919 }
2920 }
2921
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002922 /* SRV record
2923 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2924 */
2925 srvrecord = params[15];
2926 if (srvrecord && *srvrecord != '_')
2927 srvrecord = NULL;
2928
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002929 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002930 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002931 goto out;
2932
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002933 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 /* recover operational state and apply it to this server
2935 * and all servers tracking this one */
2936 switch (srv_op_state) {
2937 case SRV_ST_STOPPED:
2938 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002939 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002940 break;
2941 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002942 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002943 break;
2944 case SRV_ST_STOPPING:
2945 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002946 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002947 break;
2948 case SRV_ST_RUNNING:
2949 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002950 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002951 break;
2952 }
2953
2954 /* When applying server state, the following rules apply:
2955 * - in case of a configuration change, we apply the setting from the new
2956 * configuration, regardless of old running state
2957 * - if no configuration change, we apply old running state only if old running
2958 * state is different from new configuration state
2959 */
2960 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002961 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2962 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002963 srv_adm_set_maint(srv);
2964 else
2965 srv_adm_set_ready(srv);
2966 }
2967 /* configuration is the same, let's compate old running state and new conf state */
2968 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002969 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002970 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002971 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002972 srv_adm_set_ready(srv);
2973 }
2974 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002975 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002976 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002977 * (srv->iweight is the weight set up in configuration).
2978 * There are two possible reasons for FDRAIN to have been present :
2979 * - previous config weight was zero
2980 * - "set server b/s drain" was sent to the CLI
2981 *
2982 * In the first case, we simply want to drop this drain state
2983 * if the new weight is not zero anymore, meaning the administrator
2984 * has intentionally turned the weight back to a positive value to
2985 * enable the server again after an operation. In the second case,
2986 * the drain state was forced on the CLI regardless of the config's
2987 * weight so we don't want a change to the config weight to lose this
2988 * status. What this means is :
2989 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2990 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002991 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002992 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002993 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002994 }
2995
2996 srv->last_change = date.tv_sec - srv_last_time_change;
2997 srv->check.status = srv_check_status;
2998 srv->check.result = srv_check_result;
2999 srv->check.health = srv_check_health;
3000
3001 /* Only case we want to apply is removing ENABLED flag which could have been
3002 * done by the "disable health" command over the stats socket
3003 */
3004 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3005 (srv_check_state & CHK_ST_CONFIGURED) &&
3006 !(srv_check_state & CHK_ST_ENABLED))
3007 srv->check.state &= ~CHK_ST_ENABLED;
3008
3009 /* Only case we want to apply is removing ENABLED flag which could have been
3010 * done by the "disable agent" command over the stats socket
3011 */
3012 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3013 (srv_agent_state & CHK_ST_CONFIGURED) &&
3014 !(srv_agent_state & CHK_ST_ENABLED))
3015 srv->agent.state &= ~CHK_ST_ENABLED;
3016
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003017 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3018 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003019 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003020 * It means that a configuration file change has precedence over a unix socket change
3021 * for server's weight
3022 *
3023 * by default, HAProxy applies the following weight when parsing the configuration
3024 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003025 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003026 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003027 srv->uweight = srv_uweight;
3028 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003029 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003030
Willy Tarreaue5a60682016-11-09 14:54:53 +01003031 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003032 if (strcmp(params[0], "-"))
3033 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003034
3035 if (fqdn && srv->hostname) {
3036 if (!strcmp(srv->hostname, fqdn)) {
3037 /* Here we reset the 'set from stats socket FQDN' flag
3038 * to support such transitions:
3039 * Let's say initial FQDN value is foo1 (in configuration file).
3040 * - FQDN changed from stats socket, from foo1 to foo2 value,
3041 * - FQDN changed again from file configuration (with the same previous value
3042 set from stats socket, from foo1 to foo2 value),
3043 * - reload for any other reason than a FQDN modification,
3044 * the configuration file FQDN matches the fqdn server state file value.
3045 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3046 * any futher FQDN modification.
3047 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003048 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003049 }
3050 else {
3051 /* If the FDQN has been changed from stats socket,
3052 * apply fqdn state file value (which is the value set
3053 * from stats socket).
3054 */
3055 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003056 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003057 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003058 }
3059 }
3060 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003061 /* If all the conditions below are validated, this means
3062 * we're evaluating a server managed by SRV resolution
3063 */
3064 else if (fqdn && !srv->hostname && srvrecord) {
3065 int res;
3066
3067 /* we can't apply previous state if SRV record has changed */
3068 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3069 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);
3070 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3071 goto out;
3072 }
3073
3074 /* create or find a SRV resolution for this srv record */
3075 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3076 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3077 if (srv->srvrq == NULL) {
3078 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3079 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3080 goto out;
3081 }
3082
3083 /* prepare DNS resolution for this server */
3084 res = srv_prepare_for_resolution(srv, fqdn);
3085 if (res == -1) {
3086 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3087 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3088 goto out;
3089 }
3090
3091 /* configure check.port accordingly */
3092 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3093 !(srv->flags & SRV_F_CHECKPORT))
3094 srv->check.port = port;
3095
3096 /* Unset SRV_F_MAPPORTS for SRV records.
3097 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3098 * because no ports are provided in the configuration file.
3099 * This is because HAProxy will use the port found into the SRV record.
3100 */
3101 srv->flags &= ~SRV_F_MAPPORTS;
3102 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003103
Frédéric Lécaille31694712017-08-01 08:47:19 +02003104 if (port_str)
3105 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003106 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003107
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003108 break;
3109 default:
3110 chunk_appendf(msg, ", version '%d' not supported", version);
3111 }
3112
3113 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003114 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003115 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003116 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003117 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003118 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003119}
3120
3121/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3122 * For each proxy, it does the following:
3123 * - opens its server state file (either one or local one)
3124 * - read whole file, line by line
3125 * - analyse each line to check if it matches our current backend:
3126 * - backend name matches
3127 * - backend id matches if id is forced and name doesn't match
3128 * - if the server pointed by the line is found, then state is applied
3129 *
3130 * If the running backend uuid or id differs from the state file, then HAProxy reports
3131 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003132 *
3133 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003134 */
3135void apply_server_state(void)
3136{
3137 char *cur, *end;
3138 char mybuf[SRV_STATE_LINE_MAXLEN];
3139 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003140 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3141 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 int arg, srv_arg, version, diff;
3143 FILE *f;
3144 char *filepath;
3145 char globalfilepath[MAXPATHLEN + 1];
3146 char localfilepath[MAXPATHLEN + 1];
3147 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003148 struct proxy *curproxy, *bk;
3149 struct server *srv;
3150
3151 globalfilepathlen = 0;
3152 /* create the globalfilepath variable */
3153 if (global.server_state_file) {
3154 /* absolute path or no base directory provided */
3155 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3156 len = strlen(global.server_state_file);
3157 if (len > MAXPATHLEN) {
3158 globalfilepathlen = 0;
3159 goto globalfileerror;
3160 }
3161 memcpy(globalfilepath, global.server_state_file, len);
3162 globalfilepath[len] = '\0';
3163 globalfilepathlen = len;
3164 }
3165 else if (global.server_state_base) {
3166 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003167 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003168 globalfilepathlen = 0;
3169 goto globalfileerror;
3170 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003171 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003172 globalfilepath[len] = 0;
3173 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003174
3175 /* append a slash if needed */
3176 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3177 if (globalfilepathlen + 1 > MAXPATHLEN) {
3178 globalfilepathlen = 0;
3179 goto globalfileerror;
3180 }
3181 globalfilepath[globalfilepathlen++] = '/';
3182 }
3183
3184 len = strlen(global.server_state_file);
3185 if (globalfilepathlen + len > MAXPATHLEN) {
3186 globalfilepathlen = 0;
3187 goto globalfileerror;
3188 }
3189 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3190 globalfilepathlen += len;
3191 globalfilepath[globalfilepathlen++] = 0;
3192 }
3193 }
3194 globalfileerror:
3195 if (globalfilepathlen == 0)
3196 globalfilepath[0] = '\0';
3197
3198 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003199 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003200 /* servers are only in backends */
3201 if (!(curproxy->cap & PR_CAP_BE))
3202 continue;
3203 fileopenerr = 0;
3204 filepath = NULL;
3205
3206 /* search server state file path and name */
3207 switch (curproxy->load_server_state_from_file) {
3208 /* read servers state from global file */
3209 case PR_SRV_STATE_FILE_GLOBAL:
3210 /* there was an error while generating global server state file path */
3211 if (globalfilepathlen == 0)
3212 continue;
3213 filepath = globalfilepath;
3214 fileopenerr = 1;
3215 break;
3216 /* this backend has its own file */
3217 case PR_SRV_STATE_FILE_LOCAL:
3218 localfilepathlen = 0;
3219 localfilepath[0] = '\0';
3220 len = 0;
3221 /* create the localfilepath variable */
3222 /* absolute path or no base directory provided */
3223 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3224 len = strlen(curproxy->server_state_file_name);
3225 if (len > MAXPATHLEN) {
3226 localfilepathlen = 0;
3227 goto localfileerror;
3228 }
3229 memcpy(localfilepath, curproxy->server_state_file_name, len);
3230 localfilepath[len] = '\0';
3231 localfilepathlen = len;
3232 }
3233 else if (global.server_state_base) {
3234 len = strlen(global.server_state_base);
3235 localfilepathlen += len;
3236
3237 if (localfilepathlen > MAXPATHLEN) {
3238 localfilepathlen = 0;
3239 goto localfileerror;
3240 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003241 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003242 localfilepath[localfilepathlen] = 0;
3243
3244 /* append a slash if needed */
3245 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3246 if (localfilepathlen + 1 > MAXPATHLEN) {
3247 localfilepathlen = 0;
3248 goto localfileerror;
3249 }
3250 localfilepath[localfilepathlen++] = '/';
3251 }
3252
3253 len = strlen(curproxy->server_state_file_name);
3254 if (localfilepathlen + len > MAXPATHLEN) {
3255 localfilepathlen = 0;
3256 goto localfileerror;
3257 }
3258 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3259 localfilepathlen += len;
3260 localfilepath[localfilepathlen++] = 0;
3261 }
3262 filepath = localfilepath;
3263 localfileerror:
3264 if (localfilepathlen == 0)
3265 localfilepath[0] = '\0';
3266
3267 break;
3268 case PR_SRV_STATE_FILE_NONE:
3269 default:
3270 continue;
3271 }
3272
3273 /* preload global state file */
3274 errno = 0;
3275 f = fopen(filepath, "r");
3276 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003277 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003278 if (!f)
3279 continue;
3280
3281 mybuf[0] = '\0';
3282 mybuflen = 0;
3283 version = 0;
3284
3285 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003286 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003287 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003288 goto fileclose;
3289 }
3290
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003291 cur = mybuf;
3292 version = atoi(cur);
3293 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3294 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003295 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003296
3297 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3298 int bk_f_forced_id = 0;
3299 int check_id = 0;
3300 int check_name = 0;
3301
3302 mybuflen = strlen(mybuf);
3303 cur = mybuf;
3304 end = cur + mybuflen;
3305
3306 bk = NULL;
3307 srv = NULL;
3308
3309 /* we need at least one character */
3310 if (mybuflen == 0)
3311 continue;
3312
3313 /* ignore blank characters at the beginning of the line */
3314 while (isspace(*cur))
3315 ++cur;
3316
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003317 /* Ignore empty or commented lines */
3318 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003319 continue;
3320
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003321 /* truncated lines */
3322 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003323 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003324 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003325 }
3326
3327 /* Removes trailing '\n' */
3328 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003329
3330 /* we're now ready to move the line into *srv_params[] */
3331 params[0] = cur;
3332 arg = 1;
3333 srv_arg = 0;
3334 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3335 if (isspace(*cur)) {
3336 *cur = '\0';
3337 ++cur;
3338 while (isspace(*cur))
3339 ++cur;
3340 switch (version) {
3341 case 1:
3342 /*
3343 * srv_addr: params[4] => srv_params[0]
3344 * srv_op_state: params[5] => srv_params[1]
3345 * srv_admin_state: params[6] => srv_params[2]
3346 * srv_uweight: params[7] => srv_params[3]
3347 * srv_iweight: params[8] => srv_params[4]
3348 * srv_last_time_change: params[9] => srv_params[5]
3349 * srv_check_status: params[10] => srv_params[6]
3350 * srv_check_result: params[11] => srv_params[7]
3351 * srv_check_health: params[12] => srv_params[8]
3352 * srv_check_state: params[13] => srv_params[9]
3353 * srv_agent_state: params[14] => srv_params[10]
3354 * bk_f_forced_id: params[15] => srv_params[11]
3355 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003356 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003357 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003358 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003359 */
3360 if (arg >= 4) {
3361 srv_params[srv_arg] = cur;
3362 ++srv_arg;
3363 }
3364 break;
3365 }
3366
3367 params[arg] = cur;
3368 ++arg;
3369 }
3370 else {
3371 ++cur;
3372 }
3373 }
3374
3375 /* if line is incomplete line, then ignore it.
3376 * otherwise, update useful flags */
3377 switch (version) {
3378 case 1:
3379 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3380 continue;
3381 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3382 check_id = (atoi(params[0]) == curproxy->uuid);
3383 check_name = (strcmp(curproxy->id, params[1]) == 0);
3384 break;
3385 }
3386
3387 diff = 0;
3388 bk = curproxy;
3389
3390 /* if backend can't be found, let's continue */
3391 if (!check_id && !check_name)
3392 continue;
3393 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003394 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 +02003395 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3396 }
3397 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003398 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 +02003399 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3400 /* if name doesn't match, we still want to update curproxy if the backend id
3401 * was forced in previous the previous configuration */
3402 if (!bk_f_forced_id)
3403 continue;
3404 }
3405
3406 /* look for the server by its id: param[2] */
3407 /* else look for the server by its name: param[3] */
3408 diff = 0;
3409 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3410
3411 if (!srv) {
3412 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3414 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003415 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3416 params[3], params[2], params[0], params[1]);
3417 continue;
3418 }
3419 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003420 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 +02003421 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 +02003422 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003423 }
3424 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003425 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 +02003426 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 +02003427 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003428 }
3429
3430 /* now we can proceed with server's state update */
3431 srv_update_state(srv, version, srv_params);
3432 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003433fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003434 fclose(f);
3435 }
3436}
3437
Simon Horman7d09b9a2013-02-12 10:45:51 +09003438/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003439 * update a server's current IP address.
3440 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3441 * ip is in network format.
3442 * updater is a string which contains an information about the requester of the update.
3443 * updater is used if not NULL.
3444 *
3445 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003446 *
3447 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003448 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003449int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003450{
3451 /* generates a log line and a warning on stderr */
3452 if (1) {
3453 /* book enough space for both IPv4 and IPv6 */
3454 char oldip[INET6_ADDRSTRLEN];
3455 char newip[INET6_ADDRSTRLEN];
3456
3457 memset(oldip, '\0', INET6_ADDRSTRLEN);
3458 memset(newip, '\0', INET6_ADDRSTRLEN);
3459
3460 /* copy old IP address in a string */
3461 switch (s->addr.ss_family) {
3462 case AF_INET:
3463 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3464 break;
3465 case AF_INET6:
3466 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3467 break;
3468 };
3469
3470 /* copy new IP address in a string */
3471 switch (ip_sin_family) {
3472 case AF_INET:
3473 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3474 break;
3475 case AF_INET6:
3476 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3477 break;
3478 };
3479
3480 /* save log line into a buffer */
3481 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3482 s->proxy->id, s->id, oldip, newip, updater);
3483
3484 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003485 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003486
3487 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003488 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003489 }
3490
3491 /* save the new IP family */
3492 s->addr.ss_family = ip_sin_family;
3493 /* save the new IP address */
3494 switch (ip_sin_family) {
3495 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003496 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003497 break;
3498 case AF_INET6:
3499 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3500 break;
3501 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003502 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003503
3504 return 0;
3505}
3506
3507/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003508 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3509 *
3510 * Caller can pass its name through <updater> to get it integrated in the response message
3511 * returned by the function.
3512 *
3513 * The function first does the following, in that order:
3514 * - validates the new addr and/or port
3515 * - checks if an update is required (new IP or port is different than current ones)
3516 * - checks the update is allowed:
3517 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3518 * - allow all changes if no CHECKS are configured
3519 * - if CHECK is configured:
3520 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3521 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3522 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003523 *
3524 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003525 */
3526const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3527{
3528 struct sockaddr_storage sa;
3529 int ret, port_change_required;
3530 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003531 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003532 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003533 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003534
3535 msg = get_trash_chunk();
3536 chunk_reset(msg);
3537
3538 if (addr) {
3539 memset(&sa, 0, sizeof(struct sockaddr_storage));
3540 if (str2ip2(addr, &sa, 0) == NULL) {
3541 chunk_printf(msg, "Invalid addr '%s'", addr);
3542 goto out;
3543 }
3544
3545 /* changes are allowed on AF_INET* families only */
3546 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3547 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3548 goto out;
3549 }
3550
3551 /* collecting data currently setup */
3552 memset(current_addr, '\0', sizeof(current_addr));
3553 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3554 /* changes are allowed on AF_INET* families only */
3555 if ((ret != AF_INET) && (ret != AF_INET6)) {
3556 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3557 goto out;
3558 }
3559
3560 /* applying ADDR changes if required and allowed
3561 * ipcmp returns 0 when both ADDR are the same
3562 */
3563 if (ipcmp(&s->addr, &sa) == 0) {
3564 chunk_appendf(msg, "no need to change the addr");
3565 goto port;
3566 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003567 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003568 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003569
3570 /* we also need to update check's ADDR only if it uses the server's one */
3571 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003572 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003573 }
3574
3575 /* we also need to update agent ADDR only if it use the server's one */
3576 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003577 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003578 }
3579
3580 /* update report for caller */
3581 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3582 }
3583
3584 port:
3585 if (port) {
3586 char sign = '\0';
3587 char *endptr;
3588
3589 if (addr)
3590 chunk_appendf(msg, ", ");
3591
3592 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003593 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003594
3595 /* check if PORT change is required */
3596 port_change_required = 0;
3597
3598 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003599 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003600 new_port = strtol(port, &endptr, 10);
3601 if ((errno != 0) || (port == endptr)) {
3602 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3603 goto out;
3604 }
3605
3606 /* check if caller triggers a port mapped or offset */
3607 if (sign == '-' || (sign == '+')) {
3608 /* check if server currently uses port map */
3609 if (!(s->flags & SRV_F_MAPPORTS)) {
3610 /* switch from fixed port to port map mandatorily triggers
3611 * a port change */
3612 port_change_required = 1;
3613 /* check is configured
3614 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3615 * prevent PORT change if check doesn't have it's dedicated port while switching
3616 * to port mapping */
3617 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3618 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.");
3619 goto out;
3620 }
3621 }
3622 /* we're already using port maps */
3623 else {
3624 port_change_required = current_port != new_port;
3625 }
3626 }
3627 /* fixed port */
3628 else {
3629 port_change_required = current_port != new_port;
3630 }
3631
3632 /* applying PORT changes if required and update response message */
3633 if (port_change_required) {
3634 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003635 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003636 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003637
3638 /* prepare message */
3639 chunk_appendf(msg, "port changed from '");
3640 if (s->flags & SRV_F_MAPPORTS)
3641 chunk_appendf(msg, "+");
3642 chunk_appendf(msg, "%d' to '", current_port);
3643
3644 if (sign == '-') {
3645 s->flags |= SRV_F_MAPPORTS;
3646 chunk_appendf(msg, "%c", sign);
3647 /* just use for result output */
3648 new_port = -new_port;
3649 }
3650 else if (sign == '+') {
3651 s->flags |= SRV_F_MAPPORTS;
3652 chunk_appendf(msg, "%c", sign);
3653 }
3654 else {
3655 s->flags &= ~SRV_F_MAPPORTS;
3656 }
3657
3658 chunk_appendf(msg, "%d'", new_port);
3659
3660 /* we also need to update health checks port only if it uses server's realport */
3661 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3662 s->check.port = new_port;
3663 }
3664 }
3665 else {
3666 chunk_appendf(msg, "no need to change the port");
3667 }
3668 }
3669
3670out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003671 if (changed)
3672 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003673 if (updater)
3674 chunk_appendf(msg, " by '%s'", updater);
3675 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003676 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003677}
3678
3679
3680/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681 * update server status based on result of name resolution
3682 * returns:
3683 * 0 if server status is updated
3684 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003685 *
3686 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003687 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003688int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003689{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003690 struct dns_resolvers *resolvers = s->resolvers;
3691 struct dns_resolution *resolution = s->dns_requester->resolution;
3692 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003693
3694 switch (resolution->status) {
3695 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003696 /* status when HAProxy has just (re)started.
3697 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003698 break;
3699
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003700 case RSLV_STATUS_VALID:
3701 /*
3702 * resume health checks
3703 * server will be turned back on if health check is safe
3704 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003705 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003706 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003707 return 1;
3708 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3709 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003710 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003711 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003712
Emeric Brun52a91d32017-08-31 14:41:55 +02003713 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003714 return 1;
3715 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3716 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3717 s->proxy->id, s->id);
3718
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003719 ha_warning("%s.\n", trash.area);
3720 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003721 return 0;
3722
3723 case RSLV_STATUS_NX:
3724 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003725 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3726 if (!tick_is_expired(exp, now_ms))
3727 break;
3728
3729 if (s->next_admin & SRV_ADMF_RMAINT)
3730 return 1;
3731 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3732 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003733
3734 case RSLV_STATUS_TIMEOUT:
3735 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003736 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3737 if (!tick_is_expired(exp, now_ms))
3738 break;
3739
3740 if (s->next_admin & SRV_ADMF_RMAINT)
3741 return 1;
3742 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3743 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003744
3745 case RSLV_STATUS_REFUSED:
3746 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003747 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3748 if (!tick_is_expired(exp, now_ms))
3749 break;
3750
3751 if (s->next_admin & SRV_ADMF_RMAINT)
3752 return 1;
3753 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3754 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003755
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003756 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003757 /* stop server if resolution failed for a long enough period */
3758 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3759 if (!tick_is_expired(exp, now_ms))
3760 break;
3761
3762 if (s->next_admin & SRV_ADMF_RMAINT)
3763 return 1;
3764 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3765 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003766 }
3767
3768 return 1;
3769}
3770
3771/*
3772 * Server Name Resolution valid response callback
3773 * It expects:
3774 * - <nameserver>: the name server which answered the valid response
3775 * - <response>: buffer containing a valid DNS response
3776 * - <response_len>: size of <response>
3777 * It performs the following actions:
3778 * - ignore response if current ip found and server family not met
3779 * - update with first new ip found if family is met and current IP is not found
3780 * returns:
3781 * 0 on error
3782 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003783 *
3784 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003785 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003786int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003788 struct server *s = NULL;
3789 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003790 void *serverip, *firstip;
3791 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003792 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003793 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003794 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003795
Christopher Faulet67957bd2017-09-27 11:00:59 +02003796 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003797 if (!s)
3798 return 1;
3799
Christopher Faulet67957bd2017-09-27 11:00:59 +02003800 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003801
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003802 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003803 firstip = NULL; /* pointer to the first valid response found */
3804 /* it will be used as the new IP if a change is required */
3805 firstip_sin_family = AF_UNSPEC;
3806 serverip = NULL; /* current server IP address */
3807
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808 /* initializing server IP pointer */
3809 server_sin_family = s->addr.ss_family;
3810 switch (server_sin_family) {
3811 case AF_INET:
3812 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3813 break;
3814
3815 case AF_INET6:
3816 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3817 break;
3818
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003819 case AF_UNSPEC:
3820 break;
3821
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003822 default:
3823 goto invalid;
3824 }
3825
Baptiste Assmann729c9012017-05-22 15:13:10 +02003826 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003827 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003828 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829
3830 switch (ret) {
3831 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003832 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003833
3834 case DNS_UPD_SRVIP_NOT_FOUND:
3835 goto save_ip;
3836
3837 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003838 goto invalid;
3839
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003840 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003841 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003842 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003843
Baptiste Assmannfad03182015-10-28 02:03:32 +01003844 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003845 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003846 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003847 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003848
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003849 default:
3850 goto invalid;
3851
3852 }
3853
3854 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003855 if (nameserver) {
3856 nameserver->counters.update++;
3857 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003858 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003859 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003860 else
3861 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003862 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003863
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003864 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003865 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003866 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003867
3868 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003869 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003870 nameserver->counters.invalid++;
3871 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003872 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003873 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003874 return 0;
3875}
3876
3877/*
3878 * Server Name Resolution error management callback
3879 * returns:
3880 * 0 on error
3881 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003882 *
3883 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003884 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003886{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003887 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003888
Christopher Faulet67957bd2017-09-27 11:00:59 +02003889 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003890 if (!s)
3891 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003892 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003893 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003894 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003895 return 1;
3896}
3897
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003898/*
3899 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003900 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003901 * It returns a pointer to the first server found or NULL if <ip> is not yet
3902 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003903 *
3904 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003905 */
3906struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3907{
3908 struct server *tmpsrv;
3909 struct proxy *be;
3910
3911 if (!srv)
3912 return NULL;
3913
3914 be = srv->proxy;
3915 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003916 /* we found the current server is the same, ignore it */
3917 if (srv == tmpsrv)
3918 continue;
3919
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003920 /* We want to compare the IP in the record with the IP of the servers in the
3921 * same backend, only if:
3922 * * DNS resolution is enabled on the server
3923 * * the hostname used for the resolution by our server is the same than the
3924 * one used for the server found in the backend
3925 * * the server found in the backend is not our current server
3926 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003927 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003928 if ((tmpsrv->hostname_dn == NULL) ||
3929 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3930 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003931 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003932 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003933 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003934 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003935
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003936 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003937 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003938 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003939 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003940 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003941
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003942 /* At this point, we have 2 different servers using the same DNS hostname
3943 * for their respective resolution.
3944 */
3945 if (*ip_family == tmpsrv->addr.ss_family &&
3946 ((tmpsrv->addr.ss_family == AF_INET &&
3947 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3948 (tmpsrv->addr.ss_family == AF_INET6 &&
3949 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003950 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003951 return tmpsrv;
3952 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003953 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003954 }
3955
Emeric Brune9fd6b52017-11-02 17:20:39 +01003956
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003957 return NULL;
3958}
3959
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003960/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3961 * resolver. This is suited for initial address configuration. Returns 0 on
3962 * success otherwise a non-zero error code. In case of error, *err_code, if
3963 * not NULL, is filled up.
3964 */
3965int srv_set_addr_via_libc(struct server *srv, int *err_code)
3966{
3967 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003968 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003969 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003970 return 1;
3971 }
3972 return 0;
3973}
3974
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003975/* Set the server's FDQN (->hostname) from <hostname>.
3976 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003977 *
3978 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003979 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003980int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003981{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003982 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003983 char *hostname_dn;
3984 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003985
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02003986 /* Note that the server lock is already held. */
3987 if (!srv->resolvers)
3988 return -1;
3989
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003990 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003991 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003992 /* run time DNS resolution was not active for this server
3993 * and we can't enable it at run time for now.
3994 */
3995 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003996 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003997
3998 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003999 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004000 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004001 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4002 hostname_dn, trash.size);
4003 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004004 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004005
Christopher Faulet67957bd2017-09-27 11:00:59 +02004006 resolution = srv->dns_requester->resolution;
4007 if (resolution &&
4008 resolution->hostname_dn &&
4009 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004010 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004011
Christopher Faulet67957bd2017-09-27 11:00:59 +02004012 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004013
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004014 free(srv->hostname);
4015 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004016 srv->hostname = strdup(hostname);
4017 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018 srv->hostname_dn_len = hostname_dn_len;
4019 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004020 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004021
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004022 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004023 goto err;
4024
4025 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004026 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004027 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004028 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004029
4030 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004031 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004032 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004033 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004034}
4035
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004036/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4037 * from the state file. This is suited for initial address configuration.
4038 * Returns 0 on success otherwise a non-zero error code. In case of error,
4039 * *err_code, if not NULL, is filled up.
4040 */
4041static int srv_apply_lastaddr(struct server *srv, int *err_code)
4042{
4043 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4044 if (err_code)
4045 *err_code |= ERR_WARN;
4046 return 1;
4047 }
4048 return 0;
4049}
4050
Willy Tarreau25e51522016-11-04 15:10:17 +01004051/* returns 0 if no error, otherwise a combination of ERR_* flags */
4052static int srv_iterate_initaddr(struct server *srv)
4053{
4054 int return_code = 0;
4055 int err_code;
4056 unsigned int methods;
4057
4058 methods = srv->init_addr_methods;
4059 if (!methods) { // default to "last,libc"
4060 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4061 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4062 }
4063
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004064 /* "-dr" : always append "none" so that server addresses resolution
4065 * failures are silently ignored, this is convenient to validate some
4066 * configs out of their environment.
4067 */
4068 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4069 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4070
Willy Tarreau25e51522016-11-04 15:10:17 +01004071 while (methods) {
4072 err_code = 0;
4073 switch (srv_get_next_initaddr(&methods)) {
4074 case SRV_IADDR_LAST:
4075 if (!srv->lastaddr)
4076 continue;
4077 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004078 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004079 return_code |= err_code;
4080 break;
4081
4082 case SRV_IADDR_LIBC:
4083 if (!srv->hostname)
4084 continue;
4085 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004086 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004087 return_code |= err_code;
4088 break;
4089
Willy Tarreau37ebe122016-11-04 15:17:58 +01004090 case SRV_IADDR_NONE:
4091 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004092 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004093 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4094 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004095 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004096 return return_code;
4097
Willy Tarreau4310d362016-11-02 15:05:56 +01004098 case SRV_IADDR_IP:
4099 ipcpy(&srv->init_addr, &srv->addr);
4100 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004101 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4102 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004103 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004104 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004105
Willy Tarreau25e51522016-11-04 15:10:17 +01004106 default: /* unhandled method */
4107 break;
4108 }
4109 }
4110
4111 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004112 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004113 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4114 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004115 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004116 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004117 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4118 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004119
4120 return_code |= ERR_ALERT | ERR_FATAL;
4121 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004122out:
4123 srv_set_dyncookie(srv);
4124 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004125}
4126
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004127/*
4128 * This function parses all backends and all servers within each backend
4129 * and performs servers' addr resolution based on information provided by:
4130 * - configuration file
4131 * - server-state file (states provided by an 'old' haproxy process)
4132 *
4133 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4134 */
4135int srv_init_addr(void)
4136{
4137 struct proxy *curproxy;
4138 int return_code = 0;
4139
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004140 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004141 while (curproxy) {
4142 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004143
4144 /* servers are in backend only */
4145 if (!(curproxy->cap & PR_CAP_BE))
4146 goto srv_init_addr_next;
4147
Willy Tarreau25e51522016-11-04 15:10:17 +01004148 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004149 if (srv->hostname)
4150 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004151
4152 srv_init_addr_next:
4153 curproxy = curproxy->next;
4154 }
4155
4156 return return_code;
4157}
4158
Willy Tarreau46b7f532018-08-21 11:54:26 +02004159/*
4160 * Must be called with the server lock held.
4161 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004162const 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 +02004163{
4164
Willy Tarreau83061a82018-07-13 11:56:34 +02004165 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004166
4167 msg = get_trash_chunk();
4168 chunk_reset(msg);
4169
Olivier Houchard8da5f982017-08-04 18:35:36 +02004170 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004171 chunk_appendf(msg, "no need to change the FDQN");
4172 goto out;
4173 }
4174
4175 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4176 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4177 goto out;
4178 }
4179
4180 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4181 server->proxy->id, server->id, server->hostname, fqdn);
4182
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004183 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004184 chunk_reset(msg);
4185 chunk_appendf(msg, "could not update %s/%s FQDN",
4186 server->proxy->id, server->id);
4187 goto out;
4188 }
4189
4190 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004191 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004192
4193 out:
4194 if (updater)
4195 chunk_appendf(msg, " by '%s'", updater);
4196 chunk_appendf(msg, "\n");
4197
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004198 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004199}
4200
4201
Willy Tarreau21b069d2016-11-23 17:15:08 +01004202/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4203 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004204 * 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 +01004205 * used for CLI commands requiring a server name.
4206 * Important: the <arg> is modified to remove the '/'.
4207 */
4208struct server *cli_find_server(struct appctx *appctx, char *arg)
4209{
4210 struct proxy *px;
4211 struct server *sv;
4212 char *line;
4213
4214 /* split "backend/server" and make <line> point to server */
4215 for (line = arg; *line; line++)
4216 if (*line == '/') {
4217 *line++ = '\0';
4218 break;
4219 }
4220
4221 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004222 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004223 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004224 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004225 return NULL;
4226 }
4227
4228 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004229 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004230 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004231 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004232 return NULL;
4233 }
4234
4235 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004236 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004237 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004238 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004239 return NULL;
4240 }
4241
4242 return sv;
4243}
4244
William Lallemand222baf22016-11-19 02:00:33 +01004245
Willy Tarreau46b7f532018-08-21 11:54:26 +02004246/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004247static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004248{
4249 struct server *sv;
4250 const char *warning;
4251
4252 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4253 return 1;
4254
4255 sv = cli_find_server(appctx, args[2]);
4256 if (!sv)
4257 return 1;
4258
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004259 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004260
William Lallemand222baf22016-11-19 02:00:33 +01004261 if (strcmp(args[3], "weight") == 0) {
4262 warning = server_parse_weight_change_request(sv, args[4]);
4263 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004264 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004265 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004266 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004267 }
4268 }
4269 else if (strcmp(args[3], "state") == 0) {
4270 if (strcmp(args[4], "ready") == 0)
4271 srv_adm_set_ready(sv);
4272 else if (strcmp(args[4], "drain") == 0)
4273 srv_adm_set_drain(sv);
4274 else if (strcmp(args[4], "maint") == 0)
4275 srv_adm_set_maint(sv);
4276 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004277 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004278 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004279 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004280 }
4281 }
4282 else if (strcmp(args[3], "health") == 0) {
4283 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004284 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004285 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004286 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004287 }
4288 else if (strcmp(args[4], "up") == 0) {
4289 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004290 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004291 }
4292 else if (strcmp(args[4], "stopping") == 0) {
4293 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004294 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004295 }
4296 else if (strcmp(args[4], "down") == 0) {
4297 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004298 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004299 }
4300 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004301 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004302 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004303 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004304 }
4305 }
4306 else if (strcmp(args[3], "agent") == 0) {
4307 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004308 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004309 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004310 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004311 }
4312 else if (strcmp(args[4], "up") == 0) {
4313 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004314 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004315 }
4316 else if (strcmp(args[4], "down") == 0) {
4317 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004318 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004319 }
4320 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004321 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004322 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004323 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004324 }
4325 }
Misiek2da082d2017-01-09 09:40:42 +01004326 else if (strcmp(args[3], "agent-addr") == 0) {
4327 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004328 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004329 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4330 appctx->st0 = CLI_ST_PRINT;
4331 } else {
4332 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004333 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004334 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4335 appctx->st0 = CLI_ST_PRINT;
4336 }
4337 }
4338 }
4339 else if (strcmp(args[3], "agent-send") == 0) {
4340 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004341 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004342 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4343 appctx->st0 = CLI_ST_PRINT;
4344 } else {
4345 char *nss = strdup(args[4]);
4346 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004347 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004348 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4349 appctx->st0 = CLI_ST_PRINT;
4350 } else {
4351 free(sv->agent.send_string);
4352 sv->agent.send_string = nss;
4353 sv->agent.send_string_len = strlen(args[4]);
4354 }
4355 }
4356 }
William Lallemand222baf22016-11-19 02:00:33 +01004357 else if (strcmp(args[3], "check-port") == 0) {
4358 int i = 0;
4359 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004360 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004361 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004362 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004363 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004364 }
4365 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004366 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004367 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004368 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004369 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004370 }
4371 /* prevent the update of port to 0 if MAPPORTS are in use */
4372 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004373 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004374 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004375 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004376 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004377 }
4378 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004379 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004380 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004381 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004382 }
4383 else if (strcmp(args[3], "addr") == 0) {
4384 char *addr = NULL;
4385 char *port = NULL;
4386 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004387 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004388 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004389 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004390 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004391 }
4392 else {
4393 addr = args[4];
4394 }
4395 if (strcmp(args[5], "port") == 0) {
4396 port = args[6];
4397 }
4398 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4399 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004400 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004401 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004402 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004403 }
4404 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4405 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004406 else if (strcmp(args[3], "fqdn") == 0) {
4407 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004408 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004409 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4410 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004411 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004412 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004413 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004414 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004415 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004416 appctx->ctx.cli.msg = warning;
4417 appctx->st0 = CLI_ST_PRINT;
4418 }
4419 }
William Lallemand222baf22016-11-19 02:00:33 +01004420 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004421 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004422 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 +01004423 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004424 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004425 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004426 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004427 return 1;
4428}
4429
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004430static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004431{
4432 struct stream_interface *si = appctx->owner;
4433 struct proxy *px;
4434 struct server *sv;
4435 char *line;
4436
4437
4438 /* split "backend/server" and make <line> point to server */
4439 for (line = args[2]; *line; line++)
4440 if (*line == '/') {
4441 *line++ = '\0';
4442 break;
4443 }
4444
4445 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004446 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004447 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004448 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004449 return 1;
4450 }
4451
4452 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004453 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004454 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004455 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004456 return 1;
4457 }
4458
4459 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004460 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4461 sv->iweight);
4462 if (ci_putstr(si_ic(si), trash.area) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004463 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004464 return 0;
4465 }
William Lallemand6b160942016-11-22 12:34:35 +01004466 return 1;
4467}
4468
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004469/* Parse a "set weight" command.
4470 *
4471 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004472 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004473static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004474{
4475 struct server *sv;
4476 const char *warning;
4477
4478 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4479 return 1;
4480
4481 sv = cli_find_server(appctx, args[2]);
4482 if (!sv)
4483 return 1;
4484
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004485 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4486
William Lallemand6b160942016-11-22 12:34:35 +01004487 warning = server_parse_weight_change_request(sv, args[3]);
4488 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004489 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004490 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004491 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004492 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004493
4494 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4495
William Lallemand6b160942016-11-22 12:34:35 +01004496 return 1;
4497}
4498
Willy Tarreau46b7f532018-08-21 11:54:26 +02004499/* parse a "set maxconn server" command. It always returns 1.
4500 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004501 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004502 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004503static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004504{
4505 struct server *sv;
4506 const char *warning;
4507
4508 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4509 return 1;
4510
4511 sv = cli_find_server(appctx, args[3]);
4512 if (!sv)
4513 return 1;
4514
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004515 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4516
Willy Tarreaub8026272016-11-23 11:26:56 +01004517 warning = server_parse_maxconn_change_request(sv, args[4]);
4518 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004519 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004520 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004521 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004522 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004523
4524 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4525
Willy Tarreaub8026272016-11-23 11:26:56 +01004526 return 1;
4527}
William Lallemand6b160942016-11-22 12:34:35 +01004528
Willy Tarreau46b7f532018-08-21 11:54:26 +02004529/* parse a "disable agent" command. It always returns 1.
4530 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004531 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004532 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004533static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004534{
4535 struct server *sv;
4536
4537 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4538 return 1;
4539
4540 sv = cli_find_server(appctx, args[2]);
4541 if (!sv)
4542 return 1;
4543
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004544 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004545 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004546 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004547 return 1;
4548}
4549
Willy Tarreau46b7f532018-08-21 11:54:26 +02004550/* parse a "disable health" command. It always returns 1.
4551 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004552 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004553 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004554static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004555{
4556 struct server *sv;
4557
4558 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4559 return 1;
4560
4561 sv = cli_find_server(appctx, args[2]);
4562 if (!sv)
4563 return 1;
4564
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004565 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004566 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004567 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004568 return 1;
4569}
4570
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571/* parse a "disable server" command. It always returns 1.
4572 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004573 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004574 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004575static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004576{
4577 struct server *sv;
4578
4579 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4580 return 1;
4581
4582 sv = cli_find_server(appctx, args[2]);
4583 if (!sv)
4584 return 1;
4585
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004586 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004587 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004588 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004589 return 1;
4590}
4591
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592/* parse a "enable agent" command. It always returns 1.
4593 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004594 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004595 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004596static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004597{
4598 struct server *sv;
4599
4600 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4601 return 1;
4602
4603 sv = cli_find_server(appctx, args[2]);
4604 if (!sv)
4605 return 1;
4606
4607 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004608 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004609 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004610 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004611 return 1;
4612 }
4613
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004614 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004615 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004616 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004617 return 1;
4618}
4619
Willy Tarreau46b7f532018-08-21 11:54:26 +02004620/* parse a "enable health" command. It always returns 1.
4621 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004622 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004623 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004624static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004625{
4626 struct server *sv;
4627
4628 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4629 return 1;
4630
4631 sv = cli_find_server(appctx, args[2]);
4632 if (!sv)
4633 return 1;
4634
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004635 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004636 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004637 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004638 return 1;
4639}
4640
Willy Tarreau46b7f532018-08-21 11:54:26 +02004641/* parse a "enable server" command. It always returns 1.
4642 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004643 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004644 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004645static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004646{
4647 struct server *sv;
4648
4649 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4650 return 1;
4651
4652 sv = cli_find_server(appctx, args[2]);
4653 if (!sv)
4654 return 1;
4655
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004656 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004657 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004658 if (!(sv->flags & SRV_F_COOKIESET)
4659 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4660 sv->cookie)
4661 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004662 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004663 return 1;
4664}
4665
William Lallemand222baf22016-11-19 02:00:33 +01004666/* register cli keywords */
4667static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004668 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004669 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004670 { { "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 +01004671 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004672 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004673 { { "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 +01004674 { { "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 +01004675 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004676 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4677 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4678
William Lallemand222baf22016-11-19 02:00:33 +01004679 {{},}
4680}};
4681
4682__attribute__((constructor))
4683static void __server_init(void)
4684{
4685 cli_register_kw(&cli_kws);
4686}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004687
Emeric Brun64cc49c2017-10-03 14:46:45 +02004688/*
4689 * This function applies server's status changes, it is
4690 * is designed to be called asynchronously.
4691 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004692 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004693 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004694static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004695{
4696 struct check *check = &s->check;
4697 int xferred;
4698 struct proxy *px = s->proxy;
4699 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4700 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4701 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004702 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004703
Emeric Brun64cc49c2017-10-03 14:46:45 +02004704 /* If currently main is not set we try to apply pending state changes */
4705 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4706 int next_admin;
4707
4708 /* Backup next admin */
4709 next_admin = s->next_admin;
4710
4711 /* restore current admin state */
4712 s->next_admin = s->cur_admin;
4713
4714 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4715 s->last_change = now.tv_sec;
4716 if (s->proxy->lbprm.set_server_status_down)
4717 s->proxy->lbprm.set_server_status_down(s);
4718
4719 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4720 srv_shutdown_streams(s, SF_ERR_DOWN);
4721
4722 /* we might have streams queued on this server and waiting for
4723 * a connection. Those which are redispatchable will be queued
4724 * to another server or to the proxy itself.
4725 */
4726 xferred = pendconn_redistribute(s);
4727
4728 tmptrash = alloc_trash_chunk();
4729 if (tmptrash) {
4730 chunk_printf(tmptrash,
4731 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4732 s->proxy->id, s->id);
4733
Emeric Brun5a133512017-10-19 14:42:30 +02004734 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004735 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004736
4737 /* we don't send an alert if the server was previously paused */
4738 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004739 send_log(s->proxy, log_level, "%s.\n",
4740 tmptrash->area);
4741 send_email_alert(s, log_level, "%s",
4742 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004743 free_trash_chunk(tmptrash);
4744 tmptrash = NULL;
4745 }
4746 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4747 set_backend_down(s->proxy);
4748
4749 s->counters.down_trans++;
4750 }
4751 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4752 s->last_change = now.tv_sec;
4753 if (s->proxy->lbprm.set_server_status_down)
4754 s->proxy->lbprm.set_server_status_down(s);
4755
4756 /* we might have streams queued on this server and waiting for
4757 * a connection. Those which are redispatchable will be queued
4758 * to another server or to the proxy itself.
4759 */
4760 xferred = pendconn_redistribute(s);
4761
4762 tmptrash = alloc_trash_chunk();
4763 if (tmptrash) {
4764 chunk_printf(tmptrash,
4765 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4766 s->proxy->id, s->id);
4767
Emeric Brun5a133512017-10-19 14:42:30 +02004768 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004769
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004770 ha_warning("%s.\n", tmptrash->area);
4771 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4772 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004773 free_trash_chunk(tmptrash);
4774 tmptrash = NULL;
4775 }
4776
4777 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4778 set_backend_down(s->proxy);
4779 }
4780 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4781 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4782 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4783 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4784 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4785 s->proxy->last_change = now.tv_sec;
4786 }
4787
4788 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4789 s->down_time += now.tv_sec - s->last_change;
4790
4791 s->last_change = now.tv_sec;
4792 if (s->next_state == SRV_ST_STARTING)
4793 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4794
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004795 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004796 /* now propagate the status change to any LB algorithms */
4797 if (px->lbprm.update_server_eweight)
4798 px->lbprm.update_server_eweight(s);
4799 else if (srv_willbe_usable(s)) {
4800 if (px->lbprm.set_server_status_up)
4801 px->lbprm.set_server_status_up(s);
4802 }
4803 else {
4804 if (px->lbprm.set_server_status_down)
4805 px->lbprm.set_server_status_down(s);
4806 }
4807
4808 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4809 * and it's not a backup server and its effective weight is > 0,
4810 * then it can accept new connections, so we shut down all streams
4811 * on all backup servers.
4812 */
4813 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4814 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4815 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4816
4817 /* check if we can handle some connections queued at the proxy. We
4818 * will take as many as we can handle.
4819 */
4820 xferred = pendconn_grab_from_px(s);
4821
4822 tmptrash = alloc_trash_chunk();
4823 if (tmptrash) {
4824 chunk_printf(tmptrash,
4825 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4826 s->proxy->id, s->id);
4827
Emeric Brun5a133512017-10-19 14:42:30 +02004828 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004829 ha_warning("%s.\n", tmptrash->area);
4830 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4831 tmptrash->area);
4832 send_email_alert(s, LOG_NOTICE, "%s",
4833 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004834 free_trash_chunk(tmptrash);
4835 tmptrash = NULL;
4836 }
4837
4838 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4839 set_backend_down(s->proxy);
4840 }
4841 else if (s->cur_eweight != s->next_eweight) {
4842 /* now propagate the status change to any LB algorithms */
4843 if (px->lbprm.update_server_eweight)
4844 px->lbprm.update_server_eweight(s);
4845 else if (srv_willbe_usable(s)) {
4846 if (px->lbprm.set_server_status_up)
4847 px->lbprm.set_server_status_up(s);
4848 }
4849 else {
4850 if (px->lbprm.set_server_status_down)
4851 px->lbprm.set_server_status_down(s);
4852 }
4853
4854 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4855 set_backend_down(s->proxy);
4856 }
4857
4858 s->next_admin = next_admin;
4859 }
4860
Emeric Brun5a133512017-10-19 14:42:30 +02004861 /* reset operational state change */
4862 *s->op_st_chg.reason = 0;
4863 s->op_st_chg.status = s->op_st_chg.code = -1;
4864 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004865
4866 /* Now we try to apply pending admin changes */
4867
4868 /* Maintenance must also disable health checks */
4869 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4870 if (s->check.state & CHK_ST_ENABLED) {
4871 s->check.state |= CHK_ST_PAUSED;
4872 check->health = 0;
4873 }
4874
4875 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004876 tmptrash = alloc_trash_chunk();
4877 if (tmptrash) {
4878 chunk_printf(tmptrash,
4879 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4880 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4881 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004882
Olivier Houchard796a2b32017-10-24 17:42:47 +02004883 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004884
Olivier Houchard796a2b32017-10-24 17:42:47 +02004885 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004886 ha_warning("%s.\n", tmptrash->area);
4887 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4888 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004889 }
4890 free_trash_chunk(tmptrash);
4891 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004892 }
Emeric Brun8f298292017-12-06 16:47:17 +01004893 /* commit new admin status */
4894
4895 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004896 }
4897 else { /* server was still running */
4898 check->health = 0; /* failure */
4899 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004900
4901 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004902 if (s->proxy->lbprm.set_server_status_down)
4903 s->proxy->lbprm.set_server_status_down(s);
4904
Emeric Brun64cc49c2017-10-03 14:46:45 +02004905 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4906 srv_shutdown_streams(s, SF_ERR_DOWN);
4907
4908 /* we might have streams queued on this server and waiting for
4909 * a connection. Those which are redispatchable will be queued
4910 * to another server or to the proxy itself.
4911 */
4912 xferred = pendconn_redistribute(s);
4913
4914 tmptrash = alloc_trash_chunk();
4915 if (tmptrash) {
4916 chunk_printf(tmptrash,
4917 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4918 s->flags & SRV_F_BACKUP ? "Backup " : "",
4919 s->proxy->id, s->id,
4920 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4921
4922 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4923
4924 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004925 ha_warning("%s.\n", tmptrash->area);
4926 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4927 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004928 }
4929 free_trash_chunk(tmptrash);
4930 tmptrash = NULL;
4931 }
4932 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4933 set_backend_down(s->proxy);
4934
4935 s->counters.down_trans++;
4936 }
4937 }
4938 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4939 /* OK here we're leaving maintenance, we have many things to check,
4940 * because the server might possibly be coming back up depending on
4941 * its state. In practice, leaving maintenance means that we should
4942 * immediately turn to UP (more or less the slowstart) under the
4943 * following conditions :
4944 * - server is neither checked nor tracked
4945 * - server tracks another server which is not checked
4946 * - server tracks another server which is already up
4947 * Which sums up as something simpler :
4948 * "either the tracking server is up or the server's checks are disabled
4949 * or up". Otherwise we only re-enable health checks. There's a special
4950 * case associated to the stopping state which can be inherited. Note
4951 * that the server might still be in drain mode, which is naturally dealt
4952 * with by the lower level functions.
4953 */
4954
4955 if (s->check.state & CHK_ST_ENABLED) {
4956 s->check.state &= ~CHK_ST_PAUSED;
4957 check->health = check->rise; /* start OK but check immediately */
4958 }
4959
4960 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4961 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4962 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4963 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4964 s->next_state = SRV_ST_STOPPING;
4965 }
4966 else {
4967 s->next_state = SRV_ST_STARTING;
4968 if (s->slowstart > 0)
4969 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4970 else
4971 s->next_state = SRV_ST_RUNNING;
4972 }
4973
4974 }
4975
4976 tmptrash = alloc_trash_chunk();
4977 if (tmptrash) {
4978 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4979 chunk_printf(tmptrash,
4980 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4981 s->flags & SRV_F_BACKUP ? "Backup " : "",
4982 s->proxy->id, s->id,
4983 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4984 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4985 }
4986 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4987 chunk_printf(tmptrash,
4988 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4989 s->flags & SRV_F_BACKUP ? "Backup " : "",
4990 s->proxy->id, s->id, s->hostname,
4991 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4992 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4993 }
4994 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4995 chunk_printf(tmptrash,
4996 "%sServer %s/%s is %s/%s (leaving maintenance)",
4997 s->flags & SRV_F_BACKUP ? "Backup " : "",
4998 s->proxy->id, s->id,
4999 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5000 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5001 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005002 ha_warning("%s.\n", tmptrash->area);
5003 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5004 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005005 free_trash_chunk(tmptrash);
5006 tmptrash = NULL;
5007 }
5008
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005009 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005010 /* now propagate the status change to any LB algorithms */
5011 if (px->lbprm.update_server_eweight)
5012 px->lbprm.update_server_eweight(s);
5013 else if (srv_willbe_usable(s)) {
5014 if (px->lbprm.set_server_status_up)
5015 px->lbprm.set_server_status_up(s);
5016 }
5017 else {
5018 if (px->lbprm.set_server_status_down)
5019 px->lbprm.set_server_status_down(s);
5020 }
5021
5022 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5023 set_backend_down(s->proxy);
5024
Willy Tarreau6a78e612018-08-07 10:14:53 +02005025 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5026 * and it's not a backup server and its effective weight is > 0,
5027 * then it can accept new connections, so we shut down all streams
5028 * on all backup servers.
5029 */
5030 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5031 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5032 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5033
5034 /* check if we can handle some connections queued at the proxy. We
5035 * will take as many as we can handle.
5036 */
5037 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005038 }
5039 else if (s->next_admin & SRV_ADMF_MAINT) {
5040 /* remaining in maintenance mode, let's inform precisely about the
5041 * situation.
5042 */
5043 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5044 tmptrash = alloc_trash_chunk();
5045 if (tmptrash) {
5046 chunk_printf(tmptrash,
5047 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5048 s->flags & SRV_F_BACKUP ? "Backup " : "",
5049 s->proxy->id, s->id);
5050
5051 if (s->track) /* normally it's mandatory here */
5052 chunk_appendf(tmptrash, " via %s/%s",
5053 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005054 ha_warning("%s.\n", tmptrash->area);
5055 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5056 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005057 free_trash_chunk(tmptrash);
5058 tmptrash = NULL;
5059 }
5060 }
5061 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5062 tmptrash = alloc_trash_chunk();
5063 if (tmptrash) {
5064 chunk_printf(tmptrash,
5065 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5066 s->flags & SRV_F_BACKUP ? "Backup " : "",
5067 s->proxy->id, s->id, s->hostname);
5068
5069 if (s->track) /* normally it's mandatory here */
5070 chunk_appendf(tmptrash, " via %s/%s",
5071 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005072 ha_warning("%s.\n", tmptrash->area);
5073 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5074 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005075 free_trash_chunk(tmptrash);
5076 tmptrash = NULL;
5077 }
5078 }
5079 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5080 tmptrash = alloc_trash_chunk();
5081 if (tmptrash) {
5082 chunk_printf(tmptrash,
5083 "%sServer %s/%s remains in forced maintenance",
5084 s->flags & SRV_F_BACKUP ? "Backup " : "",
5085 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005086 ha_warning("%s.\n", tmptrash->area);
5087 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5088 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005089 free_trash_chunk(tmptrash);
5090 tmptrash = NULL;
5091 }
5092 }
5093 /* don't report anything when leaving drain mode and remaining in maintenance */
5094
5095 s->cur_admin = s->next_admin;
5096 }
5097
5098 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5099 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5100 /* drain state is applied only if not yet in maint */
5101
5102 s->last_change = now.tv_sec;
5103 if (px->lbprm.set_server_status_down)
5104 px->lbprm.set_server_status_down(s);
5105
5106 /* we might have streams queued on this server and waiting for
5107 * a connection. Those which are redispatchable will be queued
5108 * to another server or to the proxy itself.
5109 */
5110 xferred = pendconn_redistribute(s);
5111
5112 tmptrash = alloc_trash_chunk();
5113 if (tmptrash) {
5114 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5115 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5116 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5117
5118 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5119
5120 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005121 ha_warning("%s.\n", tmptrash->area);
5122 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5123 tmptrash->area);
5124 send_email_alert(s, LOG_NOTICE, "%s",
5125 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005126 }
5127 free_trash_chunk(tmptrash);
5128 tmptrash = NULL;
5129 }
5130
5131 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5132 set_backend_down(s->proxy);
5133 }
5134 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5135 /* OK completely leaving drain mode */
5136 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5137 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5138 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5139 s->proxy->last_change = now.tv_sec;
5140 }
5141
5142 if (s->last_change < now.tv_sec) // ignore negative times
5143 s->down_time += now.tv_sec - s->last_change;
5144 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005145 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005146
5147 tmptrash = alloc_trash_chunk();
5148 if (tmptrash) {
5149 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5150 chunk_printf(tmptrash,
5151 "%sServer %s/%s is %s (leaving forced drain)",
5152 s->flags & SRV_F_BACKUP ? "Backup " : "",
5153 s->proxy->id, s->id,
5154 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5155 }
5156 else {
5157 chunk_printf(tmptrash,
5158 "%sServer %s/%s is %s (leaving drain)",
5159 s->flags & SRV_F_BACKUP ? "Backup " : "",
5160 s->proxy->id, s->id,
5161 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5162 if (s->track) /* normally it's mandatory here */
5163 chunk_appendf(tmptrash, " via %s/%s",
5164 s->track->proxy->id, s->track->id);
5165 }
5166
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005167 ha_warning("%s.\n", tmptrash->area);
5168 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5169 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005170 free_trash_chunk(tmptrash);
5171 tmptrash = NULL;
5172 }
5173
5174 /* now propagate the status change to any LB algorithms */
5175 if (px->lbprm.update_server_eweight)
5176 px->lbprm.update_server_eweight(s);
5177 else if (srv_willbe_usable(s)) {
5178 if (px->lbprm.set_server_status_up)
5179 px->lbprm.set_server_status_up(s);
5180 }
5181 else {
5182 if (px->lbprm.set_server_status_down)
5183 px->lbprm.set_server_status_down(s);
5184 }
5185 }
5186 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5187 /* remaining in drain mode after removing one of its flags */
5188
5189 tmptrash = alloc_trash_chunk();
5190 if (tmptrash) {
5191 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5192 chunk_printf(tmptrash,
5193 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5194 s->flags & SRV_F_BACKUP ? "Backup " : "",
5195 s->proxy->id, s->id);
5196
5197 if (s->track) /* normally it's mandatory here */
5198 chunk_appendf(tmptrash, " via %s/%s",
5199 s->track->proxy->id, s->track->id);
5200 }
5201 else {
5202 chunk_printf(tmptrash,
5203 "%sServer %s/%s remains in forced drain mode",
5204 s->flags & SRV_F_BACKUP ? "Backup " : "",
5205 s->proxy->id, s->id);
5206 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005207 ha_warning("%s.\n", tmptrash->area);
5208 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5209 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005210 free_trash_chunk(tmptrash);
5211 tmptrash = NULL;
5212 }
5213
5214 /* commit new admin status */
5215
5216 s->cur_admin = s->next_admin;
5217 }
5218 }
5219
5220 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005221 *s->adm_st_chg_cause = 0;
5222}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005223
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005224/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005225 * Local variables:
5226 * c-indent-level: 8
5227 * c-basic-offset: 8
5228 * End:
5229 */