blob: 0cf5ecaf56e89e52b79f06fb86ff007533a97124 [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);
Olivier Houchardc7566002018-11-20 23:33:50 +01001489
1490#ifdef TLSEXT_TYPE_application_layer_protocol_negotiation
1491 if (src->ssl_ctx.alpn_str) {
1492 srv->ssl_ctx.alpn_str = malloc(src->ssl_ctx.alpn_len);
1493 if (srv->ssl_ctx.alpn_str) {
1494 memcpy(srv->ssl_ctx.alpn_str, src->ssl_ctx.alpn_str,
1495 src->ssl_ctx.alpn_len);
1496 srv->ssl_ctx.alpn_len = src->ssl_ctx.alpn_len;
1497 }
1498 }
1499#endif
1500#ifdef OPENSSL_NPN_NEGOTIATED
1501 if (src->ssl_ctx.npn_str) {
1502 srv->ssl_ctx.npn_str = malloc(src->ssl_ctx.npn_len);
1503 if (srv->ssl_ctx.npn_str) {
1504 memcpy(srv->ssl_ctx.npn_str, src->ssl_ctx.npn_str,
1505 src->ssl_ctx.npn_len);
1506 srv->ssl_ctx.npn_len = src->ssl_ctx.npn_len;
1507 }
1508 }
1509#endif
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001510}
1511#endif
1512
1513/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001514 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001515 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001516 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001517 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001518static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001519{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520 char *hostname_dn;
1521 int hostname_len, hostname_dn_len;
1522
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001523 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001524 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001525
Christopher Faulet67957bd2017-09-27 11:00:59 +02001526 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001527 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001528 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1529 hostname_dn, trash.size);
1530 if (hostname_dn_len == -1)
1531 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001532
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001533
Christopher Faulet67957bd2017-09-27 11:00:59 +02001534 free(srv->hostname);
1535 free(srv->hostname_dn);
1536 srv->hostname = strdup(hostname);
1537 srv->hostname_dn = strdup(hostname_dn);
1538 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001539 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001540 goto err;
1541
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001542 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001543
1544 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001545 free(srv->hostname); srv->hostname = NULL;
1546 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001547 return -1;
1548}
1549
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001550/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001551 * Copy <src> server settings to <srv> server allocating
1552 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001553 * This function is not supposed to be called at any time, but only
1554 * during server settings parsing or during server allocations from
1555 * a server template, and just after having calloc()'ed a new server.
1556 * So, <src> may only be a default server (when parsing server settings)
1557 * or a server template (during server allocations from a server template).
1558 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1559 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001560 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001561static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001562{
1563 /* Connection source settings copy */
1564 srv_conn_src_cpy(srv, src);
1565
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001566 if (srv_tmpl) {
1567 srv->addr = src->addr;
1568 srv->svc_port = src->svc_port;
1569 }
1570
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001571 srv->pp_opts = src->pp_opts;
1572 if (src->rdr_pfx != NULL) {
1573 srv->rdr_pfx = strdup(src->rdr_pfx);
1574 srv->rdr_len = src->rdr_len;
1575 }
1576 if (src->cookie != NULL) {
1577 srv->cookie = strdup(src->cookie);
1578 srv->cklen = src->cklen;
1579 }
1580 srv->use_ssl = src->use_ssl;
1581 srv->check.addr = srv->agent.addr = src->check.addr;
1582 srv->check.use_ssl = src->check.use_ssl;
1583 srv->check.port = src->check.port;
1584 /* Note: 'flags' field has potentially been already initialized. */
1585 srv->flags |= src->flags;
1586 srv->do_check = src->do_check;
1587 srv->do_agent = src->do_agent;
1588 if (srv->check.port)
1589 srv->flags |= SRV_F_CHECKPORT;
1590 srv->check.inter = src->check.inter;
1591 srv->check.fastinter = src->check.fastinter;
1592 srv->check.downinter = src->check.downinter;
1593 srv->agent.use_ssl = src->agent.use_ssl;
1594 srv->agent.port = src->agent.port;
1595 if (src->agent.send_string != NULL)
1596 srv->agent.send_string = strdup(src->agent.send_string);
1597 srv->agent.send_string_len = src->agent.send_string_len;
1598 srv->agent.inter = src->agent.inter;
1599 srv->agent.fastinter = src->agent.fastinter;
1600 srv->agent.downinter = src->agent.downinter;
1601 srv->maxqueue = src->maxqueue;
1602 srv->minconn = src->minconn;
1603 srv->maxconn = src->maxconn;
1604 srv->slowstart = src->slowstart;
1605 srv->observe = src->observe;
1606 srv->onerror = src->onerror;
1607 srv->onmarkeddown = src->onmarkeddown;
1608 srv->onmarkedup = src->onmarkedup;
1609 if (src->trackit != NULL)
1610 srv->trackit = strdup(src->trackit);
1611 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1612 srv->uweight = srv->iweight = src->iweight;
1613
1614 srv->check.send_proxy = src->check.send_proxy;
1615 /* health: up, but will fall down at first failure */
1616 srv->check.rise = srv->check.health = src->check.rise;
1617 srv->check.fall = src->check.fall;
1618
1619 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001620 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1621 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1622 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001623 srv->check.state |= CHK_ST_PAUSED;
1624 srv->check.health = 0;
1625 }
1626
1627 /* health: up but will fall down at first failure */
1628 srv->agent.rise = srv->agent.health = src->agent.rise;
1629 srv->agent.fall = src->agent.fall;
1630
1631 if (src->resolvers_id != NULL)
1632 srv->resolvers_id = strdup(src->resolvers_id);
1633 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001634 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001635 if (srv->dns_opts.family_prio == AF_UNSPEC)
1636 srv->dns_opts.family_prio = AF_INET6;
1637 memcpy(srv->dns_opts.pref_net,
1638 src->dns_opts.pref_net,
1639 sizeof srv->dns_opts.pref_net);
1640 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1641
1642 srv->init_addr_methods = src->init_addr_methods;
1643 srv->init_addr = src->init_addr;
1644#if defined(USE_OPENSSL)
1645 srv_ssl_settings_cpy(srv, src);
1646#endif
1647#ifdef TCP_USER_TIMEOUT
1648 srv->tcp_ut = src->tcp_ut;
1649#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001650 srv->mux_proto = src->mux_proto;
1651
Olivier Houchard8da5f982017-08-04 18:35:36 +02001652 if (srv_tmpl)
1653 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001654}
1655
William Lallemand313bfd12018-10-26 14:47:32 +02001656struct server *new_server(struct proxy *proxy)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001657{
1658 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001659 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001660
1661 srv = calloc(1, sizeof *srv);
1662 if (!srv)
1663 return NULL;
1664
1665 srv->obj_type = OBJ_TYPE_SERVER;
1666 srv->proxy = proxy;
1667 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001668 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001669
1670 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1671 goto free_srv;
1672 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1673 goto free_priv_conns;
1674 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1675 goto free_idle_conns;
1676
1677 for (i = 0; i < global.nbthread; i++) {
1678 LIST_INIT(&srv->priv_conns[i]);
1679 LIST_INIT(&srv->idle_conns[i]);
1680 LIST_INIT(&srv->safe_conns[i]);
1681 }
1682
Emeric Brun52a91d32017-08-31 14:41:55 +02001683 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001684 srv->last_change = now.tv_sec;
1685
1686 srv->check.status = HCHK_STATUS_INI;
1687 srv->check.server = srv;
1688 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1689
1690 srv->agent.status = HCHK_STATUS_INI;
1691 srv->agent.server = srv;
1692 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1693
1694 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001695
1696 free_idle_conns:
1697 free(srv->idle_conns);
1698 free_priv_conns:
1699 free(srv->priv_conns);
1700 free_srv:
1701 free(srv);
1702 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001703}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001704
1705/*
1706 * Validate <srv> server health-check settings.
1707 * Returns 0 if everything is OK, -1 if not.
1708 */
1709static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1710{
1711 struct tcpcheck_rule *r = NULL;
1712 struct list *l;
1713
1714 /*
1715 * We need at least a service port, a check port or the first tcp-check rule must
1716 * be a 'connect' one when checking an IPv4/IPv6 server.
1717 */
1718 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1719 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1720 return 0;
1721
1722 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1723 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001724 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1725 "Check has been disabled.\n",
1726 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001727 return -1;
1728 }
1729
1730 /* search the first action (connect / send / expect) in the list */
1731 l = &srv->proxy->tcpcheck_rules;
1732 list_for_each_entry(r, l, list) {
1733 if (r->action != TCPCHK_ACT_COMMENT)
1734 break;
1735 }
1736
1737 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001738 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1739 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1740 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001741 return -1;
1742 }
1743
1744 /* scan the tcp-check ruleset to ensure a port has been configured */
1745 l = &srv->proxy->tcpcheck_rules;
1746 list_for_each_entry(r, l, list) {
1747 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001748 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1749 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1750 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001751 return -1;
1752 }
1753 }
1754
1755 return 0;
1756}
1757
1758/*
1759 * Initialize <srv> health-check structure.
1760 * Returns the error string in case of memory allocation failure, NULL if not.
1761 */
1762static const char *do_health_check_init(struct server *srv, int check_type, int state)
1763{
1764 const char *ret;
1765
1766 if (!srv->do_check)
1767 return NULL;
1768
1769 ret = init_check(&srv->check, check_type);
1770 if (ret)
1771 return ret;
1772
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001773 srv->check.state |= state;
1774 global.maxsock++;
1775
1776 return NULL;
1777}
1778
1779static int server_health_check_init(const char *file, int linenum,
1780 struct server *srv, struct proxy *curproxy)
1781{
1782 const char *ret;
1783
1784 if (!srv->do_check)
1785 return 0;
1786
1787 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001788 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1789 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001790 return ERR_ALERT | ERR_FATAL;
1791 }
1792
1793 if (server_healthcheck_validate(file, linenum, srv) < 0)
1794 return ERR_ALERT | ERR_ABORT;
1795
1796 /* note: check type will be set during the config review phase */
1797 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1798 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001799 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001800 return ERR_ALERT | ERR_ABORT;
1801 }
1802
1803 return 0;
1804}
1805
1806/*
1807 * Initialize <srv> agent check structure.
1808 * Returns the error string in case of memory allocation failure, NULL if not.
1809 */
1810static const char *do_server_agent_check_init(struct server *srv, int state)
1811{
1812 const char *ret;
1813
1814 if (!srv->do_agent)
1815 return NULL;
1816
1817 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1818 if (ret)
1819 return ret;
1820
1821 if (!srv->agent.inter)
1822 srv->agent.inter = srv->check.inter;
1823
1824 srv->agent.state |= state;
1825 global.maxsock++;
1826
1827 return NULL;
1828}
1829
1830static int server_agent_check_init(const char *file, int linenum,
1831 struct server *srv, struct proxy *curproxy)
1832{
1833 const char *ret;
1834
1835 if (!srv->do_agent)
1836 return 0;
1837
1838 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001839 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 +02001840 file, linenum, srv->id);
1841 return ERR_ALERT | ERR_FATAL;
1842 }
1843
1844 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1845 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001846 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001847 return ERR_ALERT | ERR_ABORT;
1848 }
1849
1850 return 0;
1851}
1852
1853#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1854static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1855 struct server *srv, struct proxy *proxy)
1856{
1857 int ret;
1858 char *err = NULL;
1859
1860 if (!srv->sni_expr)
1861 return 0;
1862
1863 ret = server_parse_sni_expr(srv, proxy, &err);
1864 if (!ret)
1865 return 0;
1866
1867 display_parser_err(file, linenum, args, cur_arg, &err);
1868 free(err);
1869
1870 return ret;
1871}
1872#endif
1873
1874/*
1875 * Server initializations finalization.
1876 * Initialize health check, agent check and SNI expression if enabled.
1877 * Must not be called for a default server instance.
1878 */
1879static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1880 struct server *srv, struct proxy *px)
1881{
1882 int ret;
1883
1884 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1885 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1886 return ret;
1887 }
1888
1889#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1890 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1891 return ret;
1892#endif
1893
1894 if (srv->flags & SRV_F_BACKUP)
1895 px->srv_bck++;
1896 else
1897 px->srv_act++;
1898 srv_lb_commit_status(srv);
1899
1900 return 0;
1901}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001902
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001903/*
1904 * Parse as much as possible such a range string argument: low[-high]
1905 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1906 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1907 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1908 * Returns 0 if succeeded, -1 if not.
1909 */
1910static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1911{
1912 char *nb_high_arg;
1913
1914 *nb_high = 0;
1915 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001916 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001917
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001918 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001919 *nb_high_arg++ = '\0';
1920 *nb_high = atoi(nb_high_arg);
1921 }
1922 else {
1923 *nb_high += *nb_low;
1924 *nb_low = 1;
1925 }
1926
1927 if (*nb_low < 0 || *nb_high < *nb_low)
1928 return -1;
1929
1930 return 0;
1931}
1932
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001933static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1934{
1935 chunk_printf(&trash, "%s%d", prefix, nb);
1936 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001937 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001938}
1939
1940/*
1941 * Initialize as much as possible servers from <srv> server template.
1942 * Note that a server template is a special server with
1943 * a few different parameters than a server which has
1944 * been parsed mostly the same way as a server.
Joseph Herlant44466822018-11-15 08:57:51 -08001945 * Returns the number of servers successfully allocated,
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001946 * 'srv' template included.
1947 */
1948static int server_template_init(struct server *srv, struct proxy *px)
1949{
1950 int i;
1951 struct server *newsrv;
1952
1953 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1954 int check_init_state;
1955 int agent_init_state;
1956
1957 newsrv = new_server(px);
1958 if (!newsrv)
1959 goto err;
1960
1961 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001962 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001963#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1964 if (newsrv->sni_expr) {
1965 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1966 if (!newsrv->ssl_ctx.sni)
1967 goto err;
1968 }
1969#endif
1970 /* Set this new server ID. */
1971 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1972
1973 /* Initial checks states. */
1974 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1975 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1976
1977 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1978 do_server_agent_check_init(newsrv, agent_init_state))
1979 goto err;
1980
1981 /* Linked backwards first. This will be restablished after parsing. */
1982 newsrv->next = px->srv;
1983 px->srv = newsrv;
1984 }
1985 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1986
1987 return i - srv->tmpl_info.nb_low;
1988
1989 err:
1990 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1991 if (newsrv) {
1992#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1993 release_sample_expr(newsrv->ssl_ctx.sni);
1994#endif
1995 free_check(&newsrv->agent);
1996 free_check(&newsrv->check);
1997 }
1998 free(newsrv);
1999 return i - srv->tmpl_info.nb_low;
2000}
2001
Willy Tarreau272adea2014-03-31 10:39:59 +02002002int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
2003{
2004 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002005 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 char *errmsg = NULL;
2007 int err_code = 0;
2008 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02002009 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02002010
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002011 if (!strcmp(args[0], "server") ||
2012 !strcmp(args[0], "default-server") ||
2013 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002014 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002015 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002016 int srv = !defsrv && !strcmp(args[0], "server");
2017 int srv_tmpl = !defsrv && !srv;
2018 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002019
2020 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002021 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002022 err_code |= ERR_ALERT | ERR_FATAL;
2023 goto out;
2024 }
2025 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002026 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002027
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002028 /* There is no mandatory first arguments for default server. */
2029 if (srv) {
2030 if (!*args[2]) {
2031 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002032 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002033 file, linenum, args[0]);
2034 err_code |= ERR_ALERT | ERR_FATAL;
2035 goto out;
2036 }
2037
2038 err = invalid_char(args[1]);
2039 }
2040 else if (srv_tmpl) {
2041 if (!*args[3]) {
2042 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002043 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 +02002044 file, linenum, args[0]);
2045 err_code |= ERR_ALERT | ERR_FATAL;
2046 goto out;
2047 }
2048
2049 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002050 }
2051
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002052 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002053 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 +02002054 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002055 err_code |= ERR_ALERT | ERR_FATAL;
2056 goto out;
2057 }
2058
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002059 cur_arg = 2;
2060 if (srv_tmpl) {
2061 /* Parse server-template <nb | range> arg. */
2062 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002063 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002064 file, linenum, args[0], args[cur_arg]);
2065 err_code |= ERR_ALERT | ERR_FATAL;
2066 goto out;
2067 }
2068 cur_arg++;
2069 }
2070
Willy Tarreau272adea2014-03-31 10:39:59 +02002071 if (!defsrv) {
2072 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002073 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002074 struct protocol *proto;
2075
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002076 newsrv = new_server(curproxy);
2077 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002078 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002079 err_code |= ERR_ALERT | ERR_ABORT;
2080 goto out;
2081 }
2082
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002083 if (srv_tmpl) {
2084 newsrv->tmpl_info.nb_low = tmpl_range_low;
2085 newsrv->tmpl_info.nb_high = tmpl_range_high;
2086 }
2087
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 /* the servers are linked backwards first */
2089 newsrv->next = curproxy->srv;
2090 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002091 newsrv->conf.file = strdup(file);
2092 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002093 /* Note: for a server template, its id is its prefix.
2094 * This is a temporary id which will be used for server allocations to come
2095 * after parsing.
2096 */
2097 if (srv)
2098 newsrv->id = strdup(args[1]);
2099 else
2100 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002101
2102 /* several ways to check the port component :
2103 * - IP => port=+0, relative (IPv4 only)
2104 * - IP: => port=+0, relative
2105 * - IP:N => port=N, absolute
2106 * - IP:+N => port=+N, relative
2107 * - IP:-N => port=-N, relative
2108 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002109 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002110 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002111 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002112 err_code |= ERR_ALERT | ERR_FATAL;
2113 goto out;
2114 }
2115
2116 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002117 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002118 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002119 file, linenum, args[0], args[1]);
2120 err_code |= ERR_ALERT | ERR_FATAL;
2121 goto out;
2122 }
2123
2124 if (!port1 || !port2) {
2125 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002126 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002127 }
2128 else if (port1 != port2) {
2129 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002130 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002131 file, linenum, args[0], args[1], args[2]);
2132 err_code |= ERR_ALERT | ERR_FATAL;
2133 goto out;
2134 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002135
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002136 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002137 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002138 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002139 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002140 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2141 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2142 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002143 err_code |= ERR_ALERT | ERR_FATAL;
2144 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002145 }
2146 }
2147 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002148 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002149 file, linenum, newsrv->id);
2150 err_code |= ERR_ALERT | ERR_FATAL;
2151 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002152 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002153 }
2154
Willy Tarreau272adea2014-03-31 10:39:59 +02002155 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002156 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002157
Olivier Houchard8da5f982017-08-04 18:35:36 +02002158 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002159 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002160 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002161 err_code |= ERR_ALERT | ERR_FATAL;
2162 goto out;
2163 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002164
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002165 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002166 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002167 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002168 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002169 } else {
2170 newsrv = &curproxy->defsrv;
2171 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002172 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002173 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002174 }
2175
2176 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002177 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002178 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2179 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002180 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002181 file, linenum, *err, newsrv->id);
2182 err_code |= ERR_ALERT | ERR_FATAL;
2183 goto out;
2184 }
2185 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002186 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002187 file, linenum, val, args[cur_arg], newsrv->id);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191 newsrv->agent.inter = val;
2192 cur_arg += 2;
2193 }
Misiekea849332017-01-09 09:39:51 +01002194 else if (!strcmp(args[cur_arg], "agent-addr")) {
2195 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002196 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002197 goto out;
2198 }
2199
2200 cur_arg += 2;
2201 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002202 else if (!strcmp(args[cur_arg], "agent-port")) {
2203 global.maxsock++;
2204 newsrv->agent.port = atol(args[cur_arg + 1]);
2205 cur_arg += 2;
2206 }
James Brown55f9ff12015-10-21 18:19:05 -07002207 else if (!strcmp(args[cur_arg], "agent-send")) {
2208 global.maxsock++;
2209 free(newsrv->agent.send_string);
2210 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2211 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2212 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2213 cur_arg += 2;
2214 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002215 else if (!strcmp(args[cur_arg], "init-addr")) {
2216 char *p, *end;
2217 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002218 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002219
2220 newsrv->init_addr_methods = 0;
2221 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2222
2223 for (p = args[cur_arg + 1]; *p; p = end) {
2224 /* cut on next comma */
2225 for (end = p; *end && *end != ','; end++);
2226 if (*end)
2227 *(end++) = 0;
2228
Willy Tarreau4310d362016-11-02 15:05:56 +01002229 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002230 if (!strcmp(p, "libc")) {
2231 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2232 }
2233 else if (!strcmp(p, "last")) {
2234 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2235 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002236 else if (!strcmp(p, "none")) {
2237 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2238 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002239 else if (str2ip2(p, &sa, 0)) {
2240 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002241 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002242 file, linenum, args[cur_arg], p);
2243 err_code |= ERR_ALERT | ERR_FATAL;
2244 goto out;
2245 }
2246 newsrv->init_addr = sa;
2247 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2248 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002249 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002250 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 +02002251 file, linenum, args[cur_arg], p);
2252 err_code |= ERR_ALERT | ERR_FATAL;
2253 goto out;
2254 }
2255 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002256 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002257 file, linenum, args[cur_arg], p);
2258 err_code |= ERR_ALERT | ERR_FATAL;
2259 goto out;
2260 }
2261 }
2262 cur_arg += 2;
2263 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002264 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002265 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002266 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2267 cur_arg += 2;
2268 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002269 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2270 char *p, *end;
2271
2272 for (p = args[cur_arg + 1]; *p; p = end) {
2273 /* cut on next comma */
2274 for (end = p; *end && *end != ','; end++);
2275 if (*end)
2276 *(end++) = 0;
2277
2278 if (!strcmp(p, "allow-dup-ip")) {
2279 newsrv->dns_opts.accept_duplicate_ip = 1;
2280 }
2281 else if (!strcmp(p, "prevent-dup-ip")) {
2282 newsrv->dns_opts.accept_duplicate_ip = 0;
2283 }
2284 else {
2285 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2286 file, linenum, args[cur_arg], p);
2287 err_code |= ERR_ALERT | ERR_FATAL;
2288 goto out;
2289 }
2290 }
2291
2292 cur_arg += 2;
2293 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002294 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2295 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002296 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002297 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002298 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002299 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002300 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002301 file, linenum, args[cur_arg]);
2302 err_code |= ERR_ALERT | ERR_FATAL;
2303 goto out;
2304 }
2305 cur_arg += 2;
2306 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002307 else if (!strcmp(args[cur_arg], "resolve-net")) {
2308 char *p, *e;
2309 unsigned char mask;
2310 struct dns_options *opt;
2311
2312 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002313 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002314 file, linenum, args[cur_arg]);
2315 err_code |= ERR_ALERT | ERR_FATAL;
2316 goto out;
2317 }
2318
2319 opt = &newsrv->dns_opts;
2320
2321 /* Split arguments by comma, and convert it from ipv4 or ipv6
2322 * string network in in_addr or in6_addr.
2323 */
2324 p = args[cur_arg + 1];
2325 e = p;
2326 while (*p != '\0') {
Joseph Herlant44466822018-11-15 08:57:51 -08002327 /* If no room available, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002328 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002329 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002330 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2331 err_code |= ERR_ALERT | ERR_FATAL;
2332 goto out;
2333 }
2334 /* look for end or comma. */
2335 while (*e != ',' && *e != '\0')
2336 e++;
2337 if (*e == ',') {
2338 *e = '\0';
2339 e++;
2340 }
2341 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2342 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2343 /* Try to convert input string from ipv4 or ipv6 network. */
2344 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2345 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2346 &mask)) {
2347 /* Try to convert input string from ipv6 network. */
2348 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2349 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2350 } else {
2351 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002352 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002353 file, linenum, args[cur_arg], p);
2354 err_code |= ERR_ALERT | ERR_FATAL;
2355 goto out;
2356 }
2357 opt->pref_net_nb++;
2358 p = e;
2359 }
2360
2361 cur_arg += 2;
2362 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002363 else if (!strcmp(args[cur_arg], "rise")) {
2364 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002365 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002366 file, linenum, args[cur_arg]);
2367 err_code |= ERR_ALERT | ERR_FATAL;
2368 goto out;
2369 }
2370
2371 newsrv->check.rise = atol(args[cur_arg + 1]);
2372 if (newsrv->check.rise <= 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 if (newsrv->check.health)
2380 newsrv->check.health = newsrv->check.rise;
2381 cur_arg += 2;
2382 }
2383 else if (!strcmp(args[cur_arg], "fall")) {
2384 newsrv->check.fall = atol(args[cur_arg + 1]);
2385
2386 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002387 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002388 file, linenum, args[cur_arg]);
2389 err_code |= ERR_ALERT | ERR_FATAL;
2390 goto out;
2391 }
2392
2393 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002394 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002395 file, linenum, args[cur_arg]);
2396 err_code |= ERR_ALERT | ERR_FATAL;
2397 goto out;
2398 }
2399
2400 cur_arg += 2;
2401 }
2402 else if (!strcmp(args[cur_arg], "inter")) {
2403 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2404 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002405 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002406 file, linenum, *err, newsrv->id);
2407 err_code |= ERR_ALERT | ERR_FATAL;
2408 goto out;
2409 }
2410 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002411 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002412 file, linenum, val, args[cur_arg], newsrv->id);
2413 err_code |= ERR_ALERT | ERR_FATAL;
2414 goto out;
2415 }
2416 newsrv->check.inter = val;
2417 cur_arg += 2;
2418 }
2419 else if (!strcmp(args[cur_arg], "fastinter")) {
2420 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2421 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002422 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002423 file, linenum, *err, newsrv->id);
2424 err_code |= ERR_ALERT | ERR_FATAL;
2425 goto out;
2426 }
2427 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002428 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002429 file, linenum, val, args[cur_arg], newsrv->id);
2430 err_code |= ERR_ALERT | ERR_FATAL;
2431 goto out;
2432 }
2433 newsrv->check.fastinter = val;
2434 cur_arg += 2;
2435 }
2436 else if (!strcmp(args[cur_arg], "downinter")) {
2437 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2438 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002439 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002440 file, linenum, *err, newsrv->id);
2441 err_code |= ERR_ALERT | ERR_FATAL;
2442 goto out;
2443 }
2444 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002445 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002446 file, linenum, val, args[cur_arg], newsrv->id);
2447 err_code |= ERR_ALERT | ERR_FATAL;
2448 goto out;
2449 }
2450 newsrv->check.downinter = val;
2451 cur_arg += 2;
2452 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 else if (!strcmp(args[cur_arg], "port")) {
2454 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002455 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002456 cur_arg += 2;
2457 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002458 else if (!strcmp(args[cur_arg], "weight")) {
2459 int w;
2460 w = atol(args[cur_arg + 1]);
2461 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002462 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002463 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2464 err_code |= ERR_ALERT | ERR_FATAL;
2465 goto out;
2466 }
2467 newsrv->uweight = newsrv->iweight = w;
2468 cur_arg += 2;
2469 }
2470 else if (!strcmp(args[cur_arg], "minconn")) {
2471 newsrv->minconn = atol(args[cur_arg + 1]);
2472 cur_arg += 2;
2473 }
2474 else if (!strcmp(args[cur_arg], "maxconn")) {
2475 newsrv->maxconn = atol(args[cur_arg + 1]);
2476 cur_arg += 2;
2477 }
2478 else if (!strcmp(args[cur_arg], "maxqueue")) {
2479 newsrv->maxqueue = atol(args[cur_arg + 1]);
2480 cur_arg += 2;
2481 }
2482 else if (!strcmp(args[cur_arg], "slowstart")) {
2483 /* slowstart is stored in seconds */
2484 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2485 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002486 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002487 file, linenum, *err, newsrv->id);
2488 err_code |= ERR_ALERT | ERR_FATAL;
2489 goto out;
2490 }
2491 newsrv->slowstart = (val + 999) / 1000;
2492 cur_arg += 2;
2493 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002494 else if (!strcmp(args[cur_arg], "on-error")) {
2495 if (!strcmp(args[cur_arg + 1], "fastinter"))
2496 newsrv->onerror = HANA_ONERR_FASTINTER;
2497 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2498 newsrv->onerror = HANA_ONERR_FAILCHK;
2499 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2500 newsrv->onerror = HANA_ONERR_SUDDTH;
2501 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2502 newsrv->onerror = HANA_ONERR_MARKDWN;
2503 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002504 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002505 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2506 file, linenum, args[cur_arg], args[cur_arg + 1]);
2507 err_code |= ERR_ALERT | ERR_FATAL;
2508 goto out;
2509 }
2510
2511 cur_arg += 2;
2512 }
2513 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2514 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2515 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2516 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002517 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002518 file, linenum, args[cur_arg], args[cur_arg + 1]);
2519 err_code |= ERR_ALERT | ERR_FATAL;
2520 goto out;
2521 }
2522
2523 cur_arg += 2;
2524 }
2525 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2526 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2527 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2528 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002529 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002530 file, linenum, args[cur_arg], args[cur_arg + 1]);
2531 err_code |= ERR_ALERT | ERR_FATAL;
2532 goto out;
2533 }
2534
2535 cur_arg += 2;
2536 }
2537 else if (!strcmp(args[cur_arg], "error-limit")) {
2538 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002539 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002540 file, linenum, args[cur_arg]);
2541 err_code |= ERR_ALERT | ERR_FATAL;
2542 goto out;
2543 }
2544
2545 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2546
2547 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002548 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002549 file, linenum, args[cur_arg]);
2550 err_code |= ERR_ALERT | ERR_FATAL;
2551 goto out;
2552 }
2553 cur_arg += 2;
2554 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002555 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002556 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002557 file, linenum, "usesrc", "source");
2558 err_code |= ERR_ALERT | ERR_FATAL;
2559 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002560 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002561 else {
2562 static int srv_dumped;
2563 struct srv_kw *kw;
2564 char *err;
2565
2566 kw = srv_find_kw(args[cur_arg]);
2567 if (kw) {
2568 char *err = NULL;
2569 int code;
2570
2571 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002572 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 +02002573 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002574 if (kw->skip != -1)
2575 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002576 err_code |= ERR_ALERT | ERR_FATAL;
2577 goto out;
2578 }
2579
2580 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002581 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002582 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002583 if (kw->skip != -1)
2584 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002585 err_code |= ERR_ALERT;
2586 continue;
2587 }
2588
2589 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2590 err_code |= code;
2591
2592 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002593 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002594 if (code & ERR_FATAL) {
2595 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002596 if (kw->skip != -1)
2597 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002598 goto out;
2599 }
2600 }
2601 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002602 if (kw->skip != -1)
2603 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002604 continue;
2605 }
2606
2607 err = NULL;
2608 if (!srv_dumped) {
2609 srv_dump_kws(&err);
2610 indent_msg(&err, 4);
2611 srv_dumped = 1;
2612 }
2613
Christopher Faulet767a84b2017-11-24 16:50:31 +01002614 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002615 file, linenum, args[0], args[1], args[cur_arg],
2616 err ? " Registered keywords :" : "", err ? err : "");
2617 free(err);
2618
2619 err_code |= ERR_ALERT | ERR_FATAL;
2620 goto out;
2621 }
2622 }
2623
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002624 if (!defsrv)
2625 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2626 if (err_code & ERR_FATAL)
2627 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002628 if (srv_tmpl)
2629 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002630 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002631 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002632 return 0;
2633
2634 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002635 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002636 free(errmsg);
2637 return err_code;
2638}
2639
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002640/* Returns a pointer to the first server matching either id <id>.
2641 * NULL is returned if no match is found.
2642 * the lookup is performed in the backend <bk>
2643 */
2644struct server *server_find_by_id(struct proxy *bk, int id)
2645{
2646 struct eb32_node *eb32;
2647 struct server *curserver;
2648
2649 if (!bk || (id ==0))
2650 return NULL;
2651
2652 /* <bk> has no backend capabilities, so it can't have a server */
2653 if (!(bk->cap & PR_CAP_BE))
2654 return NULL;
2655
2656 curserver = NULL;
2657
2658 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2659 if (eb32)
2660 curserver = container_of(eb32, struct server, conf.id);
2661
2662 return curserver;
2663}
2664
2665/* Returns a pointer to the first server matching either name <name>, or id
2666 * if <name> starts with a '#'. NULL is returned if no match is found.
2667 * the lookup is performed in the backend <bk>
2668 */
2669struct server *server_find_by_name(struct proxy *bk, const char *name)
2670{
2671 struct server *curserver;
2672
2673 if (!bk || !name)
2674 return NULL;
2675
2676 /* <bk> has no backend capabilities, so it can't have a server */
2677 if (!(bk->cap & PR_CAP_BE))
2678 return NULL;
2679
2680 curserver = NULL;
2681 if (*name == '#') {
2682 curserver = server_find_by_id(bk, atoi(name + 1));
2683 if (curserver)
2684 return curserver;
2685 }
2686 else {
2687 curserver = bk->srv;
2688
2689 while (curserver && (strcmp(curserver->id, name) != 0))
2690 curserver = curserver->next;
2691
2692 if (curserver)
2693 return curserver;
2694 }
2695
2696 return NULL;
2697}
2698
2699struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2700{
2701 struct server *byname;
2702 struct server *byid;
2703
2704 if (!name && !id)
2705 return NULL;
2706
2707 if (diff)
2708 *diff = 0;
2709
2710 byname = byid = NULL;
2711
2712 if (name) {
2713 byname = server_find_by_name(bk, name);
2714 if (byname && (!id || byname->puid == id))
2715 return byname;
2716 }
2717
2718 /* remaining possibilities :
2719 * - name not set
2720 * - name set but not found
2721 * - name found but ID doesn't match
2722 */
2723 if (id) {
2724 byid = server_find_by_id(bk, id);
2725 if (byid) {
2726 if (byname) {
2727 /* use id only if forced by configuration */
2728 if (byid->flags & SRV_F_FORCED_ID) {
2729 if (diff)
2730 *diff |= 2;
2731 return byid;
2732 }
2733 else {
2734 if (diff)
2735 *diff |= 1;
2736 return byname;
2737 }
2738 }
2739
2740 /* remaining possibilities:
2741 * - name not set
2742 * - name set but not found
2743 */
2744 if (name && diff)
2745 *diff |= 2;
2746 return byid;
2747 }
2748
2749 /* id bot found */
2750 if (byname) {
2751 if (diff)
2752 *diff |= 1;
2753 return byname;
2754 }
2755 }
2756
2757 return NULL;
2758}
2759
Willy Tarreau46b7f532018-08-21 11:54:26 +02002760/* Update a server state using the parameters available in the params list.
2761 *
2762 * Grabs the server lock during operation.
2763 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002764static void srv_update_state(struct server *srv, int version, char **params)
2765{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002766 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002767 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002768
2769 /* fields since version 1
2770 * and common to all other upcoming versions
2771 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772 enum srv_state srv_op_state;
2773 enum srv_admin srv_admin_state;
2774 unsigned srv_uweight, srv_iweight;
2775 unsigned long srv_last_time_change;
2776 short srv_check_status;
2777 enum chk_result srv_check_result;
2778 int srv_check_health;
2779 int srv_check_state, srv_agent_state;
2780 int bk_f_forced_id;
2781 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002782 int fqdn_set_by_cli;
2783 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002784 const char *port_str;
2785 unsigned int port;
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002786 char *srvrecord;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002788 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002789 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002790 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002791 switch (version) {
2792 case 1:
2793 /*
2794 * now we can proceed with server's state update:
2795 * srv_addr: params[0]
2796 * srv_op_state: params[1]
2797 * srv_admin_state: params[2]
2798 * srv_uweight: params[3]
2799 * srv_iweight: params[4]
2800 * srv_last_time_change: params[5]
2801 * srv_check_status: params[6]
2802 * srv_check_result: params[7]
2803 * srv_check_health: params[8]
2804 * srv_check_state: params[9]
2805 * srv_agent_state: params[10]
2806 * bk_f_forced_id: params[11]
2807 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002808 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002809 * srv_port: params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002810 * srvrecord: params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002811 */
2812
2813 /* validating srv_op_state */
2814 p = NULL;
2815 errno = 0;
2816 srv_op_state = strtol(params[1], &p, 10);
2817 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2818 (srv_op_state != SRV_ST_STOPPED &&
2819 srv_op_state != SRV_ST_STARTING &&
2820 srv_op_state != SRV_ST_RUNNING &&
2821 srv_op_state != SRV_ST_STOPPING)) {
2822 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2823 }
2824
2825 /* validating srv_admin_state */
2826 p = NULL;
2827 errno = 0;
2828 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002829 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002830
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002831 /* inherited statuses will be recomputed later.
2832 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2833 */
2834 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002835
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002836 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2837 (srv_admin_state != 0 &&
2838 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002839 srv_admin_state != SRV_ADMF_CMAINT &&
2840 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002841 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002842 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002843 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2844 }
2845
2846 /* validating srv_uweight */
2847 p = NULL;
2848 errno = 0;
2849 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002850 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002851 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2852
2853 /* validating srv_iweight */
2854 p = NULL;
2855 errno = 0;
2856 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002857 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002858 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2859
2860 /* validating srv_last_time_change */
2861 p = NULL;
2862 errno = 0;
2863 srv_last_time_change = strtol(params[5], &p, 10);
2864 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2865 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2866
2867 /* validating srv_check_status */
2868 p = NULL;
2869 errno = 0;
2870 srv_check_status = strtol(params[6], &p, 10);
2871 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2872 (srv_check_status >= HCHK_STATUS_SIZE))
2873 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2874
2875 /* validating srv_check_result */
2876 p = NULL;
2877 errno = 0;
2878 srv_check_result = strtol(params[7], &p, 10);
2879 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2880 (srv_check_result != CHK_RES_UNKNOWN &&
2881 srv_check_result != CHK_RES_NEUTRAL &&
2882 srv_check_result != CHK_RES_FAILED &&
2883 srv_check_result != CHK_RES_PASSED &&
2884 srv_check_result != CHK_RES_CONDPASS)) {
2885 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2886 }
2887
2888 /* validating srv_check_health */
2889 p = NULL;
2890 errno = 0;
2891 srv_check_health = strtol(params[8], &p, 10);
2892 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2893 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2894
2895 /* validating srv_check_state */
2896 p = NULL;
2897 errno = 0;
2898 srv_check_state = strtol(params[9], &p, 10);
2899 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2900 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2901 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2902
2903 /* validating srv_agent_state */
2904 p = NULL;
2905 errno = 0;
2906 srv_agent_state = strtol(params[10], &p, 10);
2907 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2908 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2909 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2910
2911 /* validating bk_f_forced_id */
2912 p = NULL;
2913 errno = 0;
2914 bk_f_forced_id = strtol(params[11], &p, 10);
2915 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2916 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2917
2918 /* validating srv_f_forced_id */
2919 p = NULL;
2920 errno = 0;
2921 srv_f_forced_id = strtol(params[12], &p, 10);
2922 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2923 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2924
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002925 /* validating srv_fqdn */
2926 fqdn = params[13];
2927 if (fqdn && *fqdn == '-')
2928 fqdn = NULL;
2929 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2930 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2931 fqdn = NULL;
2932 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002933
Frédéric Lécaille31694712017-08-01 08:47:19 +02002934 port_str = params[14];
2935 if (port_str) {
2936 port = strl2uic(port_str, strlen(port_str));
2937 if (port > USHRT_MAX) {
2938 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2939 port_str = NULL;
2940 }
2941 }
2942
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02002943 /* SRV record
2944 * NOTE: in HAProxy, SRV records must start with an underscore '_'
2945 */
2946 srvrecord = params[15];
2947 if (srvrecord && *srvrecord != '_')
2948 srvrecord = NULL;
2949
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002950 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002951 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002952 goto out;
2953
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002954 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002955 /* recover operational state and apply it to this server
2956 * and all servers tracking this one */
2957 switch (srv_op_state) {
2958 case SRV_ST_STOPPED:
2959 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002960 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002961 break;
2962 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002963 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002964 break;
2965 case SRV_ST_STOPPING:
2966 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002967 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002968 break;
2969 case SRV_ST_RUNNING:
2970 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002971 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002972 break;
2973 }
2974
2975 /* When applying server state, the following rules apply:
2976 * - in case of a configuration change, we apply the setting from the new
2977 * configuration, regardless of old running state
2978 * - if no configuration change, we apply old running state only if old running
2979 * state is different from new configuration state
2980 */
2981 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002982 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2983 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002984 srv_adm_set_maint(srv);
2985 else
2986 srv_adm_set_ready(srv);
2987 }
2988 /* configuration is the same, let's compate old running state and new conf state */
2989 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002990 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002991 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002992 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002993 srv_adm_set_ready(srv);
2994 }
2995 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002996 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002997 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002998 * (srv->iweight is the weight set up in configuration).
2999 * There are two possible reasons for FDRAIN to have been present :
3000 * - previous config weight was zero
3001 * - "set server b/s drain" was sent to the CLI
3002 *
3003 * In the first case, we simply want to drop this drain state
3004 * if the new weight is not zero anymore, meaning the administrator
3005 * has intentionally turned the weight back to a positive value to
3006 * enable the server again after an operation. In the second case,
3007 * the drain state was forced on the CLI regardless of the config's
3008 * weight so we don't want a change to the config weight to lose this
3009 * status. What this means is :
3010 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
3011 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003012 */
Willy Tarreau22cace22016-11-03 18:19:49 +01003013 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003014 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003015 }
3016
3017 srv->last_change = date.tv_sec - srv_last_time_change;
3018 srv->check.status = srv_check_status;
3019 srv->check.result = srv_check_result;
3020 srv->check.health = srv_check_health;
3021
3022 /* Only case we want to apply is removing ENABLED flag which could have been
3023 * done by the "disable health" command over the stats socket
3024 */
3025 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3026 (srv_check_state & CHK_ST_CONFIGURED) &&
3027 !(srv_check_state & CHK_ST_ENABLED))
3028 srv->check.state &= ~CHK_ST_ENABLED;
3029
3030 /* Only case we want to apply is removing ENABLED flag which could have been
3031 * done by the "disable agent" command over the stats socket
3032 */
3033 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3034 (srv_agent_state & CHK_ST_CONFIGURED) &&
3035 !(srv_agent_state & CHK_ST_ENABLED))
3036 srv->agent.state &= ~CHK_ST_ENABLED;
3037
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003038 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3039 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003040 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003041 * It means that a configuration file change has precedence over a unix socket change
3042 * for server's weight
3043 *
3044 * by default, HAProxy applies the following weight when parsing the configuration
3045 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003046 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003047 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003048 srv->uweight = srv_uweight;
3049 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003050 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003051
Willy Tarreaue5a60682016-11-09 14:54:53 +01003052 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003053 if (strcmp(params[0], "-"))
3054 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003055
3056 if (fqdn && srv->hostname) {
3057 if (!strcmp(srv->hostname, fqdn)) {
3058 /* Here we reset the 'set from stats socket FQDN' flag
3059 * to support such transitions:
3060 * Let's say initial FQDN value is foo1 (in configuration file).
3061 * - FQDN changed from stats socket, from foo1 to foo2 value,
3062 * - FQDN changed again from file configuration (with the same previous value
3063 set from stats socket, from foo1 to foo2 value),
3064 * - reload for any other reason than a FQDN modification,
3065 * the configuration file FQDN matches the fqdn server state file value.
3066 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
Joseph Herlant44466822018-11-15 08:57:51 -08003067 * any further FQDN modification.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003068 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003069 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003070 }
3071 else {
3072 /* If the FDQN has been changed from stats socket,
3073 * apply fqdn state file value (which is the value set
3074 * from stats socket).
3075 */
3076 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003077 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003078 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003079 }
3080 }
3081 }
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003082 /* If all the conditions below are validated, this means
3083 * we're evaluating a server managed by SRV resolution
3084 */
3085 else if (fqdn && !srv->hostname && srvrecord) {
3086 int res;
3087
3088 /* we can't apply previous state if SRV record has changed */
3089 if (srv->srvrq && strcmp(srv->srvrq->name, srvrecord) != 0) {
3090 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);
3091 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3092 goto out;
3093 }
3094
3095 /* create or find a SRV resolution for this srv record */
3096 if (srv->srvrq == NULL && (srv->srvrq = find_srvrq_by_name(srvrecord, srv->proxy)) == NULL)
3097 srv->srvrq = new_dns_srvrq(srv, srvrecord);
3098 if (srv->srvrq == NULL) {
3099 chunk_appendf(msg, ", can't create or find SRV resolution '%s' for server '%s'", srvrecord, srv->id);
3100 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3101 goto out;
3102 }
3103
3104 /* prepare DNS resolution for this server */
3105 res = srv_prepare_for_resolution(srv, fqdn);
3106 if (res == -1) {
3107 chunk_appendf(msg, ", can't allocate memory for DNS resolution for server '%s'", srv->id);
3108 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
3109 goto out;
3110 }
3111
3112 /* configure check.port accordingly */
3113 if ((srv->check.state & CHK_ST_CONFIGURED) &&
3114 !(srv->flags & SRV_F_CHECKPORT))
3115 srv->check.port = port;
3116
3117 /* Unset SRV_F_MAPPORTS for SRV records.
3118 * SRV_F_MAPPORTS is unfortunately set by parse_server()
3119 * because no ports are provided in the configuration file.
3120 * This is because HAProxy will use the port found into the SRV record.
3121 */
3122 srv->flags &= ~SRV_F_MAPPORTS;
3123 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003124
Frédéric Lécaille31694712017-08-01 08:47:19 +02003125 if (port_str)
3126 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003127 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003128
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003129 break;
3130 default:
3131 chunk_appendf(msg, ", version '%d' not supported", version);
3132 }
3133
3134 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003135 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003136 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003137 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003138 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003139 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140}
3141
3142/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3143 * For each proxy, it does the following:
3144 * - opens its server state file (either one or local one)
3145 * - read whole file, line by line
3146 * - analyse each line to check if it matches our current backend:
3147 * - backend name matches
3148 * - backend id matches if id is forced and name doesn't match
3149 * - if the server pointed by the line is found, then state is applied
3150 *
3151 * If the running backend uuid or id differs from the state file, then HAProxy reports
3152 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003153 *
3154 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003155 */
3156void apply_server_state(void)
3157{
3158 char *cur, *end;
3159 char mybuf[SRV_STATE_LINE_MAXLEN];
3160 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003161 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3162 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003163 int arg, srv_arg, version, diff;
3164 FILE *f;
3165 char *filepath;
3166 char globalfilepath[MAXPATHLEN + 1];
3167 char localfilepath[MAXPATHLEN + 1];
3168 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003169 struct proxy *curproxy, *bk;
3170 struct server *srv;
3171
3172 globalfilepathlen = 0;
3173 /* create the globalfilepath variable */
3174 if (global.server_state_file) {
3175 /* absolute path or no base directory provided */
3176 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3177 len = strlen(global.server_state_file);
3178 if (len > MAXPATHLEN) {
3179 globalfilepathlen = 0;
3180 goto globalfileerror;
3181 }
3182 memcpy(globalfilepath, global.server_state_file, len);
3183 globalfilepath[len] = '\0';
3184 globalfilepathlen = len;
3185 }
3186 else if (global.server_state_base) {
3187 len = strlen(global.server_state_base);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003188 if (len > MAXPATHLEN) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003189 globalfilepathlen = 0;
3190 goto globalfileerror;
3191 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003192 memcpy(globalfilepath, global.server_state_base, len);
Willy Tarreau5dfb6c42018-10-16 19:26:12 +02003193 globalfilepath[len] = 0;
3194 globalfilepathlen = len;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003195
3196 /* append a slash if needed */
3197 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3198 if (globalfilepathlen + 1 > MAXPATHLEN) {
3199 globalfilepathlen = 0;
3200 goto globalfileerror;
3201 }
3202 globalfilepath[globalfilepathlen++] = '/';
3203 }
3204
3205 len = strlen(global.server_state_file);
3206 if (globalfilepathlen + len > MAXPATHLEN) {
3207 globalfilepathlen = 0;
3208 goto globalfileerror;
3209 }
3210 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3211 globalfilepathlen += len;
3212 globalfilepath[globalfilepathlen++] = 0;
3213 }
3214 }
3215 globalfileerror:
3216 if (globalfilepathlen == 0)
3217 globalfilepath[0] = '\0';
3218
3219 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003220 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003221 /* servers are only in backends */
3222 if (!(curproxy->cap & PR_CAP_BE))
3223 continue;
3224 fileopenerr = 0;
3225 filepath = NULL;
3226
3227 /* search server state file path and name */
3228 switch (curproxy->load_server_state_from_file) {
3229 /* read servers state from global file */
3230 case PR_SRV_STATE_FILE_GLOBAL:
3231 /* there was an error while generating global server state file path */
3232 if (globalfilepathlen == 0)
3233 continue;
3234 filepath = globalfilepath;
3235 fileopenerr = 1;
3236 break;
3237 /* this backend has its own file */
3238 case PR_SRV_STATE_FILE_LOCAL:
3239 localfilepathlen = 0;
3240 localfilepath[0] = '\0';
3241 len = 0;
3242 /* create the localfilepath variable */
3243 /* absolute path or no base directory provided */
3244 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3245 len = strlen(curproxy->server_state_file_name);
3246 if (len > MAXPATHLEN) {
3247 localfilepathlen = 0;
3248 goto localfileerror;
3249 }
3250 memcpy(localfilepath, curproxy->server_state_file_name, len);
3251 localfilepath[len] = '\0';
3252 localfilepathlen = len;
3253 }
3254 else if (global.server_state_base) {
3255 len = strlen(global.server_state_base);
3256 localfilepathlen += len;
3257
3258 if (localfilepathlen > MAXPATHLEN) {
3259 localfilepathlen = 0;
3260 goto localfileerror;
3261 }
Olivier Houchard17f8b902018-10-16 18:35:01 +02003262 memcpy(localfilepath, global.server_state_base, len);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003263 localfilepath[localfilepathlen] = 0;
3264
3265 /* append a slash if needed */
3266 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3267 if (localfilepathlen + 1 > MAXPATHLEN) {
3268 localfilepathlen = 0;
3269 goto localfileerror;
3270 }
3271 localfilepath[localfilepathlen++] = '/';
3272 }
3273
3274 len = strlen(curproxy->server_state_file_name);
3275 if (localfilepathlen + len > MAXPATHLEN) {
3276 localfilepathlen = 0;
3277 goto localfileerror;
3278 }
3279 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3280 localfilepathlen += len;
3281 localfilepath[localfilepathlen++] = 0;
3282 }
3283 filepath = localfilepath;
3284 localfileerror:
3285 if (localfilepathlen == 0)
3286 localfilepath[0] = '\0';
3287
3288 break;
3289 case PR_SRV_STATE_FILE_NONE:
3290 default:
3291 continue;
3292 }
3293
3294 /* preload global state file */
3295 errno = 0;
3296 f = fopen(filepath, "r");
3297 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003298 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003299 if (!f)
3300 continue;
3301
3302 mybuf[0] = '\0';
3303 mybuflen = 0;
3304 version = 0;
3305
3306 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003307 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003308 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003309 goto fileclose;
3310 }
3311
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003312 cur = mybuf;
3313 version = atoi(cur);
3314 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3315 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003316 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003317
3318 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3319 int bk_f_forced_id = 0;
3320 int check_id = 0;
3321 int check_name = 0;
3322
3323 mybuflen = strlen(mybuf);
3324 cur = mybuf;
3325 end = cur + mybuflen;
3326
3327 bk = NULL;
3328 srv = NULL;
3329
3330 /* we need at least one character */
3331 if (mybuflen == 0)
3332 continue;
3333
3334 /* ignore blank characters at the beginning of the line */
3335 while (isspace(*cur))
3336 ++cur;
3337
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003338 /* Ignore empty or commented lines */
3339 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003340 continue;
3341
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003342 /* truncated lines */
3343 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003344 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003345 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003346 }
3347
3348 /* Removes trailing '\n' */
3349 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003350
3351 /* we're now ready to move the line into *srv_params[] */
3352 params[0] = cur;
3353 arg = 1;
3354 srv_arg = 0;
3355 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3356 if (isspace(*cur)) {
3357 *cur = '\0';
3358 ++cur;
3359 while (isspace(*cur))
3360 ++cur;
3361 switch (version) {
3362 case 1:
3363 /*
3364 * srv_addr: params[4] => srv_params[0]
3365 * srv_op_state: params[5] => srv_params[1]
3366 * srv_admin_state: params[6] => srv_params[2]
3367 * srv_uweight: params[7] => srv_params[3]
3368 * srv_iweight: params[8] => srv_params[4]
3369 * srv_last_time_change: params[9] => srv_params[5]
3370 * srv_check_status: params[10] => srv_params[6]
3371 * srv_check_result: params[11] => srv_params[7]
3372 * srv_check_health: params[12] => srv_params[8]
3373 * srv_check_state: params[13] => srv_params[9]
3374 * srv_agent_state: params[14] => srv_params[10]
3375 * bk_f_forced_id: params[15] => srv_params[11]
3376 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003377 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003378 * srv_port: params[18] => srv_params[14]
Baptiste Assmann6d0f38f2018-07-02 17:00:54 +02003379 * srvrecord: params[19] => srv_params[15]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003380 */
3381 if (arg >= 4) {
3382 srv_params[srv_arg] = cur;
3383 ++srv_arg;
3384 }
3385 break;
3386 }
3387
3388 params[arg] = cur;
3389 ++arg;
3390 }
3391 else {
3392 ++cur;
3393 }
3394 }
3395
3396 /* if line is incomplete line, then ignore it.
3397 * otherwise, update useful flags */
3398 switch (version) {
3399 case 1:
3400 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3401 continue;
3402 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3403 check_id = (atoi(params[0]) == curproxy->uuid);
3404 check_name = (strcmp(curproxy->id, params[1]) == 0);
3405 break;
3406 }
3407
3408 diff = 0;
3409 bk = curproxy;
3410
3411 /* if backend can't be found, let's continue */
3412 if (!check_id && !check_name)
3413 continue;
3414 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 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 +02003416 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3417 }
3418 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003419 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 +02003420 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3421 /* if name doesn't match, we still want to update curproxy if the backend id
3422 * was forced in previous the previous configuration */
3423 if (!bk_f_forced_id)
3424 continue;
3425 }
3426
3427 /* look for the server by its id: param[2] */
3428 /* else look for the server by its name: param[3] */
3429 diff = 0;
3430 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3431
3432 if (!srv) {
3433 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003434 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3435 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003436 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3437 params[3], params[2], params[0], params[1]);
3438 continue;
3439 }
3440 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 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 +02003442 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 +02003443 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003444 }
3445 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003446 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 +02003447 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 +02003448 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003449 }
3450
3451 /* now we can proceed with server's state update */
3452 srv_update_state(srv, version, srv_params);
3453 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003454fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003455 fclose(f);
3456 }
3457}
3458
Simon Horman7d09b9a2013-02-12 10:45:51 +09003459/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003460 * update a server's current IP address.
3461 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3462 * ip is in network format.
3463 * updater is a string which contains an information about the requester of the update.
3464 * updater is used if not NULL.
3465 *
3466 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003467 *
3468 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003469 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003470int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003471{
3472 /* generates a log line and a warning on stderr */
3473 if (1) {
3474 /* book enough space for both IPv4 and IPv6 */
3475 char oldip[INET6_ADDRSTRLEN];
3476 char newip[INET6_ADDRSTRLEN];
3477
3478 memset(oldip, '\0', INET6_ADDRSTRLEN);
3479 memset(newip, '\0', INET6_ADDRSTRLEN);
3480
3481 /* copy old IP address in a string */
3482 switch (s->addr.ss_family) {
3483 case AF_INET:
3484 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3485 break;
3486 case AF_INET6:
3487 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3488 break;
3489 };
3490
3491 /* copy new IP address in a string */
3492 switch (ip_sin_family) {
3493 case AF_INET:
3494 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3495 break;
3496 case AF_INET6:
3497 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3498 break;
3499 };
3500
3501 /* save log line into a buffer */
3502 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3503 s->proxy->id, s->id, oldip, newip, updater);
3504
3505 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003506 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003507
3508 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003509 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003510 }
3511
3512 /* save the new IP family */
3513 s->addr.ss_family = ip_sin_family;
3514 /* save the new IP address */
3515 switch (ip_sin_family) {
3516 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003517 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003518 break;
3519 case AF_INET6:
3520 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3521 break;
3522 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003523 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003524
3525 return 0;
3526}
3527
3528/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003529 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3530 *
3531 * Caller can pass its name through <updater> to get it integrated in the response message
3532 * returned by the function.
3533 *
3534 * The function first does the following, in that order:
3535 * - validates the new addr and/or port
3536 * - checks if an update is required (new IP or port is different than current ones)
3537 * - checks the update is allowed:
3538 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3539 * - allow all changes if no CHECKS are configured
3540 * - if CHECK is configured:
3541 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3542 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3543 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003544 *
3545 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003546 */
3547const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3548{
3549 struct sockaddr_storage sa;
3550 int ret, port_change_required;
3551 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003552 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003553 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003554 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003555
3556 msg = get_trash_chunk();
3557 chunk_reset(msg);
3558
3559 if (addr) {
3560 memset(&sa, 0, sizeof(struct sockaddr_storage));
3561 if (str2ip2(addr, &sa, 0) == NULL) {
3562 chunk_printf(msg, "Invalid addr '%s'", addr);
3563 goto out;
3564 }
3565
3566 /* changes are allowed on AF_INET* families only */
3567 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3568 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3569 goto out;
3570 }
3571
3572 /* collecting data currently setup */
3573 memset(current_addr, '\0', sizeof(current_addr));
3574 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3575 /* changes are allowed on AF_INET* families only */
3576 if ((ret != AF_INET) && (ret != AF_INET6)) {
3577 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3578 goto out;
3579 }
3580
3581 /* applying ADDR changes if required and allowed
3582 * ipcmp returns 0 when both ADDR are the same
3583 */
3584 if (ipcmp(&s->addr, &sa) == 0) {
3585 chunk_appendf(msg, "no need to change the addr");
3586 goto port;
3587 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003588 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003589 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003590
3591 /* we also need to update check's ADDR only if it uses the server's one */
3592 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003593 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003594 }
3595
3596 /* we also need to update agent ADDR only if it use the server's one */
3597 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003598 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003599 }
3600
3601 /* update report for caller */
3602 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3603 }
3604
3605 port:
3606 if (port) {
3607 char sign = '\0';
3608 char *endptr;
3609
3610 if (addr)
3611 chunk_appendf(msg, ", ");
3612
3613 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003614 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003615
3616 /* check if PORT change is required */
3617 port_change_required = 0;
3618
3619 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003620 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003621 new_port = strtol(port, &endptr, 10);
3622 if ((errno != 0) || (port == endptr)) {
3623 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3624 goto out;
3625 }
3626
3627 /* check if caller triggers a port mapped or offset */
3628 if (sign == '-' || (sign == '+')) {
3629 /* check if server currently uses port map */
3630 if (!(s->flags & SRV_F_MAPPORTS)) {
3631 /* switch from fixed port to port map mandatorily triggers
3632 * a port change */
3633 port_change_required = 1;
3634 /* check is configured
3635 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3636 * prevent PORT change if check doesn't have it's dedicated port while switching
3637 * to port mapping */
3638 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3639 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.");
3640 goto out;
3641 }
3642 }
3643 /* we're already using port maps */
3644 else {
3645 port_change_required = current_port != new_port;
3646 }
3647 }
3648 /* fixed port */
3649 else {
3650 port_change_required = current_port != new_port;
3651 }
3652
3653 /* applying PORT changes if required and update response message */
3654 if (port_change_required) {
3655 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003656 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003657 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003658
3659 /* prepare message */
3660 chunk_appendf(msg, "port changed from '");
3661 if (s->flags & SRV_F_MAPPORTS)
3662 chunk_appendf(msg, "+");
3663 chunk_appendf(msg, "%d' to '", current_port);
3664
3665 if (sign == '-') {
3666 s->flags |= SRV_F_MAPPORTS;
3667 chunk_appendf(msg, "%c", sign);
3668 /* just use for result output */
3669 new_port = -new_port;
3670 }
3671 else if (sign == '+') {
3672 s->flags |= SRV_F_MAPPORTS;
3673 chunk_appendf(msg, "%c", sign);
3674 }
3675 else {
3676 s->flags &= ~SRV_F_MAPPORTS;
3677 }
3678
3679 chunk_appendf(msg, "%d'", new_port);
3680
3681 /* we also need to update health checks port only if it uses server's realport */
3682 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3683 s->check.port = new_port;
3684 }
3685 }
3686 else {
3687 chunk_appendf(msg, "no need to change the port");
3688 }
3689 }
3690
3691out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003692 if (changed)
3693 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003694 if (updater)
3695 chunk_appendf(msg, " by '%s'", updater);
3696 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003697 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003698}
3699
3700
3701/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003702 * update server status based on result of name resolution
3703 * returns:
3704 * 0 if server status is updated
3705 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003706 *
3707 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003708 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003709int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003710{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003711 struct dns_resolvers *resolvers = s->resolvers;
3712 struct dns_resolution *resolution = s->dns_requester->resolution;
3713 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003714
3715 switch (resolution->status) {
3716 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003717 /* status when HAProxy has just (re)started.
3718 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003719 break;
3720
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003721 case RSLV_STATUS_VALID:
3722 /*
3723 * resume health checks
3724 * server will be turned back on if health check is safe
3725 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003726 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003727 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003728 return 1;
3729 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3730 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003731 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003732 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003733
Emeric Brun52a91d32017-08-31 14:41:55 +02003734 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003735 return 1;
3736 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3737 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3738 s->proxy->id, s->id);
3739
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003740 ha_warning("%s.\n", trash.area);
3741 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003742 return 0;
3743
3744 case RSLV_STATUS_NX:
3745 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003746 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3747 if (!tick_is_expired(exp, now_ms))
3748 break;
3749
3750 if (s->next_admin & SRV_ADMF_RMAINT)
3751 return 1;
3752 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3753 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003754
3755 case RSLV_STATUS_TIMEOUT:
3756 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003757 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3758 if (!tick_is_expired(exp, now_ms))
3759 break;
3760
3761 if (s->next_admin & SRV_ADMF_RMAINT)
3762 return 1;
3763 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3764 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003765
3766 case RSLV_STATUS_REFUSED:
3767 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003768 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3769 if (!tick_is_expired(exp, now_ms))
3770 break;
3771
3772 if (s->next_admin & SRV_ADMF_RMAINT)
3773 return 1;
3774 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3775 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003776
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003777 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003778 /* stop server if resolution failed for a long enough period */
3779 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3780 if (!tick_is_expired(exp, now_ms))
3781 break;
3782
3783 if (s->next_admin & SRV_ADMF_RMAINT)
3784 return 1;
3785 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3786 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003787 }
3788
3789 return 1;
3790}
3791
3792/*
3793 * Server Name Resolution valid response callback
3794 * It expects:
3795 * - <nameserver>: the name server which answered the valid response
3796 * - <response>: buffer containing a valid DNS response
3797 * - <response_len>: size of <response>
3798 * It performs the following actions:
3799 * - ignore response if current ip found and server family not met
3800 * - update with first new ip found if family is met and current IP is not found
3801 * returns:
3802 * 0 on error
3803 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003804 *
3805 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003806 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003807int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003809 struct server *s = NULL;
3810 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003811 void *serverip, *firstip;
3812 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003813 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003814 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003815 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003816
Christopher Faulet67957bd2017-09-27 11:00:59 +02003817 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003818 if (!s)
3819 return 1;
3820
Christopher Faulet67957bd2017-09-27 11:00:59 +02003821 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003822
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003823 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003824 firstip = NULL; /* pointer to the first valid response found */
3825 /* it will be used as the new IP if a change is required */
3826 firstip_sin_family = AF_UNSPEC;
3827 serverip = NULL; /* current server IP address */
3828
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 /* initializing server IP pointer */
3830 server_sin_family = s->addr.ss_family;
3831 switch (server_sin_family) {
3832 case AF_INET:
3833 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3834 break;
3835
3836 case AF_INET6:
3837 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3838 break;
3839
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003840 case AF_UNSPEC:
3841 break;
3842
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003843 default:
3844 goto invalid;
3845 }
3846
Baptiste Assmann729c9012017-05-22 15:13:10 +02003847 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003848 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003849 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003850
3851 switch (ret) {
3852 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003853 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003854
3855 case DNS_UPD_SRVIP_NOT_FOUND:
3856 goto save_ip;
3857
3858 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003859 goto invalid;
3860
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003861 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003862 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003863 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003864
Baptiste Assmannfad03182015-10-28 02:03:32 +01003865 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003866 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003867 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003868 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003869
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003870 default:
3871 goto invalid;
3872
3873 }
3874
3875 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003876 if (nameserver) {
3877 nameserver->counters.update++;
3878 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003879 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003880 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003881 else
3882 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003883 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003884
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003885 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003886 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003887 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003888
3889 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003890 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003891 nameserver->counters.invalid++;
3892 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003893 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003894 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003895 return 0;
3896}
3897
3898/*
3899 * Server Name Resolution error management callback
3900 * returns:
3901 * 0 on error
3902 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003903 *
3904 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003905 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003906int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003907{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003908 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003909
Christopher Faulet67957bd2017-09-27 11:00:59 +02003910 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003911 if (!s)
3912 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003913 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003914 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003915 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003916 return 1;
3917}
3918
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003919/*
3920 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003921 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003922 * It returns a pointer to the first server found or NULL if <ip> is not yet
3923 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003924 *
3925 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003926 */
3927struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3928{
3929 struct server *tmpsrv;
3930 struct proxy *be;
3931
3932 if (!srv)
3933 return NULL;
3934
3935 be = srv->proxy;
3936 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003937 /* we found the current server is the same, ignore it */
3938 if (srv == tmpsrv)
3939 continue;
3940
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003941 /* We want to compare the IP in the record with the IP of the servers in the
3942 * same backend, only if:
3943 * * DNS resolution is enabled on the server
3944 * * the hostname used for the resolution by our server is the same than the
3945 * one used for the server found in the backend
3946 * * the server found in the backend is not our current server
3947 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003948 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003949 if ((tmpsrv->hostname_dn == NULL) ||
3950 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3951 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003952 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003953 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003954 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003955 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003956
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003957 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003958 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003959 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003960 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003961 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003962
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003963 /* At this point, we have 2 different servers using the same DNS hostname
3964 * for their respective resolution.
3965 */
3966 if (*ip_family == tmpsrv->addr.ss_family &&
3967 ((tmpsrv->addr.ss_family == AF_INET &&
3968 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3969 (tmpsrv->addr.ss_family == AF_INET6 &&
3970 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003971 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003972 return tmpsrv;
3973 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003974 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003975 }
3976
Emeric Brune9fd6b52017-11-02 17:20:39 +01003977
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003978 return NULL;
3979}
3980
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003981/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3982 * resolver. This is suited for initial address configuration. Returns 0 on
3983 * success otherwise a non-zero error code. In case of error, *err_code, if
3984 * not NULL, is filled up.
3985 */
3986int srv_set_addr_via_libc(struct server *srv, int *err_code)
3987{
3988 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003989 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003990 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003991 return 1;
3992 }
3993 return 0;
3994}
3995
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003996/* Set the server's FDQN (->hostname) from <hostname>.
3997 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003998 *
3999 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004000 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004001int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004002{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004003 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004004 char *hostname_dn;
4005 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004006
Frédéric Lécaille5afb3cf2018-08-21 15:04:23 +02004007 /* Note that the server lock is already held. */
4008 if (!srv->resolvers)
4009 return -1;
4010
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004011 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004012 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004013 /* run time DNS resolution was not active for this server
4014 * and we can't enable it at run time for now.
4015 */
4016 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004017 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004018
4019 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004020 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004021 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02004022 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
4023 hostname_dn, trash.size);
4024 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004025 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004026
Christopher Faulet67957bd2017-09-27 11:00:59 +02004027 resolution = srv->dns_requester->resolution;
4028 if (resolution &&
4029 resolution->hostname_dn &&
4030 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02004031 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004032
Christopher Faulet67957bd2017-09-27 11:00:59 +02004033 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004034
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004035 free(srv->hostname);
4036 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02004037 srv->hostname = strdup(hostname);
4038 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004039 srv->hostname_dn_len = hostname_dn_len;
4040 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004041 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004042
Olivier Houchard55dcdf42017-11-06 15:15:04 +01004043 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02004044 goto err;
4045
4046 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004047 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004048 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02004049 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02004050
4051 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004052 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004053 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02004054 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004055}
4056
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004057/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
4058 * from the state file. This is suited for initial address configuration.
4059 * Returns 0 on success otherwise a non-zero error code. In case of error,
4060 * *err_code, if not NULL, is filled up.
4061 */
4062static int srv_apply_lastaddr(struct server *srv, int *err_code)
4063{
4064 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
4065 if (err_code)
4066 *err_code |= ERR_WARN;
4067 return 1;
4068 }
4069 return 0;
4070}
4071
Willy Tarreau25e51522016-11-04 15:10:17 +01004072/* returns 0 if no error, otherwise a combination of ERR_* flags */
4073static int srv_iterate_initaddr(struct server *srv)
4074{
4075 int return_code = 0;
4076 int err_code;
4077 unsigned int methods;
4078
4079 methods = srv->init_addr_methods;
4080 if (!methods) { // default to "last,libc"
4081 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4082 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4083 }
4084
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004085 /* "-dr" : always append "none" so that server addresses resolution
4086 * failures are silently ignored, this is convenient to validate some
4087 * configs out of their environment.
4088 */
4089 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4090 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4091
Willy Tarreau25e51522016-11-04 15:10:17 +01004092 while (methods) {
4093 err_code = 0;
4094 switch (srv_get_next_initaddr(&methods)) {
4095 case SRV_IADDR_LAST:
4096 if (!srv->lastaddr)
4097 continue;
4098 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004099 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004100 return_code |= err_code;
4101 break;
4102
4103 case SRV_IADDR_LIBC:
4104 if (!srv->hostname)
4105 continue;
4106 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004107 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004108 return_code |= err_code;
4109 break;
4110
Willy Tarreau37ebe122016-11-04 15:17:58 +01004111 case SRV_IADDR_NONE:
4112 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004113 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004114 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4115 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004116 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004117 return return_code;
4118
Willy Tarreau4310d362016-11-02 15:05:56 +01004119 case SRV_IADDR_IP:
4120 ipcpy(&srv->init_addr, &srv->addr);
4121 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004122 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4123 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004124 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004125 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004126
Willy Tarreau25e51522016-11-04 15:10:17 +01004127 default: /* unhandled method */
4128 break;
4129 }
4130 }
4131
4132 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004133 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004134 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4135 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004136 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004137 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004138 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4139 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004140
4141 return_code |= ERR_ALERT | ERR_FATAL;
4142 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004143out:
4144 srv_set_dyncookie(srv);
4145 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004146}
4147
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004148/*
4149 * This function parses all backends and all servers within each backend
4150 * and performs servers' addr resolution based on information provided by:
4151 * - configuration file
4152 * - server-state file (states provided by an 'old' haproxy process)
4153 *
4154 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4155 */
4156int srv_init_addr(void)
4157{
4158 struct proxy *curproxy;
4159 int return_code = 0;
4160
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004161 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004162 while (curproxy) {
4163 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004164
4165 /* servers are in backend only */
4166 if (!(curproxy->cap & PR_CAP_BE))
4167 goto srv_init_addr_next;
4168
Willy Tarreau25e51522016-11-04 15:10:17 +01004169 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004170 if (srv->hostname)
4171 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004172
4173 srv_init_addr_next:
4174 curproxy = curproxy->next;
4175 }
4176
4177 return return_code;
4178}
4179
Willy Tarreau46b7f532018-08-21 11:54:26 +02004180/*
4181 * Must be called with the server lock held.
4182 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004183const 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 +02004184{
4185
Willy Tarreau83061a82018-07-13 11:56:34 +02004186 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004187
4188 msg = get_trash_chunk();
4189 chunk_reset(msg);
4190
Olivier Houchard8da5f982017-08-04 18:35:36 +02004191 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004192 chunk_appendf(msg, "no need to change the FDQN");
4193 goto out;
4194 }
4195
4196 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4197 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4198 goto out;
4199 }
4200
4201 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4202 server->proxy->id, server->id, server->hostname, fqdn);
4203
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004204 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004205 chunk_reset(msg);
4206 chunk_appendf(msg, "could not update %s/%s FQDN",
4207 server->proxy->id, server->id);
4208 goto out;
4209 }
4210
4211 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004212 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004213
4214 out:
4215 if (updater)
4216 chunk_appendf(msg, " by '%s'", updater);
4217 chunk_appendf(msg, "\n");
4218
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004219 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004220}
4221
4222
Willy Tarreau21b069d2016-11-23 17:15:08 +01004223/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4224 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004225 * 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 +01004226 * used for CLI commands requiring a server name.
4227 * Important: the <arg> is modified to remove the '/'.
4228 */
4229struct server *cli_find_server(struct appctx *appctx, char *arg)
4230{
4231 struct proxy *px;
4232 struct server *sv;
4233 char *line;
4234
4235 /* split "backend/server" and make <line> point to server */
4236 for (line = arg; *line; line++)
4237 if (*line == '/') {
4238 *line++ = '\0';
4239 break;
4240 }
4241
4242 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004243 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004244 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004245 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004246 return NULL;
4247 }
4248
4249 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004250 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004251 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004252 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004253 return NULL;
4254 }
4255
4256 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004257 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004258 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004259 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004260 return NULL;
4261 }
4262
4263 return sv;
4264}
4265
William Lallemand222baf22016-11-19 02:00:33 +01004266
Willy Tarreau46b7f532018-08-21 11:54:26 +02004267/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004268static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004269{
4270 struct server *sv;
4271 const char *warning;
4272
4273 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4274 return 1;
4275
4276 sv = cli_find_server(appctx, args[2]);
4277 if (!sv)
4278 return 1;
4279
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004280 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004281
William Lallemand222baf22016-11-19 02:00:33 +01004282 if (strcmp(args[3], "weight") == 0) {
4283 warning = server_parse_weight_change_request(sv, args[4]);
4284 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004285 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004286 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004287 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004288 }
4289 }
4290 else if (strcmp(args[3], "state") == 0) {
4291 if (strcmp(args[4], "ready") == 0)
4292 srv_adm_set_ready(sv);
4293 else if (strcmp(args[4], "drain") == 0)
4294 srv_adm_set_drain(sv);
4295 else if (strcmp(args[4], "maint") == 0)
4296 srv_adm_set_maint(sv);
4297 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004298 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004299 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004300 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004301 }
4302 }
4303 else if (strcmp(args[3], "health") == 0) {
4304 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004305 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004306 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004307 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004308 }
4309 else if (strcmp(args[4], "up") == 0) {
4310 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004311 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004312 }
4313 else if (strcmp(args[4], "stopping") == 0) {
4314 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004315 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004316 }
4317 else if (strcmp(args[4], "down") == 0) {
4318 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004319 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004320 }
4321 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004322 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004323 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004324 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004325 }
4326 }
4327 else if (strcmp(args[3], "agent") == 0) {
4328 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004329 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004330 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004331 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004332 }
4333 else if (strcmp(args[4], "up") == 0) {
4334 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004335 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004336 }
4337 else if (strcmp(args[4], "down") == 0) {
4338 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004339 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004340 }
4341 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004342 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004343 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004344 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004345 }
4346 }
Misiek2da082d2017-01-09 09:40:42 +01004347 else if (strcmp(args[3], "agent-addr") == 0) {
4348 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004349 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004350 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4351 appctx->st0 = CLI_ST_PRINT;
4352 } else {
4353 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004354 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004355 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4356 appctx->st0 = CLI_ST_PRINT;
4357 }
4358 }
4359 }
4360 else if (strcmp(args[3], "agent-send") == 0) {
4361 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004362 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004363 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4364 appctx->st0 = CLI_ST_PRINT;
4365 } else {
4366 char *nss = strdup(args[4]);
4367 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004368 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004369 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4370 appctx->st0 = CLI_ST_PRINT;
4371 } else {
4372 free(sv->agent.send_string);
4373 sv->agent.send_string = nss;
4374 sv->agent.send_string_len = strlen(args[4]);
4375 }
4376 }
4377 }
William Lallemand222baf22016-11-19 02:00:33 +01004378 else if (strcmp(args[3], "check-port") == 0) {
4379 int i = 0;
4380 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004381 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004382 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004383 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004384 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004385 }
4386 if ((i < 0) || (i > 65535)) {
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 = "provided port is not valid.\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 /* prevent the update of port to 0 if MAPPORTS are in use */
4393 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004394 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004395 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004396 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004397 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004398 }
4399 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004400 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004401 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004402 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004403 }
4404 else if (strcmp(args[3], "addr") == 0) {
4405 char *addr = NULL;
4406 char *port = NULL;
4407 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004408 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004409 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004410 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004411 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004412 }
4413 else {
4414 addr = args[4];
4415 }
4416 if (strcmp(args[5], "port") == 0) {
4417 port = args[6];
4418 }
4419 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4420 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004421 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004422 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004423 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004424 }
4425 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4426 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004427 else if (strcmp(args[3], "fqdn") == 0) {
4428 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004429 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004430 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4431 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004432 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004433 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004434 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004435 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004436 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004437 appctx->ctx.cli.msg = warning;
4438 appctx->st0 = CLI_ST_PRINT;
4439 }
4440 }
William Lallemand222baf22016-11-19 02:00:33 +01004441 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004442 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004443 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 +01004444 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004445 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004446 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004447 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004448 return 1;
4449}
4450
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004451static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004452{
4453 struct stream_interface *si = appctx->owner;
4454 struct proxy *px;
4455 struct server *sv;
4456 char *line;
4457
4458
4459 /* split "backend/server" and make <line> point to server */
4460 for (line = args[2]; *line; line++)
4461 if (*line == '/') {
4462 *line++ = '\0';
4463 break;
4464 }
4465
4466 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004467 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004468 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004469 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004470 return 1;
4471 }
4472
4473 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004474 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004475 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004476 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004477 return 1;
4478 }
4479
4480 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004481 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4482 sv->iweight);
4483 if (ci_putstr(si_ic(si), trash.area) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004484 si_rx_room_blk(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004485 return 0;
4486 }
William Lallemand6b160942016-11-22 12:34:35 +01004487 return 1;
4488}
4489
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004490/* Parse a "set weight" command.
4491 *
4492 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004493 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004494static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004495{
4496 struct server *sv;
4497 const char *warning;
4498
4499 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4500 return 1;
4501
4502 sv = cli_find_server(appctx, args[2]);
4503 if (!sv)
4504 return 1;
4505
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004506 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4507
William Lallemand6b160942016-11-22 12:34:35 +01004508 warning = server_parse_weight_change_request(sv, args[3]);
4509 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004510 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004511 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004512 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004513 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004514
4515 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4516
William Lallemand6b160942016-11-22 12:34:35 +01004517 return 1;
4518}
4519
Willy Tarreau46b7f532018-08-21 11:54:26 +02004520/* parse a "set maxconn server" command. It always returns 1.
4521 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004522 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004523 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004524static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004525{
4526 struct server *sv;
4527 const char *warning;
4528
4529 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4530 return 1;
4531
4532 sv = cli_find_server(appctx, args[3]);
4533 if (!sv)
4534 return 1;
4535
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004536 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4537
Willy Tarreaub8026272016-11-23 11:26:56 +01004538 warning = server_parse_maxconn_change_request(sv, args[4]);
4539 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004540 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004541 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004542 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004543 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004544
4545 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4546
Willy Tarreaub8026272016-11-23 11:26:56 +01004547 return 1;
4548}
William Lallemand6b160942016-11-22 12:34:35 +01004549
Willy Tarreau46b7f532018-08-21 11:54:26 +02004550/* parse a "disable agent" 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_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +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 Tarreau58d9cb72016-11-24 12:56:01 +01004566 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004567 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004568 return 1;
4569}
4570
Willy Tarreau46b7f532018-08-21 11:54:26 +02004571/* parse a "disable health" 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_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +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 Tarreau2c04eda2016-11-24 12:51:04 +01004587 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004588 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004589 return 1;
4590}
4591
Willy Tarreau46b7f532018-08-21 11:54:26 +02004592/* parse a "disable server" 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_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +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
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004607 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004608 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004609 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004610 return 1;
4611}
4612
Willy Tarreau46b7f532018-08-21 11:54:26 +02004613/* parse a "enable agent" command. It always returns 1.
4614 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004615 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004616 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004617static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004618{
4619 struct server *sv;
4620
4621 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4622 return 1;
4623
4624 sv = cli_find_server(appctx, args[2]);
4625 if (!sv)
4626 return 1;
4627
4628 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004629 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004630 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004631 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004632 return 1;
4633 }
4634
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004635 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004636 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004637 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004638 return 1;
4639}
4640
Willy Tarreau46b7f532018-08-21 11:54:26 +02004641/* parse a "enable health" 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_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +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 Tarreau2c04eda2016-11-24 12:51:04 +01004657 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004658 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004659 return 1;
4660}
4661
Willy Tarreau46b7f532018-08-21 11:54:26 +02004662/* parse a "enable server" command. It always returns 1.
4663 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004664 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004665 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004666static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004667{
4668 struct server *sv;
4669
4670 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4671 return 1;
4672
4673 sv = cli_find_server(appctx, args[2]);
4674 if (!sv)
4675 return 1;
4676
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004677 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004678 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004679 if (!(sv->flags & SRV_F_COOKIESET)
4680 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4681 sv->cookie)
4682 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004683 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004684 return 1;
4685}
4686
William Lallemand222baf22016-11-19 02:00:33 +01004687/* register cli keywords */
4688static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004689 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004690 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004691 { { "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 +01004692 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004693 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004694 { { "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 +01004695 { { "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 +01004696 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004697 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4698 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4699
William Lallemand222baf22016-11-19 02:00:33 +01004700 {{},}
4701}};
4702
4703__attribute__((constructor))
4704static void __server_init(void)
4705{
4706 cli_register_kw(&cli_kws);
4707}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004708
Emeric Brun64cc49c2017-10-03 14:46:45 +02004709/*
4710 * This function applies server's status changes, it is
4711 * is designed to be called asynchronously.
4712 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004713 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004714 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004715static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004716{
4717 struct check *check = &s->check;
4718 int xferred;
4719 struct proxy *px = s->proxy;
4720 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4721 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4722 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004723 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004724
Emeric Brun64cc49c2017-10-03 14:46:45 +02004725 /* If currently main is not set we try to apply pending state changes */
4726 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4727 int next_admin;
4728
4729 /* Backup next admin */
4730 next_admin = s->next_admin;
4731
4732 /* restore current admin state */
4733 s->next_admin = s->cur_admin;
4734
4735 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4736 s->last_change = now.tv_sec;
4737 if (s->proxy->lbprm.set_server_status_down)
4738 s->proxy->lbprm.set_server_status_down(s);
4739
4740 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4741 srv_shutdown_streams(s, SF_ERR_DOWN);
4742
4743 /* we might have streams queued on this server and waiting for
4744 * a connection. Those which are redispatchable will be queued
4745 * to another server or to the proxy itself.
4746 */
4747 xferred = pendconn_redistribute(s);
4748
4749 tmptrash = alloc_trash_chunk();
4750 if (tmptrash) {
4751 chunk_printf(tmptrash,
4752 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4753 s->proxy->id, s->id);
4754
Emeric Brun5a133512017-10-19 14:42:30 +02004755 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004756 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004757
4758 /* we don't send an alert if the server was previously paused */
4759 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004760 send_log(s->proxy, log_level, "%s.\n",
4761 tmptrash->area);
4762 send_email_alert(s, log_level, "%s",
4763 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004764 free_trash_chunk(tmptrash);
4765 tmptrash = NULL;
4766 }
4767 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4768 set_backend_down(s->proxy);
4769
4770 s->counters.down_trans++;
4771 }
4772 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4773 s->last_change = now.tv_sec;
4774 if (s->proxy->lbprm.set_server_status_down)
4775 s->proxy->lbprm.set_server_status_down(s);
4776
4777 /* we might have streams queued on this server and waiting for
4778 * a connection. Those which are redispatchable will be queued
4779 * to another server or to the proxy itself.
4780 */
4781 xferred = pendconn_redistribute(s);
4782
4783 tmptrash = alloc_trash_chunk();
4784 if (tmptrash) {
4785 chunk_printf(tmptrash,
4786 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4787 s->proxy->id, s->id);
4788
Emeric Brun5a133512017-10-19 14:42:30 +02004789 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004790
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004791 ha_warning("%s.\n", tmptrash->area);
4792 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4793 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004794 free_trash_chunk(tmptrash);
4795 tmptrash = NULL;
4796 }
4797
4798 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4799 set_backend_down(s->proxy);
4800 }
4801 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4802 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4803 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4804 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4805 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4806 s->proxy->last_change = now.tv_sec;
4807 }
4808
4809 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4810 s->down_time += now.tv_sec - s->last_change;
4811
4812 s->last_change = now.tv_sec;
4813 if (s->next_state == SRV_ST_STARTING)
4814 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4815
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004816 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004817 /* now propagate the status change to any LB algorithms */
4818 if (px->lbprm.update_server_eweight)
4819 px->lbprm.update_server_eweight(s);
4820 else if (srv_willbe_usable(s)) {
4821 if (px->lbprm.set_server_status_up)
4822 px->lbprm.set_server_status_up(s);
4823 }
4824 else {
4825 if (px->lbprm.set_server_status_down)
4826 px->lbprm.set_server_status_down(s);
4827 }
4828
4829 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4830 * and it's not a backup server and its effective weight is > 0,
4831 * then it can accept new connections, so we shut down all streams
4832 * on all backup servers.
4833 */
4834 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4835 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4836 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4837
4838 /* check if we can handle some connections queued at the proxy. We
4839 * will take as many as we can handle.
4840 */
4841 xferred = pendconn_grab_from_px(s);
4842
4843 tmptrash = alloc_trash_chunk();
4844 if (tmptrash) {
4845 chunk_printf(tmptrash,
4846 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4847 s->proxy->id, s->id);
4848
Emeric Brun5a133512017-10-19 14:42:30 +02004849 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004850 ha_warning("%s.\n", tmptrash->area);
4851 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4852 tmptrash->area);
4853 send_email_alert(s, LOG_NOTICE, "%s",
4854 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004855 free_trash_chunk(tmptrash);
4856 tmptrash = NULL;
4857 }
4858
4859 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4860 set_backend_down(s->proxy);
4861 }
4862 else if (s->cur_eweight != s->next_eweight) {
4863 /* now propagate the status change to any LB algorithms */
4864 if (px->lbprm.update_server_eweight)
4865 px->lbprm.update_server_eweight(s);
4866 else if (srv_willbe_usable(s)) {
4867 if (px->lbprm.set_server_status_up)
4868 px->lbprm.set_server_status_up(s);
4869 }
4870 else {
4871 if (px->lbprm.set_server_status_down)
4872 px->lbprm.set_server_status_down(s);
4873 }
4874
4875 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4876 set_backend_down(s->proxy);
4877 }
4878
4879 s->next_admin = next_admin;
4880 }
4881
Emeric Brun5a133512017-10-19 14:42:30 +02004882 /* reset operational state change */
4883 *s->op_st_chg.reason = 0;
4884 s->op_st_chg.status = s->op_st_chg.code = -1;
4885 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004886
4887 /* Now we try to apply pending admin changes */
4888
4889 /* Maintenance must also disable health checks */
4890 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4891 if (s->check.state & CHK_ST_ENABLED) {
4892 s->check.state |= CHK_ST_PAUSED;
4893 check->health = 0;
4894 }
4895
4896 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004897 tmptrash = alloc_trash_chunk();
4898 if (tmptrash) {
4899 chunk_printf(tmptrash,
4900 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4901 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4902 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004903
Olivier Houchard796a2b32017-10-24 17:42:47 +02004904 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004905
Olivier Houchard796a2b32017-10-24 17:42:47 +02004906 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004907 ha_warning("%s.\n", tmptrash->area);
4908 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4909 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004910 }
4911 free_trash_chunk(tmptrash);
4912 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004913 }
Emeric Brun8f298292017-12-06 16:47:17 +01004914 /* commit new admin status */
4915
4916 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004917 }
4918 else { /* server was still running */
4919 check->health = 0; /* failure */
4920 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004921
4922 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004923 if (s->proxy->lbprm.set_server_status_down)
4924 s->proxy->lbprm.set_server_status_down(s);
4925
Emeric Brun64cc49c2017-10-03 14:46:45 +02004926 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4927 srv_shutdown_streams(s, SF_ERR_DOWN);
4928
4929 /* we might have streams queued on this server and waiting for
4930 * a connection. Those which are redispatchable will be queued
4931 * to another server or to the proxy itself.
4932 */
4933 xferred = pendconn_redistribute(s);
4934
4935 tmptrash = alloc_trash_chunk();
4936 if (tmptrash) {
4937 chunk_printf(tmptrash,
4938 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4939 s->flags & SRV_F_BACKUP ? "Backup " : "",
4940 s->proxy->id, s->id,
4941 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4942
4943 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4944
4945 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004946 ha_warning("%s.\n", tmptrash->area);
4947 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4948 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004949 }
4950 free_trash_chunk(tmptrash);
4951 tmptrash = NULL;
4952 }
4953 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4954 set_backend_down(s->proxy);
4955
4956 s->counters.down_trans++;
4957 }
4958 }
4959 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4960 /* OK here we're leaving maintenance, we have many things to check,
4961 * because the server might possibly be coming back up depending on
4962 * its state. In practice, leaving maintenance means that we should
4963 * immediately turn to UP (more or less the slowstart) under the
4964 * following conditions :
4965 * - server is neither checked nor tracked
4966 * - server tracks another server which is not checked
4967 * - server tracks another server which is already up
4968 * Which sums up as something simpler :
4969 * "either the tracking server is up or the server's checks are disabled
4970 * or up". Otherwise we only re-enable health checks. There's a special
4971 * case associated to the stopping state which can be inherited. Note
4972 * that the server might still be in drain mode, which is naturally dealt
4973 * with by the lower level functions.
4974 */
4975
4976 if (s->check.state & CHK_ST_ENABLED) {
4977 s->check.state &= ~CHK_ST_PAUSED;
4978 check->health = check->rise; /* start OK but check immediately */
4979 }
4980
4981 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4982 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4983 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4984 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4985 s->next_state = SRV_ST_STOPPING;
4986 }
4987 else {
4988 s->next_state = SRV_ST_STARTING;
4989 if (s->slowstart > 0)
4990 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4991 else
4992 s->next_state = SRV_ST_RUNNING;
4993 }
4994
4995 }
4996
4997 tmptrash = alloc_trash_chunk();
4998 if (tmptrash) {
4999 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5000 chunk_printf(tmptrash,
5001 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
5002 s->flags & SRV_F_BACKUP ? "Backup " : "",
5003 s->proxy->id, s->id,
5004 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5005 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5006 }
5007 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5008 chunk_printf(tmptrash,
5009 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
5010 s->flags & SRV_F_BACKUP ? "Backup " : "",
5011 s->proxy->id, s->id, s->hostname,
5012 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5013 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5014 }
5015 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5016 chunk_printf(tmptrash,
5017 "%sServer %s/%s is %s/%s (leaving maintenance)",
5018 s->flags & SRV_F_BACKUP ? "Backup " : "",
5019 s->proxy->id, s->id,
5020 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
5021 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
5022 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005023 ha_warning("%s.\n", tmptrash->area);
5024 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5025 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005026 free_trash_chunk(tmptrash);
5027 tmptrash = NULL;
5028 }
5029
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005030 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005031 /* now propagate the status change to any LB algorithms */
5032 if (px->lbprm.update_server_eweight)
5033 px->lbprm.update_server_eweight(s);
5034 else if (srv_willbe_usable(s)) {
5035 if (px->lbprm.set_server_status_up)
5036 px->lbprm.set_server_status_up(s);
5037 }
5038 else {
5039 if (px->lbprm.set_server_status_down)
5040 px->lbprm.set_server_status_down(s);
5041 }
5042
5043 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5044 set_backend_down(s->proxy);
5045
Willy Tarreau6a78e612018-08-07 10:14:53 +02005046 /* If the server is set with "on-marked-up shutdown-backup-sessions",
5047 * and it's not a backup server and its effective weight is > 0,
5048 * then it can accept new connections, so we shut down all streams
5049 * on all backup servers.
5050 */
5051 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
5052 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
5053 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
5054
5055 /* check if we can handle some connections queued at the proxy. We
5056 * will take as many as we can handle.
5057 */
5058 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005059 }
5060 else if (s->next_admin & SRV_ADMF_MAINT) {
5061 /* remaining in maintenance mode, let's inform precisely about the
5062 * situation.
5063 */
5064 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
5065 tmptrash = alloc_trash_chunk();
5066 if (tmptrash) {
5067 chunk_printf(tmptrash,
5068 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
5069 s->flags & SRV_F_BACKUP ? "Backup " : "",
5070 s->proxy->id, s->id);
5071
5072 if (s->track) /* normally it's mandatory here */
5073 chunk_appendf(tmptrash, " via %s/%s",
5074 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005075 ha_warning("%s.\n", tmptrash->area);
5076 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5077 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005078 free_trash_chunk(tmptrash);
5079 tmptrash = NULL;
5080 }
5081 }
5082 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5083 tmptrash = alloc_trash_chunk();
5084 if (tmptrash) {
5085 chunk_printf(tmptrash,
5086 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5087 s->flags & SRV_F_BACKUP ? "Backup " : "",
5088 s->proxy->id, s->id, s->hostname);
5089
5090 if (s->track) /* normally it's mandatory here */
5091 chunk_appendf(tmptrash, " via %s/%s",
5092 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005093 ha_warning("%s.\n", tmptrash->area);
5094 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5095 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005096 free_trash_chunk(tmptrash);
5097 tmptrash = NULL;
5098 }
5099 }
5100 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5101 tmptrash = alloc_trash_chunk();
5102 if (tmptrash) {
5103 chunk_printf(tmptrash,
5104 "%sServer %s/%s remains in forced maintenance",
5105 s->flags & SRV_F_BACKUP ? "Backup " : "",
5106 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005107 ha_warning("%s.\n", tmptrash->area);
5108 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5109 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005110 free_trash_chunk(tmptrash);
5111 tmptrash = NULL;
5112 }
5113 }
5114 /* don't report anything when leaving drain mode and remaining in maintenance */
5115
5116 s->cur_admin = s->next_admin;
5117 }
5118
5119 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5120 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5121 /* drain state is applied only if not yet in maint */
5122
5123 s->last_change = now.tv_sec;
5124 if (px->lbprm.set_server_status_down)
5125 px->lbprm.set_server_status_down(s);
5126
5127 /* we might have streams queued on this server and waiting for
5128 * a connection. Those which are redispatchable will be queued
5129 * to another server or to the proxy itself.
5130 */
5131 xferred = pendconn_redistribute(s);
5132
5133 tmptrash = alloc_trash_chunk();
5134 if (tmptrash) {
5135 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5136 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5137 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5138
5139 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5140
5141 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005142 ha_warning("%s.\n", tmptrash->area);
5143 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5144 tmptrash->area);
5145 send_email_alert(s, LOG_NOTICE, "%s",
5146 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005147 }
5148 free_trash_chunk(tmptrash);
5149 tmptrash = NULL;
5150 }
5151
5152 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5153 set_backend_down(s->proxy);
5154 }
5155 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5156 /* OK completely leaving drain mode */
5157 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5158 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5159 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5160 s->proxy->last_change = now.tv_sec;
5161 }
5162
5163 if (s->last_change < now.tv_sec) // ignore negative times
5164 s->down_time += now.tv_sec - s->last_change;
5165 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005166 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005167
5168 tmptrash = alloc_trash_chunk();
5169 if (tmptrash) {
5170 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5171 chunk_printf(tmptrash,
5172 "%sServer %s/%s is %s (leaving forced drain)",
5173 s->flags & SRV_F_BACKUP ? "Backup " : "",
5174 s->proxy->id, s->id,
5175 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5176 }
5177 else {
5178 chunk_printf(tmptrash,
5179 "%sServer %s/%s is %s (leaving drain)",
5180 s->flags & SRV_F_BACKUP ? "Backup " : "",
5181 s->proxy->id, s->id,
5182 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5183 if (s->track) /* normally it's mandatory here */
5184 chunk_appendf(tmptrash, " via %s/%s",
5185 s->track->proxy->id, s->track->id);
5186 }
5187
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005188 ha_warning("%s.\n", tmptrash->area);
5189 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5190 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005191 free_trash_chunk(tmptrash);
5192 tmptrash = NULL;
5193 }
5194
5195 /* now propagate the status change to any LB algorithms */
5196 if (px->lbprm.update_server_eweight)
5197 px->lbprm.update_server_eweight(s);
5198 else if (srv_willbe_usable(s)) {
5199 if (px->lbprm.set_server_status_up)
5200 px->lbprm.set_server_status_up(s);
5201 }
5202 else {
5203 if (px->lbprm.set_server_status_down)
5204 px->lbprm.set_server_status_down(s);
5205 }
5206 }
5207 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5208 /* remaining in drain mode after removing one of its flags */
5209
5210 tmptrash = alloc_trash_chunk();
5211 if (tmptrash) {
5212 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5213 chunk_printf(tmptrash,
5214 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5215 s->flags & SRV_F_BACKUP ? "Backup " : "",
5216 s->proxy->id, s->id);
5217
5218 if (s->track) /* normally it's mandatory here */
5219 chunk_appendf(tmptrash, " via %s/%s",
5220 s->track->proxy->id, s->track->id);
5221 }
5222 else {
5223 chunk_printf(tmptrash,
5224 "%sServer %s/%s remains in forced drain mode",
5225 s->flags & SRV_F_BACKUP ? "Backup " : "",
5226 s->proxy->id, s->id);
5227 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005228 ha_warning("%s.\n", tmptrash->area);
5229 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5230 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005231 free_trash_chunk(tmptrash);
5232 tmptrash = NULL;
5233 }
5234
5235 /* commit new admin status */
5236
5237 s->cur_admin = s->next_admin;
5238 }
5239 }
5240
5241 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005242 *s->adm_st_chg_cause = 0;
5243}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005244
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005245/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005246 * Local variables:
5247 * c-indent-level: 8
5248 * c-basic-offset: 8
5249 * End:
5250 */