blob: 78d5a0fcc3000890a133b1706052201b6577dcb0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Server management functions.
3 *
Willy Tarreau21faa912012-10-10 08:27:36 +02004 * Copyright 2000-2012 Willy Tarreau <w@1wt.eu>
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +01005 * Copyright 2007-2008 Krzysztof Piotr Oledzki <ole@ans.pl>
Willy Tarreaubaaee002006-06-26 02:48:02 +02006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 */
13
Willy Tarreau272adea2014-03-31 10:39:59 +020014#include <ctype.h>
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020015#include <errno.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020016
Olivier Houchard4e694042017-03-14 20:01:29 +010017#include <import/xxhash.h>
18
Willy Tarreau272adea2014-03-31 10:39:59 +020019#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreaudff55432012-10-10 17:51:05 +020021#include <common/errors.h>
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +010022#include <common/namespace.h>
Krzysztof Oledzki85130942007-10-22 16:21:10 +020023#include <common/time.h>
24
William Lallemand222baf22016-11-19 02:00:33 +010025#include <types/applet.h>
26#include <types/cli.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020027#include <types/global.h>
Willy Tarreau21b069d2016-11-23 17:15:08 +010028#include <types/cli.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020029#include <types/dns.h>
William Lallemand222baf22016-11-19 02:00:33 +010030#include <types/stats.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020031
William Lallemand222baf22016-11-19 02:00:33 +010032#include <proto/applet.h>
33#include <proto/cli.h>
Simon Hormanb1900d52015-01-30 11:22:54 +090034#include <proto/checks.h>
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +020035#include <proto/connection.h>
Willy Tarreau272adea2014-03-31 10:39:59 +020036#include <proto/port_range.h>
37#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020038#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010039#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020040#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020041#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010042#include <proto/stream_interface.h>
43#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020044#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020045#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010046#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020047
Willy Tarreau3ff577e2018-08-02 11:48:52 +020048static void srv_update_status(struct server *s);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020049static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010051static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Simon Hormana3608442013-11-01 16:46:15 +090058int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020059{
Emeric Brun52a91d32017-08-31 14:41:55 +020060 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061 return s->down_time;
62
63 return now.tv_sec - s->last_change + s->down_time;
64}
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066int srv_lastsession(const struct server *s)
67{
68 if (s->counters.last_sess)
69 return now.tv_sec - s->counters.last_sess;
70
71 return -1;
72}
73
Simon Horman4a741432013-02-23 15:35:38 +090074int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Simon Horman4a741432013-02-23 15:35:38 +090076 const struct server *s = check->server;
77
Willy Tarreauff5ae352013-12-11 20:36:34 +010078 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090079 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085}
86
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010087/*
88 * Check that we did not get a hash collision.
89 * Unlikely, but it can happen.
90 */
91static inline void srv_check_for_dup_dyncookie(struct server *s)
Olivier Houchard4e694042017-03-14 20:01:29 +010092{
93 struct proxy *p = s->proxy;
94 struct server *tmpserv;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +010095
96 for (tmpserv = p->srv; tmpserv != NULL;
97 tmpserv = tmpserv->next) {
98 if (tmpserv == s)
99 continue;
100 if (tmpserv->next_admin & SRV_ADMF_FMAINT)
101 continue;
102 if (tmpserv->cookie &&
103 strcmp(tmpserv->cookie, s->cookie) == 0) {
104 ha_warning("We generated two equal cookies for two different servers.\n"
105 "Please change the secret key for '%s'.\n",
106 s->proxy->id);
107 }
108 }
109
110}
111
Willy Tarreau46b7f532018-08-21 11:54:26 +0200112/*
113 * Must be called with the server lock held.
114 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100115void srv_set_dyncookie(struct server *s)
116{
117 struct proxy *p = s->proxy;
Olivier Houchard4e694042017-03-14 20:01:29 +0100118 char *tmpbuf;
119 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100120 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +0100121 size_t buffer_len;
122 int addr_len;
123 int port;
124
125 if ((s->flags & SRV_F_COOKIESET) ||
126 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
127 s->proxy->dyncookie_key == NULL)
128 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100129 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100130
131 if (s->addr.ss_family != AF_INET &&
132 s->addr.ss_family != AF_INET6)
133 return;
134 /*
135 * Buffer to calculate the cookie value.
136 * The buffer contains the secret key + the server IP address
137 * + the TCP port.
138 */
139 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
140 /*
141 * The TCP port should use only 2 bytes, but is stored in
142 * an unsigned int in struct server, so let's use 4, to be
143 * on the safe side.
144 */
145 buffer_len = key_len + addr_len + 4;
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200146 tmpbuf = trash.area;
Olivier Houchard4e694042017-03-14 20:01:29 +0100147 memcpy(tmpbuf, p->dyncookie_key, key_len);
148 memcpy(&(tmpbuf[key_len]),
149 s->addr.ss_family == AF_INET ?
150 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
151 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
152 addr_len);
153 /*
154 * Make sure it's the same across all the load balancers,
155 * no matter their endianness.
156 */
157 port = htonl(s->svc_port);
158 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
159 hash_value = XXH64(tmpbuf, buffer_len, 0);
160 memprintf(&s->cookie, "%016llx", hash_value);
161 if (!s->cookie)
162 return;
163 s->cklen = 16;
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100164
165 /* Don't bother checking if the dyncookie is duplicated if
166 * the server is marked as "disabled", maybe it doesn't have
167 * its real IP yet, but just a place holder.
Olivier Houchard4e694042017-03-14 20:01:29 +0100168 */
Olivier Houcharde9bad0a2018-01-17 17:39:34 +0100169 if (!(s->next_admin & SRV_ADMF_FMAINT))
170 srv_check_for_dup_dyncookie(s);
Olivier Houchard4e694042017-03-14 20:01:29 +0100171}
172
Willy Tarreau21faa912012-10-10 08:27:36 +0200173/*
174 * Registers the server keyword list <kwl> as a list of valid keywords for next
175 * parsing sessions.
176 */
177void srv_register_keywords(struct srv_kw_list *kwl)
178{
179 LIST_ADDQ(&srv_keywords.list, &kwl->list);
180}
181
182/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
183 * keyword is found with a NULL ->parse() function, then an attempt is made to
184 * find one with a valid ->parse() function. This way it is possible to declare
185 * platform-dependant, known keywords as NULL, then only declare them as valid
186 * if some options are met. Note that if the requested keyword contains an
187 * opening parenthesis, everything from this point is ignored.
188 */
189struct srv_kw *srv_find_kw(const char *kw)
190{
191 int index;
192 const char *kwend;
193 struct srv_kw_list *kwl;
194 struct srv_kw *ret = NULL;
195
196 kwend = strchr(kw, '(');
197 if (!kwend)
198 kwend = kw + strlen(kw);
199
200 list_for_each_entry(kwl, &srv_keywords.list, list) {
201 for (index = 0; kwl->kw[index].kw != NULL; index++) {
202 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
203 kwl->kw[index].kw[kwend-kw] == 0) {
204 if (kwl->kw[index].parse)
205 return &kwl->kw[index]; /* found it !*/
206 else
207 ret = &kwl->kw[index]; /* may be OK */
208 }
209 }
210 }
211 return ret;
212}
213
214/* Dumps all registered "server" keywords to the <out> string pointer. The
215 * unsupported keywords are only dumped if their supported form was not
216 * found.
217 */
218void srv_dump_kws(char **out)
219{
220 struct srv_kw_list *kwl;
221 int index;
222
223 *out = NULL;
224 list_for_each_entry(kwl, &srv_keywords.list, list) {
225 for (index = 0; kwl->kw[index].kw != NULL; index++) {
226 if (kwl->kw[index].parse ||
227 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
228 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
229 kwl->scope,
230 kwl->kw[index].kw,
231 kwl->kw[index].skip ? " <arg>" : "",
232 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
233 kwl->kw[index].parse ? "" : " (not supported)");
234 }
235 }
236 }
237}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100238
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100239/* Parse the "addr" server keyword */
240static int srv_parse_addr(char **args, int *cur_arg,
241 struct proxy *curproxy, struct server *newsrv, char **err)
242{
243 char *errmsg, *arg;
244 struct sockaddr_storage *sk;
245 int port1, port2;
246 struct protocol *proto;
247
248 errmsg = NULL;
249 arg = args[*cur_arg + 1];
250
251 if (!*arg) {
252 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
253 goto err;
254 }
255
256 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
257 if (!sk) {
258 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
259 goto err;
260 }
261
262 proto = protocol_by_family(sk->ss_family);
263 if (!proto || !proto->connect) {
264 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
265 args[*cur_arg], arg);
266 goto err;
267 }
268
269 if (port1 != port2) {
270 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
271 args[*cur_arg], arg);
272 goto err;
273 }
274
275 newsrv->check.addr = newsrv->agent.addr = *sk;
276 newsrv->flags |= SRV_F_CHECKADDR;
277 newsrv->flags |= SRV_F_AGENTADDR;
278
279 return 0;
280
281 err:
282 free(errmsg);
283 return ERR_ALERT | ERR_FATAL;
284}
285
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100286/* Parse the "agent-check" server keyword */
287static int srv_parse_agent_check(char **args, int *cur_arg,
288 struct proxy *curproxy, struct server *newsrv, char **err)
289{
290 newsrv->do_agent = 1;
291 return 0;
292}
293
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100294/* Parse the "backup" server keyword */
295static int srv_parse_backup(char **args, int *cur_arg,
296 struct proxy *curproxy, struct server *newsrv, char **err)
297{
298 newsrv->flags |= SRV_F_BACKUP;
299 return 0;
300}
301
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100302/* Parse the "check" server keyword */
303static int srv_parse_check(char **args, int *cur_arg,
304 struct proxy *curproxy, struct server *newsrv, char **err)
305{
306 newsrv->do_check = 1;
307 return 0;
308}
309
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100310/* Parse the "check-send-proxy" server keyword */
311static int srv_parse_check_send_proxy(char **args, int *cur_arg,
312 struct proxy *curproxy, struct server *newsrv, char **err)
313{
314 newsrv->check.send_proxy = 1;
315 return 0;
316}
317
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100318/* Parse the "cookie" server keyword */
319static int srv_parse_cookie(char **args, int *cur_arg,
320 struct proxy *curproxy, struct server *newsrv, char **err)
321{
322 char *arg;
323
324 arg = args[*cur_arg + 1];
325 if (!*arg) {
326 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
327 return ERR_ALERT | ERR_FATAL;
328 }
329
330 free(newsrv->cookie);
331 newsrv->cookie = strdup(arg);
332 newsrv->cklen = strlen(arg);
333 newsrv->flags |= SRV_F_COOKIESET;
334 return 0;
335}
336
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100337/* Parse the "disabled" server keyword */
338static int srv_parse_disabled(char **args, int *cur_arg,
339 struct proxy *curproxy, struct server *newsrv, char **err)
340{
Emeric Brun52a91d32017-08-31 14:41:55 +0200341 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
342 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100343 newsrv->check.state |= CHK_ST_PAUSED;
344 newsrv->check.health = 0;
345 return 0;
346}
347
348/* Parse the "enabled" server keyword */
349static int srv_parse_enabled(char **args, int *cur_arg,
350 struct proxy *curproxy, struct server *newsrv, char **err)
351{
Emeric Brun52a91d32017-08-31 14:41:55 +0200352 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
353 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100354 newsrv->check.state &= ~CHK_ST_PAUSED;
355 newsrv->check.health = newsrv->check.rise;
356 return 0;
357}
358
Willy Tarreaudff55432012-10-10 17:51:05 +0200359/* parse the "id" server keyword */
360static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
361{
362 struct eb32_node *node;
363
364 if (!*args[*cur_arg + 1]) {
365 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
366 return ERR_ALERT | ERR_FATAL;
367 }
368
369 newsrv->puid = atol(args[*cur_arg + 1]);
370 newsrv->conf.id.key = newsrv->puid;
371
372 if (newsrv->puid <= 0) {
373 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
374 return ERR_ALERT | ERR_FATAL;
375 }
376
377 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
378 if (node) {
379 struct server *target = container_of(node, struct server, conf.id);
380 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
381 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
382 target->id);
383 return ERR_ALERT | ERR_FATAL;
384 }
385
386 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200387 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200388 return 0;
389}
390
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100391/* Parse the "namespace" server keyword */
392static int srv_parse_namespace(char **args, int *cur_arg,
393 struct proxy *curproxy, struct server *newsrv, char **err)
394{
395#ifdef CONFIG_HAP_NS
396 char *arg;
397
398 arg = args[*cur_arg + 1];
399 if (!*arg) {
400 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
401 return ERR_ALERT | ERR_FATAL;
402 }
403
404 if (!strcmp(arg, "*")) {
405 /* Use the namespace associated with the connection (if present). */
406 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
407 return 0;
408 }
409
410 /*
411 * As this parser may be called several times for the same 'default-server'
412 * object, or for a new 'server' instance deriving from a 'default-server'
413 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
414 */
415 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
416
417 newsrv->netns = netns_store_lookup(arg, strlen(arg));
418 if (!newsrv->netns)
419 newsrv->netns = netns_store_insert(arg);
420
421 if (!newsrv->netns) {
422 memprintf(err, "Cannot open namespace '%s'", arg);
423 return ERR_ALERT | ERR_FATAL;
424 }
425
426 return 0;
427#else
428 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
429 return ERR_ALERT | ERR_FATAL;
430#endif
431}
432
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100433/* Parse the "no-agent-check" server keyword */
434static int srv_parse_no_agent_check(char **args, int *cur_arg,
435 struct proxy *curproxy, struct server *newsrv, char **err)
436{
437 free_check(&newsrv->agent);
438 newsrv->agent.inter = 0;
439 newsrv->agent.port = 0;
440 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
441 newsrv->do_agent = 0;
442 return 0;
443}
444
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100445/* Parse the "no-backup" server keyword */
446static int srv_parse_no_backup(char **args, int *cur_arg,
447 struct proxy *curproxy, struct server *newsrv, char **err)
448{
449 newsrv->flags &= ~SRV_F_BACKUP;
450 return 0;
451}
452
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100453/* Parse the "no-check" server keyword */
454static int srv_parse_no_check(char **args, int *cur_arg,
455 struct proxy *curproxy, struct server *newsrv, char **err)
456{
457 free_check(&newsrv->check);
458 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
459 newsrv->do_check = 0;
460 return 0;
461}
462
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100463/* Parse the "no-check-send-proxy" server keyword */
464static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
465 struct proxy *curproxy, struct server *newsrv, char **err)
466{
467 newsrv->check.send_proxy = 0;
468 return 0;
469}
470
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100471/* Disable server PROXY protocol flags. */
472static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
473{
474 srv->pp_opts &= ~flags;
475 return 0;
476}
477
478/* Parse the "no-send-proxy" server keyword */
479static int srv_parse_no_send_proxy(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
482 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
483}
484
485/* Parse the "no-send-proxy-v2" server keyword */
486static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
487 struct proxy *curproxy, struct server *newsrv, char **err)
488{
489 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
490}
491
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100492/* Parse the "non-stick" server keyword */
493static int srv_parse_non_stick(char **args, int *cur_arg,
494 struct proxy *curproxy, struct server *newsrv, char **err)
495{
496 newsrv->flags |= SRV_F_NON_STICK;
497 return 0;
498}
499
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100500/* Enable server PROXY protocol flags. */
501static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
502{
503 srv->pp_opts |= flags;
504 return 0;
505}
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +0200506/* parse the "proto" server keyword */
507static int srv_parse_proto(char **args, int *cur_arg,
508 struct proxy *px, struct server *newsrv, char **err)
509{
510 struct ist proto;
511
512 if (!*args[*cur_arg + 1]) {
513 memprintf(err, "'%s' : missing value", args[*cur_arg]);
514 return ERR_ALERT | ERR_FATAL;
515 }
516 proto = ist2(args[*cur_arg + 1], strlen(args[*cur_arg + 1]));
517 newsrv->mux_proto = get_mux_proto(proto);
518 if (!newsrv->mux_proto) {
519 memprintf(err, "'%s' : unknown MUX protocol '%s'", args[*cur_arg], args[*cur_arg+1]);
520 return ERR_ALERT | ERR_FATAL;
521 }
522 else if (!(newsrv->mux_proto->side & PROTO_SIDE_BE)) {
523 memprintf(err, "'%s' : MUX protocol '%s' cannot be used for outgoing connections",
524 args[*cur_arg], args[*cur_arg+1]);
525 return ERR_ALERT | ERR_FATAL;
526 }
527 return 0;
528}
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100529
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100530/* parse the "proxy-v2-options" */
531static int srv_parse_proxy_v2_options(char **args, int *cur_arg,
532 struct proxy *px, struct server *newsrv, char **err)
533{
534 char *p, *n;
535 for (p = args[*cur_arg+1]; p; p = n) {
536 n = strchr(p, ',');
537 if (n)
538 *n++ = '\0';
539 if (!strcmp(p, "ssl")) {
540 newsrv->pp_opts |= SRV_PP_V2_SSL;
541 } else if (!strcmp(p, "cert-cn")) {
542 newsrv->pp_opts |= SRV_PP_V2_SSL;
543 newsrv->pp_opts |= SRV_PP_V2_SSL_CN;
Emmanuel Hocdetfa8d0f12018-02-01 15:53:52 +0100544 } else if (!strcmp(p, "cert-key")) {
545 newsrv->pp_opts |= SRV_PP_V2_SSL;
546 newsrv->pp_opts |= SRV_PP_V2_SSL_KEY_ALG;
547 } else if (!strcmp(p, "cert-sig")) {
548 newsrv->pp_opts |= SRV_PP_V2_SSL;
549 newsrv->pp_opts |= SRV_PP_V2_SSL_SIG_ALG;
550 } else if (!strcmp(p, "ssl-cipher")) {
551 newsrv->pp_opts |= SRV_PP_V2_SSL;
552 newsrv->pp_opts |= SRV_PP_V2_SSL_CIPHER;
Emmanuel Hocdet253c3b72018-02-01 18:29:59 +0100553 } else if (!strcmp(p, "authority")) {
554 newsrv->pp_opts |= SRV_PP_V2_AUTHORITY;
Emmanuel Hocdet4399c752018-02-05 15:26:43 +0100555 } else if (!strcmp(p, "crc32c")) {
556 newsrv->pp_opts |= SRV_PP_V2_CRC32C;
Emmanuel Hocdetf643b802018-02-01 15:20:32 +0100557 } else
558 goto fail;
559 }
560 return 0;
561 fail:
562 if (err)
563 memprintf(err, "'%s' : proxy v2 option not implemented", p);
564 return ERR_ALERT | ERR_FATAL;
565}
566
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100567/* Parse the "observe" server keyword */
568static int srv_parse_observe(char **args, int *cur_arg,
569 struct proxy *curproxy, struct server *newsrv, char **err)
570{
571 char *arg;
572
573 arg = args[*cur_arg + 1];
574 if (!*arg) {
575 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
576 return ERR_ALERT | ERR_FATAL;
577 }
578
579 if (!strcmp(arg, "none")) {
580 newsrv->observe = HANA_OBS_NONE;
581 }
582 else if (!strcmp(arg, "layer4")) {
583 newsrv->observe = HANA_OBS_LAYER4;
584 }
585 else if (!strcmp(arg, "layer7")) {
586 if (curproxy->mode != PR_MODE_HTTP) {
587 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
588 return ERR_ALERT;
589 }
590 newsrv->observe = HANA_OBS_LAYER7;
591 }
592 else {
593 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
594 "but got '%s'\n", args[*cur_arg], arg);
595 return ERR_ALERT | ERR_FATAL;
596 }
597
598 return 0;
599}
600
Frédéric Lécaille16186232017-03-14 16:42:49 +0100601/* Parse the "redir" server keyword */
602static int srv_parse_redir(char **args, int *cur_arg,
603 struct proxy *curproxy, struct server *newsrv, char **err)
604{
605 char *arg;
606
607 arg = args[*cur_arg + 1];
608 if (!*arg) {
609 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
610 return ERR_ALERT | ERR_FATAL;
611 }
612
613 free(newsrv->rdr_pfx);
614 newsrv->rdr_pfx = strdup(arg);
615 newsrv->rdr_len = strlen(arg);
616
617 return 0;
618}
619
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100620/* Parse the "send-proxy" server keyword */
621static int srv_parse_send_proxy(char **args, int *cur_arg,
622 struct proxy *curproxy, struct server *newsrv, char **err)
623{
624 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
625}
626
627/* Parse the "send-proxy-v2" server keyword */
628static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
629 struct proxy *curproxy, struct server *newsrv, char **err)
630{
631 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
632}
633
Frédéric Lécailledba97072017-03-17 15:33:50 +0100634
635/* Parse the "source" server keyword */
636static int srv_parse_source(char **args, int *cur_arg,
637 struct proxy *curproxy, struct server *newsrv, char **err)
638{
639 char *errmsg;
640 int port_low, port_high;
641 struct sockaddr_storage *sk;
642 struct protocol *proto;
643
644 errmsg = NULL;
645
646 if (!*args[*cur_arg + 1]) {
647 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
648 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
649 goto err;
650 }
651
652 /* 'sk' is statically allocated (no need to be freed). */
653 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
654 if (!sk) {
655 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
656 goto err;
657 }
658
659 proto = protocol_by_family(sk->ss_family);
660 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100661 ha_alert("'%s %s' : connect() not supported for this address family.\n",
662 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100663 goto err;
664 }
665
666 newsrv->conn_src.opts |= CO_SRC_BIND;
667 newsrv->conn_src.source_addr = *sk;
668
669 if (port_low != port_high) {
670 int i;
671
672 if (!port_low || !port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100673 ha_alert("'%s' does not support port offsets (found '%s').\n",
674 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100675 goto err;
676 }
677
678 if (port_low <= 0 || port_low > 65535 ||
679 port_high <= 0 || port_high > 65535 ||
680 port_low > port_high) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100681 ha_alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100682 goto err;
683 }
684 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
685 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
686 newsrv->conn_src.sport_range->ports[i] = port_low + i;
687 }
688
689 *cur_arg += 2;
690 while (*(args[*cur_arg])) {
691 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
692#if defined(CONFIG_HAP_TRANSPARENT)
693 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100694 ha_alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
695 "or 'hdr_ip(name,#)' as argument.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100696 goto err;
697 }
698 if (!strcmp(args[*cur_arg + 1], "client")) {
699 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
700 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
701 }
702 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
703 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
704 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
705 }
706 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
707 char *name, *end;
708
709 name = args[*cur_arg + 1] + 7;
710 while (isspace(*name))
711 name++;
712
713 end = name;
714 while (*end && !isspace(*end) && *end != ',' && *end != ')')
715 end++;
716
717 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
718 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
719 free(newsrv->conn_src.bind_hdr_name);
720 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
721 newsrv->conn_src.bind_hdr_len = end - name;
722 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
723 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
724 newsrv->conn_src.bind_hdr_occ = -1;
725
726 /* now look for an occurrence number */
727 while (isspace(*end))
728 end++;
729 if (*end == ',') {
730 end++;
731 name = end;
732 if (*end == '-')
733 end++;
734 while (isdigit((int)*end))
735 end++;
736 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
737 }
738
739 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100740 ha_alert("usesrc hdr_ip(name,num) does not support negative"
741 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100742 goto err;
743 }
744 }
745 else {
746 struct sockaddr_storage *sk;
747 int port1, port2;
748
749 /* 'sk' is statically allocated (no need to be freed). */
750 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
751 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100752 ha_alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100753 goto err;
754 }
755
756 proto = protocol_by_family(sk->ss_family);
757 if (!proto || !proto->connect) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100758 ha_alert("'%s %s' : connect() not supported for this address family.\n",
759 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760 goto err;
761 }
762
763 if (port1 != port2) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100764 ha_alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
765 args[*cur_arg], args[*cur_arg + 1]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100766 goto err;
767 }
768 newsrv->conn_src.tproxy_addr = *sk;
769 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
770 }
771 global.last_checks |= LSTCHK_NETADM;
772 *cur_arg += 2;
773 continue;
774#else /* no TPROXY support */
Christopher Faulet767a84b2017-11-24 16:50:31 +0100775 ha_alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
Frédéric Lécailledba97072017-03-17 15:33:50 +0100776 goto err;
777#endif /* defined(CONFIG_HAP_TRANSPARENT) */
778 } /* "usesrc" */
779
780 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
781#ifdef SO_BINDTODEVICE
782 if (!*args[*cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +0100783 ha_alert("'%s' : missing interface name.\n", args[0]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100784 goto err;
785 }
786 free(newsrv->conn_src.iface_name);
787 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
788 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
789 global.last_checks |= LSTCHK_NETADM;
790#else
Christopher Faulet767a84b2017-11-24 16:50:31 +0100791 ha_alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
Frédéric Lécailledba97072017-03-17 15:33:50 +0100792 goto err;
793#endif
794 *cur_arg += 2;
795 continue;
796 }
797 /* this keyword in not an option of "source" */
798 break;
799 } /* while */
800
801 return 0;
802
803 err:
804 free(errmsg);
805 return ERR_ALERT | ERR_FATAL;
806}
807
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100808/* Parse the "stick" server keyword */
809static int srv_parse_stick(char **args, int *cur_arg,
810 struct proxy *curproxy, struct server *newsrv, char **err)
811{
812 newsrv->flags &= ~SRV_F_NON_STICK;
813 return 0;
814}
815
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100816/* Parse the "track" server keyword */
817static int srv_parse_track(char **args, int *cur_arg,
818 struct proxy *curproxy, struct server *newsrv, char **err)
819{
820 char *arg;
821
822 arg = args[*cur_arg + 1];
823 if (!*arg) {
824 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
825 return ERR_ALERT | ERR_FATAL;
826 }
827
828 free(newsrv->trackit);
829 newsrv->trackit = strdup(arg);
830
831 return 0;
832}
833
Frédéric Lécailledba97072017-03-17 15:33:50 +0100834
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200835/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200836 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200837 * shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200838 *
839 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200840 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200841void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200842{
Willy Tarreau87b09662015-04-03 00:22:06 +0200843 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200844
Willy Tarreau87b09662015-04-03 00:22:06 +0200845 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
846 if (stream->srv_conn == srv)
847 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200848}
849
850/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200851 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200852 * the reason for the shutdown.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200853 *
854 * Must be called with the server lock held.
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200855 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200856void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200857{
858 struct server *srv;
859
860 for (srv = px->srv; srv != NULL; srv = srv->next)
861 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200862 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200863}
864
Willy Tarreaubda92272014-05-20 21:55:30 +0200865/* Appends some information to a message string related to a server going UP or
866 * DOWN. If both <forced> and <reason> are null and the server tracks another
867 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200868 * If <check> is non-null, an entire string describing the check result will be
869 * appended after a comma and a space (eg: to report some information from the
870 * check that changed the state). In the other case, the string will be built
871 * using the check results stored into the struct server if present.
872 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200873 * provided.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200874 *
875 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200876 */
Willy Tarreau83061a82018-07-13 11:56:34 +0200877void srv_append_status(struct buffer *msg, struct server *s,
878 struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200879{
Emeric Brun5a133512017-10-19 14:42:30 +0200880 short status = s->op_st_chg.status;
881 short code = s->op_st_chg.code;
882 long duration = s->op_st_chg.duration;
883 char *desc = s->op_st_chg.reason;
884
885 if (check) {
886 status = check->status;
887 code = check->code;
888 duration = check->duration;
889 desc = check->desc;
890 }
891
892 if (status != -1) {
893 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
894
895 if (status >= HCHK_STATUS_L57DATA)
896 chunk_appendf(msg, ", code: %d", code);
897
898 if (desc && *desc) {
Willy Tarreau83061a82018-07-13 11:56:34 +0200899 struct buffer src;
Emeric Brun5a133512017-10-19 14:42:30 +0200900
901 chunk_appendf(msg, ", info: \"");
902
903 chunk_initlen(&src, desc, 0, strlen(desc));
904 chunk_asciiencode(msg, &src, '"');
905
906 chunk_appendf(msg, "\"");
907 }
908
909 if (duration >= 0)
910 chunk_appendf(msg, ", check duration: %ldms", duration);
911 }
912 else if (desc && *desc) {
913 chunk_appendf(msg, ", %s", desc);
914 }
915 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200916 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200917 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200918
919 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200920 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200921 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
922 " %d sessions active, %d requeued, %d remaining in queue",
923 s->proxy->srv_act, s->proxy->srv_bck,
924 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
925 s->cur_sess, xferred, s->nbpend);
926 else
927 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
928 " %d sessions requeued, %d total in queue",
929 s->proxy->srv_act, s->proxy->srv_bck,
930 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
931 xferred, s->nbpend);
932 }
933}
934
Emeric Brun5a133512017-10-19 14:42:30 +0200935/* Marks server <s> down, regardless of its checks' statuses. The server is
936 * registered in a list to postpone the counting of the remaining servers on
937 * the proxy and transfers queued streams whenever possible to other servers at
938 * a sync point. Maintenance servers are ignored. It stores the <reason> if
939 * non-null as the reason for going down or the available data from the check
940 * struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200941 *
942 * Must be called with the server lock held.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200943 */
Emeric Brun5a133512017-10-19 14:42:30 +0200944void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200945{
946 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200947
Emeric Brun64cc49c2017-10-03 14:46:45 +0200948 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200949 return;
950
Emeric Brun52a91d32017-08-31 14:41:55 +0200951 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200952 *s->op_st_chg.reason = 0;
953 s->op_st_chg.status = -1;
954 if (reason) {
955 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
956 }
957 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +0100958 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +0200959 s->op_st_chg.code = check->code;
960 s->op_st_chg.status = check->status;
961 s->op_st_chg.duration = check->duration;
962 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200963
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200964 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200965 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +0200966
Emeric Brun9f0b4582017-10-23 14:39:51 +0200967 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100968 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200969 srv_set_stopped(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100970 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200971 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200972}
973
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200974/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200975 * in maintenance. The server is registered in a list to postpone the counting
976 * of the remaining servers on the proxy and tries to grab requests from the
977 * proxy at a sync point. Maintenance servers are ignored. It stores the
978 * <reason> if non-null as the reason for going down or the available data
979 * from the check struct to recompute this reason later.
Willy Tarreau46b7f532018-08-21 11:54:26 +0200980 *
981 * Must be called with the server lock held.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200982 */
Emeric Brun5a133512017-10-19 14:42:30 +0200983void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200984{
985 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200986
Emeric Brun64cc49c2017-10-03 14:46:45 +0200987 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200988 return;
989
Emeric Brun52a91d32017-08-31 14:41:55 +0200990 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200991 return;
992
Emeric Brun52a91d32017-08-31 14:41:55 +0200993 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200994 *s->op_st_chg.reason = 0;
995 s->op_st_chg.status = -1;
996 if (reason) {
997 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
998 }
999 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001000 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001001 s->op_st_chg.code = check->code;
1002 s->op_st_chg.status = check->status;
1003 s->op_st_chg.duration = check->duration;
1004 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001005
Emeric Brun64cc49c2017-10-03 14:46:45 +02001006 if (s->slowstart <= 0)
1007 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001008
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001009 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001010 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001011
Emeric Brun9f0b4582017-10-23 14:39:51 +02001012 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001013 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001014 srv_set_running(srv, NULL, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001015 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001016 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001017}
1018
Willy Tarreau8eb77842014-05-21 13:54:57 +02001019/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +02001020 * isn't in maintenance. The server is registered in a list to postpone the
1021 * counting of the remaining servers on the proxy and tries to grab requests
1022 * from the proxy. Maintenance servers are ignored. It stores the
1023 * <reason> if non-null as the reason for going down or the available data
1024 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001025 * up. Note that it makes use of the trash to build the log strings, so <reason>
1026 * must not be placed there.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001027 *
1028 * Must be called with the server lock held.
Willy Tarreau8eb77842014-05-21 13:54:57 +02001029 */
Emeric Brun5a133512017-10-19 14:42:30 +02001030void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001031{
1032 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +02001033
Emeric Brun64cc49c2017-10-03 14:46:45 +02001034 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001035 return;
1036
Emeric Brun52a91d32017-08-31 14:41:55 +02001037 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +02001038 return;
1039
Emeric Brun52a91d32017-08-31 14:41:55 +02001040 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +02001041 *s->op_st_chg.reason = 0;
1042 s->op_st_chg.status = -1;
1043 if (reason) {
1044 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
1045 }
1046 else if (check) {
Willy Tarreau358847f2017-11-20 21:33:21 +01001047 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
Emeric Brun5a133512017-10-19 14:42:30 +02001048 s->op_st_chg.code = check->code;
1049 s->op_st_chg.status = check->status;
1050 s->op_st_chg.duration = check->duration;
1051 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001052
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001053 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001054 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001055
Emeric Brun9f0b4582017-10-23 14:39:51 +02001056 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001057 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +02001058 srv_set_stopping(srv, NULL, NULL);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001059 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001060 }
Willy Tarreau8eb77842014-05-21 13:54:57 +02001061}
Willy Tarreaudbd5e782014-05-20 22:46:35 +02001062
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001063/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1064 * enforce either maint mode or drain mode. It is not allowed to set more than
1065 * one flag at once. The equivalent "inherited" flag is propagated to all
1066 * tracking servers. Maintenance mode disables health checks (but not agent
1067 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +01001068 * is done. If <cause> is non-null, it will be displayed at the end of the log
1069 * lines to justify the state change.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001070 *
1071 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001072 */
Willy Tarreau8b428482016-11-07 15:53:43 +01001073void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001074{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001075 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001076
1077 if (!mode)
1078 return;
1079
1080 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001081 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001082 return;
1083
Emeric Brun52a91d32017-08-31 14:41:55 +02001084 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +02001085 if (cause)
1086 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
1087
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001088 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001089 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001090
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001091 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001092 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1093 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001094 return;
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001095
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001096 /* compute the inherited flag to propagate */
1097 if (mode & SRV_ADMF_MAINT)
1098 mode = SRV_ADMF_IMAINT;
1099 else if (mode & SRV_ADMF_DRAIN)
1100 mode = SRV_ADMF_IDRAIN;
1101
Emeric Brun9f0b4582017-10-23 14:39:51 +02001102 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001103 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001104 srv_set_admin_flag(srv, mode, cause);
Christopher Faulet8d01fd62018-01-24 21:49:41 +01001105 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001106 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001107}
1108
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001109/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1110 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1111 * than one flag at once. The equivalent "inherited" flag is propagated to all
1112 * tracking servers. Leaving maintenance mode re-enables health checks. When
1113 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001114 *
1115 * Must be called with the server lock held.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001116 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001117void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001118{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001119 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001120
1121 if (!mode)
1122 return;
1123
1124 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001125 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001126 return;
1127
Emeric Brun52a91d32017-08-31 14:41:55 +02001128 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001129
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001130 /* propagate changes */
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001131 srv_update_status(s);
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001132
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001133 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1135 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaueeba36b2018-08-21 08:22:26 +02001136 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001137
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001138 if (mode & SRV_ADMF_MAINT)
1139 mode = SRV_ADMF_IMAINT;
1140 else if (mode & SRV_ADMF_DRAIN)
1141 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001142
Emeric Brun9f0b4582017-10-23 14:39:51 +02001143 for (srv = s->trackers; srv; srv = srv->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001144 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001145 srv_clr_admin_flag(srv, mode);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001146 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001147 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001148}
1149
Willy Tarreau757478e2016-11-03 19:22:19 +01001150/* principle: propagate maint and drain to tracking servers. This is useful
1151 * upon startup so that inherited states are correct.
1152 */
1153static void srv_propagate_admin_state(struct server *srv)
1154{
1155 struct server *srv2;
1156
1157 if (!srv->trackers)
1158 return;
1159
1160 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001161 HA_SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001162 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001163 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001164
Emeric Brun52a91d32017-08-31 14:41:55 +02001165 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001166 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001167 HA_SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001168 }
1169}
1170
1171/* Compute and propagate the admin states for all servers in proxy <px>.
1172 * Only servers *not* tracking another one are considered, because other
1173 * ones will be handled when the server they track is visited.
1174 */
1175void srv_compute_all_admin_states(struct proxy *px)
1176{
1177 struct server *srv;
1178
1179 for (srv = px->srv; srv; srv = srv->next) {
1180 if (srv->track)
1181 continue;
1182 srv_propagate_admin_state(srv);
1183 }
1184}
1185
Willy Tarreaudff55432012-10-10 17:51:05 +02001186/* Note: must not be declared <const> as its list will be overwritten.
1187 * Please take care of keeping this list alphabetically sorted, doing so helps
1188 * all code contributors.
1189 * Optional keywords are also declared with a NULL ->parse() function so that
1190 * the config parser can report an appropriate error when a known keyword was
1191 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001192 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001193 */
1194static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001195 { "addr", srv_parse_addr, 1, 1 }, /* IP address to send health to or to probe from agent-check */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001196 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001197 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001198 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001199 { "check-send-proxy", srv_parse_check_send_proxy, 0, 1 }, /* enable PROXY protocol for health checks */
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +01001200 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001201 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1202 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001203 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001204 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001205 { "no-agent-check", srv_parse_no_agent_check, 0, 1 }, /* Do not enable any auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001206 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001207 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001208 { "no-check-send-proxy", srv_parse_no_check_send_proxy, 0, 1 }, /* disable PROXY protol for health checks */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001209 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1210 { "no-send-proxy-v2", srv_parse_no_send_proxy_v2, 0, 1 }, /* Disable use of PROXY V2 protocol */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001211 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001212 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001213 { "proto", srv_parse_proto, 1, 1 }, /* Set the proto to use for all outgoing connections */
Emmanuel Hocdetf643b802018-02-01 15:20:32 +01001214 { "proxy-v2-options", srv_parse_proxy_v2_options, 1, 1 }, /* options for send-proxy-v2 */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001215 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001216 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1217 { "send-proxy-v2", srv_parse_send_proxy_v2, 0, 1 }, /* Enforce use of PROXY V2 protocol */
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001218 { "source", srv_parse_source, -1, 1 }, /* Set the source address to be used to connect to the server */
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +01001219 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001220 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001221 { NULL, NULL, 0 },
1222}};
1223
1224__attribute__((constructor))
1225static void __listener_init(void)
1226{
1227 srv_register_keywords(&srv_kws);
1228}
1229
Willy Tarreau004e0452013-11-21 11:22:01 +01001230/* Recomputes the server's eweight based on its state, uweight, the current time,
1231 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001232 * state is automatically disabled if the time is elapsed. If <must_update> is
1233 * not zero, the update will be propagated immediately.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001234 *
1235 * Must be called with the server lock held.
Willy Tarreau004e0452013-11-21 11:22:01 +01001236 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001237void server_recalc_eweight(struct server *sv, int must_update)
Willy Tarreau004e0452013-11-21 11:22:01 +01001238{
1239 struct proxy *px = sv->proxy;
1240 unsigned w;
1241
1242 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1243 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001244 if (sv->next_state == SRV_ST_STARTING)
1245 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001246 }
1247
1248 /* We must take care of not pushing the server to full throttle during slow starts.
1249 * It must also start immediately, at least at the minimal step when leaving maintenance.
1250 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001251 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001252 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1253 else
1254 w = px->lbprm.wdiv;
1255
Emeric Brun52a91d32017-08-31 14:41:55 +02001256 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001257
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001258 /* propagate changes only if needed (i.e. not recursively) */
Willy Tarreau49725a02018-08-21 19:54:09 +02001259 if (must_update)
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001260 srv_update_status(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001261}
1262
Willy Tarreaubaaee002006-06-26 02:48:02 +02001263/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001264 * Parses weight_str and configures sv accordingly.
1265 * Returns NULL on success, error message string otherwise.
Willy Tarreau46b7f532018-08-21 11:54:26 +02001266 *
1267 * Must be called with the server lock held.
Simon Horman7d09b9a2013-02-12 10:45:51 +09001268 */
1269const char *server_parse_weight_change_request(struct server *sv,
1270 const char *weight_str)
1271{
1272 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001273 long int w;
1274 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001275
1276 px = sv->proxy;
1277
1278 /* if the weight is terminated with '%', it is set relative to
1279 * the initial weight, otherwise it is absolute.
1280 */
1281 if (!*weight_str)
1282 return "Require <weight> or <weight%>.\n";
1283
Simon Hormanb796afa2013-02-12 10:45:53 +09001284 w = strtol(weight_str, &end, 10);
1285 if (end == weight_str)
1286 return "Empty weight string empty or preceded by garbage";
1287 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001288 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001289 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001290 /* Avoid integer overflow */
1291 if (w > 25600)
1292 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001293 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001294 if (w > 256)
1295 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001296 }
1297 else if (w < 0 || w > 256)
1298 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001299 else if (end[0] != '\0')
1300 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001301
1302 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1303 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1304
1305 sv->uweight = w;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02001306 server_recalc_eweight(sv, 1);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001307
1308 return NULL;
1309}
1310
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001311/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001312 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1313 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001314 * Returns:
1315 * - error string on error
1316 * - NULL on success
Willy Tarreau46b7f532018-08-21 11:54:26 +02001317 *
1318 * Must be called with the server lock held.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001319 */
1320const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001321 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001322{
1323 unsigned char ip[INET6_ADDRSTRLEN];
1324
1325 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001326 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001327 return NULL;
1328 }
1329 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001330 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001331 return NULL;
1332 }
1333
1334 return "Could not understand IP address format.\n";
1335}
1336
Willy Tarreau46b7f532018-08-21 11:54:26 +02001337/*
1338 * Must be called with the server lock held.
1339 */
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001340const char *server_parse_maxconn_change_request(struct server *sv,
1341 const char *maxconn_str)
1342{
1343 long int v;
1344 char *end;
1345
1346 if (!*maxconn_str)
1347 return "Require <maxconn>.\n";
1348
1349 v = strtol(maxconn_str, &end, 10);
1350 if (end == maxconn_str)
1351 return "maxconn string empty or preceded by garbage";
1352 else if (end[0] != '\0')
1353 return "Trailing garbage in maxconn string";
1354
1355 if (sv->maxconn == sv->minconn) { // static maxconn
1356 sv->maxconn = sv->minconn = v;
1357 } else { // dynamic maxconn
1358 sv->maxconn = v;
1359 }
1360
1361 if (may_dequeue_tasks(sv, sv->proxy))
1362 process_srv_queue(sv);
1363
1364 return NULL;
1365}
1366
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001367#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001368static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1369 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001370{
1371 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001372 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001373 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001374 NULL,
1375 };
1376
1377 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001378 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001379
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001380 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1381}
1382
1383static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1384{
1385 struct sample_expr *expr;
1386
1387 expr = srv_sni_sample_parse_expr(newsrv, px, px->conf.file, px->conf.line, err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001388 if (!expr) {
1389 memprintf(err, "error detected while parsing sni expression : %s", *err);
1390 return ERR_ALERT | ERR_FATAL;
1391 }
1392
1393 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1394 memprintf(err, "error detected while parsing sni expression : "
1395 " fetch method '%s' extracts information from '%s', "
1396 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001397 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001398 return ERR_ALERT | ERR_FATAL;
1399 }
1400
1401 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1402 release_sample_expr(newsrv->ssl_ctx.sni);
1403 newsrv->ssl_ctx.sni = expr;
1404
1405 return 0;
1406}
1407#endif
1408
1409static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1410{
1411 if (err && *err) {
1412 indent_msg(err, 2);
Christopher Faulet767a84b2017-11-24 16:50:31 +01001413 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001414 }
1415 else
Christopher Faulet767a84b2017-11-24 16:50:31 +01001416 ha_alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1417 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001418}
1419
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001420static void srv_conn_src_sport_range_cpy(struct server *srv,
1421 struct server *src)
1422{
1423 int range_sz;
1424
1425 range_sz = src->conn_src.sport_range->size;
1426 if (range_sz > 0) {
1427 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1428 if (srv->conn_src.sport_range != NULL) {
1429 int i;
1430
1431 for (i = 0; i < range_sz; i++) {
1432 srv->conn_src.sport_range->ports[i] =
1433 src->conn_src.sport_range->ports[i];
1434 }
1435 }
1436 }
1437}
1438
1439/*
1440 * Copy <src> server connection source settings to <srv> server everything needed.
1441 */
1442static void srv_conn_src_cpy(struct server *srv, struct server *src)
1443{
1444 srv->conn_src.opts = src->conn_src.opts;
1445 srv->conn_src.source_addr = src->conn_src.source_addr;
1446
1447 /* Source port range copy. */
1448 if (src->conn_src.sport_range != NULL)
1449 srv_conn_src_sport_range_cpy(srv, src);
1450
1451#ifdef CONFIG_HAP_TRANSPARENT
1452 if (src->conn_src.bind_hdr_name != NULL) {
1453 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1454 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1455 }
1456 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1457 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1458#endif
1459 if (src->conn_src.iface_name != NULL)
1460 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1461}
1462
1463/*
1464 * Copy <src> server SSL settings to <srv> server allocating
1465 * everything needed.
1466 */
1467#if defined(USE_OPENSSL)
1468static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1469{
1470 if (src->ssl_ctx.ca_file != NULL)
1471 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1472 if (src->ssl_ctx.crl_file != NULL)
1473 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1474 if (src->ssl_ctx.client_crt != NULL)
1475 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1476
1477 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1478
1479 if (src->ssl_ctx.verify_host != NULL)
1480 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1481 if (src->ssl_ctx.ciphers != NULL)
1482 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1483 if (src->sni_expr != NULL)
1484 srv->sni_expr = strdup(src->sni_expr);
1485}
1486#endif
1487
1488/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001489 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001490 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001491 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001492 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001493static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001494{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001495 char *hostname_dn;
1496 int hostname_len, hostname_dn_len;
1497
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001498 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001499 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001500
Christopher Faulet67957bd2017-09-27 11:00:59 +02001501 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001502 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001503 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1504 hostname_dn, trash.size);
1505 if (hostname_dn_len == -1)
1506 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001507
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001508
Christopher Faulet67957bd2017-09-27 11:00:59 +02001509 free(srv->hostname);
1510 free(srv->hostname_dn);
1511 srv->hostname = strdup(hostname);
1512 srv->hostname_dn = strdup(hostname_dn);
1513 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001514 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001515 goto err;
1516
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001517 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001518
1519 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001520 free(srv->hostname); srv->hostname = NULL;
1521 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001522 return -1;
1523}
1524
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001525/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001526 * Copy <src> server settings to <srv> server allocating
1527 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001528 * This function is not supposed to be called at any time, but only
1529 * during server settings parsing or during server allocations from
1530 * a server template, and just after having calloc()'ed a new server.
1531 * So, <src> may only be a default server (when parsing server settings)
1532 * or a server template (during server allocations from a server template).
1533 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1534 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001535 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001536static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001537{
1538 /* Connection source settings copy */
1539 srv_conn_src_cpy(srv, src);
1540
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001541 if (srv_tmpl) {
1542 srv->addr = src->addr;
1543 srv->svc_port = src->svc_port;
1544 }
1545
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001546 srv->pp_opts = src->pp_opts;
1547 if (src->rdr_pfx != NULL) {
1548 srv->rdr_pfx = strdup(src->rdr_pfx);
1549 srv->rdr_len = src->rdr_len;
1550 }
1551 if (src->cookie != NULL) {
1552 srv->cookie = strdup(src->cookie);
1553 srv->cklen = src->cklen;
1554 }
1555 srv->use_ssl = src->use_ssl;
1556 srv->check.addr = srv->agent.addr = src->check.addr;
1557 srv->check.use_ssl = src->check.use_ssl;
1558 srv->check.port = src->check.port;
1559 /* Note: 'flags' field has potentially been already initialized. */
1560 srv->flags |= src->flags;
1561 srv->do_check = src->do_check;
1562 srv->do_agent = src->do_agent;
1563 if (srv->check.port)
1564 srv->flags |= SRV_F_CHECKPORT;
1565 srv->check.inter = src->check.inter;
1566 srv->check.fastinter = src->check.fastinter;
1567 srv->check.downinter = src->check.downinter;
1568 srv->agent.use_ssl = src->agent.use_ssl;
1569 srv->agent.port = src->agent.port;
1570 if (src->agent.send_string != NULL)
1571 srv->agent.send_string = strdup(src->agent.send_string);
1572 srv->agent.send_string_len = src->agent.send_string_len;
1573 srv->agent.inter = src->agent.inter;
1574 srv->agent.fastinter = src->agent.fastinter;
1575 srv->agent.downinter = src->agent.downinter;
1576 srv->maxqueue = src->maxqueue;
1577 srv->minconn = src->minconn;
1578 srv->maxconn = src->maxconn;
1579 srv->slowstart = src->slowstart;
1580 srv->observe = src->observe;
1581 srv->onerror = src->onerror;
1582 srv->onmarkeddown = src->onmarkeddown;
1583 srv->onmarkedup = src->onmarkedup;
1584 if (src->trackit != NULL)
1585 srv->trackit = strdup(src->trackit);
1586 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1587 srv->uweight = srv->iweight = src->iweight;
1588
1589 srv->check.send_proxy = src->check.send_proxy;
1590 /* health: up, but will fall down at first failure */
1591 srv->check.rise = srv->check.health = src->check.rise;
1592 srv->check.fall = src->check.fall;
1593
1594 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001595 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1596 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1597 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001598 srv->check.state |= CHK_ST_PAUSED;
1599 srv->check.health = 0;
1600 }
1601
1602 /* health: up but will fall down at first failure */
1603 srv->agent.rise = srv->agent.health = src->agent.rise;
1604 srv->agent.fall = src->agent.fall;
1605
1606 if (src->resolvers_id != NULL)
1607 srv->resolvers_id = strdup(src->resolvers_id);
1608 srv->dns_opts.family_prio = src->dns_opts.family_prio;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02001609 srv->dns_opts.accept_duplicate_ip = src->dns_opts.accept_duplicate_ip;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001610 if (srv->dns_opts.family_prio == AF_UNSPEC)
1611 srv->dns_opts.family_prio = AF_INET6;
1612 memcpy(srv->dns_opts.pref_net,
1613 src->dns_opts.pref_net,
1614 sizeof srv->dns_opts.pref_net);
1615 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1616
1617 srv->init_addr_methods = src->init_addr_methods;
1618 srv->init_addr = src->init_addr;
1619#if defined(USE_OPENSSL)
1620 srv_ssl_settings_cpy(srv, src);
1621#endif
1622#ifdef TCP_USER_TIMEOUT
1623 srv->tcp_ut = src->tcp_ut;
1624#endif
Christopher Faulet8ed0a3e2018-04-10 14:45:45 +02001625 srv->mux_proto = src->mux_proto;
1626
Olivier Houchard8da5f982017-08-04 18:35:36 +02001627 if (srv_tmpl)
1628 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001629}
1630
1631static struct server *new_server(struct proxy *proxy)
1632{
1633 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001634 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001635
1636 srv = calloc(1, sizeof *srv);
1637 if (!srv)
1638 return NULL;
1639
1640 srv->obj_type = OBJ_TYPE_SERVER;
1641 srv->proxy = proxy;
1642 LIST_INIT(&srv->actconns);
Patrick Hemmer0355dab2018-05-11 12:52:31 -04001643 srv->pendconns = EB_ROOT;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001644
1645 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1646 goto free_srv;
1647 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1648 goto free_priv_conns;
1649 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1650 goto free_idle_conns;
1651
1652 for (i = 0; i < global.nbthread; i++) {
1653 LIST_INIT(&srv->priv_conns[i]);
1654 LIST_INIT(&srv->idle_conns[i]);
1655 LIST_INIT(&srv->safe_conns[i]);
1656 }
1657
Emeric Brun52a91d32017-08-31 14:41:55 +02001658 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001659 srv->last_change = now.tv_sec;
1660
1661 srv->check.status = HCHK_STATUS_INI;
1662 srv->check.server = srv;
1663 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1664
1665 srv->agent.status = HCHK_STATUS_INI;
1666 srv->agent.server = srv;
1667 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1668
1669 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001670
1671 free_idle_conns:
1672 free(srv->idle_conns);
1673 free_priv_conns:
1674 free(srv->priv_conns);
1675 free_srv:
1676 free(srv);
1677 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001678}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001679
1680/*
1681 * Validate <srv> server health-check settings.
1682 * Returns 0 if everything is OK, -1 if not.
1683 */
1684static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1685{
1686 struct tcpcheck_rule *r = NULL;
1687 struct list *l;
1688
1689 /*
1690 * We need at least a service port, a check port or the first tcp-check rule must
1691 * be a 'connect' one when checking an IPv4/IPv6 server.
1692 */
1693 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1694 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1695 return 0;
1696
1697 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1698 if (!r) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001699 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1700 "Check has been disabled.\n",
1701 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001702 return -1;
1703 }
1704
1705 /* search the first action (connect / send / expect) in the list */
1706 l = &srv->proxy->tcpcheck_rules;
1707 list_for_each_entry(r, l, list) {
1708 if (r->action != TCPCHK_ACT_COMMENT)
1709 break;
1710 }
1711
1712 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001713 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port "
1714 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1715 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001716 return -1;
1717 }
1718
1719 /* scan the tcp-check ruleset to ensure a port has been configured */
1720 l = &srv->proxy->tcpcheck_rules;
1721 list_for_each_entry(r, l, list) {
1722 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001723 ha_alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1724 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1725 file, linenum, srv->id);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001726 return -1;
1727 }
1728 }
1729
1730 return 0;
1731}
1732
1733/*
1734 * Initialize <srv> health-check structure.
1735 * Returns the error string in case of memory allocation failure, NULL if not.
1736 */
1737static const char *do_health_check_init(struct server *srv, int check_type, int state)
1738{
1739 const char *ret;
1740
1741 if (!srv->do_check)
1742 return NULL;
1743
1744 ret = init_check(&srv->check, check_type);
1745 if (ret)
1746 return ret;
1747
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001748 srv->check.state |= state;
1749 global.maxsock++;
1750
1751 return NULL;
1752}
1753
1754static int server_health_check_init(const char *file, int linenum,
1755 struct server *srv, struct proxy *curproxy)
1756{
1757 const char *ret;
1758
1759 if (!srv->do_check)
1760 return 0;
1761
1762 if (srv->trackit) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001763 ha_alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1764 file, linenum);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001765 return ERR_ALERT | ERR_FATAL;
1766 }
1767
1768 if (server_healthcheck_validate(file, linenum, srv) < 0)
1769 return ERR_ALERT | ERR_ABORT;
1770
1771 /* note: check type will be set during the config review phase */
1772 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1773 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001774 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001775 return ERR_ALERT | ERR_ABORT;
1776 }
1777
1778 return 0;
1779}
1780
1781/*
1782 * Initialize <srv> agent check structure.
1783 * Returns the error string in case of memory allocation failure, NULL if not.
1784 */
1785static const char *do_server_agent_check_init(struct server *srv, int state)
1786{
1787 const char *ret;
1788
1789 if (!srv->do_agent)
1790 return NULL;
1791
1792 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1793 if (ret)
1794 return ret;
1795
1796 if (!srv->agent.inter)
1797 srv->agent.inter = srv->check.inter;
1798
1799 srv->agent.state |= state;
1800 global.maxsock++;
1801
1802 return NULL;
1803}
1804
1805static int server_agent_check_init(const char *file, int linenum,
1806 struct server *srv, struct proxy *curproxy)
1807{
1808 const char *ret;
1809
1810 if (!srv->do_agent)
1811 return 0;
1812
1813 if (!srv->agent.port) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001814 ha_alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001815 file, linenum, srv->id);
1816 return ERR_ALERT | ERR_FATAL;
1817 }
1818
1819 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1820 if (ret) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001821 ha_alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001822 return ERR_ALERT | ERR_ABORT;
1823 }
1824
1825 return 0;
1826}
1827
1828#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1829static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1830 struct server *srv, struct proxy *proxy)
1831{
1832 int ret;
1833 char *err = NULL;
1834
1835 if (!srv->sni_expr)
1836 return 0;
1837
1838 ret = server_parse_sni_expr(srv, proxy, &err);
1839 if (!ret)
1840 return 0;
1841
1842 display_parser_err(file, linenum, args, cur_arg, &err);
1843 free(err);
1844
1845 return ret;
1846}
1847#endif
1848
1849/*
1850 * Server initializations finalization.
1851 * Initialize health check, agent check and SNI expression if enabled.
1852 * Must not be called for a default server instance.
1853 */
1854static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1855 struct server *srv, struct proxy *px)
1856{
1857 int ret;
1858
1859 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1860 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1861 return ret;
1862 }
1863
1864#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1865 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1866 return ret;
1867#endif
1868
1869 if (srv->flags & SRV_F_BACKUP)
1870 px->srv_bck++;
1871 else
1872 px->srv_act++;
1873 srv_lb_commit_status(srv);
1874
1875 return 0;
1876}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001877
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001878/*
1879 * Parse as much as possible such a range string argument: low[-high]
1880 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1881 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1882 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1883 * Returns 0 if succeeded, -1 if not.
1884 */
1885static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1886{
1887 char *nb_high_arg;
1888
1889 *nb_high = 0;
1890 chunk_printf(&trash, "%s", arg);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001891 *nb_low = atoi(trash.area);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001892
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001893 if ((nb_high_arg = strchr(trash.area, '-'))) {
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001894 *nb_high_arg++ = '\0';
1895 *nb_high = atoi(nb_high_arg);
1896 }
1897 else {
1898 *nb_high += *nb_low;
1899 *nb_low = 1;
1900 }
1901
1902 if (*nb_low < 0 || *nb_high < *nb_low)
1903 return -1;
1904
1905 return 0;
1906}
1907
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001908static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1909{
1910 chunk_printf(&trash, "%s%d", prefix, nb);
1911 free(srv->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001912 srv->id = strdup(trash.area);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001913}
1914
1915/*
1916 * Initialize as much as possible servers from <srv> server template.
1917 * Note that a server template is a special server with
1918 * a few different parameters than a server which has
1919 * been parsed mostly the same way as a server.
1920 * Returns the number of servers succesfully allocated,
1921 * 'srv' template included.
1922 */
1923static int server_template_init(struct server *srv, struct proxy *px)
1924{
1925 int i;
1926 struct server *newsrv;
1927
1928 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1929 int check_init_state;
1930 int agent_init_state;
1931
1932 newsrv = new_server(px);
1933 if (!newsrv)
1934 goto err;
1935
1936 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001937 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001938#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1939 if (newsrv->sni_expr) {
1940 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1941 if (!newsrv->ssl_ctx.sni)
1942 goto err;
1943 }
1944#endif
1945 /* Set this new server ID. */
1946 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1947
1948 /* Initial checks states. */
1949 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1950 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1951
1952 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1953 do_server_agent_check_init(newsrv, agent_init_state))
1954 goto err;
1955
1956 /* Linked backwards first. This will be restablished after parsing. */
1957 newsrv->next = px->srv;
1958 px->srv = newsrv;
1959 }
1960 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1961
1962 return i - srv->tmpl_info.nb_low;
1963
1964 err:
1965 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1966 if (newsrv) {
1967#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1968 release_sample_expr(newsrv->ssl_ctx.sni);
1969#endif
1970 free_check(&newsrv->agent);
1971 free_check(&newsrv->check);
1972 }
1973 free(newsrv);
1974 return i - srv->tmpl_info.nb_low;
1975}
1976
Willy Tarreau272adea2014-03-31 10:39:59 +02001977int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1978{
1979 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001980 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001981 char *errmsg = NULL;
1982 int err_code = 0;
1983 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001984 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001985
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001986 if (!strcmp(args[0], "server") ||
1987 !strcmp(args[0], "default-server") ||
1988 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001990 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001991 int srv = !defsrv && !strcmp(args[0], "server");
1992 int srv_tmpl = !defsrv && !srv;
1993 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001994
1995 if (!defsrv && curproxy == defproxy) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01001996 ha_alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001997 err_code |= ERR_ALERT | ERR_FATAL;
1998 goto out;
1999 }
2000 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
Olivier Houchard306e6532018-07-24 16:48:59 +02002001 err_code |= ERR_WARN;
Willy Tarreau272adea2014-03-31 10:39:59 +02002002
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002003 /* There is no mandatory first arguments for default server. */
2004 if (srv) {
2005 if (!*args[2]) {
2006 /* 'server' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002007 ha_alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002008 file, linenum, args[0]);
2009 err_code |= ERR_ALERT | ERR_FATAL;
2010 goto out;
2011 }
2012
2013 err = invalid_char(args[1]);
2014 }
2015 else if (srv_tmpl) {
2016 if (!*args[3]) {
2017 /* 'server-template' line number of argument check. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002018 ha_alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002019 file, linenum, args[0]);
2020 err_code |= ERR_ALERT | ERR_FATAL;
2021 goto out;
2022 }
2023
2024 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002025 }
2026
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002027 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002028 ha_alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002029 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002030 err_code |= ERR_ALERT | ERR_FATAL;
2031 goto out;
2032 }
2033
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002034 cur_arg = 2;
2035 if (srv_tmpl) {
2036 /* Parse server-template <nb | range> arg. */
2037 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002038 ha_alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002039 file, linenum, args[0], args[cur_arg]);
2040 err_code |= ERR_ALERT | ERR_FATAL;
2041 goto out;
2042 }
2043 cur_arg++;
2044 }
2045
Willy Tarreau272adea2014-03-31 10:39:59 +02002046 if (!defsrv) {
2047 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002048 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002049 struct protocol *proto;
2050
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002051 newsrv = new_server(curproxy);
2052 if (!newsrv) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002053 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Willy Tarreau272adea2014-03-31 10:39:59 +02002054 err_code |= ERR_ALERT | ERR_ABORT;
2055 goto out;
2056 }
2057
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002058 if (srv_tmpl) {
2059 newsrv->tmpl_info.nb_low = tmpl_range_low;
2060 newsrv->tmpl_info.nb_high = tmpl_range_high;
2061 }
2062
Willy Tarreau272adea2014-03-31 10:39:59 +02002063 /* the servers are linked backwards first */
2064 newsrv->next = curproxy->srv;
2065 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02002066 newsrv->conf.file = strdup(file);
2067 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002068 /* Note: for a server template, its id is its prefix.
2069 * This is a temporary id which will be used for server allocations to come
2070 * after parsing.
2071 */
2072 if (srv)
2073 newsrv->id = strdup(args[1]);
2074 else
2075 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002076
2077 /* several ways to check the port component :
2078 * - IP => port=+0, relative (IPv4 only)
2079 * - IP: => port=+0, relative
2080 * - IP:N => port=N, absolute
2081 * - IP:+N => port=+N, relative
2082 * - IP:-N => port=-N, relative
2083 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002084 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02002085 if (!sk) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002086 ha_alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
Willy Tarreau272adea2014-03-31 10:39:59 +02002087 err_code |= ERR_ALERT | ERR_FATAL;
2088 goto out;
2089 }
2090
2091 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01002092 if (!fqdn && (!proto || !proto->connect)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002093 ha_alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002094 file, linenum, args[0], args[1]);
2095 err_code |= ERR_ALERT | ERR_FATAL;
2096 goto out;
2097 }
2098
2099 if (!port1 || !port2) {
2100 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02002101 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02002102 }
2103 else if (port1 != port2) {
2104 /* port range */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002105 ha_alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002106 file, linenum, args[0], args[1], args[2]);
2107 err_code |= ERR_ALERT | ERR_FATAL;
2108 goto out;
2109 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002110
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002111 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002112 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002113 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002114 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002115 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2116 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2117 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002118 err_code |= ERR_ALERT | ERR_FATAL;
2119 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002120 }
2121 }
2122 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002123 ha_alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
Christopher Faulet67957bd2017-09-27 11:00:59 +02002124 file, linenum, newsrv->id);
2125 err_code |= ERR_ALERT | ERR_FATAL;
2126 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002127 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002128 }
2129
Willy Tarreau272adea2014-03-31 10:39:59 +02002130 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002131 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002132
Olivier Houchard8da5f982017-08-04 18:35:36 +02002133 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002134 ha_alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002135 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002136 err_code |= ERR_ALERT | ERR_FATAL;
2137 goto out;
2138 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002139
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002140 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002141 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002142 HA_SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002143 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002144 } else {
2145 newsrv = &curproxy->defsrv;
2146 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002147 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002148 newsrv->dns_opts.accept_duplicate_ip = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02002149 }
2150
2151 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002152 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002153 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2154 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002155 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002156 file, linenum, *err, newsrv->id);
2157 err_code |= ERR_ALERT | ERR_FATAL;
2158 goto out;
2159 }
2160 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002161 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002162 file, linenum, val, args[cur_arg], newsrv->id);
2163 err_code |= ERR_ALERT | ERR_FATAL;
2164 goto out;
2165 }
2166 newsrv->agent.inter = val;
2167 cur_arg += 2;
2168 }
Misiekea849332017-01-09 09:39:51 +01002169 else if (!strcmp(args[cur_arg], "agent-addr")) {
2170 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002171 ha_alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
Misiekea849332017-01-09 09:39:51 +01002172 goto out;
2173 }
2174
2175 cur_arg += 2;
2176 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002177 else if (!strcmp(args[cur_arg], "agent-port")) {
2178 global.maxsock++;
2179 newsrv->agent.port = atol(args[cur_arg + 1]);
2180 cur_arg += 2;
2181 }
James Brown55f9ff12015-10-21 18:19:05 -07002182 else if (!strcmp(args[cur_arg], "agent-send")) {
2183 global.maxsock++;
2184 free(newsrv->agent.send_string);
2185 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2186 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2187 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2188 cur_arg += 2;
2189 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002190 else if (!strcmp(args[cur_arg], "init-addr")) {
2191 char *p, *end;
2192 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002193 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002194
2195 newsrv->init_addr_methods = 0;
2196 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2197
2198 for (p = args[cur_arg + 1]; *p; p = end) {
2199 /* cut on next comma */
2200 for (end = p; *end && *end != ','; end++);
2201 if (*end)
2202 *(end++) = 0;
2203
Willy Tarreau4310d362016-11-02 15:05:56 +01002204 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002205 if (!strcmp(p, "libc")) {
2206 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2207 }
2208 else if (!strcmp(p, "last")) {
2209 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2210 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002211 else if (!strcmp(p, "none")) {
2212 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2213 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002214 else if (str2ip2(p, &sa, 0)) {
2215 if (is_addr(&newsrv->init_addr)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002216 ha_alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
Willy Tarreau4310d362016-11-02 15:05:56 +01002217 file, linenum, args[cur_arg], p);
2218 err_code |= ERR_ALERT | ERR_FATAL;
2219 goto out;
2220 }
2221 newsrv->init_addr = sa;
2222 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2223 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002224 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002225 ha_alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002226 file, linenum, args[cur_arg], p);
2227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230 if (!done) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002231 ha_alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002232 file, linenum, args[cur_arg], p);
2233 err_code |= ERR_ALERT | ERR_FATAL;
2234 goto out;
2235 }
2236 }
2237 cur_arg += 2;
2238 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002239 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002240 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002241 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2242 cur_arg += 2;
2243 }
Baptiste Assmann8e2d9432018-06-22 15:04:43 +02002244 else if (!strcmp(args[cur_arg], "resolve-opts")) {
2245 char *p, *end;
2246
2247 for (p = args[cur_arg + 1]; *p; p = end) {
2248 /* cut on next comma */
2249 for (end = p; *end && *end != ','; end++);
2250 if (*end)
2251 *(end++) = 0;
2252
2253 if (!strcmp(p, "allow-dup-ip")) {
2254 newsrv->dns_opts.accept_duplicate_ip = 1;
2255 }
2256 else if (!strcmp(p, "prevent-dup-ip")) {
2257 newsrv->dns_opts.accept_duplicate_ip = 0;
2258 }
2259 else {
2260 ha_alert("parsing [%s:%d]: '%s' : unknown resolve-opts option '%s', supported methods are 'allow-dup-ip' and 'prevent-dup-ip'.\n",
2261 file, linenum, args[cur_arg], p);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265 }
2266
2267 cur_arg += 2;
2268 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002269 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2270 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002271 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002272 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002273 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002274 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002275 ha_alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002276 file, linenum, args[cur_arg]);
2277 err_code |= ERR_ALERT | ERR_FATAL;
2278 goto out;
2279 }
2280 cur_arg += 2;
2281 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002282 else if (!strcmp(args[cur_arg], "resolve-net")) {
2283 char *p, *e;
2284 unsigned char mask;
2285 struct dns_options *opt;
2286
2287 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002288 ha_alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002289 file, linenum, args[cur_arg]);
2290 err_code |= ERR_ALERT | ERR_FATAL;
2291 goto out;
2292 }
2293
2294 opt = &newsrv->dns_opts;
2295
2296 /* Split arguments by comma, and convert it from ipv4 or ipv6
2297 * string network in in_addr or in6_addr.
2298 */
2299 p = args[cur_arg + 1];
2300 e = p;
2301 while (*p != '\0') {
2302 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002303 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002304 ha_alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002305 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2306 err_code |= ERR_ALERT | ERR_FATAL;
2307 goto out;
2308 }
2309 /* look for end or comma. */
2310 while (*e != ',' && *e != '\0')
2311 e++;
2312 if (*e == ',') {
2313 *e = '\0';
2314 e++;
2315 }
2316 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2317 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2318 /* Try to convert input string from ipv4 or ipv6 network. */
2319 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2320 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2321 &mask)) {
2322 /* Try to convert input string from ipv6 network. */
2323 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2324 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2325 } else {
2326 /* All network conversions fail, retrun error. */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002327 ha_alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002328 file, linenum, args[cur_arg], p);
2329 err_code |= ERR_ALERT | ERR_FATAL;
2330 goto out;
2331 }
2332 opt->pref_net_nb++;
2333 p = e;
2334 }
2335
2336 cur_arg += 2;
2337 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002338 else if (!strcmp(args[cur_arg], "rise")) {
2339 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002340 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002341 file, linenum, args[cur_arg]);
2342 err_code |= ERR_ALERT | ERR_FATAL;
2343 goto out;
2344 }
2345
2346 newsrv->check.rise = atol(args[cur_arg + 1]);
2347 if (newsrv->check.rise <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002348 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002349 file, linenum, args[cur_arg]);
2350 err_code |= ERR_ALERT | ERR_FATAL;
2351 goto out;
2352 }
2353
2354 if (newsrv->check.health)
2355 newsrv->check.health = newsrv->check.rise;
2356 cur_arg += 2;
2357 }
2358 else if (!strcmp(args[cur_arg], "fall")) {
2359 newsrv->check.fall = atol(args[cur_arg + 1]);
2360
2361 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002362 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002363 file, linenum, args[cur_arg]);
2364 err_code |= ERR_ALERT | ERR_FATAL;
2365 goto out;
2366 }
2367
2368 if (newsrv->check.fall <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002369 ha_alert("parsing [%s:%d]: '%s' has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002370 file, linenum, args[cur_arg]);
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373 }
2374
2375 cur_arg += 2;
2376 }
2377 else if (!strcmp(args[cur_arg], "inter")) {
2378 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2379 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002380 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002381 file, linenum, *err, newsrv->id);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002386 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002387 file, linenum, val, args[cur_arg], newsrv->id);
2388 err_code |= ERR_ALERT | ERR_FATAL;
2389 goto out;
2390 }
2391 newsrv->check.inter = val;
2392 cur_arg += 2;
2393 }
2394 else if (!strcmp(args[cur_arg], "fastinter")) {
2395 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2396 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002397 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002398 file, linenum, *err, newsrv->id);
2399 err_code |= ERR_ALERT | ERR_FATAL;
2400 goto out;
2401 }
2402 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002403 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002404 file, linenum, val, args[cur_arg], newsrv->id);
2405 err_code |= ERR_ALERT | ERR_FATAL;
2406 goto out;
2407 }
2408 newsrv->check.fastinter = val;
2409 cur_arg += 2;
2410 }
2411 else if (!strcmp(args[cur_arg], "downinter")) {
2412 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2413 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002414 ha_alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 file, linenum, *err, newsrv->id);
2416 err_code |= ERR_ALERT | ERR_FATAL;
2417 goto out;
2418 }
2419 if (val <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002420 ha_alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002421 file, linenum, val, args[cur_arg], newsrv->id);
2422 err_code |= ERR_ALERT | ERR_FATAL;
2423 goto out;
2424 }
2425 newsrv->check.downinter = val;
2426 cur_arg += 2;
2427 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002428 else if (!strcmp(args[cur_arg], "port")) {
2429 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002430 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002431 cur_arg += 2;
2432 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002433 else if (!strcmp(args[cur_arg], "weight")) {
2434 int w;
2435 w = atol(args[cur_arg + 1]);
2436 if (w < 0 || w > SRV_UWGHT_MAX) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002437 ha_alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002438 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2439 err_code |= ERR_ALERT | ERR_FATAL;
2440 goto out;
2441 }
2442 newsrv->uweight = newsrv->iweight = w;
2443 cur_arg += 2;
2444 }
2445 else if (!strcmp(args[cur_arg], "minconn")) {
2446 newsrv->minconn = atol(args[cur_arg + 1]);
2447 cur_arg += 2;
2448 }
2449 else if (!strcmp(args[cur_arg], "maxconn")) {
2450 newsrv->maxconn = atol(args[cur_arg + 1]);
2451 cur_arg += 2;
2452 }
2453 else if (!strcmp(args[cur_arg], "maxqueue")) {
2454 newsrv->maxqueue = atol(args[cur_arg + 1]);
2455 cur_arg += 2;
2456 }
2457 else if (!strcmp(args[cur_arg], "slowstart")) {
2458 /* slowstart is stored in seconds */
2459 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2460 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002461 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 file, linenum, *err, newsrv->id);
2463 err_code |= ERR_ALERT | ERR_FATAL;
2464 goto out;
2465 }
2466 newsrv->slowstart = (val + 999) / 1000;
2467 cur_arg += 2;
2468 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002469 else if (!strcmp(args[cur_arg], "on-error")) {
2470 if (!strcmp(args[cur_arg + 1], "fastinter"))
2471 newsrv->onerror = HANA_ONERR_FASTINTER;
2472 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2473 newsrv->onerror = HANA_ONERR_FAILCHK;
2474 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2475 newsrv->onerror = HANA_ONERR_SUDDTH;
2476 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2477 newsrv->onerror = HANA_ONERR_MARKDWN;
2478 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002479 ha_alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
Willy Tarreau272adea2014-03-31 10:39:59 +02002480 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2481 file, linenum, args[cur_arg], args[cur_arg + 1]);
2482 err_code |= ERR_ALERT | ERR_FATAL;
2483 goto out;
2484 }
2485
2486 cur_arg += 2;
2487 }
2488 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2489 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2490 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2491 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002492 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002493 file, linenum, args[cur_arg], args[cur_arg + 1]);
2494 err_code |= ERR_ALERT | ERR_FATAL;
2495 goto out;
2496 }
2497
2498 cur_arg += 2;
2499 }
2500 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2501 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2502 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2503 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002504 ha_alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002505 file, linenum, args[cur_arg], args[cur_arg + 1]);
2506 err_code |= ERR_ALERT | ERR_FATAL;
2507 goto out;
2508 }
2509
2510 cur_arg += 2;
2511 }
2512 else if (!strcmp(args[cur_arg], "error-limit")) {
2513 if (!*args[cur_arg + 1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002514 ha_alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002515 file, linenum, args[cur_arg]);
2516 err_code |= ERR_ALERT | ERR_FATAL;
2517 goto out;
2518 }
2519
2520 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2521
2522 if (newsrv->consecutive_errors_limit <= 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002523 ha_alert("parsing [%s:%d]: %s has to be > 0.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002524 file, linenum, args[cur_arg]);
2525 err_code |= ERR_ALERT | ERR_FATAL;
2526 goto out;
2527 }
2528 cur_arg += 2;
2529 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002530 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Christopher Faulet767a84b2017-11-24 16:50:31 +01002531 ha_alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002532 file, linenum, "usesrc", "source");
2533 err_code |= ERR_ALERT | ERR_FATAL;
2534 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002535 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002536 else {
2537 static int srv_dumped;
2538 struct srv_kw *kw;
2539 char *err;
2540
2541 kw = srv_find_kw(args[cur_arg]);
2542 if (kw) {
2543 char *err = NULL;
2544 int code;
2545
2546 if (!kw->parse) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002547 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002548 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002549 if (kw->skip != -1)
2550 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002551 err_code |= ERR_ALERT | ERR_FATAL;
2552 goto out;
2553 }
2554
2555 if (defsrv && !kw->default_ok) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002556 ha_alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002557 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002558 if (kw->skip != -1)
2559 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002560 err_code |= ERR_ALERT;
2561 continue;
2562 }
2563
2564 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2565 err_code |= code;
2566
2567 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002568 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002569 if (code & ERR_FATAL) {
2570 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002571 if (kw->skip != -1)
2572 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002573 goto out;
2574 }
2575 }
2576 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002577 if (kw->skip != -1)
2578 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002579 continue;
2580 }
2581
2582 err = NULL;
2583 if (!srv_dumped) {
2584 srv_dump_kws(&err);
2585 indent_msg(&err, 4);
2586 srv_dumped = 1;
2587 }
2588
Christopher Faulet767a84b2017-11-24 16:50:31 +01002589 ha_alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
Willy Tarreau272adea2014-03-31 10:39:59 +02002590 file, linenum, args[0], args[1], args[cur_arg],
2591 err ? " Registered keywords :" : "", err ? err : "");
2592 free(err);
2593
2594 err_code |= ERR_ALERT | ERR_FATAL;
2595 goto out;
2596 }
2597 }
2598
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002599 if (!defsrv)
2600 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2601 if (err_code & ERR_FATAL)
2602 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002603 if (srv_tmpl)
2604 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002605 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002606 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002607 return 0;
2608
2609 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002610 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002611 free(errmsg);
2612 return err_code;
2613}
2614
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002615/* Returns a pointer to the first server matching either id <id>.
2616 * NULL is returned if no match is found.
2617 * the lookup is performed in the backend <bk>
2618 */
2619struct server *server_find_by_id(struct proxy *bk, int id)
2620{
2621 struct eb32_node *eb32;
2622 struct server *curserver;
2623
2624 if (!bk || (id ==0))
2625 return NULL;
2626
2627 /* <bk> has no backend capabilities, so it can't have a server */
2628 if (!(bk->cap & PR_CAP_BE))
2629 return NULL;
2630
2631 curserver = NULL;
2632
2633 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2634 if (eb32)
2635 curserver = container_of(eb32, struct server, conf.id);
2636
2637 return curserver;
2638}
2639
2640/* Returns a pointer to the first server matching either name <name>, or id
2641 * if <name> starts with a '#'. NULL is returned if no match is found.
2642 * the lookup is performed in the backend <bk>
2643 */
2644struct server *server_find_by_name(struct proxy *bk, const char *name)
2645{
2646 struct server *curserver;
2647
2648 if (!bk || !name)
2649 return NULL;
2650
2651 /* <bk> has no backend capabilities, so it can't have a server */
2652 if (!(bk->cap & PR_CAP_BE))
2653 return NULL;
2654
2655 curserver = NULL;
2656 if (*name == '#') {
2657 curserver = server_find_by_id(bk, atoi(name + 1));
2658 if (curserver)
2659 return curserver;
2660 }
2661 else {
2662 curserver = bk->srv;
2663
2664 while (curserver && (strcmp(curserver->id, name) != 0))
2665 curserver = curserver->next;
2666
2667 if (curserver)
2668 return curserver;
2669 }
2670
2671 return NULL;
2672}
2673
2674struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2675{
2676 struct server *byname;
2677 struct server *byid;
2678
2679 if (!name && !id)
2680 return NULL;
2681
2682 if (diff)
2683 *diff = 0;
2684
2685 byname = byid = NULL;
2686
2687 if (name) {
2688 byname = server_find_by_name(bk, name);
2689 if (byname && (!id || byname->puid == id))
2690 return byname;
2691 }
2692
2693 /* remaining possibilities :
2694 * - name not set
2695 * - name set but not found
2696 * - name found but ID doesn't match
2697 */
2698 if (id) {
2699 byid = server_find_by_id(bk, id);
2700 if (byid) {
2701 if (byname) {
2702 /* use id only if forced by configuration */
2703 if (byid->flags & SRV_F_FORCED_ID) {
2704 if (diff)
2705 *diff |= 2;
2706 return byid;
2707 }
2708 else {
2709 if (diff)
2710 *diff |= 1;
2711 return byname;
2712 }
2713 }
2714
2715 /* remaining possibilities:
2716 * - name not set
2717 * - name set but not found
2718 */
2719 if (name && diff)
2720 *diff |= 2;
2721 return byid;
2722 }
2723
2724 /* id bot found */
2725 if (byname) {
2726 if (diff)
2727 *diff |= 1;
2728 return byname;
2729 }
2730 }
2731
2732 return NULL;
2733}
2734
Willy Tarreau46b7f532018-08-21 11:54:26 +02002735/* Update a server state using the parameters available in the params list.
2736 *
2737 * Grabs the server lock during operation.
2738 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002739static void srv_update_state(struct server *srv, int version, char **params)
2740{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002741 char *p;
Willy Tarreau83061a82018-07-13 11:56:34 +02002742 struct buffer *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002743
2744 /* fields since version 1
2745 * and common to all other upcoming versions
2746 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002747 enum srv_state srv_op_state;
2748 enum srv_admin srv_admin_state;
2749 unsigned srv_uweight, srv_iweight;
2750 unsigned long srv_last_time_change;
2751 short srv_check_status;
2752 enum chk_result srv_check_result;
2753 int srv_check_health;
2754 int srv_check_state, srv_agent_state;
2755 int bk_f_forced_id;
2756 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002757 int fqdn_set_by_cli;
2758 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002759 const char *port_str;
2760 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002761
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002762 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002763 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002764 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002765 switch (version) {
2766 case 1:
2767 /*
2768 * now we can proceed with server's state update:
2769 * srv_addr: params[0]
2770 * srv_op_state: params[1]
2771 * srv_admin_state: params[2]
2772 * srv_uweight: params[3]
2773 * srv_iweight: params[4]
2774 * srv_last_time_change: params[5]
2775 * srv_check_status: params[6]
2776 * srv_check_result: params[7]
2777 * srv_check_health: params[8]
2778 * srv_check_state: params[9]
2779 * srv_agent_state: params[10]
2780 * bk_f_forced_id: params[11]
2781 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002782 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002783 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784 */
2785
2786 /* validating srv_op_state */
2787 p = NULL;
2788 errno = 0;
2789 srv_op_state = strtol(params[1], &p, 10);
2790 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2791 (srv_op_state != SRV_ST_STOPPED &&
2792 srv_op_state != SRV_ST_STARTING &&
2793 srv_op_state != SRV_ST_RUNNING &&
2794 srv_op_state != SRV_ST_STOPPING)) {
2795 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2796 }
2797
2798 /* validating srv_admin_state */
2799 p = NULL;
2800 errno = 0;
2801 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002802 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002803
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002804 /* inherited statuses will be recomputed later.
2805 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2806 */
2807 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002808
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002809 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2810 (srv_admin_state != 0 &&
2811 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002812 srv_admin_state != SRV_ADMF_CMAINT &&
2813 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002814 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002815 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002816 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2817 }
2818
2819 /* validating srv_uweight */
2820 p = NULL;
2821 errno = 0;
2822 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002823 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2825
2826 /* validating srv_iweight */
2827 p = NULL;
2828 errno = 0;
2829 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002830 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002831 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2832
2833 /* validating srv_last_time_change */
2834 p = NULL;
2835 errno = 0;
2836 srv_last_time_change = strtol(params[5], &p, 10);
2837 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2838 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2839
2840 /* validating srv_check_status */
2841 p = NULL;
2842 errno = 0;
2843 srv_check_status = strtol(params[6], &p, 10);
2844 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2845 (srv_check_status >= HCHK_STATUS_SIZE))
2846 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2847
2848 /* validating srv_check_result */
2849 p = NULL;
2850 errno = 0;
2851 srv_check_result = strtol(params[7], &p, 10);
2852 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2853 (srv_check_result != CHK_RES_UNKNOWN &&
2854 srv_check_result != CHK_RES_NEUTRAL &&
2855 srv_check_result != CHK_RES_FAILED &&
2856 srv_check_result != CHK_RES_PASSED &&
2857 srv_check_result != CHK_RES_CONDPASS)) {
2858 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2859 }
2860
2861 /* validating srv_check_health */
2862 p = NULL;
2863 errno = 0;
2864 srv_check_health = strtol(params[8], &p, 10);
2865 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2866 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2867
2868 /* validating srv_check_state */
2869 p = NULL;
2870 errno = 0;
2871 srv_check_state = strtol(params[9], &p, 10);
2872 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2873 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2874 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2875
2876 /* validating srv_agent_state */
2877 p = NULL;
2878 errno = 0;
2879 srv_agent_state = strtol(params[10], &p, 10);
2880 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2881 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2882 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2883
2884 /* validating bk_f_forced_id */
2885 p = NULL;
2886 errno = 0;
2887 bk_f_forced_id = strtol(params[11], &p, 10);
2888 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2889 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2890
2891 /* validating srv_f_forced_id */
2892 p = NULL;
2893 errno = 0;
2894 srv_f_forced_id = strtol(params[12], &p, 10);
2895 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2896 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2897
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002898 /* validating srv_fqdn */
2899 fqdn = params[13];
2900 if (fqdn && *fqdn == '-')
2901 fqdn = NULL;
2902 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2903 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2904 fqdn = NULL;
2905 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002906
Frédéric Lécaille31694712017-08-01 08:47:19 +02002907 port_str = params[14];
2908 if (port_str) {
2909 port = strl2uic(port_str, strlen(port_str));
2910 if (port > USHRT_MAX) {
2911 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2912 port_str = NULL;
2913 }
2914 }
2915
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002916 /* don't apply anything if one error has been detected */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002917 if (msg->data)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002918 goto out;
2919
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002920 HA_SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002921 /* recover operational state and apply it to this server
2922 * and all servers tracking this one */
2923 switch (srv_op_state) {
2924 case SRV_ST_STOPPED:
2925 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002926 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002927 break;
2928 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002929 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002930 break;
2931 case SRV_ST_STOPPING:
2932 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002933 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002934 break;
2935 case SRV_ST_RUNNING:
2936 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002937 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002938 break;
2939 }
2940
2941 /* When applying server state, the following rules apply:
2942 * - in case of a configuration change, we apply the setting from the new
2943 * configuration, regardless of old running state
2944 * - if no configuration change, we apply old running state only if old running
2945 * state is different from new configuration state
2946 */
2947 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002948 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2949 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002950 srv_adm_set_maint(srv);
2951 else
2952 srv_adm_set_ready(srv);
2953 }
2954 /* configuration is the same, let's compate old running state and new conf state */
2955 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002956 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002957 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002958 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002959 srv_adm_set_ready(srv);
2960 }
2961 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002962 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002963 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002964 * (srv->iweight is the weight set up in configuration).
2965 * There are two possible reasons for FDRAIN to have been present :
2966 * - previous config weight was zero
2967 * - "set server b/s drain" was sent to the CLI
2968 *
2969 * In the first case, we simply want to drop this drain state
2970 * if the new weight is not zero anymore, meaning the administrator
2971 * has intentionally turned the weight back to a positive value to
2972 * enable the server again after an operation. In the second case,
2973 * the drain state was forced on the CLI regardless of the config's
2974 * weight so we don't want a change to the config weight to lose this
2975 * status. What this means is :
2976 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2977 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002978 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002979 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002980 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002981 }
2982
2983 srv->last_change = date.tv_sec - srv_last_time_change;
2984 srv->check.status = srv_check_status;
2985 srv->check.result = srv_check_result;
2986 srv->check.health = srv_check_health;
2987
2988 /* Only case we want to apply is removing ENABLED flag which could have been
2989 * done by the "disable health" command over the stats socket
2990 */
2991 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2992 (srv_check_state & CHK_ST_CONFIGURED) &&
2993 !(srv_check_state & CHK_ST_ENABLED))
2994 srv->check.state &= ~CHK_ST_ENABLED;
2995
2996 /* Only case we want to apply is removing ENABLED flag which could have been
2997 * done by the "disable agent" command over the stats socket
2998 */
2999 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
3000 (srv_agent_state & CHK_ST_CONFIGURED) &&
3001 !(srv_agent_state & CHK_ST_ENABLED))
3002 srv->agent.state &= ~CHK_ST_ENABLED;
3003
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003004 /* We want to apply the previous 'running' weight (srv_uweight) only if there
3005 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003006 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003007 * It means that a configuration file change has precedence over a unix socket change
3008 * for server's weight
3009 *
3010 * by default, HAProxy applies the following weight when parsing the configuration
3011 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003012 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02003013 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003014 srv->uweight = srv_uweight;
3015 }
Willy Tarreau3ff577e2018-08-02 11:48:52 +02003016 server_recalc_eweight(srv, 1);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003017
Willy Tarreaue5a60682016-11-09 14:54:53 +01003018 /* load server IP address */
Daniel Corbett9215ffa2018-05-19 19:43:24 -04003019 if (strcmp(params[0], "-"))
3020 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003021
3022 if (fqdn && srv->hostname) {
3023 if (!strcmp(srv->hostname, fqdn)) {
3024 /* Here we reset the 'set from stats socket FQDN' flag
3025 * to support such transitions:
3026 * Let's say initial FQDN value is foo1 (in configuration file).
3027 * - FQDN changed from stats socket, from foo1 to foo2 value,
3028 * - FQDN changed again from file configuration (with the same previous value
3029 set from stats socket, from foo1 to foo2 value),
3030 * - reload for any other reason than a FQDN modification,
3031 * the configuration file FQDN matches the fqdn server state file value.
3032 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
3033 * any futher FQDN modification.
3034 */
Emeric Brun52a91d32017-08-31 14:41:55 +02003035 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003036 }
3037 else {
3038 /* If the FDQN has been changed from stats socket,
3039 * apply fqdn state file value (which is the value set
3040 * from stats socket).
3041 */
3042 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003043 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02003044 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003045 }
3046 }
3047 }
3048
Frédéric Lécaille31694712017-08-01 08:47:19 +02003049 if (port_str)
3050 srv->svc_port = port;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003051 HA_SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02003052
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003053 break;
3054 default:
3055 chunk_appendf(msg, ", version '%d' not supported", version);
3056 }
3057
3058 out:
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003059 if (msg->data) {
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003060 chunk_appendf(msg, "\n");
Christopher Faulet767a84b2017-11-24 16:50:31 +01003061 ha_warning("server-state application failed for server '%s/%s'%s",
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003062 srv->proxy->id, srv->id, msg->area);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01003063 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003064}
3065
3066/* This function parses all the proxies and only take care of the backends (since we're looking for server)
3067 * For each proxy, it does the following:
3068 * - opens its server state file (either one or local one)
3069 * - read whole file, line by line
3070 * - analyse each line to check if it matches our current backend:
3071 * - backend name matches
3072 * - backend id matches if id is forced and name doesn't match
3073 * - if the server pointed by the line is found, then state is applied
3074 *
3075 * If the running backend uuid or id differs from the state file, then HAProxy reports
3076 * a warning.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003077 *
3078 * Grabs the server's lock via srv_update_state().
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003079 */
3080void apply_server_state(void)
3081{
3082 char *cur, *end;
3083 char mybuf[SRV_STATE_LINE_MAXLEN];
3084 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003085 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
3086 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003087 int arg, srv_arg, version, diff;
3088 FILE *f;
3089 char *filepath;
3090 char globalfilepath[MAXPATHLEN + 1];
3091 char localfilepath[MAXPATHLEN + 1];
3092 int len, fileopenerr, globalfilepathlen, localfilepathlen;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003093 struct proxy *curproxy, *bk;
3094 struct server *srv;
3095
3096 globalfilepathlen = 0;
3097 /* create the globalfilepath variable */
3098 if (global.server_state_file) {
3099 /* absolute path or no base directory provided */
3100 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3101 len = strlen(global.server_state_file);
3102 if (len > MAXPATHLEN) {
3103 globalfilepathlen = 0;
3104 goto globalfileerror;
3105 }
3106 memcpy(globalfilepath, global.server_state_file, len);
3107 globalfilepath[len] = '\0';
3108 globalfilepathlen = len;
3109 }
3110 else if (global.server_state_base) {
3111 len = strlen(global.server_state_base);
3112 globalfilepathlen += len;
3113
3114 if (globalfilepathlen > MAXPATHLEN) {
3115 globalfilepathlen = 0;
3116 goto globalfileerror;
3117 }
3118 strncpy(globalfilepath, global.server_state_base, len);
3119 globalfilepath[globalfilepathlen] = 0;
3120
3121 /* append a slash if needed */
3122 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3123 if (globalfilepathlen + 1 > MAXPATHLEN) {
3124 globalfilepathlen = 0;
3125 goto globalfileerror;
3126 }
3127 globalfilepath[globalfilepathlen++] = '/';
3128 }
3129
3130 len = strlen(global.server_state_file);
3131 if (globalfilepathlen + len > MAXPATHLEN) {
3132 globalfilepathlen = 0;
3133 goto globalfileerror;
3134 }
3135 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3136 globalfilepathlen += len;
3137 globalfilepath[globalfilepathlen++] = 0;
3138 }
3139 }
3140 globalfileerror:
3141 if (globalfilepathlen == 0)
3142 globalfilepath[0] = '\0';
3143
3144 /* read servers state from local file */
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003145 for (curproxy = proxies_list; curproxy != NULL; curproxy = curproxy->next) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003146 /* servers are only in backends */
3147 if (!(curproxy->cap & PR_CAP_BE))
3148 continue;
3149 fileopenerr = 0;
3150 filepath = NULL;
3151
3152 /* search server state file path and name */
3153 switch (curproxy->load_server_state_from_file) {
3154 /* read servers state from global file */
3155 case PR_SRV_STATE_FILE_GLOBAL:
3156 /* there was an error while generating global server state file path */
3157 if (globalfilepathlen == 0)
3158 continue;
3159 filepath = globalfilepath;
3160 fileopenerr = 1;
3161 break;
3162 /* this backend has its own file */
3163 case PR_SRV_STATE_FILE_LOCAL:
3164 localfilepathlen = 0;
3165 localfilepath[0] = '\0';
3166 len = 0;
3167 /* create the localfilepath variable */
3168 /* absolute path or no base directory provided */
3169 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3170 len = strlen(curproxy->server_state_file_name);
3171 if (len > MAXPATHLEN) {
3172 localfilepathlen = 0;
3173 goto localfileerror;
3174 }
3175 memcpy(localfilepath, curproxy->server_state_file_name, len);
3176 localfilepath[len] = '\0';
3177 localfilepathlen = len;
3178 }
3179 else if (global.server_state_base) {
3180 len = strlen(global.server_state_base);
3181 localfilepathlen += len;
3182
3183 if (localfilepathlen > MAXPATHLEN) {
3184 localfilepathlen = 0;
3185 goto localfileerror;
3186 }
3187 strncpy(localfilepath, global.server_state_base, len);
3188 localfilepath[localfilepathlen] = 0;
3189
3190 /* append a slash if needed */
3191 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3192 if (localfilepathlen + 1 > MAXPATHLEN) {
3193 localfilepathlen = 0;
3194 goto localfileerror;
3195 }
3196 localfilepath[localfilepathlen++] = '/';
3197 }
3198
3199 len = strlen(curproxy->server_state_file_name);
3200 if (localfilepathlen + len > MAXPATHLEN) {
3201 localfilepathlen = 0;
3202 goto localfileerror;
3203 }
3204 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3205 localfilepathlen += len;
3206 localfilepath[localfilepathlen++] = 0;
3207 }
3208 filepath = localfilepath;
3209 localfileerror:
3210 if (localfilepathlen == 0)
3211 localfilepath[0] = '\0';
3212
3213 break;
3214 case PR_SRV_STATE_FILE_NONE:
3215 default:
3216 continue;
3217 }
3218
3219 /* preload global state file */
3220 errno = 0;
3221 f = fopen(filepath, "r");
3222 if (errno && fileopenerr)
Christopher Faulet767a84b2017-11-24 16:50:31 +01003223 ha_warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003224 if (!f)
3225 continue;
3226
3227 mybuf[0] = '\0';
3228 mybuflen = 0;
3229 version = 0;
3230
3231 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003232 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003233 ha_warning("Can't read first line of the server state file '%s'\n", filepath);
Dragan Dosencf4fb032015-11-04 23:03:26 +01003234 goto fileclose;
3235 }
3236
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003237 cur = mybuf;
3238 version = atoi(cur);
3239 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3240 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003241 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003242
3243 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3244 int bk_f_forced_id = 0;
3245 int check_id = 0;
3246 int check_name = 0;
3247
3248 mybuflen = strlen(mybuf);
3249 cur = mybuf;
3250 end = cur + mybuflen;
3251
3252 bk = NULL;
3253 srv = NULL;
3254
3255 /* we need at least one character */
3256 if (mybuflen == 0)
3257 continue;
3258
3259 /* ignore blank characters at the beginning of the line */
3260 while (isspace(*cur))
3261 ++cur;
3262
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003263 /* Ignore empty or commented lines */
3264 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003265 continue;
3266
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003267 /* truncated lines */
3268 if (mybuf[mybuflen - 1] != '\n') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003269 ha_warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003270 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003271 }
3272
3273 /* Removes trailing '\n' */
3274 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003275
3276 /* we're now ready to move the line into *srv_params[] */
3277 params[0] = cur;
3278 arg = 1;
3279 srv_arg = 0;
3280 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3281 if (isspace(*cur)) {
3282 *cur = '\0';
3283 ++cur;
3284 while (isspace(*cur))
3285 ++cur;
3286 switch (version) {
3287 case 1:
3288 /*
3289 * srv_addr: params[4] => srv_params[0]
3290 * srv_op_state: params[5] => srv_params[1]
3291 * srv_admin_state: params[6] => srv_params[2]
3292 * srv_uweight: params[7] => srv_params[3]
3293 * srv_iweight: params[8] => srv_params[4]
3294 * srv_last_time_change: params[9] => srv_params[5]
3295 * srv_check_status: params[10] => srv_params[6]
3296 * srv_check_result: params[11] => srv_params[7]
3297 * srv_check_health: params[12] => srv_params[8]
3298 * srv_check_state: params[13] => srv_params[9]
3299 * srv_agent_state: params[14] => srv_params[10]
3300 * bk_f_forced_id: params[15] => srv_params[11]
3301 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003302 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003303 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003304 */
3305 if (arg >= 4) {
3306 srv_params[srv_arg] = cur;
3307 ++srv_arg;
3308 }
3309 break;
3310 }
3311
3312 params[arg] = cur;
3313 ++arg;
3314 }
3315 else {
3316 ++cur;
3317 }
3318 }
3319
3320 /* if line is incomplete line, then ignore it.
3321 * otherwise, update useful flags */
3322 switch (version) {
3323 case 1:
3324 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3325 continue;
3326 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3327 check_id = (atoi(params[0]) == curproxy->uuid);
3328 check_name = (strcmp(curproxy->id, params[1]) == 0);
3329 break;
3330 }
3331
3332 diff = 0;
3333 bk = curproxy;
3334
3335 /* if backend can't be found, let's continue */
3336 if (!check_id && !check_name)
3337 continue;
3338 else if (!check_id && check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003339 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 +02003340 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3341 }
3342 else if (check_id && !check_name) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003343 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 +02003344 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3345 /* if name doesn't match, we still want to update curproxy if the backend id
3346 * was forced in previous the previous configuration */
3347 if (!bk_f_forced_id)
3348 continue;
3349 }
3350
3351 /* look for the server by its id: param[2] */
3352 /* else look for the server by its name: param[3] */
3353 diff = 0;
3354 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3355
3356 if (!srv) {
3357 /* if no server found, then warning and continue with next line */
Christopher Faulet767a84b2017-11-24 16:50:31 +01003358 ha_warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3359 params[3], params[2], params[0], params[1]);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003360 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3361 params[3], params[2], params[0], params[1]);
3362 continue;
3363 }
3364 else if (diff & PR_FBM_MISMATCH_ID) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003365 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 +02003366 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 +02003367 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003368 }
3369 else if (diff & PR_FBM_MISMATCH_NAME) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003370 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 +02003371 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 +02003372 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003373 }
3374
3375 /* now we can proceed with server's state update */
3376 srv_update_state(srv, version, srv_params);
3377 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003378fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003379 fclose(f);
3380 }
3381}
3382
Simon Horman7d09b9a2013-02-12 10:45:51 +09003383/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003384 * update a server's current IP address.
3385 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3386 * ip is in network format.
3387 * updater is a string which contains an information about the requester of the update.
3388 * updater is used if not NULL.
3389 *
3390 * A log line and a stderr warning message is generated based on server's backend options.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003391 *
3392 * Must be called with the server lock held.
Baptiste Assmann14e40142015-04-14 01:13:07 +02003393 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003394int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003395{
3396 /* generates a log line and a warning on stderr */
3397 if (1) {
3398 /* book enough space for both IPv4 and IPv6 */
3399 char oldip[INET6_ADDRSTRLEN];
3400 char newip[INET6_ADDRSTRLEN];
3401
3402 memset(oldip, '\0', INET6_ADDRSTRLEN);
3403 memset(newip, '\0', INET6_ADDRSTRLEN);
3404
3405 /* copy old IP address in a string */
3406 switch (s->addr.ss_family) {
3407 case AF_INET:
3408 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3409 break;
3410 case AF_INET6:
3411 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3412 break;
3413 };
3414
3415 /* copy new IP address in a string */
3416 switch (ip_sin_family) {
3417 case AF_INET:
3418 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3419 break;
3420 case AF_INET6:
3421 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3422 break;
3423 };
3424
3425 /* save log line into a buffer */
3426 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3427 s->proxy->id, s->id, oldip, newip, updater);
3428
3429 /* write the buffer on stderr */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003430 ha_warning("%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003431
3432 /* send a log */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003433 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003434 }
3435
3436 /* save the new IP family */
3437 s->addr.ss_family = ip_sin_family;
3438 /* save the new IP address */
3439 switch (ip_sin_family) {
3440 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003441 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003442 break;
3443 case AF_INET6:
3444 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3445 break;
3446 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003447 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003448
3449 return 0;
3450}
3451
3452/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003453 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3454 *
3455 * Caller can pass its name through <updater> to get it integrated in the response message
3456 * returned by the function.
3457 *
3458 * The function first does the following, in that order:
3459 * - validates the new addr and/or port
3460 * - checks if an update is required (new IP or port is different than current ones)
3461 * - checks the update is allowed:
3462 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3463 * - allow all changes if no CHECKS are configured
3464 * - if CHECK is configured:
3465 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3466 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3467 * conditions are met
Willy Tarreau46b7f532018-08-21 11:54:26 +02003468 *
3469 * Must be called with the server lock held.
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003470 */
3471const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3472{
3473 struct sockaddr_storage sa;
3474 int ret, port_change_required;
3475 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003476 uint16_t current_port, new_port;
Willy Tarreau83061a82018-07-13 11:56:34 +02003477 struct buffer *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003478 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003479
3480 msg = get_trash_chunk();
3481 chunk_reset(msg);
3482
3483 if (addr) {
3484 memset(&sa, 0, sizeof(struct sockaddr_storage));
3485 if (str2ip2(addr, &sa, 0) == NULL) {
3486 chunk_printf(msg, "Invalid addr '%s'", addr);
3487 goto out;
3488 }
3489
3490 /* changes are allowed on AF_INET* families only */
3491 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3492 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3493 goto out;
3494 }
3495
3496 /* collecting data currently setup */
3497 memset(current_addr, '\0', sizeof(current_addr));
3498 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3499 /* changes are allowed on AF_INET* families only */
3500 if ((ret != AF_INET) && (ret != AF_INET6)) {
3501 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3502 goto out;
3503 }
3504
3505 /* applying ADDR changes if required and allowed
3506 * ipcmp returns 0 when both ADDR are the same
3507 */
3508 if (ipcmp(&s->addr, &sa) == 0) {
3509 chunk_appendf(msg, "no need to change the addr");
3510 goto port;
3511 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003512 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003513 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003514
3515 /* we also need to update check's ADDR only if it uses the server's one */
3516 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003517 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003518 }
3519
3520 /* we also need to update agent ADDR only if it use the server's one */
3521 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003522 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003523 }
3524
3525 /* update report for caller */
3526 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3527 }
3528
3529 port:
3530 if (port) {
3531 char sign = '\0';
3532 char *endptr;
3533
3534 if (addr)
3535 chunk_appendf(msg, ", ");
3536
3537 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003538 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003539
3540 /* check if PORT change is required */
3541 port_change_required = 0;
3542
3543 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003544 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003545 new_port = strtol(port, &endptr, 10);
3546 if ((errno != 0) || (port == endptr)) {
3547 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3548 goto out;
3549 }
3550
3551 /* check if caller triggers a port mapped or offset */
3552 if (sign == '-' || (sign == '+')) {
3553 /* check if server currently uses port map */
3554 if (!(s->flags & SRV_F_MAPPORTS)) {
3555 /* switch from fixed port to port map mandatorily triggers
3556 * a port change */
3557 port_change_required = 1;
3558 /* check is configured
3559 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3560 * prevent PORT change if check doesn't have it's dedicated port while switching
3561 * to port mapping */
3562 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3563 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.");
3564 goto out;
3565 }
3566 }
3567 /* we're already using port maps */
3568 else {
3569 port_change_required = current_port != new_port;
3570 }
3571 }
3572 /* fixed port */
3573 else {
3574 port_change_required = current_port != new_port;
3575 }
3576
3577 /* applying PORT changes if required and update response message */
3578 if (port_change_required) {
3579 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003580 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003581 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003582
3583 /* prepare message */
3584 chunk_appendf(msg, "port changed from '");
3585 if (s->flags & SRV_F_MAPPORTS)
3586 chunk_appendf(msg, "+");
3587 chunk_appendf(msg, "%d' to '", current_port);
3588
3589 if (sign == '-') {
3590 s->flags |= SRV_F_MAPPORTS;
3591 chunk_appendf(msg, "%c", sign);
3592 /* just use for result output */
3593 new_port = -new_port;
3594 }
3595 else if (sign == '+') {
3596 s->flags |= SRV_F_MAPPORTS;
3597 chunk_appendf(msg, "%c", sign);
3598 }
3599 else {
3600 s->flags &= ~SRV_F_MAPPORTS;
3601 }
3602
3603 chunk_appendf(msg, "%d'", new_port);
3604
3605 /* we also need to update health checks port only if it uses server's realport */
3606 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3607 s->check.port = new_port;
3608 }
3609 }
3610 else {
3611 chunk_appendf(msg, "no need to change the port");
3612 }
3613 }
3614
3615out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003616 if (changed)
3617 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003618 if (updater)
3619 chunk_appendf(msg, " by '%s'", updater);
3620 chunk_appendf(msg, "\n");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003621 return msg->area;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003622}
3623
3624
3625/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003626 * update server status based on result of name resolution
3627 * returns:
3628 * 0 if server status is updated
3629 * 1 if server status has not changed
Willy Tarreau46b7f532018-08-21 11:54:26 +02003630 *
3631 * Must be called with the server lock held.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003632 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003633int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003634{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003635 struct dns_resolvers *resolvers = s->resolvers;
3636 struct dns_resolution *resolution = s->dns_requester->resolution;
3637 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003638
3639 switch (resolution->status) {
3640 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003641 /* status when HAProxy has just (re)started.
3642 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003643 break;
3644
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003645 case RSLV_STATUS_VALID:
3646 /*
3647 * resume health checks
3648 * server will be turned back on if health check is safe
3649 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003650 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003651 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003652 return 1;
3653 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3654 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003655 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003656 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003657
Emeric Brun52a91d32017-08-31 14:41:55 +02003658 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003659 return 1;
3660 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3661 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3662 s->proxy->id, s->id);
3663
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003664 ha_warning("%s.\n", trash.area);
3665 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.area);
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003666 return 0;
3667
3668 case RSLV_STATUS_NX:
3669 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003670 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3671 if (!tick_is_expired(exp, now_ms))
3672 break;
3673
3674 if (s->next_admin & SRV_ADMF_RMAINT)
3675 return 1;
3676 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3677 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003678
3679 case RSLV_STATUS_TIMEOUT:
3680 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003681 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3682 if (!tick_is_expired(exp, now_ms))
3683 break;
3684
3685 if (s->next_admin & SRV_ADMF_RMAINT)
3686 return 1;
3687 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3688 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003689
3690 case RSLV_STATUS_REFUSED:
3691 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003692 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3693 if (!tick_is_expired(exp, now_ms))
3694 break;
3695
3696 if (s->next_admin & SRV_ADMF_RMAINT)
3697 return 1;
3698 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3699 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003700
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003701 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003702 /* stop server if resolution failed for a long enough period */
3703 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3704 if (!tick_is_expired(exp, now_ms))
3705 break;
3706
3707 if (s->next_admin & SRV_ADMF_RMAINT)
3708 return 1;
3709 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3710 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003711 }
3712
3713 return 1;
3714}
3715
3716/*
3717 * Server Name Resolution valid response callback
3718 * It expects:
3719 * - <nameserver>: the name server which answered the valid response
3720 * - <response>: buffer containing a valid DNS response
3721 * - <response_len>: size of <response>
3722 * It performs the following actions:
3723 * - ignore response if current ip found and server family not met
3724 * - update with first new ip found if family is met and current IP is not found
3725 * returns:
3726 * 0 on error
3727 * 1 when no error or safe ignore
Olivier Houchard28381072017-11-06 17:30:28 +01003728 *
3729 * Must be called with server lock held
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003730 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003731int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003732{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003733 struct server *s = NULL;
3734 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003735 void *serverip, *firstip;
3736 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003737 int ret;
Willy Tarreau83061a82018-07-13 11:56:34 +02003738 struct buffer *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003739 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003740
Christopher Faulet67957bd2017-09-27 11:00:59 +02003741 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003742 if (!s)
3743 return 1;
3744
Christopher Faulet67957bd2017-09-27 11:00:59 +02003745 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003746
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003747 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003748 firstip = NULL; /* pointer to the first valid response found */
3749 /* it will be used as the new IP if a change is required */
3750 firstip_sin_family = AF_UNSPEC;
3751 serverip = NULL; /* current server IP address */
3752
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003753 /* initializing server IP pointer */
3754 server_sin_family = s->addr.ss_family;
3755 switch (server_sin_family) {
3756 case AF_INET:
3757 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3758 break;
3759
3760 case AF_INET6:
3761 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3762 break;
3763
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003764 case AF_UNSPEC:
3765 break;
3766
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003767 default:
3768 goto invalid;
3769 }
3770
Baptiste Assmann729c9012017-05-22 15:13:10 +02003771 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003772 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003773 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003774
3775 switch (ret) {
3776 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003777 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003778
3779 case DNS_UPD_SRVIP_NOT_FOUND:
3780 goto save_ip;
3781
3782 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003783 goto invalid;
3784
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003785 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003786 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003787 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003788
Baptiste Assmannfad03182015-10-28 02:03:32 +01003789 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003790 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003791 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003792 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003793
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003794 default:
3795 goto invalid;
3796
3797 }
3798
3799 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003800 if (nameserver) {
3801 nameserver->counters.update++;
3802 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003803 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003804 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003805 else
3806 chunk_printf(chk, "DNS cache");
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003807 update_server_addr(s, firstip, firstip_sin_family, (char *) chk->area);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003808
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003809 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003810 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003811 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003812
3813 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003814 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003815 nameserver->counters.invalid++;
3816 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003817 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003818 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003819 return 0;
3820}
3821
3822/*
3823 * Server Name Resolution error management callback
3824 * returns:
3825 * 0 on error
3826 * 1 when no error or safe ignore
Willy Tarreau46b7f532018-08-21 11:54:26 +02003827 *
3828 * Grabs the server's lock.
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003829 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003830int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003831{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003832 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003833
Christopher Faulet67957bd2017-09-27 11:00:59 +02003834 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003835 if (!s)
3836 return 1;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003837 HA_SPIN_LOCK(SERVER_LOCK, &s->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003838 snr_update_srv_status(s, 0);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003839 HA_SPIN_UNLOCK(SERVER_LOCK, &s->lock);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003840 return 1;
3841}
3842
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003843/*
3844 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003845 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003846 * It returns a pointer to the first server found or NULL if <ip> is not yet
3847 * assigned.
Olivier Houchard28381072017-11-06 17:30:28 +01003848 *
3849 * Must be called with server lock held
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003850 */
3851struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3852{
3853 struct server *tmpsrv;
3854 struct proxy *be;
3855
3856 if (!srv)
3857 return NULL;
3858
3859 be = srv->proxy;
3860 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
Emeric Brune9fd6b52017-11-02 17:20:39 +01003861 /* we found the current server is the same, ignore it */
3862 if (srv == tmpsrv)
3863 continue;
3864
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003865 /* We want to compare the IP in the record with the IP of the servers in the
3866 * same backend, only if:
3867 * * DNS resolution is enabled on the server
3868 * * the hostname used for the resolution by our server is the same than the
3869 * one used for the server found in the backend
3870 * * the server found in the backend is not our current server
3871 */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003872 HA_SPIN_LOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003873 if ((tmpsrv->hostname_dn == NULL) ||
3874 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3875 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Emeric Brune9fd6b52017-11-02 17:20:39 +01003876 (srv->puid == tmpsrv->puid)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003877 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003878 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003879 }
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003880
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003881 /* If the server has been taken down, don't consider it */
Emeric Brune9fd6b52017-11-02 17:20:39 +01003882 if (tmpsrv->next_admin & SRV_ADMF_RMAINT) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003883 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003884 continue;
Emeric Brune9fd6b52017-11-02 17:20:39 +01003885 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003886
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003887 /* At this point, we have 2 different servers using the same DNS hostname
3888 * for their respective resolution.
3889 */
3890 if (*ip_family == tmpsrv->addr.ss_family &&
3891 ((tmpsrv->addr.ss_family == AF_INET &&
3892 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3893 (tmpsrv->addr.ss_family == AF_INET6 &&
3894 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003895 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003896 return tmpsrv;
3897 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003898 HA_SPIN_UNLOCK(SERVER_LOCK, &tmpsrv->lock);
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003899 }
3900
Emeric Brune9fd6b52017-11-02 17:20:39 +01003901
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003902 return NULL;
3903}
3904
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003905/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3906 * resolver. This is suited for initial address configuration. Returns 0 on
3907 * success otherwise a non-zero error code. In case of error, *err_code, if
3908 * not NULL, is filled up.
3909 */
3910int srv_set_addr_via_libc(struct server *srv, int *err_code)
3911{
3912 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003913 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003914 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003915 return 1;
3916 }
3917 return 0;
3918}
3919
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003920/* Set the server's FDQN (->hostname) from <hostname>.
3921 * Returns -1 if failed, 0 if not.
Willy Tarreau46b7f532018-08-21 11:54:26 +02003922 *
3923 * Must be called with the server lock held.
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003924 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003925int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003926{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003927 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003928 char *hostname_dn;
3929 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003930
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003931 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003932 HA_SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003933 /* run time DNS resolution was not active for this server
3934 * and we can't enable it at run time for now.
3935 */
3936 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003937 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003938
3939 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003940 hostname_len = strlen(hostname);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003941 hostname_dn = trash.area;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003942 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3943 hostname_dn, trash.size);
3944 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003945 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003946
Christopher Faulet67957bd2017-09-27 11:00:59 +02003947 resolution = srv->dns_requester->resolution;
3948 if (resolution &&
3949 resolution->hostname_dn &&
3950 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003951 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003952
Christopher Faulet67957bd2017-09-27 11:00:59 +02003953 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003954
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003955 free(srv->hostname);
3956 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003957 srv->hostname = strdup(hostname);
3958 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003959 srv->hostname_dn_len = hostname_dn_len;
3960 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003961 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003962
Olivier Houchard55dcdf42017-11-06 15:15:04 +01003963 if (dns_link_resolution(srv, OBJ_TYPE_SERVER, 1) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003964 goto err;
3965
3966 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003967 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003968 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003969 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003970
3971 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003972 if (!dns_locked)
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003973 HA_SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003974 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003975}
3976
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003977/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3978 * from the state file. This is suited for initial address configuration.
3979 * Returns 0 on success otherwise a non-zero error code. In case of error,
3980 * *err_code, if not NULL, is filled up.
3981 */
3982static int srv_apply_lastaddr(struct server *srv, int *err_code)
3983{
3984 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3985 if (err_code)
3986 *err_code |= ERR_WARN;
3987 return 1;
3988 }
3989 return 0;
3990}
3991
Willy Tarreau25e51522016-11-04 15:10:17 +01003992/* returns 0 if no error, otherwise a combination of ERR_* flags */
3993static int srv_iterate_initaddr(struct server *srv)
3994{
3995 int return_code = 0;
3996 int err_code;
3997 unsigned int methods;
3998
3999 methods = srv->init_addr_methods;
4000 if (!methods) { // default to "last,libc"
4001 srv_append_initaddr(&methods, SRV_IADDR_LAST);
4002 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
4003 }
4004
Willy Tarreau3eed10e2016-11-07 21:03:16 +01004005 /* "-dr" : always append "none" so that server addresses resolution
4006 * failures are silently ignored, this is convenient to validate some
4007 * configs out of their environment.
4008 */
4009 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
4010 srv_append_initaddr(&methods, SRV_IADDR_NONE);
4011
Willy Tarreau25e51522016-11-04 15:10:17 +01004012 while (methods) {
4013 err_code = 0;
4014 switch (srv_get_next_initaddr(&methods)) {
4015 case SRV_IADDR_LAST:
4016 if (!srv->lastaddr)
4017 continue;
4018 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004019 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004020 return_code |= err_code;
4021 break;
4022
4023 case SRV_IADDR_LIBC:
4024 if (!srv->hostname)
4025 continue;
4026 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01004027 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01004028 return_code |= err_code;
4029 break;
4030
Willy Tarreau37ebe122016-11-04 15:17:58 +01004031 case SRV_IADDR_NONE:
4032 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004033 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004034 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
4035 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau465b6e52016-11-07 19:19:22 +01004036 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01004037 return return_code;
4038
Willy Tarreau4310d362016-11-02 15:05:56 +01004039 case SRV_IADDR_IP:
4040 ipcpy(&srv->init_addr, &srv->addr);
4041 if (return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004042 ha_warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
4043 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
Willy Tarreau4310d362016-11-02 15:05:56 +01004044 }
Olivier Houchard4e694042017-03-14 20:01:29 +01004045 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01004046
Willy Tarreau25e51522016-11-04 15:10:17 +01004047 default: /* unhandled method */
4048 break;
4049 }
4050 }
4051
4052 if (!return_code) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004053 ha_alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
Willy Tarreau25e51522016-11-04 15:10:17 +01004054 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4055 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01004056 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004057 ha_alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
Willy Tarreau465b6e52016-11-07 19:19:22 +01004058 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
4059 }
Willy Tarreau25e51522016-11-04 15:10:17 +01004060
4061 return_code |= ERR_ALERT | ERR_FATAL;
4062 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01004063out:
4064 srv_set_dyncookie(srv);
4065 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01004066}
4067
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004068/*
4069 * This function parses all backends and all servers within each backend
4070 * and performs servers' addr resolution based on information provided by:
4071 * - configuration file
4072 * - server-state file (states provided by an 'old' haproxy process)
4073 *
4074 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
4075 */
4076int srv_init_addr(void)
4077{
4078 struct proxy *curproxy;
4079 int return_code = 0;
4080
Olivier Houchardfbc74e82017-11-24 16:54:05 +01004081 curproxy = proxies_list;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004082 while (curproxy) {
4083 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004084
4085 /* servers are in backend only */
4086 if (!(curproxy->cap & PR_CAP_BE))
4087 goto srv_init_addr_next;
4088
Willy Tarreau25e51522016-11-04 15:10:17 +01004089 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02004090 if (srv->hostname)
4091 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004092
4093 srv_init_addr_next:
4094 curproxy = curproxy->next;
4095 }
4096
4097 return return_code;
4098}
4099
Willy Tarreau46b7f532018-08-21 11:54:26 +02004100/*
4101 * Must be called with the server lock held.
4102 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004103const 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 +02004104{
4105
Willy Tarreau83061a82018-07-13 11:56:34 +02004106 struct buffer *msg;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004107
4108 msg = get_trash_chunk();
4109 chunk_reset(msg);
4110
Olivier Houchard8da5f982017-08-04 18:35:36 +02004111 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004112 chunk_appendf(msg, "no need to change the FDQN");
4113 goto out;
4114 }
4115
4116 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4117 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4118 goto out;
4119 }
4120
4121 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4122 server->proxy->id, server->id, server->hostname, fqdn);
4123
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004124 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004125 chunk_reset(msg);
4126 chunk_appendf(msg, "could not update %s/%s FQDN",
4127 server->proxy->id, server->id);
4128 goto out;
4129 }
4130
4131 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004132 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004133
4134 out:
4135 if (updater)
4136 chunk_appendf(msg, " by '%s'", updater);
4137 chunk_appendf(msg, "\n");
4138
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004139 return msg->area;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004140}
4141
4142
Willy Tarreau21b069d2016-11-23 17:15:08 +01004143/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4144 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004145 * 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 +01004146 * used for CLI commands requiring a server name.
4147 * Important: the <arg> is modified to remove the '/'.
4148 */
4149struct server *cli_find_server(struct appctx *appctx, char *arg)
4150{
4151 struct proxy *px;
4152 struct server *sv;
4153 char *line;
4154
4155 /* split "backend/server" and make <line> point to server */
4156 for (line = arg; *line; line++)
4157 if (*line == '/') {
4158 *line++ = '\0';
4159 break;
4160 }
4161
4162 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004163 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004164 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004165 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004166 return NULL;
4167 }
4168
4169 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004170 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004171 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004172 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004173 return NULL;
4174 }
4175
4176 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004177 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004178 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004179 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004180 return NULL;
4181 }
4182
4183 return sv;
4184}
4185
William Lallemand222baf22016-11-19 02:00:33 +01004186
Willy Tarreau46b7f532018-08-21 11:54:26 +02004187/* grabs the server lock */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004188static int cli_parse_set_server(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand222baf22016-11-19 02:00:33 +01004189{
4190 struct server *sv;
4191 const char *warning;
4192
4193 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4194 return 1;
4195
4196 sv = cli_find_server(appctx, args[2]);
4197 if (!sv)
4198 return 1;
4199
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004200 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Emeric Brun9f0b4582017-10-23 14:39:51 +02004201
William Lallemand222baf22016-11-19 02:00:33 +01004202 if (strcmp(args[3], "weight") == 0) {
4203 warning = server_parse_weight_change_request(sv, args[4]);
4204 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004205 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004206 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004207 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004208 }
4209 }
4210 else if (strcmp(args[3], "state") == 0) {
4211 if (strcmp(args[4], "ready") == 0)
4212 srv_adm_set_ready(sv);
4213 else if (strcmp(args[4], "drain") == 0)
4214 srv_adm_set_drain(sv);
4215 else if (strcmp(args[4], "maint") == 0)
4216 srv_adm_set_maint(sv);
4217 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004218 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004219 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004220 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004221 }
4222 }
4223 else if (strcmp(args[3], "health") == 0) {
4224 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004225 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004226 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004227 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004228 }
4229 else if (strcmp(args[4], "up") == 0) {
4230 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004231 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004232 }
4233 else if (strcmp(args[4], "stopping") == 0) {
4234 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004235 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004236 }
4237 else if (strcmp(args[4], "down") == 0) {
4238 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004239 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004240 }
4241 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004242 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004243 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004244 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004245 }
4246 }
4247 else if (strcmp(args[3], "agent") == 0) {
4248 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004249 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004250 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004251 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004252 }
4253 else if (strcmp(args[4], "up") == 0) {
4254 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004255 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004256 }
4257 else if (strcmp(args[4], "down") == 0) {
4258 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004259 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004260 }
4261 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004262 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004263 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004264 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004265 }
4266 }
Misiek2da082d2017-01-09 09:40:42 +01004267 else if (strcmp(args[3], "agent-addr") == 0) {
4268 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004269 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004270 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4271 appctx->st0 = CLI_ST_PRINT;
4272 } else {
4273 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004274 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004275 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4276 appctx->st0 = CLI_ST_PRINT;
4277 }
4278 }
4279 }
4280 else if (strcmp(args[3], "agent-send") == 0) {
4281 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004282 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004283 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4284 appctx->st0 = CLI_ST_PRINT;
4285 } else {
4286 char *nss = strdup(args[4]);
4287 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004288 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004289 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4290 appctx->st0 = CLI_ST_PRINT;
4291 } else {
4292 free(sv->agent.send_string);
4293 sv->agent.send_string = nss;
4294 sv->agent.send_string_len = strlen(args[4]);
4295 }
4296 }
4297 }
William Lallemand222baf22016-11-19 02:00:33 +01004298 else if (strcmp(args[3], "check-port") == 0) {
4299 int i = 0;
4300 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004301 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004302 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004303 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004304 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004305 }
4306 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004307 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004308 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004309 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004310 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004311 }
4312 /* prevent the update of port to 0 if MAPPORTS are in use */
4313 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004314 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004315 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004316 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004317 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004318 }
4319 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004320 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004321 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004322 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004323 }
4324 else if (strcmp(args[3], "addr") == 0) {
4325 char *addr = NULL;
4326 char *port = NULL;
4327 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004328 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004329 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004330 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004331 goto out_unlock;
William Lallemand222baf22016-11-19 02:00:33 +01004332 }
4333 else {
4334 addr = args[4];
4335 }
4336 if (strcmp(args[5], "port") == 0) {
4337 port = args[6];
4338 }
4339 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4340 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004341 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004342 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004343 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004344 }
4345 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4346 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004347 else if (strcmp(args[3], "fqdn") == 0) {
4348 if (!*args[4]) {
Willy Tarreaua0752582017-11-05 10:17:49 +01004349 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004350 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4351 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004352 goto out_unlock;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004353 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004354 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004355 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004356 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004357 appctx->ctx.cli.msg = warning;
4358 appctx->st0 = CLI_ST_PRINT;
4359 }
4360 }
William Lallemand222baf22016-11-19 02:00:33 +01004361 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004362 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004363 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 +01004364 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004365 }
Willy Tarreau6ce38f32017-11-05 10:19:23 +01004366 out_unlock:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01004367 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004368 return 1;
4369}
4370
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004371static int cli_parse_get_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004372{
4373 struct stream_interface *si = appctx->owner;
4374 struct proxy *px;
4375 struct server *sv;
4376 char *line;
4377
4378
4379 /* split "backend/server" and make <line> point to server */
4380 for (line = args[2]; *line; line++)
4381 if (*line == '/') {
4382 *line++ = '\0';
4383 break;
4384 }
4385
4386 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004387 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004388 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004389 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004390 return 1;
4391 }
4392
4393 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004394 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004395 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004396 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004397 return 1;
4398 }
4399
4400 /* return server's effective weight at the moment */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004401 snprintf(trash.area, trash.size, "%d (initial %d)\n", sv->uweight,
4402 sv->iweight);
4403 if (ci_putstr(si_ic(si), trash.area) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004404 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004405 return 0;
4406 }
William Lallemand6b160942016-11-22 12:34:35 +01004407 return 1;
4408}
4409
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004410/* Parse a "set weight" command.
4411 *
4412 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004413 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004414static int cli_parse_set_weight(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand6b160942016-11-22 12:34:35 +01004415{
4416 struct server *sv;
4417 const char *warning;
4418
4419 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4420 return 1;
4421
4422 sv = cli_find_server(appctx, args[2]);
4423 if (!sv)
4424 return 1;
4425
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004426 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4427
William Lallemand6b160942016-11-22 12:34:35 +01004428 warning = server_parse_weight_change_request(sv, args[3]);
4429 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004430 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004431 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004432 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004433 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004434
4435 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4436
William Lallemand6b160942016-11-22 12:34:35 +01004437 return 1;
4438}
4439
Willy Tarreau46b7f532018-08-21 11:54:26 +02004440/* parse a "set maxconn server" command. It always returns 1.
4441 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004442 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004443 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004444static int cli_parse_set_maxconn_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreaub8026272016-11-23 11:26:56 +01004445{
4446 struct server *sv;
4447 const char *warning;
4448
4449 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4450 return 1;
4451
4452 sv = cli_find_server(appctx, args[3]);
4453 if (!sv)
4454 return 1;
4455
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004456 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
4457
Willy Tarreaub8026272016-11-23 11:26:56 +01004458 warning = server_parse_maxconn_change_request(sv, args[4]);
4459 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004460 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004461 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004462 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004463 }
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004464
4465 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
4466
Willy Tarreaub8026272016-11-23 11:26:56 +01004467 return 1;
4468}
William Lallemand6b160942016-11-22 12:34:35 +01004469
Willy Tarreau46b7f532018-08-21 11:54:26 +02004470/* parse a "disable agent" command. It always returns 1.
4471 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004472 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004473 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004474static int cli_parse_disable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004475{
4476 struct server *sv;
4477
4478 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4479 return 1;
4480
4481 sv = cli_find_server(appctx, args[2]);
4482 if (!sv)
4483 return 1;
4484
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004485 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004486 sv->agent.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004487 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004488 return 1;
4489}
4490
Willy Tarreau46b7f532018-08-21 11:54:26 +02004491/* parse a "disable health" command. It always returns 1.
4492 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004493 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004494 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004495static int cli_parse_disable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004496{
4497 struct server *sv;
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);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004507 sv->check.state &= ~CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004508 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004509 return 1;
4510}
4511
Willy Tarreau46b7f532018-08-21 11:54:26 +02004512/* parse a "disable server" command. It always returns 1.
4513 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004514 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004515 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004516static int cli_parse_disable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004517{
4518 struct server *sv;
4519
4520 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4521 return 1;
4522
4523 sv = cli_find_server(appctx, args[2]);
4524 if (!sv)
4525 return 1;
4526
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004527 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004528 srv_adm_set_maint(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004529 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004530 return 1;
4531}
4532
Willy Tarreau46b7f532018-08-21 11:54:26 +02004533/* parse a "enable agent" command. It always returns 1.
4534 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004535 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004536 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004537static int cli_parse_enable_agent(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004538{
4539 struct server *sv;
4540
4541 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4542 return 1;
4543
4544 sv = cli_find_server(appctx, args[2]);
4545 if (!sv)
4546 return 1;
4547
4548 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004549 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004550 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004551 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004552 return 1;
4553 }
4554
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004555 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004556 sv->agent.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004557 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004558 return 1;
4559}
4560
Willy Tarreau46b7f532018-08-21 11:54:26 +02004561/* parse a "enable health" command. It always returns 1.
4562 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004563 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004564 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004565static int cli_parse_enable_health(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004566{
4567 struct server *sv;
4568
4569 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4570 return 1;
4571
4572 sv = cli_find_server(appctx, args[2]);
4573 if (!sv)
4574 return 1;
4575
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004576 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004577 sv->check.state |= CHK_ST_ENABLED;
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004578 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004579 return 1;
4580}
4581
Willy Tarreau46b7f532018-08-21 11:54:26 +02004582/* parse a "enable server" command. It always returns 1.
4583 *
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004584 * Grabs the server lock.
Willy Tarreau46b7f532018-08-21 11:54:26 +02004585 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02004586static int cli_parse_enable_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreauffb4d582016-11-24 12:47:00 +01004587{
4588 struct server *sv;
4589
4590 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4591 return 1;
4592
4593 sv = cli_find_server(appctx, args[2]);
4594 if (!sv)
4595 return 1;
4596
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004597 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004598 srv_adm_set_ready(sv);
Olivier Houcharde9bad0a2018-01-17 17:39:34 +01004599 if (!(sv->flags & SRV_F_COOKIESET)
4600 && (sv->proxy->ck_opts & PR_CK_DYNAMIC) &&
4601 sv->cookie)
4602 srv_check_for_dup_dyncookie(sv);
Willy Tarreau3bcc2692018-08-21 15:35:31 +02004603 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreauffb4d582016-11-24 12:47:00 +01004604 return 1;
4605}
4606
William Lallemand222baf22016-11-19 02:00:33 +01004607/* register cli keywords */
4608static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004609 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004610 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004611 { { "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 +01004612 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004613 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004614 { { "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 +01004615 { { "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 +01004616 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004617 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4618 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4619
William Lallemand222baf22016-11-19 02:00:33 +01004620 {{},}
4621}};
4622
4623__attribute__((constructor))
4624static void __server_init(void)
4625{
4626 cli_register_kw(&cli_kws);
4627}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004628
Emeric Brun64cc49c2017-10-03 14:46:45 +02004629/*
4630 * This function applies server's status changes, it is
4631 * is designed to be called asynchronously.
4632 *
Willy Tarreau46b7f532018-08-21 11:54:26 +02004633 * Must be called with the server lock held.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004634 */
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004635static void srv_update_status(struct server *s)
Emeric Brun64cc49c2017-10-03 14:46:45 +02004636{
4637 struct check *check = &s->check;
4638 int xferred;
4639 struct proxy *px = s->proxy;
4640 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4641 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4642 int log_level;
Willy Tarreau83061a82018-07-13 11:56:34 +02004643 struct buffer *tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004644
Emeric Brun64cc49c2017-10-03 14:46:45 +02004645 /* If currently main is not set we try to apply pending state changes */
4646 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4647 int next_admin;
4648
4649 /* Backup next admin */
4650 next_admin = s->next_admin;
4651
4652 /* restore current admin state */
4653 s->next_admin = s->cur_admin;
4654
4655 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4656 s->last_change = now.tv_sec;
4657 if (s->proxy->lbprm.set_server_status_down)
4658 s->proxy->lbprm.set_server_status_down(s);
4659
4660 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4661 srv_shutdown_streams(s, SF_ERR_DOWN);
4662
4663 /* we might have streams queued on this server and waiting for
4664 * a connection. Those which are redispatchable will be queued
4665 * to another server or to the proxy itself.
4666 */
4667 xferred = pendconn_redistribute(s);
4668
4669 tmptrash = alloc_trash_chunk();
4670 if (tmptrash) {
4671 chunk_printf(tmptrash,
4672 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4673 s->proxy->id, s->id);
4674
Emeric Brun5a133512017-10-19 14:42:30 +02004675 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004676 ha_warning("%s.\n", tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004677
4678 /* we don't send an alert if the server was previously paused */
4679 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004680 send_log(s->proxy, log_level, "%s.\n",
4681 tmptrash->area);
4682 send_email_alert(s, log_level, "%s",
4683 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004684 free_trash_chunk(tmptrash);
4685 tmptrash = NULL;
4686 }
4687 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4688 set_backend_down(s->proxy);
4689
4690 s->counters.down_trans++;
4691 }
4692 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4693 s->last_change = now.tv_sec;
4694 if (s->proxy->lbprm.set_server_status_down)
4695 s->proxy->lbprm.set_server_status_down(s);
4696
4697 /* we might have streams queued on this server and waiting for
4698 * a connection. Those which are redispatchable will be queued
4699 * to another server or to the proxy itself.
4700 */
4701 xferred = pendconn_redistribute(s);
4702
4703 tmptrash = alloc_trash_chunk();
4704 if (tmptrash) {
4705 chunk_printf(tmptrash,
4706 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4707 s->proxy->id, s->id);
4708
Emeric Brun5a133512017-10-19 14:42:30 +02004709 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004710
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004711 ha_warning("%s.\n", tmptrash->area);
4712 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4713 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004714 free_trash_chunk(tmptrash);
4715 tmptrash = NULL;
4716 }
4717
4718 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4719 set_backend_down(s->proxy);
4720 }
4721 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4722 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4723 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4724 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4725 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4726 s->proxy->last_change = now.tv_sec;
4727 }
4728
4729 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4730 s->down_time += now.tv_sec - s->last_change;
4731
4732 s->last_change = now.tv_sec;
4733 if (s->next_state == SRV_ST_STARTING)
4734 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4735
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004736 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004737 /* now propagate the status change to any LB algorithms */
4738 if (px->lbprm.update_server_eweight)
4739 px->lbprm.update_server_eweight(s);
4740 else if (srv_willbe_usable(s)) {
4741 if (px->lbprm.set_server_status_up)
4742 px->lbprm.set_server_status_up(s);
4743 }
4744 else {
4745 if (px->lbprm.set_server_status_down)
4746 px->lbprm.set_server_status_down(s);
4747 }
4748
4749 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4750 * and it's not a backup server and its effective weight is > 0,
4751 * then it can accept new connections, so we shut down all streams
4752 * on all backup servers.
4753 */
4754 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4755 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4756 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4757
4758 /* check if we can handle some connections queued at the proxy. We
4759 * will take as many as we can handle.
4760 */
4761 xferred = pendconn_grab_from_px(s);
4762
4763 tmptrash = alloc_trash_chunk();
4764 if (tmptrash) {
4765 chunk_printf(tmptrash,
4766 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4767 s->proxy->id, s->id);
4768
Emeric Brun5a133512017-10-19 14:42:30 +02004769 srv_append_status(tmptrash, s, NULL, xferred, 0);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004770 ha_warning("%s.\n", tmptrash->area);
4771 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4772 tmptrash->area);
4773 send_email_alert(s, LOG_NOTICE, "%s",
4774 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004775 free_trash_chunk(tmptrash);
4776 tmptrash = NULL;
4777 }
4778
4779 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4780 set_backend_down(s->proxy);
4781 }
4782 else if (s->cur_eweight != s->next_eweight) {
4783 /* now propagate the status change to any LB algorithms */
4784 if (px->lbprm.update_server_eweight)
4785 px->lbprm.update_server_eweight(s);
4786 else if (srv_willbe_usable(s)) {
4787 if (px->lbprm.set_server_status_up)
4788 px->lbprm.set_server_status_up(s);
4789 }
4790 else {
4791 if (px->lbprm.set_server_status_down)
4792 px->lbprm.set_server_status_down(s);
4793 }
4794
4795 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4796 set_backend_down(s->proxy);
4797 }
4798
4799 s->next_admin = next_admin;
4800 }
4801
Emeric Brun5a133512017-10-19 14:42:30 +02004802 /* reset operational state change */
4803 *s->op_st_chg.reason = 0;
4804 s->op_st_chg.status = s->op_st_chg.code = -1;
4805 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004806
4807 /* Now we try to apply pending admin changes */
4808
4809 /* Maintenance must also disable health checks */
4810 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4811 if (s->check.state & CHK_ST_ENABLED) {
4812 s->check.state |= CHK_ST_PAUSED;
4813 check->health = 0;
4814 }
4815
4816 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004817 tmptrash = alloc_trash_chunk();
4818 if (tmptrash) {
4819 chunk_printf(tmptrash,
4820 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4821 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4822 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004823
Olivier Houchard796a2b32017-10-24 17:42:47 +02004824 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004825
Olivier Houchard796a2b32017-10-24 17:42:47 +02004826 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004827 ha_warning("%s.\n", tmptrash->area);
4828 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4829 tmptrash->area);
Olivier Houchard796a2b32017-10-24 17:42:47 +02004830 }
4831 free_trash_chunk(tmptrash);
4832 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004833 }
Emeric Brun8f298292017-12-06 16:47:17 +01004834 /* commit new admin status */
4835
4836 s->cur_admin = s->next_admin;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004837 }
4838 else { /* server was still running */
4839 check->health = 0; /* failure */
4840 s->last_change = now.tv_sec;
Emeric Brune3114802017-12-21 14:42:26 +01004841
4842 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004843 if (s->proxy->lbprm.set_server_status_down)
4844 s->proxy->lbprm.set_server_status_down(s);
4845
Emeric Brun64cc49c2017-10-03 14:46:45 +02004846 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4847 srv_shutdown_streams(s, SF_ERR_DOWN);
4848
4849 /* we might have streams queued on this server and waiting for
4850 * a connection. Those which are redispatchable will be queued
4851 * to another server or to the proxy itself.
4852 */
4853 xferred = pendconn_redistribute(s);
4854
4855 tmptrash = alloc_trash_chunk();
4856 if (tmptrash) {
4857 chunk_printf(tmptrash,
4858 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4859 s->flags & SRV_F_BACKUP ? "Backup " : "",
4860 s->proxy->id, s->id,
4861 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4862
4863 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4864
4865 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004866 ha_warning("%s.\n", tmptrash->area);
4867 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n",
4868 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004869 }
4870 free_trash_chunk(tmptrash);
4871 tmptrash = NULL;
4872 }
4873 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4874 set_backend_down(s->proxy);
4875
4876 s->counters.down_trans++;
4877 }
4878 }
4879 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4880 /* OK here we're leaving maintenance, we have many things to check,
4881 * because the server might possibly be coming back up depending on
4882 * its state. In practice, leaving maintenance means that we should
4883 * immediately turn to UP (more or less the slowstart) under the
4884 * following conditions :
4885 * - server is neither checked nor tracked
4886 * - server tracks another server which is not checked
4887 * - server tracks another server which is already up
4888 * Which sums up as something simpler :
4889 * "either the tracking server is up or the server's checks are disabled
4890 * or up". Otherwise we only re-enable health checks. There's a special
4891 * case associated to the stopping state which can be inherited. Note
4892 * that the server might still be in drain mode, which is naturally dealt
4893 * with by the lower level functions.
4894 */
4895
4896 if (s->check.state & CHK_ST_ENABLED) {
4897 s->check.state &= ~CHK_ST_PAUSED;
4898 check->health = check->rise; /* start OK but check immediately */
4899 }
4900
4901 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4902 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4903 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4904 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4905 s->next_state = SRV_ST_STOPPING;
4906 }
4907 else {
4908 s->next_state = SRV_ST_STARTING;
4909 if (s->slowstart > 0)
4910 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4911 else
4912 s->next_state = SRV_ST_RUNNING;
4913 }
4914
4915 }
4916
4917 tmptrash = alloc_trash_chunk();
4918 if (tmptrash) {
4919 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4920 chunk_printf(tmptrash,
4921 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4922 s->flags & SRV_F_BACKUP ? "Backup " : "",
4923 s->proxy->id, s->id,
4924 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4925 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4926 }
4927 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4928 chunk_printf(tmptrash,
4929 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4930 s->flags & SRV_F_BACKUP ? "Backup " : "",
4931 s->proxy->id, s->id, s->hostname,
4932 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4933 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4934 }
4935 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4936 chunk_printf(tmptrash,
4937 "%sServer %s/%s is %s/%s (leaving maintenance)",
4938 s->flags & SRV_F_BACKUP ? "Backup " : "",
4939 s->proxy->id, s->id,
4940 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4941 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4942 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004943 ha_warning("%s.\n", tmptrash->area);
4944 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4945 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004946 free_trash_chunk(tmptrash);
4947 tmptrash = NULL;
4948 }
4949
Willy Tarreau3ff577e2018-08-02 11:48:52 +02004950 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004951 /* now propagate the status change to any LB algorithms */
4952 if (px->lbprm.update_server_eweight)
4953 px->lbprm.update_server_eweight(s);
4954 else if (srv_willbe_usable(s)) {
4955 if (px->lbprm.set_server_status_up)
4956 px->lbprm.set_server_status_up(s);
4957 }
4958 else {
4959 if (px->lbprm.set_server_status_down)
4960 px->lbprm.set_server_status_down(s);
4961 }
4962
4963 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4964 set_backend_down(s->proxy);
4965
Willy Tarreau6a78e612018-08-07 10:14:53 +02004966 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4967 * and it's not a backup server and its effective weight is > 0,
4968 * then it can accept new connections, so we shut down all streams
4969 * on all backup servers.
4970 */
4971 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4972 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4973 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4974
4975 /* check if we can handle some connections queued at the proxy. We
4976 * will take as many as we can handle.
4977 */
4978 xferred = pendconn_grab_from_px(s);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004979 }
4980 else if (s->next_admin & SRV_ADMF_MAINT) {
4981 /* remaining in maintenance mode, let's inform precisely about the
4982 * situation.
4983 */
4984 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4985 tmptrash = alloc_trash_chunk();
4986 if (tmptrash) {
4987 chunk_printf(tmptrash,
4988 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4989 s->flags & SRV_F_BACKUP ? "Backup " : "",
4990 s->proxy->id, s->id);
4991
4992 if (s->track) /* normally it's mandatory here */
4993 chunk_appendf(tmptrash, " via %s/%s",
4994 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004995 ha_warning("%s.\n", tmptrash->area);
4996 send_log(s->proxy, LOG_NOTICE, "%s.\n",
4997 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004998 free_trash_chunk(tmptrash);
4999 tmptrash = NULL;
5000 }
5001 }
5002 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
5003 tmptrash = alloc_trash_chunk();
5004 if (tmptrash) {
5005 chunk_printf(tmptrash,
5006 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
5007 s->flags & SRV_F_BACKUP ? "Backup " : "",
5008 s->proxy->id, s->id, s->hostname);
5009
5010 if (s->track) /* normally it's mandatory here */
5011 chunk_appendf(tmptrash, " via %s/%s",
5012 s->track->proxy->id, s->track->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005013 ha_warning("%s.\n", tmptrash->area);
5014 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5015 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005016 free_trash_chunk(tmptrash);
5017 tmptrash = NULL;
5018 }
5019 }
5020 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
5021 tmptrash = alloc_trash_chunk();
5022 if (tmptrash) {
5023 chunk_printf(tmptrash,
5024 "%sServer %s/%s remains in forced maintenance",
5025 s->flags & SRV_F_BACKUP ? "Backup " : "",
5026 s->proxy->id, s->id);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005027 ha_warning("%s.\n", tmptrash->area);
5028 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5029 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005030 free_trash_chunk(tmptrash);
5031 tmptrash = NULL;
5032 }
5033 }
5034 /* don't report anything when leaving drain mode and remaining in maintenance */
5035
5036 s->cur_admin = s->next_admin;
5037 }
5038
5039 if (!(s->next_admin & SRV_ADMF_MAINT)) {
5040 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
5041 /* drain state is applied only if not yet in maint */
5042
5043 s->last_change = now.tv_sec;
5044 if (px->lbprm.set_server_status_down)
5045 px->lbprm.set_server_status_down(s);
5046
5047 /* we might have streams queued on this server and waiting for
5048 * a connection. Those which are redispatchable will be queued
5049 * to another server or to the proxy itself.
5050 */
5051 xferred = pendconn_redistribute(s);
5052
5053 tmptrash = alloc_trash_chunk();
5054 if (tmptrash) {
5055 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
5056 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
5057 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
5058
5059 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
5060
5061 if (!(global.mode & MODE_STARTING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005062 ha_warning("%s.\n", tmptrash->area);
5063 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5064 tmptrash->area);
5065 send_email_alert(s, LOG_NOTICE, "%s",
5066 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005067 }
5068 free_trash_chunk(tmptrash);
5069 tmptrash = NULL;
5070 }
5071
5072 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
5073 set_backend_down(s->proxy);
5074 }
5075 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
5076 /* OK completely leaving drain mode */
5077 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
5078 if (s->proxy->last_change < now.tv_sec) // ignore negative times
5079 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
5080 s->proxy->last_change = now.tv_sec;
5081 }
5082
5083 if (s->last_change < now.tv_sec) // ignore negative times
5084 s->down_time += now.tv_sec - s->last_change;
5085 s->last_change = now.tv_sec;
Willy Tarreau3ff577e2018-08-02 11:48:52 +02005086 server_recalc_eweight(s, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005087
5088 tmptrash = alloc_trash_chunk();
5089 if (tmptrash) {
5090 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5091 chunk_printf(tmptrash,
5092 "%sServer %s/%s is %s (leaving forced drain)",
5093 s->flags & SRV_F_BACKUP ? "Backup " : "",
5094 s->proxy->id, s->id,
5095 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5096 }
5097 else {
5098 chunk_printf(tmptrash,
5099 "%sServer %s/%s is %s (leaving drain)",
5100 s->flags & SRV_F_BACKUP ? "Backup " : "",
5101 s->proxy->id, s->id,
5102 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
5103 if (s->track) /* normally it's mandatory here */
5104 chunk_appendf(tmptrash, " via %s/%s",
5105 s->track->proxy->id, s->track->id);
5106 }
5107
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005108 ha_warning("%s.\n", tmptrash->area);
5109 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5110 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005111 free_trash_chunk(tmptrash);
5112 tmptrash = NULL;
5113 }
5114
5115 /* now propagate the status change to any LB algorithms */
5116 if (px->lbprm.update_server_eweight)
5117 px->lbprm.update_server_eweight(s);
5118 else if (srv_willbe_usable(s)) {
5119 if (px->lbprm.set_server_status_up)
5120 px->lbprm.set_server_status_up(s);
5121 }
5122 else {
5123 if (px->lbprm.set_server_status_down)
5124 px->lbprm.set_server_status_down(s);
5125 }
5126 }
5127 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
5128 /* remaining in drain mode after removing one of its flags */
5129
5130 tmptrash = alloc_trash_chunk();
5131 if (tmptrash) {
5132 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
5133 chunk_printf(tmptrash,
5134 "%sServer %s/%s is leaving forced drain but remains in drain mode",
5135 s->flags & SRV_F_BACKUP ? "Backup " : "",
5136 s->proxy->id, s->id);
5137
5138 if (s->track) /* normally it's mandatory here */
5139 chunk_appendf(tmptrash, " via %s/%s",
5140 s->track->proxy->id, s->track->id);
5141 }
5142 else {
5143 chunk_printf(tmptrash,
5144 "%sServer %s/%s remains in forced drain mode",
5145 s->flags & SRV_F_BACKUP ? "Backup " : "",
5146 s->proxy->id, s->id);
5147 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005148 ha_warning("%s.\n", tmptrash->area);
5149 send_log(s->proxy, LOG_NOTICE, "%s.\n",
5150 tmptrash->area);
Emeric Brun64cc49c2017-10-03 14:46:45 +02005151 free_trash_chunk(tmptrash);
5152 tmptrash = NULL;
5153 }
5154
5155 /* commit new admin status */
5156
5157 s->cur_admin = s->next_admin;
5158 }
5159 }
5160
5161 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02005162 *s->adm_st_chg_cause = 0;
5163}
Emeric Brun64cc49c2017-10-03 14:46:45 +02005164
Baptiste Assmanna68ca962015-04-14 01:15:08 +02005165/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02005166 * Local variables:
5167 * c-indent-level: 8
5168 * c-basic-offset: 8
5169 * End:
5170 */