blob: f0b912f66dd5a130046adae06cf74f682d6d4420 [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>
Willy Tarreau272adea2014-03-31 10:39:59 +020035#include <proto/port_range.h>
36#include <proto/protocol.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020037#include <proto/queue.h>
Frédéric Lécaille9a146de2017-03-20 14:54:41 +010038#include <proto/sample.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020039#include <proto/server.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020040#include <proto/stream.h>
William Lallemand222baf22016-11-19 02:00:33 +010041#include <proto/stream_interface.h>
42#include <proto/stats.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020043#include <proto/task.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020044#include <proto/dns.h>
David Carlier6f182082017-04-03 21:58:04 +010045#include <netinet/tcp.h>
Willy Tarreau4aac7db2014-05-16 11:48:10 +020046
Emeric Brun64cc49c2017-10-03 14:46:45 +020047struct list updated_servers = LIST_HEAD_INIT(updated_servers);
48
Christopher Faulet5d42e092017-10-16 12:00:40 +020049#ifdef USE_THREAD
50HA_SPINLOCK_T updated_servers_lock;
51#endif
52
53static void srv_register_update(struct server *srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020054static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010055static int srv_apply_lastaddr(struct server *srv, int *err_code);
Frédéric Lécailleb418c122017-04-26 11:24:02 +020056static int srv_set_fqdn(struct server *srv, const char *fqdn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
Willy Tarreau21faa912012-10-10 08:27:36 +020058/* List head of all known server keywords */
59static struct srv_kw_list srv_keywords = {
60 .list = LIST_HEAD_INIT(srv_keywords.list)
61};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020062
Simon Hormana3608442013-11-01 16:46:15 +090063int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020064{
Emeric Brun52a91d32017-08-31 14:41:55 +020065 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020066 return s->down_time;
67
68 return now.tv_sec - s->last_change + s->down_time;
69}
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050071int srv_lastsession(const struct server *s)
72{
73 if (s->counters.last_sess)
74 return now.tv_sec - s->counters.last_sess;
75
76 return -1;
77}
78
Simon Horman4a741432013-02-23 15:35:38 +090079int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020080{
Simon Horman4a741432013-02-23 15:35:38 +090081 const struct server *s = check->server;
82
Willy Tarreauff5ae352013-12-11 20:36:34 +010083 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090084 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085
Emeric Brun52a91d32017-08-31 14:41:55 +020086 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090087 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010088
Simon Horman4a741432013-02-23 15:35:38 +090089 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010090}
91
Olivier Houchard4e694042017-03-14 20:01:29 +010092void srv_set_dyncookie(struct server *s)
93{
94 struct proxy *p = s->proxy;
95 struct server *tmpserv;
96 char *tmpbuf;
97 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010098 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010099 size_t buffer_len;
100 int addr_len;
101 int port;
102
103 if ((s->flags & SRV_F_COOKIESET) ||
104 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
105 s->proxy->dyncookie_key == NULL)
106 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100107 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100108
109 if (s->addr.ss_family != AF_INET &&
110 s->addr.ss_family != AF_INET6)
111 return;
112 /*
113 * Buffer to calculate the cookie value.
114 * The buffer contains the secret key + the server IP address
115 * + the TCP port.
116 */
117 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
118 /*
119 * The TCP port should use only 2 bytes, but is stored in
120 * an unsigned int in struct server, so let's use 4, to be
121 * on the safe side.
122 */
123 buffer_len = key_len + addr_len + 4;
124 tmpbuf = trash.str;
125 memcpy(tmpbuf, p->dyncookie_key, key_len);
126 memcpy(&(tmpbuf[key_len]),
127 s->addr.ss_family == AF_INET ?
128 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
129 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
130 addr_len);
131 /*
132 * Make sure it's the same across all the load balancers,
133 * no matter their endianness.
134 */
135 port = htonl(s->svc_port);
136 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
137 hash_value = XXH64(tmpbuf, buffer_len, 0);
138 memprintf(&s->cookie, "%016llx", hash_value);
139 if (!s->cookie)
140 return;
141 s->cklen = 16;
142 /*
143 * Check that we did not get a hash collision.
144 * Unlikely, but it can happen.
145 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200146 for (tmpserv = p->srv; tmpserv != NULL;
147 tmpserv = tmpserv->next) {
148 if (tmpserv == s)
149 continue;
150 if (tmpserv->cookie &&
151 strcmp(tmpserv->cookie, s->cookie) == 0) {
152 Warning("We generated two equal cookies for two different servers.\n"
153 "Please change the secret key for '%s'.\n",
154 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100155 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200156 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100157}
158
Willy Tarreau21faa912012-10-10 08:27:36 +0200159/*
160 * Registers the server keyword list <kwl> as a list of valid keywords for next
161 * parsing sessions.
162 */
163void srv_register_keywords(struct srv_kw_list *kwl)
164{
165 LIST_ADDQ(&srv_keywords.list, &kwl->list);
166}
167
168/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
169 * keyword is found with a NULL ->parse() function, then an attempt is made to
170 * find one with a valid ->parse() function. This way it is possible to declare
171 * platform-dependant, known keywords as NULL, then only declare them as valid
172 * if some options are met. Note that if the requested keyword contains an
173 * opening parenthesis, everything from this point is ignored.
174 */
175struct srv_kw *srv_find_kw(const char *kw)
176{
177 int index;
178 const char *kwend;
179 struct srv_kw_list *kwl;
180 struct srv_kw *ret = NULL;
181
182 kwend = strchr(kw, '(');
183 if (!kwend)
184 kwend = kw + strlen(kw);
185
186 list_for_each_entry(kwl, &srv_keywords.list, list) {
187 for (index = 0; kwl->kw[index].kw != NULL; index++) {
188 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
189 kwl->kw[index].kw[kwend-kw] == 0) {
190 if (kwl->kw[index].parse)
191 return &kwl->kw[index]; /* found it !*/
192 else
193 ret = &kwl->kw[index]; /* may be OK */
194 }
195 }
196 }
197 return ret;
198}
199
200/* Dumps all registered "server" keywords to the <out> string pointer. The
201 * unsupported keywords are only dumped if their supported form was not
202 * found.
203 */
204void srv_dump_kws(char **out)
205{
206 struct srv_kw_list *kwl;
207 int index;
208
209 *out = NULL;
210 list_for_each_entry(kwl, &srv_keywords.list, list) {
211 for (index = 0; kwl->kw[index].kw != NULL; index++) {
212 if (kwl->kw[index].parse ||
213 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
214 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
215 kwl->scope,
216 kwl->kw[index].kw,
217 kwl->kw[index].skip ? " <arg>" : "",
218 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
219 kwl->kw[index].parse ? "" : " (not supported)");
220 }
221 }
222 }
223}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100224
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100225/* Parse the "addr" server keyword */
226static int srv_parse_addr(char **args, int *cur_arg,
227 struct proxy *curproxy, struct server *newsrv, char **err)
228{
229 char *errmsg, *arg;
230 struct sockaddr_storage *sk;
231 int port1, port2;
232 struct protocol *proto;
233
234 errmsg = NULL;
235 arg = args[*cur_arg + 1];
236
237 if (!*arg) {
238 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
239 goto err;
240 }
241
242 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
243 if (!sk) {
244 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
245 goto err;
246 }
247
248 proto = protocol_by_family(sk->ss_family);
249 if (!proto || !proto->connect) {
250 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
251 args[*cur_arg], arg);
252 goto err;
253 }
254
255 if (port1 != port2) {
256 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
257 args[*cur_arg], arg);
258 goto err;
259 }
260
261 newsrv->check.addr = newsrv->agent.addr = *sk;
262 newsrv->flags |= SRV_F_CHECKADDR;
263 newsrv->flags |= SRV_F_AGENTADDR;
264
265 return 0;
266
267 err:
268 free(errmsg);
269 return ERR_ALERT | ERR_FATAL;
270}
271
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100272/* Parse the "agent-check" server keyword */
273static int srv_parse_agent_check(char **args, int *cur_arg,
274 struct proxy *curproxy, struct server *newsrv, char **err)
275{
276 newsrv->do_agent = 1;
277 return 0;
278}
279
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100280/* Parse the "backup" server keyword */
281static int srv_parse_backup(char **args, int *cur_arg,
282 struct proxy *curproxy, struct server *newsrv, char **err)
283{
284 newsrv->flags |= SRV_F_BACKUP;
285 return 0;
286}
287
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100288/* Parse the "check" server keyword */
289static int srv_parse_check(char **args, int *cur_arg,
290 struct proxy *curproxy, struct server *newsrv, char **err)
291{
292 newsrv->do_check = 1;
293 return 0;
294}
295
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100296/* Parse the "check-send-proxy" server keyword */
297static int srv_parse_check_send_proxy(char **args, int *cur_arg,
298 struct proxy *curproxy, struct server *newsrv, char **err)
299{
300 newsrv->check.send_proxy = 1;
301 return 0;
302}
303
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100304/* Parse the "cookie" server keyword */
305static int srv_parse_cookie(char **args, int *cur_arg,
306 struct proxy *curproxy, struct server *newsrv, char **err)
307{
308 char *arg;
309
310 arg = args[*cur_arg + 1];
311 if (!*arg) {
312 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
313 return ERR_ALERT | ERR_FATAL;
314 }
315
316 free(newsrv->cookie);
317 newsrv->cookie = strdup(arg);
318 newsrv->cklen = strlen(arg);
319 newsrv->flags |= SRV_F_COOKIESET;
320 return 0;
321}
322
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100323/* Parse the "disabled" server keyword */
324static int srv_parse_disabled(char **args, int *cur_arg,
325 struct proxy *curproxy, struct server *newsrv, char **err)
326{
Emeric Brun52a91d32017-08-31 14:41:55 +0200327 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
328 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100329 newsrv->check.state |= CHK_ST_PAUSED;
330 newsrv->check.health = 0;
331 return 0;
332}
333
334/* Parse the "enabled" server keyword */
335static int srv_parse_enabled(char **args, int *cur_arg,
336 struct proxy *curproxy, struct server *newsrv, char **err)
337{
Emeric Brun52a91d32017-08-31 14:41:55 +0200338 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
339 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100340 newsrv->check.state &= ~CHK_ST_PAUSED;
341 newsrv->check.health = newsrv->check.rise;
342 return 0;
343}
344
Willy Tarreaudff55432012-10-10 17:51:05 +0200345/* parse the "id" server keyword */
346static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
347{
348 struct eb32_node *node;
349
350 if (!*args[*cur_arg + 1]) {
351 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
352 return ERR_ALERT | ERR_FATAL;
353 }
354
355 newsrv->puid = atol(args[*cur_arg + 1]);
356 newsrv->conf.id.key = newsrv->puid;
357
358 if (newsrv->puid <= 0) {
359 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
360 return ERR_ALERT | ERR_FATAL;
361 }
362
363 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
364 if (node) {
365 struct server *target = container_of(node, struct server, conf.id);
366 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
367 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
368 target->id);
369 return ERR_ALERT | ERR_FATAL;
370 }
371
372 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200373 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200374 return 0;
375}
376
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100377/* Parse the "namespace" server keyword */
378static int srv_parse_namespace(char **args, int *cur_arg,
379 struct proxy *curproxy, struct server *newsrv, char **err)
380{
381#ifdef CONFIG_HAP_NS
382 char *arg;
383
384 arg = args[*cur_arg + 1];
385 if (!*arg) {
386 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
387 return ERR_ALERT | ERR_FATAL;
388 }
389
390 if (!strcmp(arg, "*")) {
391 /* Use the namespace associated with the connection (if present). */
392 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
393 return 0;
394 }
395
396 /*
397 * As this parser may be called several times for the same 'default-server'
398 * object, or for a new 'server' instance deriving from a 'default-server'
399 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
400 */
401 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
402
403 newsrv->netns = netns_store_lookup(arg, strlen(arg));
404 if (!newsrv->netns)
405 newsrv->netns = netns_store_insert(arg);
406
407 if (!newsrv->netns) {
408 memprintf(err, "Cannot open namespace '%s'", arg);
409 return ERR_ALERT | ERR_FATAL;
410 }
411
412 return 0;
413#else
414 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
415 return ERR_ALERT | ERR_FATAL;
416#endif
417}
418
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100419/* Parse the "no-agent-check" server keyword */
420static int srv_parse_no_agent_check(char **args, int *cur_arg,
421 struct proxy *curproxy, struct server *newsrv, char **err)
422{
423 free_check(&newsrv->agent);
424 newsrv->agent.inter = 0;
425 newsrv->agent.port = 0;
426 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
427 newsrv->do_agent = 0;
428 return 0;
429}
430
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100431/* Parse the "no-backup" server keyword */
432static int srv_parse_no_backup(char **args, int *cur_arg,
433 struct proxy *curproxy, struct server *newsrv, char **err)
434{
435 newsrv->flags &= ~SRV_F_BACKUP;
436 return 0;
437}
438
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100439/* Parse the "no-check" server keyword */
440static int srv_parse_no_check(char **args, int *cur_arg,
441 struct proxy *curproxy, struct server *newsrv, char **err)
442{
443 free_check(&newsrv->check);
444 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
445 newsrv->do_check = 0;
446 return 0;
447}
448
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100449/* Parse the "no-check-send-proxy" server keyword */
450static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
451 struct proxy *curproxy, struct server *newsrv, char **err)
452{
453 newsrv->check.send_proxy = 0;
454 return 0;
455}
456
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100457/* Disable server PROXY protocol flags. */
458static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
459{
460 srv->pp_opts &= ~flags;
461 return 0;
462}
463
464/* Parse the "no-send-proxy" server keyword */
465static int srv_parse_no_send_proxy(char **args, int *cur_arg,
466 struct proxy *curproxy, struct server *newsrv, char **err)
467{
468 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
469}
470
471/* Parse the "no-send-proxy-v2" server keyword */
472static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
473 struct proxy *curproxy, struct server *newsrv, char **err)
474{
475 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
476}
477
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100478/* Parse the "non-stick" server keyword */
479static int srv_parse_non_stick(char **args, int *cur_arg,
480 struct proxy *curproxy, struct server *newsrv, char **err)
481{
482 newsrv->flags |= SRV_F_NON_STICK;
483 return 0;
484}
485
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100486/* Enable server PROXY protocol flags. */
487static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
488{
489 srv->pp_opts |= flags;
490 return 0;
491}
492
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100493/* Parse the "observe" server keyword */
494static int srv_parse_observe(char **args, int *cur_arg,
495 struct proxy *curproxy, struct server *newsrv, char **err)
496{
497 char *arg;
498
499 arg = args[*cur_arg + 1];
500 if (!*arg) {
501 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
502 return ERR_ALERT | ERR_FATAL;
503 }
504
505 if (!strcmp(arg, "none")) {
506 newsrv->observe = HANA_OBS_NONE;
507 }
508 else if (!strcmp(arg, "layer4")) {
509 newsrv->observe = HANA_OBS_LAYER4;
510 }
511 else if (!strcmp(arg, "layer7")) {
512 if (curproxy->mode != PR_MODE_HTTP) {
513 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
514 return ERR_ALERT;
515 }
516 newsrv->observe = HANA_OBS_LAYER7;
517 }
518 else {
519 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
520 "but got '%s'\n", args[*cur_arg], arg);
521 return ERR_ALERT | ERR_FATAL;
522 }
523
524 return 0;
525}
526
Frédéric Lécaille16186232017-03-14 16:42:49 +0100527/* Parse the "redir" server keyword */
528static int srv_parse_redir(char **args, int *cur_arg,
529 struct proxy *curproxy, struct server *newsrv, char **err)
530{
531 char *arg;
532
533 arg = args[*cur_arg + 1];
534 if (!*arg) {
535 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
536 return ERR_ALERT | ERR_FATAL;
537 }
538
539 free(newsrv->rdr_pfx);
540 newsrv->rdr_pfx = strdup(arg);
541 newsrv->rdr_len = strlen(arg);
542
543 return 0;
544}
545
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100546/* Parse the "send-proxy" server keyword */
547static int srv_parse_send_proxy(char **args, int *cur_arg,
548 struct proxy *curproxy, struct server *newsrv, char **err)
549{
550 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
551}
552
553/* Parse the "send-proxy-v2" server keyword */
554static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
555 struct proxy *curproxy, struct server *newsrv, char **err)
556{
557 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
558}
559
Frédéric Lécailledba97072017-03-17 15:33:50 +0100560
561/* Parse the "source" server keyword */
562static int srv_parse_source(char **args, int *cur_arg,
563 struct proxy *curproxy, struct server *newsrv, char **err)
564{
565 char *errmsg;
566 int port_low, port_high;
567 struct sockaddr_storage *sk;
568 struct protocol *proto;
569
570 errmsg = NULL;
571
572 if (!*args[*cur_arg + 1]) {
573 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
574 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
575 goto err;
576 }
577
578 /* 'sk' is statically allocated (no need to be freed). */
579 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
580 if (!sk) {
581 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
582 goto err;
583 }
584
585 proto = protocol_by_family(sk->ss_family);
586 if (!proto || !proto->connect) {
587 Alert("'%s %s' : connect() not supported for this address family.\n",
588 args[*cur_arg], args[*cur_arg + 1]);
589 goto err;
590 }
591
592 newsrv->conn_src.opts |= CO_SRC_BIND;
593 newsrv->conn_src.source_addr = *sk;
594
595 if (port_low != port_high) {
596 int i;
597
598 if (!port_low || !port_high) {
599 Alert("'%s' does not support port offsets (found '%s').\n",
600 args[*cur_arg], args[*cur_arg + 1]);
601 goto err;
602 }
603
604 if (port_low <= 0 || port_low > 65535 ||
605 port_high <= 0 || port_high > 65535 ||
606 port_low > port_high) {
607 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
608 goto err;
609 }
610 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
611 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
612 newsrv->conn_src.sport_range->ports[i] = port_low + i;
613 }
614
615 *cur_arg += 2;
616 while (*(args[*cur_arg])) {
617 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
618#if defined(CONFIG_HAP_TRANSPARENT)
619 if (!*args[*cur_arg + 1]) {
620 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
621 "or 'hdr_ip(name,#)' as argument.\n");
622 goto err;
623 }
624 if (!strcmp(args[*cur_arg + 1], "client")) {
625 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
626 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
627 }
628 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
629 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
630 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
631 }
632 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
633 char *name, *end;
634
635 name = args[*cur_arg + 1] + 7;
636 while (isspace(*name))
637 name++;
638
639 end = name;
640 while (*end && !isspace(*end) && *end != ',' && *end != ')')
641 end++;
642
643 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
644 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
645 free(newsrv->conn_src.bind_hdr_name);
646 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
647 newsrv->conn_src.bind_hdr_len = end - name;
648 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
649 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
650 newsrv->conn_src.bind_hdr_occ = -1;
651
652 /* now look for an occurrence number */
653 while (isspace(*end))
654 end++;
655 if (*end == ',') {
656 end++;
657 name = end;
658 if (*end == '-')
659 end++;
660 while (isdigit((int)*end))
661 end++;
662 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
663 }
664
665 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
666 Alert("usesrc hdr_ip(name,num) does not support negative"
667 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
668 goto err;
669 }
670 }
671 else {
672 struct sockaddr_storage *sk;
673 int port1, port2;
674
675 /* 'sk' is statically allocated (no need to be freed). */
676 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
677 if (!sk) {
678 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
679 goto err;
680 }
681
682 proto = protocol_by_family(sk->ss_family);
683 if (!proto || !proto->connect) {
684 Alert("'%s %s' : connect() not supported for this address family.\n",
685 args[*cur_arg], args[*cur_arg + 1]);
686 goto err;
687 }
688
689 if (port1 != port2) {
690 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
691 args[*cur_arg], args[*cur_arg + 1]);
692 goto err;
693 }
694 newsrv->conn_src.tproxy_addr = *sk;
695 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
696 }
697 global.last_checks |= LSTCHK_NETADM;
698 *cur_arg += 2;
699 continue;
700#else /* no TPROXY support */
701 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
702 goto err;
703#endif /* defined(CONFIG_HAP_TRANSPARENT) */
704 } /* "usesrc" */
705
706 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
707#ifdef SO_BINDTODEVICE
708 if (!*args[*cur_arg + 1]) {
709 Alert("'%s' : missing interface name.\n", args[0]);
710 goto err;
711 }
712 free(newsrv->conn_src.iface_name);
713 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
714 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
715 global.last_checks |= LSTCHK_NETADM;
716#else
717 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
718 goto err;
719#endif
720 *cur_arg += 2;
721 continue;
722 }
723 /* this keyword in not an option of "source" */
724 break;
725 } /* while */
726
727 return 0;
728
729 err:
730 free(errmsg);
731 return ERR_ALERT | ERR_FATAL;
732}
733
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100734/* Parse the "stick" server keyword */
735static int srv_parse_stick(char **args, int *cur_arg,
736 struct proxy *curproxy, struct server *newsrv, char **err)
737{
738 newsrv->flags &= ~SRV_F_NON_STICK;
739 return 0;
740}
741
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100742/* Parse the "track" server keyword */
743static int srv_parse_track(char **args, int *cur_arg,
744 struct proxy *curproxy, struct server *newsrv, char **err)
745{
746 char *arg;
747
748 arg = args[*cur_arg + 1];
749 if (!*arg) {
750 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
751 return ERR_ALERT | ERR_FATAL;
752 }
753
754 free(newsrv->trackit);
755 newsrv->trackit = strdup(arg);
756
757 return 0;
758}
759
Frédéric Lécailledba97072017-03-17 15:33:50 +0100760
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200761/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200762 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200763 * shutdown.
764 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200765void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200766{
Willy Tarreau87b09662015-04-03 00:22:06 +0200767 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200768
Willy Tarreau87b09662015-04-03 00:22:06 +0200769 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
770 if (stream->srv_conn == srv)
771 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200772}
773
774/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200775 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200776 * the reason for the shutdown.
777 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200778void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200779{
780 struct server *srv;
781
782 for (srv = px->srv; srv != NULL; srv = srv->next)
783 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200784 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200785}
786
Willy Tarreaubda92272014-05-20 21:55:30 +0200787/* Appends some information to a message string related to a server going UP or
788 * DOWN. If both <forced> and <reason> are null and the server tracks another
789 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200790 * If <check> is non-null, an entire string describing the check result will be
791 * appended after a comma and a space (eg: to report some information from the
792 * check that changed the state). In the other case, the string will be built
793 * using the check results stored into the struct server if present.
794 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200795 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200796 */
Emeric Brun5a133512017-10-19 14:42:30 +0200797void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200798{
Emeric Brun5a133512017-10-19 14:42:30 +0200799 short status = s->op_st_chg.status;
800 short code = s->op_st_chg.code;
801 long duration = s->op_st_chg.duration;
802 char *desc = s->op_st_chg.reason;
803
804 if (check) {
805 status = check->status;
806 code = check->code;
807 duration = check->duration;
808 desc = check->desc;
809 }
810
811 if (status != -1) {
812 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
813
814 if (status >= HCHK_STATUS_L57DATA)
815 chunk_appendf(msg, ", code: %d", code);
816
817 if (desc && *desc) {
818 struct chunk src;
819
820 chunk_appendf(msg, ", info: \"");
821
822 chunk_initlen(&src, desc, 0, strlen(desc));
823 chunk_asciiencode(msg, &src, '"');
824
825 chunk_appendf(msg, "\"");
826 }
827
828 if (duration >= 0)
829 chunk_appendf(msg, ", check duration: %ldms", duration);
830 }
831 else if (desc && *desc) {
832 chunk_appendf(msg, ", %s", desc);
833 }
834 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200835 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200836 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200837
838 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200839 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200840 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
841 " %d sessions active, %d requeued, %d remaining in queue",
842 s->proxy->srv_act, s->proxy->srv_bck,
843 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
844 s->cur_sess, xferred, s->nbpend);
845 else
846 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
847 " %d sessions requeued, %d total in queue",
848 s->proxy->srv_act, s->proxy->srv_bck,
849 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
850 xferred, s->nbpend);
851 }
852}
853
Emeric Brun5a133512017-10-19 14:42:30 +0200854/* Marks server <s> down, regardless of its checks' statuses. The server is
855 * registered in a list to postpone the counting of the remaining servers on
856 * the proxy and transfers queued streams whenever possible to other servers at
857 * a sync point. Maintenance servers are ignored. It stores the <reason> if
858 * non-null as the reason for going down or the available data from the check
859 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200860 */
Emeric Brun5a133512017-10-19 14:42:30 +0200861void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200862{
863 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200864
Emeric Brun64cc49c2017-10-03 14:46:45 +0200865 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200866 return;
867
Emeric Brun52a91d32017-08-31 14:41:55 +0200868 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200869 *s->op_st_chg.reason = 0;
870 s->op_st_chg.status = -1;
871 if (reason) {
872 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
873 }
874 else if (check) {
875 if (check->desc)
876 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
877 s->op_st_chg.code = check->code;
878 s->op_st_chg.status = check->status;
879 s->op_st_chg.duration = check->duration;
880 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200881
Christopher Faulet5d42e092017-10-16 12:00:40 +0200882 srv_register_update(s);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200883 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200884 srv_set_stopped(srv, NULL, NULL);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200885}
886
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200887/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200888 * in maintenance. The server is registered in a list to postpone the counting
889 * of the remaining servers on the proxy and tries to grab requests from the
890 * proxy at a sync point. Maintenance servers are ignored. It stores the
891 * <reason> if non-null as the reason for going down or the available data
892 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200893 */
Emeric Brun5a133512017-10-19 14:42:30 +0200894void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200895{
896 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200897
Emeric Brun64cc49c2017-10-03 14:46:45 +0200898 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200899 return;
900
Emeric Brun52a91d32017-08-31 14:41:55 +0200901 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200902 return;
903
Emeric Brun52a91d32017-08-31 14:41:55 +0200904 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200905 *s->op_st_chg.reason = 0;
906 s->op_st_chg.status = -1;
907 if (reason) {
908 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
909 }
910 else if (check) {
911 if (check->desc)
912 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
913 s->op_st_chg.code = check->code;
914 s->op_st_chg.status = check->status;
915 s->op_st_chg.duration = check->duration;
916 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200917
Emeric Brun64cc49c2017-10-03 14:46:45 +0200918 if (s->slowstart <= 0)
919 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200920
Christopher Faulet5d42e092017-10-16 12:00:40 +0200921 srv_register_update(s);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200922 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200923 srv_set_running(srv, NULL, NULL);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200924}
925
Willy Tarreau8eb77842014-05-21 13:54:57 +0200926/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200927 * isn't in maintenance. The server is registered in a list to postpone the
928 * counting of the remaining servers on the proxy and tries to grab requests
929 * from the proxy. Maintenance servers are ignored. It stores the
930 * <reason> if non-null as the reason for going down or the available data
931 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200932 * up. Note that it makes use of the trash to build the log strings, so <reason>
933 * must not be placed there.
934 */
Emeric Brun5a133512017-10-19 14:42:30 +0200935void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200936{
937 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200938
Emeric Brun64cc49c2017-10-03 14:46:45 +0200939 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200940 return;
941
Emeric Brun52a91d32017-08-31 14:41:55 +0200942 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200943 return;
944
Emeric Brun52a91d32017-08-31 14:41:55 +0200945 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200946 *s->op_st_chg.reason = 0;
947 s->op_st_chg.status = -1;
948 if (reason) {
949 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
950 }
951 else if (check) {
952 if (check->desc)
953 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
954 s->op_st_chg.code = check->code;
955 s->op_st_chg.status = check->status;
956 s->op_st_chg.duration = check->duration;
957 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200958
Christopher Faulet5d42e092017-10-16 12:00:40 +0200959 srv_register_update(s);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200960 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200961 srv_set_stopping(srv, NULL, NULL);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200962}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200963
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200964/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
965 * enforce either maint mode or drain mode. It is not allowed to set more than
966 * one flag at once. The equivalent "inherited" flag is propagated to all
967 * tracking servers. Maintenance mode disables health checks (but not agent
968 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100969 * is done. If <cause> is non-null, it will be displayed at the end of the log
970 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200971 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100972void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200973{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200974 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200975
976 if (!mode)
977 return;
978
979 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200980 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200981 return;
982
Emeric Brun52a91d32017-08-31 14:41:55 +0200983 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200984 if (cause)
985 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
986
Christopher Faulet5d42e092017-10-16 12:00:40 +0200987 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200988
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200989 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200990 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
991 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200992 return;
993
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200994 /* compute the inherited flag to propagate */
995 if (mode & SRV_ADMF_MAINT)
996 mode = SRV_ADMF_IMAINT;
997 else if (mode & SRV_ADMF_DRAIN)
998 mode = SRV_ADMF_IDRAIN;
999
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001000 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001001 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001002}
1003
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001004/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1005 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1006 * than one flag at once. The equivalent "inherited" flag is propagated to all
1007 * tracking servers. Leaving maintenance mode re-enables health checks. When
1008 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001009 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001010void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001011{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001012 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001013
1014 if (!mode)
1015 return;
1016
1017 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001018 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019 return;
1020
Emeric Brun52a91d32017-08-31 14:41:55 +02001021 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001022
Christopher Faulet5d42e092017-10-16 12:00:40 +02001023 srv_register_update(s);
1024
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001025 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001026 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1027 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001028 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001029
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001030 if (mode & SRV_ADMF_MAINT)
1031 mode = SRV_ADMF_IMAINT;
1032 else if (mode & SRV_ADMF_DRAIN)
1033 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001034
1035 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001037}
1038
Willy Tarreau757478e2016-11-03 19:22:19 +01001039/* principle: propagate maint and drain to tracking servers. This is useful
1040 * upon startup so that inherited states are correct.
1041 */
1042static void srv_propagate_admin_state(struct server *srv)
1043{
1044 struct server *srv2;
1045
1046 if (!srv->trackers)
1047 return;
1048
1049 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001050 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001051 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001052
Emeric Brun52a91d32017-08-31 14:41:55 +02001053 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001054 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001055 }
1056}
1057
1058/* Compute and propagate the admin states for all servers in proxy <px>.
1059 * Only servers *not* tracking another one are considered, because other
1060 * ones will be handled when the server they track is visited.
1061 */
1062void srv_compute_all_admin_states(struct proxy *px)
1063{
1064 struct server *srv;
1065
1066 for (srv = px->srv; srv; srv = srv->next) {
1067 if (srv->track)
1068 continue;
1069 srv_propagate_admin_state(srv);
1070 }
1071}
1072
Willy Tarreaudff55432012-10-10 17:51:05 +02001073/* Note: must not be declared <const> as its list will be overwritten.
1074 * Please take care of keeping this list alphabetically sorted, doing so helps
1075 * all code contributors.
1076 * Optional keywords are also declared with a NULL ->parse() function so that
1077 * the config parser can report an appropriate error when a known keyword was
1078 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001079 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001080 */
1081static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001082 { "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 +01001083 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001084 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001085 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001086 { "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 +01001087 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001088 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1089 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001090 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001091 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001092 { "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 +01001093 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001094 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001095 { "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 +01001096 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1097 { "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 +01001098 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001099 { "observe", srv_parse_observe, 1, 1 }, /* Enables health adjusting based on observing communication with the server */
Frédéric Lécaille16186232017-03-14 16:42:49 +01001100 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001101 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1102 { "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 +02001103 { "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 +01001104 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001105 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001106 { NULL, NULL, 0 },
1107}};
1108
1109__attribute__((constructor))
1110static void __listener_init(void)
1111{
1112 srv_register_keywords(&srv_kws);
1113}
1114
Willy Tarreau004e0452013-11-21 11:22:01 +01001115/* Recomputes the server's eweight based on its state, uweight, the current time,
1116 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1117 * state is automatically disabled if the time is elapsed.
1118 */
1119void server_recalc_eweight(struct server *sv)
1120{
1121 struct proxy *px = sv->proxy;
1122 unsigned w;
1123
1124 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1125 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001126 if (sv->next_state == SRV_ST_STARTING)
1127 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001128 }
1129
1130 /* We must take care of not pushing the server to full throttle during slow starts.
1131 * It must also start immediately, at least at the minimal step when leaving maintenance.
1132 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001133 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001134 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1135 else
1136 w = px->lbprm.wdiv;
1137
Emeric Brun52a91d32017-08-31 14:41:55 +02001138 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001139
Christopher Faulet5d42e092017-10-16 12:00:40 +02001140 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001141}
1142
Willy Tarreaubaaee002006-06-26 02:48:02 +02001143/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001144 * Parses weight_str and configures sv accordingly.
1145 * Returns NULL on success, error message string otherwise.
1146 */
1147const char *server_parse_weight_change_request(struct server *sv,
1148 const char *weight_str)
1149{
1150 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001151 long int w;
1152 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001153
1154 px = sv->proxy;
1155
1156 /* if the weight is terminated with '%', it is set relative to
1157 * the initial weight, otherwise it is absolute.
1158 */
1159 if (!*weight_str)
1160 return "Require <weight> or <weight%>.\n";
1161
Simon Hormanb796afa2013-02-12 10:45:53 +09001162 w = strtol(weight_str, &end, 10);
1163 if (end == weight_str)
1164 return "Empty weight string empty or preceded by garbage";
1165 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001166 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001167 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001168 /* Avoid integer overflow */
1169 if (w > 25600)
1170 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001171 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001172 if (w > 256)
1173 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001174 }
1175 else if (w < 0 || w > 256)
1176 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001177 else if (end[0] != '\0')
1178 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001179
1180 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1181 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1182
1183 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001184 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001185
1186 return NULL;
1187}
1188
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001189/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001190 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1191 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001192 * Returns:
1193 * - error string on error
1194 * - NULL on success
1195 */
1196const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001197 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001198{
1199 unsigned char ip[INET6_ADDRSTRLEN];
1200
1201 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001202 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001203 return NULL;
1204 }
1205 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001206 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001207 return NULL;
1208 }
1209
1210 return "Could not understand IP address format.\n";
1211}
1212
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001213const char *server_parse_maxconn_change_request(struct server *sv,
1214 const char *maxconn_str)
1215{
1216 long int v;
1217 char *end;
1218
1219 if (!*maxconn_str)
1220 return "Require <maxconn>.\n";
1221
1222 v = strtol(maxconn_str, &end, 10);
1223 if (end == maxconn_str)
1224 return "maxconn string empty or preceded by garbage";
1225 else if (end[0] != '\0')
1226 return "Trailing garbage in maxconn string";
1227
1228 if (sv->maxconn == sv->minconn) { // static maxconn
1229 sv->maxconn = sv->minconn = v;
1230 } else { // dynamic maxconn
1231 sv->maxconn = v;
1232 }
1233
1234 if (may_dequeue_tasks(sv, sv->proxy))
1235 process_srv_queue(sv);
1236
1237 return NULL;
1238}
1239
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001240#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001241static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1242 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001243{
1244 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001245 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001246 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001247 NULL,
1248 };
1249
1250 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001251 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001252
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001253 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1254}
1255
1256static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1257{
1258 struct sample_expr *expr;
1259
1260 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 +01001261 if (!expr) {
1262 memprintf(err, "error detected while parsing sni expression : %s", *err);
1263 return ERR_ALERT | ERR_FATAL;
1264 }
1265
1266 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1267 memprintf(err, "error detected while parsing sni expression : "
1268 " fetch method '%s' extracts information from '%s', "
1269 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001270 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001271 return ERR_ALERT | ERR_FATAL;
1272 }
1273
1274 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1275 release_sample_expr(newsrv->ssl_ctx.sni);
1276 newsrv->ssl_ctx.sni = expr;
1277
1278 return 0;
1279}
1280#endif
1281
1282static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1283{
1284 if (err && *err) {
1285 indent_msg(err, 2);
1286 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1287 }
1288 else
1289 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1290 file, linenum, args[0], args[1], args[cur_arg]);
1291}
1292
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001293static void srv_conn_src_sport_range_cpy(struct server *srv,
1294 struct server *src)
1295{
1296 int range_sz;
1297
1298 range_sz = src->conn_src.sport_range->size;
1299 if (range_sz > 0) {
1300 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1301 if (srv->conn_src.sport_range != NULL) {
1302 int i;
1303
1304 for (i = 0; i < range_sz; i++) {
1305 srv->conn_src.sport_range->ports[i] =
1306 src->conn_src.sport_range->ports[i];
1307 }
1308 }
1309 }
1310}
1311
1312/*
1313 * Copy <src> server connection source settings to <srv> server everything needed.
1314 */
1315static void srv_conn_src_cpy(struct server *srv, struct server *src)
1316{
1317 srv->conn_src.opts = src->conn_src.opts;
1318 srv->conn_src.source_addr = src->conn_src.source_addr;
1319
1320 /* Source port range copy. */
1321 if (src->conn_src.sport_range != NULL)
1322 srv_conn_src_sport_range_cpy(srv, src);
1323
1324#ifdef CONFIG_HAP_TRANSPARENT
1325 if (src->conn_src.bind_hdr_name != NULL) {
1326 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1327 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1328 }
1329 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1330 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1331#endif
1332 if (src->conn_src.iface_name != NULL)
1333 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1334}
1335
1336/*
1337 * Copy <src> server SSL settings to <srv> server allocating
1338 * everything needed.
1339 */
1340#if defined(USE_OPENSSL)
1341static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1342{
1343 if (src->ssl_ctx.ca_file != NULL)
1344 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1345 if (src->ssl_ctx.crl_file != NULL)
1346 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1347 if (src->ssl_ctx.client_crt != NULL)
1348 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1349
1350 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1351
1352 if (src->ssl_ctx.verify_host != NULL)
1353 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1354 if (src->ssl_ctx.ciphers != NULL)
1355 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1356 if (src->sni_expr != NULL)
1357 srv->sni_expr = strdup(src->sni_expr);
1358}
1359#endif
1360
1361/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001362 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001363 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001364 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001365 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001366static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001367{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001368 char *hostname_dn;
1369 int hostname_len, hostname_dn_len;
1370
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001371 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001372 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001373
Christopher Faulet67957bd2017-09-27 11:00:59 +02001374 hostname_len = strlen(hostname);
1375 hostname_dn = trash.str;
1376 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1377 hostname_dn, trash.size);
1378 if (hostname_dn_len == -1)
1379 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001380
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001381
Christopher Faulet67957bd2017-09-27 11:00:59 +02001382 free(srv->hostname);
1383 free(srv->hostname_dn);
1384 srv->hostname = strdup(hostname);
1385 srv->hostname_dn = strdup(hostname_dn);
1386 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001387 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001388 goto err;
1389
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001390 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001391
1392 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393 free(srv->hostname); srv->hostname = NULL;
1394 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001395 return -1;
1396}
1397
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001398/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001399 * Copy <src> server settings to <srv> server allocating
1400 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001401 * This function is not supposed to be called at any time, but only
1402 * during server settings parsing or during server allocations from
1403 * a server template, and just after having calloc()'ed a new server.
1404 * So, <src> may only be a default server (when parsing server settings)
1405 * or a server template (during server allocations from a server template).
1406 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1407 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001408 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001409static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001410{
1411 /* Connection source settings copy */
1412 srv_conn_src_cpy(srv, src);
1413
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001414 if (srv_tmpl) {
1415 srv->addr = src->addr;
1416 srv->svc_port = src->svc_port;
1417 }
1418
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001419 srv->pp_opts = src->pp_opts;
1420 if (src->rdr_pfx != NULL) {
1421 srv->rdr_pfx = strdup(src->rdr_pfx);
1422 srv->rdr_len = src->rdr_len;
1423 }
1424 if (src->cookie != NULL) {
1425 srv->cookie = strdup(src->cookie);
1426 srv->cklen = src->cklen;
1427 }
1428 srv->use_ssl = src->use_ssl;
1429 srv->check.addr = srv->agent.addr = src->check.addr;
1430 srv->check.use_ssl = src->check.use_ssl;
1431 srv->check.port = src->check.port;
1432 /* Note: 'flags' field has potentially been already initialized. */
1433 srv->flags |= src->flags;
1434 srv->do_check = src->do_check;
1435 srv->do_agent = src->do_agent;
1436 if (srv->check.port)
1437 srv->flags |= SRV_F_CHECKPORT;
1438 srv->check.inter = src->check.inter;
1439 srv->check.fastinter = src->check.fastinter;
1440 srv->check.downinter = src->check.downinter;
1441 srv->agent.use_ssl = src->agent.use_ssl;
1442 srv->agent.port = src->agent.port;
1443 if (src->agent.send_string != NULL)
1444 srv->agent.send_string = strdup(src->agent.send_string);
1445 srv->agent.send_string_len = src->agent.send_string_len;
1446 srv->agent.inter = src->agent.inter;
1447 srv->agent.fastinter = src->agent.fastinter;
1448 srv->agent.downinter = src->agent.downinter;
1449 srv->maxqueue = src->maxqueue;
1450 srv->minconn = src->minconn;
1451 srv->maxconn = src->maxconn;
1452 srv->slowstart = src->slowstart;
1453 srv->observe = src->observe;
1454 srv->onerror = src->onerror;
1455 srv->onmarkeddown = src->onmarkeddown;
1456 srv->onmarkedup = src->onmarkedup;
1457 if (src->trackit != NULL)
1458 srv->trackit = strdup(src->trackit);
1459 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1460 srv->uweight = srv->iweight = src->iweight;
1461
1462 srv->check.send_proxy = src->check.send_proxy;
1463 /* health: up, but will fall down at first failure */
1464 srv->check.rise = srv->check.health = src->check.rise;
1465 srv->check.fall = src->check.fall;
1466
1467 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001468 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1469 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1470 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001471 srv->check.state |= CHK_ST_PAUSED;
1472 srv->check.health = 0;
1473 }
1474
1475 /* health: up but will fall down at first failure */
1476 srv->agent.rise = srv->agent.health = src->agent.rise;
1477 srv->agent.fall = src->agent.fall;
1478
1479 if (src->resolvers_id != NULL)
1480 srv->resolvers_id = strdup(src->resolvers_id);
1481 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1482 if (srv->dns_opts.family_prio == AF_UNSPEC)
1483 srv->dns_opts.family_prio = AF_INET6;
1484 memcpy(srv->dns_opts.pref_net,
1485 src->dns_opts.pref_net,
1486 sizeof srv->dns_opts.pref_net);
1487 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1488
1489 srv->init_addr_methods = src->init_addr_methods;
1490 srv->init_addr = src->init_addr;
1491#if defined(USE_OPENSSL)
1492 srv_ssl_settings_cpy(srv, src);
1493#endif
1494#ifdef TCP_USER_TIMEOUT
1495 srv->tcp_ut = src->tcp_ut;
1496#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001497 if (srv_tmpl)
1498 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001499}
1500
1501static struct server *new_server(struct proxy *proxy)
1502{
1503 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001504 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001505
1506 srv = calloc(1, sizeof *srv);
1507 if (!srv)
1508 return NULL;
1509
1510 srv->obj_type = OBJ_TYPE_SERVER;
1511 srv->proxy = proxy;
1512 LIST_INIT(&srv->actconns);
1513 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001514
1515 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1516 goto free_srv;
1517 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1518 goto free_priv_conns;
1519 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1520 goto free_idle_conns;
1521
1522 for (i = 0; i < global.nbthread; i++) {
1523 LIST_INIT(&srv->priv_conns[i]);
1524 LIST_INIT(&srv->idle_conns[i]);
1525 LIST_INIT(&srv->safe_conns[i]);
1526 }
1527
Emeric Brun64cc49c2017-10-03 14:46:45 +02001528 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001529
Emeric Brun52a91d32017-08-31 14:41:55 +02001530 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001531 srv->last_change = now.tv_sec;
1532
1533 srv->check.status = HCHK_STATUS_INI;
1534 srv->check.server = srv;
1535 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1536
1537 srv->agent.status = HCHK_STATUS_INI;
1538 srv->agent.server = srv;
1539 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1540
1541 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001542
1543 free_idle_conns:
1544 free(srv->idle_conns);
1545 free_priv_conns:
1546 free(srv->priv_conns);
1547 free_srv:
1548 free(srv);
1549 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001550}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001551
1552/*
1553 * Validate <srv> server health-check settings.
1554 * Returns 0 if everything is OK, -1 if not.
1555 */
1556static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1557{
1558 struct tcpcheck_rule *r = NULL;
1559 struct list *l;
1560
1561 /*
1562 * We need at least a service port, a check port or the first tcp-check rule must
1563 * be a 'connect' one when checking an IPv4/IPv6 server.
1564 */
1565 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1566 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1567 return 0;
1568
1569 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1570 if (!r) {
1571 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1572 "Check has been disabled.\n",
1573 file, linenum, srv->id);
1574 return -1;
1575 }
1576
1577 /* search the first action (connect / send / expect) in the list */
1578 l = &srv->proxy->tcpcheck_rules;
1579 list_for_each_entry(r, l, list) {
1580 if (r->action != TCPCHK_ACT_COMMENT)
1581 break;
1582 }
1583
1584 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1585 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1586 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1587 file, linenum, srv->id);
1588 return -1;
1589 }
1590
1591 /* scan the tcp-check ruleset to ensure a port has been configured */
1592 l = &srv->proxy->tcpcheck_rules;
1593 list_for_each_entry(r, l, list) {
1594 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1595 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1596 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1597 file, linenum, srv->id);
1598 return -1;
1599 }
1600 }
1601
1602 return 0;
1603}
1604
1605/*
1606 * Initialize <srv> health-check structure.
1607 * Returns the error string in case of memory allocation failure, NULL if not.
1608 */
1609static const char *do_health_check_init(struct server *srv, int check_type, int state)
1610{
1611 const char *ret;
1612
1613 if (!srv->do_check)
1614 return NULL;
1615
1616 ret = init_check(&srv->check, check_type);
1617 if (ret)
1618 return ret;
1619
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001620 srv->check.state |= state;
1621 global.maxsock++;
1622
1623 return NULL;
1624}
1625
1626static int server_health_check_init(const char *file, int linenum,
1627 struct server *srv, struct proxy *curproxy)
1628{
1629 const char *ret;
1630
1631 if (!srv->do_check)
1632 return 0;
1633
1634 if (srv->trackit) {
1635 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1636 file, linenum);
1637 return ERR_ALERT | ERR_FATAL;
1638 }
1639
1640 if (server_healthcheck_validate(file, linenum, srv) < 0)
1641 return ERR_ALERT | ERR_ABORT;
1642
1643 /* note: check type will be set during the config review phase */
1644 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1645 if (ret) {
1646 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1647 return ERR_ALERT | ERR_ABORT;
1648 }
1649
1650 return 0;
1651}
1652
1653/*
1654 * Initialize <srv> agent check structure.
1655 * Returns the error string in case of memory allocation failure, NULL if not.
1656 */
1657static const char *do_server_agent_check_init(struct server *srv, int state)
1658{
1659 const char *ret;
1660
1661 if (!srv->do_agent)
1662 return NULL;
1663
1664 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1665 if (ret)
1666 return ret;
1667
1668 if (!srv->agent.inter)
1669 srv->agent.inter = srv->check.inter;
1670
1671 srv->agent.state |= state;
1672 global.maxsock++;
1673
1674 return NULL;
1675}
1676
1677static int server_agent_check_init(const char *file, int linenum,
1678 struct server *srv, struct proxy *curproxy)
1679{
1680 const char *ret;
1681
1682 if (!srv->do_agent)
1683 return 0;
1684
1685 if (!srv->agent.port) {
1686 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1687 file, linenum, srv->id);
1688 return ERR_ALERT | ERR_FATAL;
1689 }
1690
1691 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1692 if (ret) {
1693 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1694 return ERR_ALERT | ERR_ABORT;
1695 }
1696
1697 return 0;
1698}
1699
1700#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1701static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1702 struct server *srv, struct proxy *proxy)
1703{
1704 int ret;
1705 char *err = NULL;
1706
1707 if (!srv->sni_expr)
1708 return 0;
1709
1710 ret = server_parse_sni_expr(srv, proxy, &err);
1711 if (!ret)
1712 return 0;
1713
1714 display_parser_err(file, linenum, args, cur_arg, &err);
1715 free(err);
1716
1717 return ret;
1718}
1719#endif
1720
1721/*
1722 * Server initializations finalization.
1723 * Initialize health check, agent check and SNI expression if enabled.
1724 * Must not be called for a default server instance.
1725 */
1726static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1727 struct server *srv, struct proxy *px)
1728{
1729 int ret;
1730
1731 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1732 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1733 return ret;
1734 }
1735
1736#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1737 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1738 return ret;
1739#endif
1740
1741 if (srv->flags & SRV_F_BACKUP)
1742 px->srv_bck++;
1743 else
1744 px->srv_act++;
1745 srv_lb_commit_status(srv);
1746
1747 return 0;
1748}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001749
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001750/*
1751 * Parse as much as possible such a range string argument: low[-high]
1752 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1753 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1754 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1755 * Returns 0 if succeeded, -1 if not.
1756 */
1757static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1758{
1759 char *nb_high_arg;
1760
1761 *nb_high = 0;
1762 chunk_printf(&trash, "%s", arg);
1763 *nb_low = atoi(trash.str);
1764
1765 if ((nb_high_arg = strchr(trash.str, '-'))) {
1766 *nb_high_arg++ = '\0';
1767 *nb_high = atoi(nb_high_arg);
1768 }
1769 else {
1770 *nb_high += *nb_low;
1771 *nb_low = 1;
1772 }
1773
1774 if (*nb_low < 0 || *nb_high < *nb_low)
1775 return -1;
1776
1777 return 0;
1778}
1779
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001780static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1781{
1782 chunk_printf(&trash, "%s%d", prefix, nb);
1783 free(srv->id);
1784 srv->id = strdup(trash.str);
1785}
1786
1787/*
1788 * Initialize as much as possible servers from <srv> server template.
1789 * Note that a server template is a special server with
1790 * a few different parameters than a server which has
1791 * been parsed mostly the same way as a server.
1792 * Returns the number of servers succesfully allocated,
1793 * 'srv' template included.
1794 */
1795static int server_template_init(struct server *srv, struct proxy *px)
1796{
1797 int i;
1798 struct server *newsrv;
1799
1800 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1801 int check_init_state;
1802 int agent_init_state;
1803
1804 newsrv = new_server(px);
1805 if (!newsrv)
1806 goto err;
1807
1808 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001809 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001810#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1811 if (newsrv->sni_expr) {
1812 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1813 if (!newsrv->ssl_ctx.sni)
1814 goto err;
1815 }
1816#endif
1817 /* Set this new server ID. */
1818 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1819
1820 /* Initial checks states. */
1821 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1822 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1823
1824 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1825 do_server_agent_check_init(newsrv, agent_init_state))
1826 goto err;
1827
1828 /* Linked backwards first. This will be restablished after parsing. */
1829 newsrv->next = px->srv;
1830 px->srv = newsrv;
1831 }
1832 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1833
1834 return i - srv->tmpl_info.nb_low;
1835
1836 err:
1837 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1838 if (newsrv) {
1839#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1840 release_sample_expr(newsrv->ssl_ctx.sni);
1841#endif
1842 free_check(&newsrv->agent);
1843 free_check(&newsrv->check);
1844 }
1845 free(newsrv);
1846 return i - srv->tmpl_info.nb_low;
1847}
1848
Willy Tarreau272adea2014-03-31 10:39:59 +02001849int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1850{
1851 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001852 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001853 char *errmsg = NULL;
1854 int err_code = 0;
1855 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001856 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001857
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001858 if (!strcmp(args[0], "server") ||
1859 !strcmp(args[0], "default-server") ||
1860 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001861 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001862 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001863 int srv = !defsrv && !strcmp(args[0], "server");
1864 int srv_tmpl = !defsrv && !srv;
1865 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001866
1867 if (!defsrv && curproxy == defproxy) {
1868 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1869 err_code |= ERR_ALERT | ERR_FATAL;
1870 goto out;
1871 }
1872 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1873 err_code |= ERR_ALERT | ERR_FATAL;
1874
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001875 /* There is no mandatory first arguments for default server. */
1876 if (srv) {
1877 if (!*args[2]) {
1878 /* 'server' line number of argument check. */
1879 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1880 file, linenum, args[0]);
1881 err_code |= ERR_ALERT | ERR_FATAL;
1882 goto out;
1883 }
1884
1885 err = invalid_char(args[1]);
1886 }
1887 else if (srv_tmpl) {
1888 if (!*args[3]) {
1889 /* 'server-template' line number of argument check. */
1890 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1891 file, linenum, args[0]);
1892 err_code |= ERR_ALERT | ERR_FATAL;
1893 goto out;
1894 }
1895
1896 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001897 }
1898
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001899 if (err) {
1900 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1901 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001902 err_code |= ERR_ALERT | ERR_FATAL;
1903 goto out;
1904 }
1905
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001906 cur_arg = 2;
1907 if (srv_tmpl) {
1908 /* Parse server-template <nb | range> arg. */
1909 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1910 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1911 file, linenum, args[0], args[cur_arg]);
1912 err_code |= ERR_ALERT | ERR_FATAL;
1913 goto out;
1914 }
1915 cur_arg++;
1916 }
1917
Willy Tarreau272adea2014-03-31 10:39:59 +02001918 if (!defsrv) {
1919 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001920 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001921 struct protocol *proto;
1922
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001923 newsrv = new_server(curproxy);
1924 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001925 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1926 err_code |= ERR_ALERT | ERR_ABORT;
1927 goto out;
1928 }
1929
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 if (srv_tmpl) {
1931 newsrv->tmpl_info.nb_low = tmpl_range_low;
1932 newsrv->tmpl_info.nb_high = tmpl_range_high;
1933 }
1934
Willy Tarreau272adea2014-03-31 10:39:59 +02001935 /* the servers are linked backwards first */
1936 newsrv->next = curproxy->srv;
1937 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001938 newsrv->conf.file = strdup(file);
1939 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001940 /* Note: for a server template, its id is its prefix.
1941 * This is a temporary id which will be used for server allocations to come
1942 * after parsing.
1943 */
1944 if (srv)
1945 newsrv->id = strdup(args[1]);
1946 else
1947 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001948
1949 /* several ways to check the port component :
1950 * - IP => port=+0, relative (IPv4 only)
1951 * - IP: => port=+0, relative
1952 * - IP:N => port=N, absolute
1953 * - IP:+N => port=+N, relative
1954 * - IP:-N => port=-N, relative
1955 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001956 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001957 if (!sk) {
1958 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1959 err_code |= ERR_ALERT | ERR_FATAL;
1960 goto out;
1961 }
1962
1963 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001964 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001965 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1966 file, linenum, args[0], args[1]);
1967 err_code |= ERR_ALERT | ERR_FATAL;
1968 goto out;
1969 }
1970
1971 if (!port1 || !port2) {
1972 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001973 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001974 }
1975 else if (port1 != port2) {
1976 /* port range */
1977 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1978 file, linenum, args[0], args[1], args[2]);
1979 err_code |= ERR_ALERT | ERR_FATAL;
1980 goto out;
1981 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001982
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001983 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001984 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001985 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001986 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001987 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
1988 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
1989 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02001990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001992 }
1993 }
1994 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
1995 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
1996 file, linenum, newsrv->id);
1997 err_code |= ERR_ALERT | ERR_FATAL;
1998 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001999 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002000 }
2001
Willy Tarreau272adea2014-03-31 10:39:59 +02002002 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002003 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002004
Olivier Houchard8da5f982017-08-04 18:35:36 +02002005 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002007 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
2010 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002011
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002012 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002013 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet29f77e82017-06-08 14:04:45 +02002014 SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002015 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002016 } else {
2017 newsrv = &curproxy->defsrv;
2018 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002019 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002020 }
2021
2022 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002023 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002024 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2025 if (err) {
2026 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2027 file, linenum, *err, newsrv->id);
2028 err_code |= ERR_ALERT | ERR_FATAL;
2029 goto out;
2030 }
2031 if (val <= 0) {
2032 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2033 file, linenum, val, args[cur_arg], newsrv->id);
2034 err_code |= ERR_ALERT | ERR_FATAL;
2035 goto out;
2036 }
2037 newsrv->agent.inter = val;
2038 cur_arg += 2;
2039 }
Misiekea849332017-01-09 09:39:51 +01002040 else if (!strcmp(args[cur_arg], "agent-addr")) {
2041 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2042 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2043 goto out;
2044 }
2045
2046 cur_arg += 2;
2047 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002048 else if (!strcmp(args[cur_arg], "agent-port")) {
2049 global.maxsock++;
2050 newsrv->agent.port = atol(args[cur_arg + 1]);
2051 cur_arg += 2;
2052 }
James Brown55f9ff12015-10-21 18:19:05 -07002053 else if (!strcmp(args[cur_arg], "agent-send")) {
2054 global.maxsock++;
2055 free(newsrv->agent.send_string);
2056 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2057 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2058 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2059 cur_arg += 2;
2060 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002061 else if (!strcmp(args[cur_arg], "init-addr")) {
2062 char *p, *end;
2063 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002064 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002065
2066 newsrv->init_addr_methods = 0;
2067 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2068
2069 for (p = args[cur_arg + 1]; *p; p = end) {
2070 /* cut on next comma */
2071 for (end = p; *end && *end != ','; end++);
2072 if (*end)
2073 *(end++) = 0;
2074
Willy Tarreau4310d362016-11-02 15:05:56 +01002075 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002076 if (!strcmp(p, "libc")) {
2077 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2078 }
2079 else if (!strcmp(p, "last")) {
2080 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2081 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002082 else if (!strcmp(p, "none")) {
2083 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2084 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002085 else if (str2ip2(p, &sa, 0)) {
2086 if (is_addr(&newsrv->init_addr)) {
2087 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2088 file, linenum, args[cur_arg], p);
2089 err_code |= ERR_ALERT | ERR_FATAL;
2090 goto out;
2091 }
2092 newsrv->init_addr = sa;
2093 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2094 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002095 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002096 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002097 file, linenum, args[cur_arg], p);
2098 err_code |= ERR_ALERT | ERR_FATAL;
2099 goto out;
2100 }
2101 if (!done) {
2102 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2103 file, linenum, args[cur_arg], p);
2104 err_code |= ERR_ALERT | ERR_FATAL;
2105 goto out;
2106 }
2107 }
2108 cur_arg += 2;
2109 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002110 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002111 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002112 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2113 cur_arg += 2;
2114 }
2115 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2116 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002117 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002118 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002119 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002120 else {
2121 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2122 file, linenum, args[cur_arg]);
2123 err_code |= ERR_ALERT | ERR_FATAL;
2124 goto out;
2125 }
2126 cur_arg += 2;
2127 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002128 else if (!strcmp(args[cur_arg], "resolve-net")) {
2129 char *p, *e;
2130 unsigned char mask;
2131 struct dns_options *opt;
2132
2133 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2134 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2135 file, linenum, args[cur_arg]);
2136 err_code |= ERR_ALERT | ERR_FATAL;
2137 goto out;
2138 }
2139
2140 opt = &newsrv->dns_opts;
2141
2142 /* Split arguments by comma, and convert it from ipv4 or ipv6
2143 * string network in in_addr or in6_addr.
2144 */
2145 p = args[cur_arg + 1];
2146 e = p;
2147 while (*p != '\0') {
2148 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002149 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002150 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2151 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2152 err_code |= ERR_ALERT | ERR_FATAL;
2153 goto out;
2154 }
2155 /* look for end or comma. */
2156 while (*e != ',' && *e != '\0')
2157 e++;
2158 if (*e == ',') {
2159 *e = '\0';
2160 e++;
2161 }
2162 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2163 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2164 /* Try to convert input string from ipv4 or ipv6 network. */
2165 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2166 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2167 &mask)) {
2168 /* Try to convert input string from ipv6 network. */
2169 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2170 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2171 } else {
2172 /* All network conversions fail, retrun error. */
2173 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2174 file, linenum, args[cur_arg], p);
2175 err_code |= ERR_ALERT | ERR_FATAL;
2176 goto out;
2177 }
2178 opt->pref_net_nb++;
2179 p = e;
2180 }
2181
2182 cur_arg += 2;
2183 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002184 else if (!strcmp(args[cur_arg], "rise")) {
2185 if (!*args[cur_arg + 1]) {
2186 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2187 file, linenum, args[cur_arg]);
2188 err_code |= ERR_ALERT | ERR_FATAL;
2189 goto out;
2190 }
2191
2192 newsrv->check.rise = atol(args[cur_arg + 1]);
2193 if (newsrv->check.rise <= 0) {
2194 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2195 file, linenum, args[cur_arg]);
2196 err_code |= ERR_ALERT | ERR_FATAL;
2197 goto out;
2198 }
2199
2200 if (newsrv->check.health)
2201 newsrv->check.health = newsrv->check.rise;
2202 cur_arg += 2;
2203 }
2204 else if (!strcmp(args[cur_arg], "fall")) {
2205 newsrv->check.fall = atol(args[cur_arg + 1]);
2206
2207 if (!*args[cur_arg + 1]) {
2208 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2209 file, linenum, args[cur_arg]);
2210 err_code |= ERR_ALERT | ERR_FATAL;
2211 goto out;
2212 }
2213
2214 if (newsrv->check.fall <= 0) {
2215 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2216 file, linenum, args[cur_arg]);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220
2221 cur_arg += 2;
2222 }
2223 else if (!strcmp(args[cur_arg], "inter")) {
2224 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2225 if (err) {
2226 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2227 file, linenum, *err, newsrv->id);
2228 err_code |= ERR_ALERT | ERR_FATAL;
2229 goto out;
2230 }
2231 if (val <= 0) {
2232 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2233 file, linenum, val, args[cur_arg], newsrv->id);
2234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
2236 }
2237 newsrv->check.inter = val;
2238 cur_arg += 2;
2239 }
2240 else if (!strcmp(args[cur_arg], "fastinter")) {
2241 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2242 if (err) {
2243 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2244 file, linenum, *err, newsrv->id);
2245 err_code |= ERR_ALERT | ERR_FATAL;
2246 goto out;
2247 }
2248 if (val <= 0) {
2249 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2250 file, linenum, val, args[cur_arg], newsrv->id);
2251 err_code |= ERR_ALERT | ERR_FATAL;
2252 goto out;
2253 }
2254 newsrv->check.fastinter = val;
2255 cur_arg += 2;
2256 }
2257 else if (!strcmp(args[cur_arg], "downinter")) {
2258 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2259 if (err) {
2260 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2261 file, linenum, *err, newsrv->id);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265 if (val <= 0) {
2266 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2267 file, linenum, val, args[cur_arg], newsrv->id);
2268 err_code |= ERR_ALERT | ERR_FATAL;
2269 goto out;
2270 }
2271 newsrv->check.downinter = val;
2272 cur_arg += 2;
2273 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002274 else if (!strcmp(args[cur_arg], "port")) {
2275 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002276 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002277 cur_arg += 2;
2278 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002279 else if (!strcmp(args[cur_arg], "weight")) {
2280 int w;
2281 w = atol(args[cur_arg + 1]);
2282 if (w < 0 || w > SRV_UWGHT_MAX) {
2283 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2284 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2285 err_code |= ERR_ALERT | ERR_FATAL;
2286 goto out;
2287 }
2288 newsrv->uweight = newsrv->iweight = w;
2289 cur_arg += 2;
2290 }
2291 else if (!strcmp(args[cur_arg], "minconn")) {
2292 newsrv->minconn = atol(args[cur_arg + 1]);
2293 cur_arg += 2;
2294 }
2295 else if (!strcmp(args[cur_arg], "maxconn")) {
2296 newsrv->maxconn = atol(args[cur_arg + 1]);
2297 cur_arg += 2;
2298 }
2299 else if (!strcmp(args[cur_arg], "maxqueue")) {
2300 newsrv->maxqueue = atol(args[cur_arg + 1]);
2301 cur_arg += 2;
2302 }
2303 else if (!strcmp(args[cur_arg], "slowstart")) {
2304 /* slowstart is stored in seconds */
2305 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2306 if (err) {
2307 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2308 file, linenum, *err, newsrv->id);
2309 err_code |= ERR_ALERT | ERR_FATAL;
2310 goto out;
2311 }
2312 newsrv->slowstart = (val + 999) / 1000;
2313 cur_arg += 2;
2314 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002315 else if (!strcmp(args[cur_arg], "on-error")) {
2316 if (!strcmp(args[cur_arg + 1], "fastinter"))
2317 newsrv->onerror = HANA_ONERR_FASTINTER;
2318 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2319 newsrv->onerror = HANA_ONERR_FAILCHK;
2320 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2321 newsrv->onerror = HANA_ONERR_SUDDTH;
2322 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2323 newsrv->onerror = HANA_ONERR_MARKDWN;
2324 else {
2325 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2326 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2327 file, linenum, args[cur_arg], args[cur_arg + 1]);
2328 err_code |= ERR_ALERT | ERR_FATAL;
2329 goto out;
2330 }
2331
2332 cur_arg += 2;
2333 }
2334 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2335 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2336 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2337 else {
2338 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2339 file, linenum, args[cur_arg], args[cur_arg + 1]);
2340 err_code |= ERR_ALERT | ERR_FATAL;
2341 goto out;
2342 }
2343
2344 cur_arg += 2;
2345 }
2346 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2347 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2348 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2349 else {
2350 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2351 file, linenum, args[cur_arg], args[cur_arg + 1]);
2352 err_code |= ERR_ALERT | ERR_FATAL;
2353 goto out;
2354 }
2355
2356 cur_arg += 2;
2357 }
2358 else if (!strcmp(args[cur_arg], "error-limit")) {
2359 if (!*args[cur_arg + 1]) {
2360 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2361 file, linenum, args[cur_arg]);
2362 err_code |= ERR_ALERT | ERR_FATAL;
2363 goto out;
2364 }
2365
2366 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2367
2368 if (newsrv->consecutive_errors_limit <= 0) {
2369 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2370 file, linenum, args[cur_arg]);
2371 err_code |= ERR_ALERT | ERR_FATAL;
2372 goto out;
2373 }
2374 cur_arg += 2;
2375 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002376 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002377 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2378 file, linenum, "usesrc", "source");
2379 err_code |= ERR_ALERT | ERR_FATAL;
2380 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002381 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002382 else {
2383 static int srv_dumped;
2384 struct srv_kw *kw;
2385 char *err;
2386
2387 kw = srv_find_kw(args[cur_arg]);
2388 if (kw) {
2389 char *err = NULL;
2390 int code;
2391
2392 if (!kw->parse) {
2393 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2394 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002395 if (kw->skip != -1)
2396 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002397 err_code |= ERR_ALERT | ERR_FATAL;
2398 goto out;
2399 }
2400
2401 if (defsrv && !kw->default_ok) {
2402 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2403 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002404 if (kw->skip != -1)
2405 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002406 err_code |= ERR_ALERT;
2407 continue;
2408 }
2409
2410 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2411 err_code |= code;
2412
2413 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002414 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002415 if (code & ERR_FATAL) {
2416 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002417 if (kw->skip != -1)
2418 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002419 goto out;
2420 }
2421 }
2422 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002423 if (kw->skip != -1)
2424 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002425 continue;
2426 }
2427
2428 err = NULL;
2429 if (!srv_dumped) {
2430 srv_dump_kws(&err);
2431 indent_msg(&err, 4);
2432 srv_dumped = 1;
2433 }
2434
2435 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2436 file, linenum, args[0], args[1], args[cur_arg],
2437 err ? " Registered keywords :" : "", err ? err : "");
2438 free(err);
2439
2440 err_code |= ERR_ALERT | ERR_FATAL;
2441 goto out;
2442 }
2443 }
2444
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002445 if (!defsrv)
2446 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2447 if (err_code & ERR_FATAL)
2448 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002449 if (srv_tmpl)
2450 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002451 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002452 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002453 return 0;
2454
2455 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002456 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002457 free(errmsg);
2458 return err_code;
2459}
2460
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002461/* Returns a pointer to the first server matching either id <id>.
2462 * NULL is returned if no match is found.
2463 * the lookup is performed in the backend <bk>
2464 */
2465struct server *server_find_by_id(struct proxy *bk, int id)
2466{
2467 struct eb32_node *eb32;
2468 struct server *curserver;
2469
2470 if (!bk || (id ==0))
2471 return NULL;
2472
2473 /* <bk> has no backend capabilities, so it can't have a server */
2474 if (!(bk->cap & PR_CAP_BE))
2475 return NULL;
2476
2477 curserver = NULL;
2478
2479 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2480 if (eb32)
2481 curserver = container_of(eb32, struct server, conf.id);
2482
2483 return curserver;
2484}
2485
2486/* Returns a pointer to the first server matching either name <name>, or id
2487 * if <name> starts with a '#'. NULL is returned if no match is found.
2488 * the lookup is performed in the backend <bk>
2489 */
2490struct server *server_find_by_name(struct proxy *bk, const char *name)
2491{
2492 struct server *curserver;
2493
2494 if (!bk || !name)
2495 return NULL;
2496
2497 /* <bk> has no backend capabilities, so it can't have a server */
2498 if (!(bk->cap & PR_CAP_BE))
2499 return NULL;
2500
2501 curserver = NULL;
2502 if (*name == '#') {
2503 curserver = server_find_by_id(bk, atoi(name + 1));
2504 if (curserver)
2505 return curserver;
2506 }
2507 else {
2508 curserver = bk->srv;
2509
2510 while (curserver && (strcmp(curserver->id, name) != 0))
2511 curserver = curserver->next;
2512
2513 if (curserver)
2514 return curserver;
2515 }
2516
2517 return NULL;
2518}
2519
2520struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2521{
2522 struct server *byname;
2523 struct server *byid;
2524
2525 if (!name && !id)
2526 return NULL;
2527
2528 if (diff)
2529 *diff = 0;
2530
2531 byname = byid = NULL;
2532
2533 if (name) {
2534 byname = server_find_by_name(bk, name);
2535 if (byname && (!id || byname->puid == id))
2536 return byname;
2537 }
2538
2539 /* remaining possibilities :
2540 * - name not set
2541 * - name set but not found
2542 * - name found but ID doesn't match
2543 */
2544 if (id) {
2545 byid = server_find_by_id(bk, id);
2546 if (byid) {
2547 if (byname) {
2548 /* use id only if forced by configuration */
2549 if (byid->flags & SRV_F_FORCED_ID) {
2550 if (diff)
2551 *diff |= 2;
2552 return byid;
2553 }
2554 else {
2555 if (diff)
2556 *diff |= 1;
2557 return byname;
2558 }
2559 }
2560
2561 /* remaining possibilities:
2562 * - name not set
2563 * - name set but not found
2564 */
2565 if (name && diff)
2566 *diff |= 2;
2567 return byid;
2568 }
2569
2570 /* id bot found */
2571 if (byname) {
2572 if (diff)
2573 *diff |= 1;
2574 return byname;
2575 }
2576 }
2577
2578 return NULL;
2579}
2580
Christopher Faulet5d42e092017-10-16 12:00:40 +02002581/* Registers changes to be applied asynchronously */
2582static void srv_register_update(struct server *srv)
2583{
2584 if (LIST_ISEMPTY(&srv->update_status)) {
2585 THREAD_WANT_SYNC();
2586 SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
2587 if (LIST_ISEMPTY(&srv->update_status))
2588 LIST_ADDQ(&updated_servers, &srv->update_status);
2589 SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
2590 }
2591}
2592
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002593/* Update a server state using the parameters available in the params list */
2594static void srv_update_state(struct server *srv, int version, char **params)
2595{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002596 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002597 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002598
2599 /* fields since version 1
2600 * and common to all other upcoming versions
2601 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002602 enum srv_state srv_op_state;
2603 enum srv_admin srv_admin_state;
2604 unsigned srv_uweight, srv_iweight;
2605 unsigned long srv_last_time_change;
2606 short srv_check_status;
2607 enum chk_result srv_check_result;
2608 int srv_check_health;
2609 int srv_check_state, srv_agent_state;
2610 int bk_f_forced_id;
2611 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002612 int fqdn_set_by_cli;
2613 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002614 const char *port_str;
2615 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002616
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002617 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002618 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002619 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002620 switch (version) {
2621 case 1:
2622 /*
2623 * now we can proceed with server's state update:
2624 * srv_addr: params[0]
2625 * srv_op_state: params[1]
2626 * srv_admin_state: params[2]
2627 * srv_uweight: params[3]
2628 * srv_iweight: params[4]
2629 * srv_last_time_change: params[5]
2630 * srv_check_status: params[6]
2631 * srv_check_result: params[7]
2632 * srv_check_health: params[8]
2633 * srv_check_state: params[9]
2634 * srv_agent_state: params[10]
2635 * bk_f_forced_id: params[11]
2636 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002637 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002638 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002639 */
2640
2641 /* validating srv_op_state */
2642 p = NULL;
2643 errno = 0;
2644 srv_op_state = strtol(params[1], &p, 10);
2645 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2646 (srv_op_state != SRV_ST_STOPPED &&
2647 srv_op_state != SRV_ST_STARTING &&
2648 srv_op_state != SRV_ST_RUNNING &&
2649 srv_op_state != SRV_ST_STOPPING)) {
2650 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2651 }
2652
2653 /* validating srv_admin_state */
2654 p = NULL;
2655 errno = 0;
2656 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002657 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002658
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002659 /* inherited statuses will be recomputed later.
2660 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2661 */
2662 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002663
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002664 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2665 (srv_admin_state != 0 &&
2666 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002667 srv_admin_state != SRV_ADMF_CMAINT &&
2668 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002669 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002670 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002671 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2672 }
2673
2674 /* validating srv_uweight */
2675 p = NULL;
2676 errno = 0;
2677 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002678 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002679 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2680
2681 /* validating srv_iweight */
2682 p = NULL;
2683 errno = 0;
2684 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002685 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002686 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2687
2688 /* validating srv_last_time_change */
2689 p = NULL;
2690 errno = 0;
2691 srv_last_time_change = strtol(params[5], &p, 10);
2692 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2693 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2694
2695 /* validating srv_check_status */
2696 p = NULL;
2697 errno = 0;
2698 srv_check_status = strtol(params[6], &p, 10);
2699 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2700 (srv_check_status >= HCHK_STATUS_SIZE))
2701 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2702
2703 /* validating srv_check_result */
2704 p = NULL;
2705 errno = 0;
2706 srv_check_result = strtol(params[7], &p, 10);
2707 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2708 (srv_check_result != CHK_RES_UNKNOWN &&
2709 srv_check_result != CHK_RES_NEUTRAL &&
2710 srv_check_result != CHK_RES_FAILED &&
2711 srv_check_result != CHK_RES_PASSED &&
2712 srv_check_result != CHK_RES_CONDPASS)) {
2713 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2714 }
2715
2716 /* validating srv_check_health */
2717 p = NULL;
2718 errno = 0;
2719 srv_check_health = strtol(params[8], &p, 10);
2720 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2721 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2722
2723 /* validating srv_check_state */
2724 p = NULL;
2725 errno = 0;
2726 srv_check_state = strtol(params[9], &p, 10);
2727 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2728 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2729 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2730
2731 /* validating srv_agent_state */
2732 p = NULL;
2733 errno = 0;
2734 srv_agent_state = strtol(params[10], &p, 10);
2735 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2736 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2737 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2738
2739 /* validating bk_f_forced_id */
2740 p = NULL;
2741 errno = 0;
2742 bk_f_forced_id = strtol(params[11], &p, 10);
2743 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2744 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2745
2746 /* validating srv_f_forced_id */
2747 p = NULL;
2748 errno = 0;
2749 srv_f_forced_id = strtol(params[12], &p, 10);
2750 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2751 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2752
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002753 /* validating srv_fqdn */
2754 fqdn = params[13];
2755 if (fqdn && *fqdn == '-')
2756 fqdn = NULL;
2757 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2758 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2759 fqdn = NULL;
2760 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002761
Frédéric Lécaille31694712017-08-01 08:47:19 +02002762 port_str = params[14];
2763 if (port_str) {
2764 port = strl2uic(port_str, strlen(port_str));
2765 if (port > USHRT_MAX) {
2766 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2767 port_str = NULL;
2768 }
2769 }
2770
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002771 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002772 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002773 goto out;
2774
2775 /* recover operational state and apply it to this server
2776 * and all servers tracking this one */
2777 switch (srv_op_state) {
2778 case SRV_ST_STOPPED:
2779 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002780 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002781 break;
2782 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002783 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002784 break;
2785 case SRV_ST_STOPPING:
2786 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002787 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002788 break;
2789 case SRV_ST_RUNNING:
2790 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002791 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002792 break;
2793 }
2794
2795 /* When applying server state, the following rules apply:
2796 * - in case of a configuration change, we apply the setting from the new
2797 * configuration, regardless of old running state
2798 * - if no configuration change, we apply old running state only if old running
2799 * state is different from new configuration state
2800 */
2801 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002802 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2803 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002804 srv_adm_set_maint(srv);
2805 else
2806 srv_adm_set_ready(srv);
2807 }
2808 /* configuration is the same, let's compate old running state and new conf state */
2809 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002810 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002811 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002812 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002813 srv_adm_set_ready(srv);
2814 }
2815 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002816 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002817 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002818 * (srv->iweight is the weight set up in configuration).
2819 * There are two possible reasons for FDRAIN to have been present :
2820 * - previous config weight was zero
2821 * - "set server b/s drain" was sent to the CLI
2822 *
2823 * In the first case, we simply want to drop this drain state
2824 * if the new weight is not zero anymore, meaning the administrator
2825 * has intentionally turned the weight back to a positive value to
2826 * enable the server again after an operation. In the second case,
2827 * the drain state was forced on the CLI regardless of the config's
2828 * weight so we don't want a change to the config weight to lose this
2829 * status. What this means is :
2830 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2831 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002832 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002833 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002834 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002835 }
2836
2837 srv->last_change = date.tv_sec - srv_last_time_change;
2838 srv->check.status = srv_check_status;
2839 srv->check.result = srv_check_result;
2840 srv->check.health = srv_check_health;
2841
2842 /* Only case we want to apply is removing ENABLED flag which could have been
2843 * done by the "disable health" command over the stats socket
2844 */
2845 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2846 (srv_check_state & CHK_ST_CONFIGURED) &&
2847 !(srv_check_state & CHK_ST_ENABLED))
2848 srv->check.state &= ~CHK_ST_ENABLED;
2849
2850 /* Only case we want to apply is removing ENABLED flag which could have been
2851 * done by the "disable agent" command over the stats socket
2852 */
2853 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2854 (srv_agent_state & CHK_ST_CONFIGURED) &&
2855 !(srv_agent_state & CHK_ST_ENABLED))
2856 srv->agent.state &= ~CHK_ST_ENABLED;
2857
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002858 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2859 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002860 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002861 * It means that a configuration file change has precedence over a unix socket change
2862 * for server's weight
2863 *
2864 * by default, HAProxy applies the following weight when parsing the configuration
2865 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002866 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002867 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002868 srv->uweight = srv_uweight;
2869 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002870 server_recalc_eweight(srv);
2871
Willy Tarreaue5a60682016-11-09 14:54:53 +01002872 /* load server IP address */
2873 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002874
2875 if (fqdn && srv->hostname) {
2876 if (!strcmp(srv->hostname, fqdn)) {
2877 /* Here we reset the 'set from stats socket FQDN' flag
2878 * to support such transitions:
2879 * Let's say initial FQDN value is foo1 (in configuration file).
2880 * - FQDN changed from stats socket, from foo1 to foo2 value,
2881 * - FQDN changed again from file configuration (with the same previous value
2882 set from stats socket, from foo1 to foo2 value),
2883 * - reload for any other reason than a FQDN modification,
2884 * the configuration file FQDN matches the fqdn server state file value.
2885 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2886 * any futher FQDN modification.
2887 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002888 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002889 }
2890 else {
2891 /* If the FDQN has been changed from stats socket,
2892 * apply fqdn state file value (which is the value set
2893 * from stats socket).
2894 */
2895 if (fqdn_set_by_cli) {
2896 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02002897 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002898 }
2899 }
2900 }
2901
Frédéric Lécaille31694712017-08-01 08:47:19 +02002902 if (port_str)
2903 srv->svc_port = port;
2904
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905 break;
2906 default:
2907 chunk_appendf(msg, ", version '%d' not supported", version);
2908 }
2909
2910 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002911 if (msg->len) {
2912 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002913 Warning("server-state application failed for server '%s/%s'%s",
2914 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002915 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002916}
2917
2918/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2919 * For each proxy, it does the following:
2920 * - opens its server state file (either one or local one)
2921 * - read whole file, line by line
2922 * - analyse each line to check if it matches our current backend:
2923 * - backend name matches
2924 * - backend id matches if id is forced and name doesn't match
2925 * - if the server pointed by the line is found, then state is applied
2926 *
2927 * If the running backend uuid or id differs from the state file, then HAProxy reports
2928 * a warning.
2929 */
2930void apply_server_state(void)
2931{
2932 char *cur, *end;
2933 char mybuf[SRV_STATE_LINE_MAXLEN];
2934 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002935 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2936 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002937 int arg, srv_arg, version, diff;
2938 FILE *f;
2939 char *filepath;
2940 char globalfilepath[MAXPATHLEN + 1];
2941 char localfilepath[MAXPATHLEN + 1];
2942 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2943 extern struct proxy *proxy;
2944 struct proxy *curproxy, *bk;
2945 struct server *srv;
2946
2947 globalfilepathlen = 0;
2948 /* create the globalfilepath variable */
2949 if (global.server_state_file) {
2950 /* absolute path or no base directory provided */
2951 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2952 len = strlen(global.server_state_file);
2953 if (len > MAXPATHLEN) {
2954 globalfilepathlen = 0;
2955 goto globalfileerror;
2956 }
2957 memcpy(globalfilepath, global.server_state_file, len);
2958 globalfilepath[len] = '\0';
2959 globalfilepathlen = len;
2960 }
2961 else if (global.server_state_base) {
2962 len = strlen(global.server_state_base);
2963 globalfilepathlen += len;
2964
2965 if (globalfilepathlen > MAXPATHLEN) {
2966 globalfilepathlen = 0;
2967 goto globalfileerror;
2968 }
2969 strncpy(globalfilepath, global.server_state_base, len);
2970 globalfilepath[globalfilepathlen] = 0;
2971
2972 /* append a slash if needed */
2973 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2974 if (globalfilepathlen + 1 > MAXPATHLEN) {
2975 globalfilepathlen = 0;
2976 goto globalfileerror;
2977 }
2978 globalfilepath[globalfilepathlen++] = '/';
2979 }
2980
2981 len = strlen(global.server_state_file);
2982 if (globalfilepathlen + len > MAXPATHLEN) {
2983 globalfilepathlen = 0;
2984 goto globalfileerror;
2985 }
2986 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2987 globalfilepathlen += len;
2988 globalfilepath[globalfilepathlen++] = 0;
2989 }
2990 }
2991 globalfileerror:
2992 if (globalfilepathlen == 0)
2993 globalfilepath[0] = '\0';
2994
2995 /* read servers state from local file */
2996 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2997 /* servers are only in backends */
2998 if (!(curproxy->cap & PR_CAP_BE))
2999 continue;
3000 fileopenerr = 0;
3001 filepath = NULL;
3002
3003 /* search server state file path and name */
3004 switch (curproxy->load_server_state_from_file) {
3005 /* read servers state from global file */
3006 case PR_SRV_STATE_FILE_GLOBAL:
3007 /* there was an error while generating global server state file path */
3008 if (globalfilepathlen == 0)
3009 continue;
3010 filepath = globalfilepath;
3011 fileopenerr = 1;
3012 break;
3013 /* this backend has its own file */
3014 case PR_SRV_STATE_FILE_LOCAL:
3015 localfilepathlen = 0;
3016 localfilepath[0] = '\0';
3017 len = 0;
3018 /* create the localfilepath variable */
3019 /* absolute path or no base directory provided */
3020 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3021 len = strlen(curproxy->server_state_file_name);
3022 if (len > MAXPATHLEN) {
3023 localfilepathlen = 0;
3024 goto localfileerror;
3025 }
3026 memcpy(localfilepath, curproxy->server_state_file_name, len);
3027 localfilepath[len] = '\0';
3028 localfilepathlen = len;
3029 }
3030 else if (global.server_state_base) {
3031 len = strlen(global.server_state_base);
3032 localfilepathlen += len;
3033
3034 if (localfilepathlen > MAXPATHLEN) {
3035 localfilepathlen = 0;
3036 goto localfileerror;
3037 }
3038 strncpy(localfilepath, global.server_state_base, len);
3039 localfilepath[localfilepathlen] = 0;
3040
3041 /* append a slash if needed */
3042 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3043 if (localfilepathlen + 1 > MAXPATHLEN) {
3044 localfilepathlen = 0;
3045 goto localfileerror;
3046 }
3047 localfilepath[localfilepathlen++] = '/';
3048 }
3049
3050 len = strlen(curproxy->server_state_file_name);
3051 if (localfilepathlen + len > MAXPATHLEN) {
3052 localfilepathlen = 0;
3053 goto localfileerror;
3054 }
3055 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3056 localfilepathlen += len;
3057 localfilepath[localfilepathlen++] = 0;
3058 }
3059 filepath = localfilepath;
3060 localfileerror:
3061 if (localfilepathlen == 0)
3062 localfilepath[0] = '\0';
3063
3064 break;
3065 case PR_SRV_STATE_FILE_NONE:
3066 default:
3067 continue;
3068 }
3069
3070 /* preload global state file */
3071 errno = 0;
3072 f = fopen(filepath, "r");
3073 if (errno && fileopenerr)
3074 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3075 if (!f)
3076 continue;
3077
3078 mybuf[0] = '\0';
3079 mybuflen = 0;
3080 version = 0;
3081
3082 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003083 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3084 Warning("Can't read first line of the server state file '%s'\n", filepath);
3085 goto fileclose;
3086 }
3087
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003088 cur = mybuf;
3089 version = atoi(cur);
3090 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3091 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003092 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003093
3094 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3095 int bk_f_forced_id = 0;
3096 int check_id = 0;
3097 int check_name = 0;
3098
3099 mybuflen = strlen(mybuf);
3100 cur = mybuf;
3101 end = cur + mybuflen;
3102
3103 bk = NULL;
3104 srv = NULL;
3105
3106 /* we need at least one character */
3107 if (mybuflen == 0)
3108 continue;
3109
3110 /* ignore blank characters at the beginning of the line */
3111 while (isspace(*cur))
3112 ++cur;
3113
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003114 /* Ignore empty or commented lines */
3115 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003116 continue;
3117
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003118 /* truncated lines */
3119 if (mybuf[mybuflen - 1] != '\n') {
3120 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003121 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003122 }
3123
3124 /* Removes trailing '\n' */
3125 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003126
3127 /* we're now ready to move the line into *srv_params[] */
3128 params[0] = cur;
3129 arg = 1;
3130 srv_arg = 0;
3131 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3132 if (isspace(*cur)) {
3133 *cur = '\0';
3134 ++cur;
3135 while (isspace(*cur))
3136 ++cur;
3137 switch (version) {
3138 case 1:
3139 /*
3140 * srv_addr: params[4] => srv_params[0]
3141 * srv_op_state: params[5] => srv_params[1]
3142 * srv_admin_state: params[6] => srv_params[2]
3143 * srv_uweight: params[7] => srv_params[3]
3144 * srv_iweight: params[8] => srv_params[4]
3145 * srv_last_time_change: params[9] => srv_params[5]
3146 * srv_check_status: params[10] => srv_params[6]
3147 * srv_check_result: params[11] => srv_params[7]
3148 * srv_check_health: params[12] => srv_params[8]
3149 * srv_check_state: params[13] => srv_params[9]
3150 * srv_agent_state: params[14] => srv_params[10]
3151 * bk_f_forced_id: params[15] => srv_params[11]
3152 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003153 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003154 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003155 */
3156 if (arg >= 4) {
3157 srv_params[srv_arg] = cur;
3158 ++srv_arg;
3159 }
3160 break;
3161 }
3162
3163 params[arg] = cur;
3164 ++arg;
3165 }
3166 else {
3167 ++cur;
3168 }
3169 }
3170
3171 /* if line is incomplete line, then ignore it.
3172 * otherwise, update useful flags */
3173 switch (version) {
3174 case 1:
3175 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3176 continue;
3177 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3178 check_id = (atoi(params[0]) == curproxy->uuid);
3179 check_name = (strcmp(curproxy->id, params[1]) == 0);
3180 break;
3181 }
3182
3183 diff = 0;
3184 bk = curproxy;
3185
3186 /* if backend can't be found, let's continue */
3187 if (!check_id && !check_name)
3188 continue;
3189 else if (!check_id && check_name) {
3190 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3191 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3192 }
3193 else if (check_id && !check_name) {
3194 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3195 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3196 /* if name doesn't match, we still want to update curproxy if the backend id
3197 * was forced in previous the previous configuration */
3198 if (!bk_f_forced_id)
3199 continue;
3200 }
3201
3202 /* look for the server by its id: param[2] */
3203 /* else look for the server by its name: param[3] */
3204 diff = 0;
3205 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3206
3207 if (!srv) {
3208 /* if no server found, then warning and continue with next line */
3209 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3210 params[3], params[2], params[0], params[1]);
3211 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3212 params[3], params[2], params[0], params[1]);
3213 continue;
3214 }
3215 else if (diff & PR_FBM_MISMATCH_ID) {
3216 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);
3217 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 +02003218 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 }
3220 else if (diff & PR_FBM_MISMATCH_NAME) {
3221 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);
3222 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 +02003223 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003224 }
3225
3226 /* now we can proceed with server's state update */
3227 srv_update_state(srv, version, srv_params);
3228 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003229fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003230 fclose(f);
3231 }
3232}
3233
Simon Horman7d09b9a2013-02-12 10:45:51 +09003234/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003235 * update a server's current IP address.
3236 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3237 * ip is in network format.
3238 * updater is a string which contains an information about the requester of the update.
3239 * updater is used if not NULL.
3240 *
3241 * A log line and a stderr warning message is generated based on server's backend options.
3242 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003243int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003244{
3245 /* generates a log line and a warning on stderr */
3246 if (1) {
3247 /* book enough space for both IPv4 and IPv6 */
3248 char oldip[INET6_ADDRSTRLEN];
3249 char newip[INET6_ADDRSTRLEN];
3250
3251 memset(oldip, '\0', INET6_ADDRSTRLEN);
3252 memset(newip, '\0', INET6_ADDRSTRLEN);
3253
3254 /* copy old IP address in a string */
3255 switch (s->addr.ss_family) {
3256 case AF_INET:
3257 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3258 break;
3259 case AF_INET6:
3260 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3261 break;
3262 };
3263
3264 /* copy new IP address in a string */
3265 switch (ip_sin_family) {
3266 case AF_INET:
3267 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3268 break;
3269 case AF_INET6:
3270 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3271 break;
3272 };
3273
3274 /* save log line into a buffer */
3275 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3276 s->proxy->id, s->id, oldip, newip, updater);
3277
3278 /* write the buffer on stderr */
3279 Warning("%s.\n", trash.str);
3280
3281 /* send a log */
3282 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3283 }
3284
3285 /* save the new IP family */
3286 s->addr.ss_family = ip_sin_family;
3287 /* save the new IP address */
3288 switch (ip_sin_family) {
3289 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003290 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003291 break;
3292 case AF_INET6:
3293 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3294 break;
3295 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003296 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003297
3298 return 0;
3299}
3300
3301/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003302 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3303 *
3304 * Caller can pass its name through <updater> to get it integrated in the response message
3305 * returned by the function.
3306 *
3307 * The function first does the following, in that order:
3308 * - validates the new addr and/or port
3309 * - checks if an update is required (new IP or port is different than current ones)
3310 * - checks the update is allowed:
3311 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3312 * - allow all changes if no CHECKS are configured
3313 * - if CHECK is configured:
3314 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3315 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3316 * conditions are met
3317 */
3318const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3319{
3320 struct sockaddr_storage sa;
3321 int ret, port_change_required;
3322 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003323 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003324 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003325 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003326
3327 msg = get_trash_chunk();
3328 chunk_reset(msg);
3329
3330 if (addr) {
3331 memset(&sa, 0, sizeof(struct sockaddr_storage));
3332 if (str2ip2(addr, &sa, 0) == NULL) {
3333 chunk_printf(msg, "Invalid addr '%s'", addr);
3334 goto out;
3335 }
3336
3337 /* changes are allowed on AF_INET* families only */
3338 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3339 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3340 goto out;
3341 }
3342
3343 /* collecting data currently setup */
3344 memset(current_addr, '\0', sizeof(current_addr));
3345 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3346 /* changes are allowed on AF_INET* families only */
3347 if ((ret != AF_INET) && (ret != AF_INET6)) {
3348 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3349 goto out;
3350 }
3351
3352 /* applying ADDR changes if required and allowed
3353 * ipcmp returns 0 when both ADDR are the same
3354 */
3355 if (ipcmp(&s->addr, &sa) == 0) {
3356 chunk_appendf(msg, "no need to change the addr");
3357 goto port;
3358 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003359 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003360 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003361
3362 /* we also need to update check's ADDR only if it uses the server's one */
3363 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003364 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003365 }
3366
3367 /* we also need to update agent ADDR only if it use the server's one */
3368 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003369 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003370 }
3371
3372 /* update report for caller */
3373 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3374 }
3375
3376 port:
3377 if (port) {
3378 char sign = '\0';
3379 char *endptr;
3380
3381 if (addr)
3382 chunk_appendf(msg, ", ");
3383
3384 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003385 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003386
3387 /* check if PORT change is required */
3388 port_change_required = 0;
3389
3390 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003391 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003392 new_port = strtol(port, &endptr, 10);
3393 if ((errno != 0) || (port == endptr)) {
3394 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3395 goto out;
3396 }
3397
3398 /* check if caller triggers a port mapped or offset */
3399 if (sign == '-' || (sign == '+')) {
3400 /* check if server currently uses port map */
3401 if (!(s->flags & SRV_F_MAPPORTS)) {
3402 /* switch from fixed port to port map mandatorily triggers
3403 * a port change */
3404 port_change_required = 1;
3405 /* check is configured
3406 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3407 * prevent PORT change if check doesn't have it's dedicated port while switching
3408 * to port mapping */
3409 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3410 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.");
3411 goto out;
3412 }
3413 }
3414 /* we're already using port maps */
3415 else {
3416 port_change_required = current_port != new_port;
3417 }
3418 }
3419 /* fixed port */
3420 else {
3421 port_change_required = current_port != new_port;
3422 }
3423
3424 /* applying PORT changes if required and update response message */
3425 if (port_change_required) {
3426 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003427 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003428 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003429
3430 /* prepare message */
3431 chunk_appendf(msg, "port changed from '");
3432 if (s->flags & SRV_F_MAPPORTS)
3433 chunk_appendf(msg, "+");
3434 chunk_appendf(msg, "%d' to '", current_port);
3435
3436 if (sign == '-') {
3437 s->flags |= SRV_F_MAPPORTS;
3438 chunk_appendf(msg, "%c", sign);
3439 /* just use for result output */
3440 new_port = -new_port;
3441 }
3442 else if (sign == '+') {
3443 s->flags |= SRV_F_MAPPORTS;
3444 chunk_appendf(msg, "%c", sign);
3445 }
3446 else {
3447 s->flags &= ~SRV_F_MAPPORTS;
3448 }
3449
3450 chunk_appendf(msg, "%d'", new_port);
3451
3452 /* we also need to update health checks port only if it uses server's realport */
3453 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3454 s->check.port = new_port;
3455 }
3456 }
3457 else {
3458 chunk_appendf(msg, "no need to change the port");
3459 }
3460 }
3461
3462out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003463 if (changed)
3464 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003465 if (updater)
3466 chunk_appendf(msg, " by '%s'", updater);
3467 chunk_appendf(msg, "\n");
3468 return msg->str;
3469}
3470
3471
3472/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003473 * update server status based on result of name resolution
3474 * returns:
3475 * 0 if server status is updated
3476 * 1 if server status has not changed
3477 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003478int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003479{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003480 struct dns_resolvers *resolvers = s->resolvers;
3481 struct dns_resolution *resolution = s->dns_requester->resolution;
3482 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003483
3484 switch (resolution->status) {
3485 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003486 /* status when HAProxy has just (re)started.
3487 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003488 break;
3489
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003490 case RSLV_STATUS_VALID:
3491 /*
3492 * resume health checks
3493 * server will be turned back on if health check is safe
3494 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003495 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003496 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003497 return 1;
3498 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3499 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003500 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003501 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003502
Emeric Brun52a91d32017-08-31 14:41:55 +02003503 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003504 return 1;
3505 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3506 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3507 s->proxy->id, s->id);
3508
3509 Warning("%s.\n", trash.str);
3510 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3511 return 0;
3512
3513 case RSLV_STATUS_NX:
3514 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003515 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3516 if (!tick_is_expired(exp, now_ms))
3517 break;
3518
3519 if (s->next_admin & SRV_ADMF_RMAINT)
3520 return 1;
3521 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3522 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003523
3524 case RSLV_STATUS_TIMEOUT:
3525 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003526 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3527 if (!tick_is_expired(exp, now_ms))
3528 break;
3529
3530 if (s->next_admin & SRV_ADMF_RMAINT)
3531 return 1;
3532 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3533 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003534
3535 case RSLV_STATUS_REFUSED:
3536 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003537 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3538 if (!tick_is_expired(exp, now_ms))
3539 break;
3540
3541 if (s->next_admin & SRV_ADMF_RMAINT)
3542 return 1;
3543 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3544 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003545
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003546 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003547 /* stop server if resolution failed for a long enough period */
3548 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3549 if (!tick_is_expired(exp, now_ms))
3550 break;
3551
3552 if (s->next_admin & SRV_ADMF_RMAINT)
3553 return 1;
3554 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3555 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003556 }
3557
3558 return 1;
3559}
3560
3561/*
3562 * Server Name Resolution valid response callback
3563 * It expects:
3564 * - <nameserver>: the name server which answered the valid response
3565 * - <response>: buffer containing a valid DNS response
3566 * - <response_len>: size of <response>
3567 * It performs the following actions:
3568 * - ignore response if current ip found and server family not met
3569 * - update with first new ip found if family is met and current IP is not found
3570 * returns:
3571 * 0 on error
3572 * 1 when no error or safe ignore
3573 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003574int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003575{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003576 struct server *s = NULL;
3577 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003578 void *serverip, *firstip;
3579 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003580 int ret;
3581 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003582 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003583
Christopher Faulet67957bd2017-09-27 11:00:59 +02003584 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003585 if (!s)
3586 return 1;
3587
Christopher Faulet67957bd2017-09-27 11:00:59 +02003588 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003589
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003590 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003591 firstip = NULL; /* pointer to the first valid response found */
3592 /* it will be used as the new IP if a change is required */
3593 firstip_sin_family = AF_UNSPEC;
3594 serverip = NULL; /* current server IP address */
3595
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003596 /* initializing server IP pointer */
3597 server_sin_family = s->addr.ss_family;
3598 switch (server_sin_family) {
3599 case AF_INET:
3600 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3601 break;
3602
3603 case AF_INET6:
3604 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3605 break;
3606
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003607 case AF_UNSPEC:
3608 break;
3609
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003610 default:
3611 goto invalid;
3612 }
3613
Baptiste Assmann729c9012017-05-22 15:13:10 +02003614 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003615 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003616 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003617
3618 switch (ret) {
3619 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003620 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003621
3622 case DNS_UPD_SRVIP_NOT_FOUND:
3623 goto save_ip;
3624
3625 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003626 goto invalid;
3627
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003628 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003629 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003630 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003631
Baptiste Assmannfad03182015-10-28 02:03:32 +01003632 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003633 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003634 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003635 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003636
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003637 default:
3638 goto invalid;
3639
3640 }
3641
3642 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003643 if (nameserver) {
3644 nameserver->counters.update++;
3645 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003646 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003647 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003648 else
3649 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003650 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3651
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003652 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003653 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003654 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003655
3656 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003657 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003658 nameserver->counters.invalid++;
3659 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003660 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003661 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003662 return 0;
3663}
3664
3665/*
3666 * Server Name Resolution error management callback
3667 * returns:
3668 * 0 on error
3669 * 1 when no error or safe ignore
3670 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003671int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003672{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003673 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003674
Christopher Faulet67957bd2017-09-27 11:00:59 +02003675 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003676 if (!s)
3677 return 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003678 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003679 return 1;
3680}
3681
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003682/*
3683 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003684 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003685 * It returns a pointer to the first server found or NULL if <ip> is not yet
3686 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003687 */
3688struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3689{
3690 struct server *tmpsrv;
3691 struct proxy *be;
3692
3693 if (!srv)
3694 return NULL;
3695
3696 be = srv->proxy;
3697 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3698 /* We want to compare the IP in the record with the IP of the servers in the
3699 * same backend, only if:
3700 * * DNS resolution is enabled on the server
3701 * * the hostname used for the resolution by our server is the same than the
3702 * one used for the server found in the backend
3703 * * the server found in the backend is not our current server
3704 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003705 if ((tmpsrv->hostname_dn == NULL) ||
3706 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3707 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003708 (srv->puid == tmpsrv->puid))
3709 continue;
3710
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003711 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003712 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003713 continue;
3714
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003715 /* At this point, we have 2 different servers using the same DNS hostname
3716 * for their respective resolution.
3717 */
3718 if (*ip_family == tmpsrv->addr.ss_family &&
3719 ((tmpsrv->addr.ss_family == AF_INET &&
3720 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3721 (tmpsrv->addr.ss_family == AF_INET6 &&
3722 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3723 return tmpsrv;
3724 }
3725 }
3726
3727 return NULL;
3728}
3729
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003730/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3731 * resolver. This is suited for initial address configuration. Returns 0 on
3732 * success otherwise a non-zero error code. In case of error, *err_code, if
3733 * not NULL, is filled up.
3734 */
3735int srv_set_addr_via_libc(struct server *srv, int *err_code)
3736{
3737 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003738 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003739 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003740 return 1;
3741 }
3742 return 0;
3743}
3744
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003745/* Set the server's FDQN (->hostname) from <hostname>.
3746 * Returns -1 if failed, 0 if not.
3747 */
3748int srv_set_fqdn(struct server *srv, const char *hostname)
3749{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003750 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003751 char *hostname_dn;
3752 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003753
Christopher Fauletb2812a62017-10-04 16:17:58 +02003754 SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003755 /* run time DNS resolution was not active for this server
3756 * and we can't enable it at run time for now.
3757 */
3758 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003759 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003760
3761 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003762 hostname_len = strlen(hostname);
3763 hostname_dn = trash.str;
3764 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3765 hostname_dn, trash.size);
3766 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003767 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003768
Christopher Faulet67957bd2017-09-27 11:00:59 +02003769 resolution = srv->dns_requester->resolution;
3770 if (resolution &&
3771 resolution->hostname_dn &&
3772 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003773 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003774
Christopher Faulet67957bd2017-09-27 11:00:59 +02003775 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003776
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003777 free(srv->hostname);
3778 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003779 srv->hostname = strdup(hostname);
3780 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003781 srv->hostname_dn_len = hostname_dn_len;
3782 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003783 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003784
Christopher Faulet67957bd2017-09-27 11:00:59 +02003785 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003786 goto err;
3787
3788 end:
3789 SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003790 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003791
3792 err:
3793 SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
3794 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003795}
3796
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003797/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3798 * from the state file. This is suited for initial address configuration.
3799 * Returns 0 on success otherwise a non-zero error code. In case of error,
3800 * *err_code, if not NULL, is filled up.
3801 */
3802static int srv_apply_lastaddr(struct server *srv, int *err_code)
3803{
3804 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3805 if (err_code)
3806 *err_code |= ERR_WARN;
3807 return 1;
3808 }
3809 return 0;
3810}
3811
Willy Tarreau25e51522016-11-04 15:10:17 +01003812/* returns 0 if no error, otherwise a combination of ERR_* flags */
3813static int srv_iterate_initaddr(struct server *srv)
3814{
3815 int return_code = 0;
3816 int err_code;
3817 unsigned int methods;
3818
3819 methods = srv->init_addr_methods;
3820 if (!methods) { // default to "last,libc"
3821 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3822 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3823 }
3824
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003825 /* "-dr" : always append "none" so that server addresses resolution
3826 * failures are silently ignored, this is convenient to validate some
3827 * configs out of their environment.
3828 */
3829 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3830 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3831
Willy Tarreau25e51522016-11-04 15:10:17 +01003832 while (methods) {
3833 err_code = 0;
3834 switch (srv_get_next_initaddr(&methods)) {
3835 case SRV_IADDR_LAST:
3836 if (!srv->lastaddr)
3837 continue;
3838 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003839 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003840 return_code |= err_code;
3841 break;
3842
3843 case SRV_IADDR_LIBC:
3844 if (!srv->hostname)
3845 continue;
3846 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003847 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003848 return_code |= err_code;
3849 break;
3850
Willy Tarreau37ebe122016-11-04 15:17:58 +01003851 case SRV_IADDR_NONE:
3852 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003853 if (return_code) {
3854 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3855 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3856 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003857 return return_code;
3858
Willy Tarreau4310d362016-11-02 15:05:56 +01003859 case SRV_IADDR_IP:
3860 ipcpy(&srv->init_addr, &srv->addr);
3861 if (return_code) {
3862 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3863 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3864 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003865 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003866
Willy Tarreau25e51522016-11-04 15:10:17 +01003867 default: /* unhandled method */
3868 break;
3869 }
3870 }
3871
3872 if (!return_code) {
3873 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3874 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3875 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003876 else {
3877 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3878 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3879 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003880
3881 return_code |= ERR_ALERT | ERR_FATAL;
3882 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003883out:
3884 srv_set_dyncookie(srv);
3885 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003886}
3887
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003888/*
3889 * This function parses all backends and all servers within each backend
3890 * and performs servers' addr resolution based on information provided by:
3891 * - configuration file
3892 * - server-state file (states provided by an 'old' haproxy process)
3893 *
3894 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3895 */
3896int srv_init_addr(void)
3897{
3898 struct proxy *curproxy;
3899 int return_code = 0;
3900
3901 curproxy = proxy;
3902 while (curproxy) {
3903 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003904
3905 /* servers are in backend only */
3906 if (!(curproxy->cap & PR_CAP_BE))
3907 goto srv_init_addr_next;
3908
Willy Tarreau25e51522016-11-04 15:10:17 +01003909 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003910 if (srv->hostname)
3911 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003912
3913 srv_init_addr_next:
3914 curproxy = curproxy->next;
3915 }
3916
3917 return return_code;
3918}
3919
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003920const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
3921{
3922
3923 struct chunk *msg;
3924
3925 msg = get_trash_chunk();
3926 chunk_reset(msg);
3927
Olivier Houchard8da5f982017-08-04 18:35:36 +02003928 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003929 chunk_appendf(msg, "no need to change the FDQN");
3930 goto out;
3931 }
3932
3933 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3934 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3935 goto out;
3936 }
3937
3938 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3939 server->proxy->id, server->id, server->hostname, fqdn);
3940
3941 if (srv_set_fqdn(server, fqdn) < 0) {
3942 chunk_reset(msg);
3943 chunk_appendf(msg, "could not update %s/%s FQDN",
3944 server->proxy->id, server->id);
3945 goto out;
3946 }
3947
3948 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003949 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003950
3951 out:
3952 if (updater)
3953 chunk_appendf(msg, " by '%s'", updater);
3954 chunk_appendf(msg, "\n");
3955
3956 return msg->str;
3957}
3958
3959
Willy Tarreau21b069d2016-11-23 17:15:08 +01003960/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3961 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003962 * 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 +01003963 * used for CLI commands requiring a server name.
3964 * Important: the <arg> is modified to remove the '/'.
3965 */
3966struct server *cli_find_server(struct appctx *appctx, char *arg)
3967{
3968 struct proxy *px;
3969 struct server *sv;
3970 char *line;
3971
3972 /* split "backend/server" and make <line> point to server */
3973 for (line = arg; *line; line++)
3974 if (*line == '/') {
3975 *line++ = '\0';
3976 break;
3977 }
3978
3979 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003980 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003981 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003982 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003983 return NULL;
3984 }
3985
3986 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003987 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003988 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003989 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003990 return NULL;
3991 }
3992
3993 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003994 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003995 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003996 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003997 return NULL;
3998 }
3999
4000 return sv;
4001}
4002
William Lallemand222baf22016-11-19 02:00:33 +01004003
4004static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4005{
4006 struct server *sv;
4007 const char *warning;
4008
4009 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4010 return 1;
4011
4012 sv = cli_find_server(appctx, args[2]);
4013 if (!sv)
4014 return 1;
4015
4016 if (strcmp(args[3], "weight") == 0) {
4017 warning = server_parse_weight_change_request(sv, args[4]);
4018 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004019 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004020 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004021 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004022 }
4023 }
4024 else if (strcmp(args[3], "state") == 0) {
4025 if (strcmp(args[4], "ready") == 0)
4026 srv_adm_set_ready(sv);
4027 else if (strcmp(args[4], "drain") == 0)
4028 srv_adm_set_drain(sv);
4029 else if (strcmp(args[4], "maint") == 0)
4030 srv_adm_set_maint(sv);
4031 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004032 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004033 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004034 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004035 }
4036 }
4037 else if (strcmp(args[3], "health") == 0) {
4038 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004039 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004040 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004041 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004042 }
4043 else if (strcmp(args[4], "up") == 0) {
4044 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004045 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004046 }
4047 else if (strcmp(args[4], "stopping") == 0) {
4048 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004049 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004050 }
4051 else if (strcmp(args[4], "down") == 0) {
4052 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004053 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004054 }
4055 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004056 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004057 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004058 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004059 }
4060 }
4061 else if (strcmp(args[3], "agent") == 0) {
4062 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004063 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004064 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004065 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004066 }
4067 else if (strcmp(args[4], "up") == 0) {
4068 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004069 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004070 }
4071 else if (strcmp(args[4], "down") == 0) {
4072 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004073 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004074 }
4075 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004076 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004077 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004078 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004079 }
4080 }
Misiek2da082d2017-01-09 09:40:42 +01004081 else if (strcmp(args[3], "agent-addr") == 0) {
4082 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004083 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004084 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4085 appctx->st0 = CLI_ST_PRINT;
4086 } else {
4087 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004088 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004089 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4090 appctx->st0 = CLI_ST_PRINT;
4091 }
4092 }
4093 }
4094 else if (strcmp(args[3], "agent-send") == 0) {
4095 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004096 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004097 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4098 appctx->st0 = CLI_ST_PRINT;
4099 } else {
4100 char *nss = strdup(args[4]);
4101 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004102 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004103 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4104 appctx->st0 = CLI_ST_PRINT;
4105 } else {
4106 free(sv->agent.send_string);
4107 sv->agent.send_string = nss;
4108 sv->agent.send_string_len = strlen(args[4]);
4109 }
4110 }
4111 }
William Lallemand222baf22016-11-19 02:00:33 +01004112 else if (strcmp(args[3], "check-port") == 0) {
4113 int i = 0;
4114 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004115 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004116 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004117 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004118 }
4119 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004120 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004121 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004122 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004123 }
4124 /* prevent the update of port to 0 if MAPPORTS are in use */
4125 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004126 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004127 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004128 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004129 return 1;
4130 }
4131 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004132 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004133 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004134 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004135 }
4136 else if (strcmp(args[3], "addr") == 0) {
4137 char *addr = NULL;
4138 char *port = NULL;
4139 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004140 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004141 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004142 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004143 return 1;
4144 }
4145 else {
4146 addr = args[4];
4147 }
4148 if (strcmp(args[5], "port") == 0) {
4149 port = args[6];
4150 }
4151 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4152 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004153 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004154 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004155 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004156 }
4157 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4158 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004159 else if (strcmp(args[3], "fqdn") == 0) {
4160 if (!*args[4]) {
4161 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4162 appctx->st0 = CLI_ST_PRINT;
4163 return 1;
4164 }
4165 warning = update_server_fqdn(sv, args[4], "stats socket command");
4166 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004167 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004168 appctx->ctx.cli.msg = warning;
4169 appctx->st0 = CLI_ST_PRINT;
4170 }
4171 }
William Lallemand222baf22016-11-19 02:00:33 +01004172 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004173 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004174 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 +01004175 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004176 }
4177 return 1;
4178}
4179
William Lallemand6b160942016-11-22 12:34:35 +01004180static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4181{
4182 struct stream_interface *si = appctx->owner;
4183 struct proxy *px;
4184 struct server *sv;
4185 char *line;
4186
4187
4188 /* split "backend/server" and make <line> point to server */
4189 for (line = args[2]; *line; line++)
4190 if (*line == '/') {
4191 *line++ = '\0';
4192 break;
4193 }
4194
4195 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004196 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004197 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004198 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004199 return 1;
4200 }
4201
4202 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004203 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004204 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004205 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004206 return 1;
4207 }
4208
4209 /* return server's effective weight at the moment */
4210 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004211 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004212 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004213 return 0;
4214 }
William Lallemand6b160942016-11-22 12:34:35 +01004215 return 1;
4216}
4217
4218static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4219{
4220 struct server *sv;
4221 const char *warning;
4222
4223 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4224 return 1;
4225
4226 sv = cli_find_server(appctx, args[2]);
4227 if (!sv)
4228 return 1;
4229
4230 warning = server_parse_weight_change_request(sv, args[3]);
4231 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004232 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004233 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004234 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004235 }
4236 return 1;
4237}
4238
Willy Tarreaub8026272016-11-23 11:26:56 +01004239/* parse a "set maxconn server" command. It always returns 1. */
4240static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4241{
4242 struct server *sv;
4243 const char *warning;
4244
4245 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4246 return 1;
4247
4248 sv = cli_find_server(appctx, args[3]);
4249 if (!sv)
4250 return 1;
4251
4252 warning = server_parse_maxconn_change_request(sv, args[4]);
4253 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004254 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004255 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004256 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004257 }
4258 return 1;
4259}
William Lallemand6b160942016-11-22 12:34:35 +01004260
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004261/* parse a "disable agent" command. It always returns 1. */
4262static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4263{
4264 struct server *sv;
4265
4266 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4267 return 1;
4268
4269 sv = cli_find_server(appctx, args[2]);
4270 if (!sv)
4271 return 1;
4272
4273 sv->agent.state &= ~CHK_ST_ENABLED;
4274 return 1;
4275}
4276
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004277/* parse a "disable health" command. It always returns 1. */
4278static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4279{
4280 struct server *sv;
4281
4282 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4283 return 1;
4284
4285 sv = cli_find_server(appctx, args[2]);
4286 if (!sv)
4287 return 1;
4288
4289 sv->check.state &= ~CHK_ST_ENABLED;
4290 return 1;
4291}
4292
Willy Tarreauffb4d582016-11-24 12:47:00 +01004293/* parse a "disable server" command. It always returns 1. */
4294static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4295{
4296 struct server *sv;
4297
4298 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4299 return 1;
4300
4301 sv = cli_find_server(appctx, args[2]);
4302 if (!sv)
4303 return 1;
4304
4305 srv_adm_set_maint(sv);
4306 return 1;
4307}
4308
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004309/* parse a "enable agent" command. It always returns 1. */
4310static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4311{
4312 struct server *sv;
4313
4314 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4315 return 1;
4316
4317 sv = cli_find_server(appctx, args[2]);
4318 if (!sv)
4319 return 1;
4320
4321 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004322 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004323 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004324 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004325 return 1;
4326 }
4327
4328 sv->agent.state |= CHK_ST_ENABLED;
4329 return 1;
4330}
4331
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004332/* parse a "enable health" command. It always returns 1. */
4333static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4334{
4335 struct server *sv;
4336
4337 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4338 return 1;
4339
4340 sv = cli_find_server(appctx, args[2]);
4341 if (!sv)
4342 return 1;
4343
4344 sv->check.state |= CHK_ST_ENABLED;
4345 return 1;
4346}
4347
Willy Tarreauffb4d582016-11-24 12:47:00 +01004348/* parse a "enable server" command. It always returns 1. */
4349static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4350{
4351 struct server *sv;
4352
4353 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4354 return 1;
4355
4356 sv = cli_find_server(appctx, args[2]);
4357 if (!sv)
4358 return 1;
4359
4360 srv_adm_set_ready(sv);
4361 return 1;
4362}
4363
William Lallemand222baf22016-11-19 02:00:33 +01004364/* register cli keywords */
4365static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004366 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004367 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004368 { { "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 +01004369 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004370 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004371 { { "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 +01004372 { { "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 +01004373 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004374 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4375 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4376
William Lallemand222baf22016-11-19 02:00:33 +01004377 {{},}
4378}};
4379
4380__attribute__((constructor))
4381static void __server_init(void)
4382{
Christopher Faulet5d42e092017-10-16 12:00:40 +02004383 SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004384 cli_register_kw(&cli_kws);
4385}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004386
Emeric Brun64cc49c2017-10-03 14:46:45 +02004387/*
4388 * This function applies server's status changes, it is
4389 * is designed to be called asynchronously.
4390 *
4391 */
4392void srv_update_status(struct server *s)
4393{
4394 struct check *check = &s->check;
4395 int xferred;
4396 struct proxy *px = s->proxy;
4397 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4398 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4399 int log_level;
4400 struct chunk *tmptrash = NULL;
4401
4402
4403 /* If currently main is not set we try to apply pending state changes */
4404 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4405 int next_admin;
4406
4407 /* Backup next admin */
4408 next_admin = s->next_admin;
4409
4410 /* restore current admin state */
4411 s->next_admin = s->cur_admin;
4412
4413 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4414 s->last_change = now.tv_sec;
4415 if (s->proxy->lbprm.set_server_status_down)
4416 s->proxy->lbprm.set_server_status_down(s);
4417
4418 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4419 srv_shutdown_streams(s, SF_ERR_DOWN);
4420
4421 /* we might have streams queued on this server and waiting for
4422 * a connection. Those which are redispatchable will be queued
4423 * to another server or to the proxy itself.
4424 */
4425 xferred = pendconn_redistribute(s);
4426
4427 tmptrash = alloc_trash_chunk();
4428 if (tmptrash) {
4429 chunk_printf(tmptrash,
4430 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4431 s->proxy->id, s->id);
4432
Emeric Brun5a133512017-10-19 14:42:30 +02004433 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004434 Warning("%s.\n", tmptrash->str);
4435
4436 /* we don't send an alert if the server was previously paused */
4437 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4438 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4439 send_email_alert(s, log_level, "%s", tmptrash->str);
4440 free_trash_chunk(tmptrash);
4441 tmptrash = NULL;
4442 }
4443 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4444 set_backend_down(s->proxy);
4445
4446 s->counters.down_trans++;
4447 }
4448 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4449 s->last_change = now.tv_sec;
4450 if (s->proxy->lbprm.set_server_status_down)
4451 s->proxy->lbprm.set_server_status_down(s);
4452
4453 /* we might have streams queued on this server and waiting for
4454 * a connection. Those which are redispatchable will be queued
4455 * to another server or to the proxy itself.
4456 */
4457 xferred = pendconn_redistribute(s);
4458
4459 tmptrash = alloc_trash_chunk();
4460 if (tmptrash) {
4461 chunk_printf(tmptrash,
4462 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4463 s->proxy->id, s->id);
4464
Emeric Brun5a133512017-10-19 14:42:30 +02004465 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004466
4467 Warning("%s.\n", tmptrash->str);
4468 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4469 free_trash_chunk(tmptrash);
4470 tmptrash = NULL;
4471 }
4472
4473 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4474 set_backend_down(s->proxy);
4475 }
4476 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4477 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4478 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4479 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4480 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4481 s->proxy->last_change = now.tv_sec;
4482 }
4483
4484 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4485 s->down_time += now.tv_sec - s->last_change;
4486
4487 s->last_change = now.tv_sec;
4488 if (s->next_state == SRV_ST_STARTING)
4489 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4490
4491 server_recalc_eweight(s);
4492 /* now propagate the status change to any LB algorithms */
4493 if (px->lbprm.update_server_eweight)
4494 px->lbprm.update_server_eweight(s);
4495 else if (srv_willbe_usable(s)) {
4496 if (px->lbprm.set_server_status_up)
4497 px->lbprm.set_server_status_up(s);
4498 }
4499 else {
4500 if (px->lbprm.set_server_status_down)
4501 px->lbprm.set_server_status_down(s);
4502 }
4503
4504 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4505 * and it's not a backup server and its effective weight is > 0,
4506 * then it can accept new connections, so we shut down all streams
4507 * on all backup servers.
4508 */
4509 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4510 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4511 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4512
4513 /* check if we can handle some connections queued at the proxy. We
4514 * will take as many as we can handle.
4515 */
4516 xferred = pendconn_grab_from_px(s);
4517
4518 tmptrash = alloc_trash_chunk();
4519 if (tmptrash) {
4520 chunk_printf(tmptrash,
4521 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4522 s->proxy->id, s->id);
4523
Emeric Brun5a133512017-10-19 14:42:30 +02004524 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004525 Warning("%s.\n", tmptrash->str);
4526 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4527 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4528 free_trash_chunk(tmptrash);
4529 tmptrash = NULL;
4530 }
4531
4532 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4533 set_backend_down(s->proxy);
4534 }
4535 else if (s->cur_eweight != s->next_eweight) {
4536 /* now propagate the status change to any LB algorithms */
4537 if (px->lbprm.update_server_eweight)
4538 px->lbprm.update_server_eweight(s);
4539 else if (srv_willbe_usable(s)) {
4540 if (px->lbprm.set_server_status_up)
4541 px->lbprm.set_server_status_up(s);
4542 }
4543 else {
4544 if (px->lbprm.set_server_status_down)
4545 px->lbprm.set_server_status_down(s);
4546 }
4547
4548 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4549 set_backend_down(s->proxy);
4550 }
4551
4552 s->next_admin = next_admin;
4553 }
4554
Emeric Brun5a133512017-10-19 14:42:30 +02004555 /* reset operational state change */
4556 *s->op_st_chg.reason = 0;
4557 s->op_st_chg.status = s->op_st_chg.code = -1;
4558 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004559
4560 /* Now we try to apply pending admin changes */
4561
4562 /* Maintenance must also disable health checks */
4563 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4564 if (s->check.state & CHK_ST_ENABLED) {
4565 s->check.state |= CHK_ST_PAUSED;
4566 check->health = 0;
4567 }
4568
4569 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004570 tmptrash = alloc_trash_chunk();
4571 if (tmptrash) {
4572 chunk_printf(tmptrash,
4573 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4574 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4575 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004576
Olivier Houchard796a2b32017-10-24 17:42:47 +02004577 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004578
Olivier Houchard796a2b32017-10-24 17:42:47 +02004579 if (!(global.mode & MODE_STARTING)) {
4580 Warning("%s.\n", tmptrash->str);
4581 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4582 }
4583 free_trash_chunk(tmptrash);
4584 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004585 }
4586 }
4587 else { /* server was still running */
4588 check->health = 0; /* failure */
4589 s->last_change = now.tv_sec;
4590 if (s->proxy->lbprm.set_server_status_down)
4591 s->proxy->lbprm.set_server_status_down(s);
4592
4593 s->next_state = SRV_ST_STOPPED;
4594 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4595 srv_shutdown_streams(s, SF_ERR_DOWN);
4596
4597 /* we might have streams queued on this server and waiting for
4598 * a connection. Those which are redispatchable will be queued
4599 * to another server or to the proxy itself.
4600 */
4601 xferred = pendconn_redistribute(s);
4602
4603 tmptrash = alloc_trash_chunk();
4604 if (tmptrash) {
4605 chunk_printf(tmptrash,
4606 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4607 s->flags & SRV_F_BACKUP ? "Backup " : "",
4608 s->proxy->id, s->id,
4609 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4610
4611 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4612
4613 if (!(global.mode & MODE_STARTING)) {
4614 Warning("%s.\n", tmptrash->str);
4615 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4616 }
4617 free_trash_chunk(tmptrash);
4618 tmptrash = NULL;
4619 }
4620 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4621 set_backend_down(s->proxy);
4622
4623 s->counters.down_trans++;
4624 }
4625 }
4626 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4627 /* OK here we're leaving maintenance, we have many things to check,
4628 * because the server might possibly be coming back up depending on
4629 * its state. In practice, leaving maintenance means that we should
4630 * immediately turn to UP (more or less the slowstart) under the
4631 * following conditions :
4632 * - server is neither checked nor tracked
4633 * - server tracks another server which is not checked
4634 * - server tracks another server which is already up
4635 * Which sums up as something simpler :
4636 * "either the tracking server is up or the server's checks are disabled
4637 * or up". Otherwise we only re-enable health checks. There's a special
4638 * case associated to the stopping state which can be inherited. Note
4639 * that the server might still be in drain mode, which is naturally dealt
4640 * with by the lower level functions.
4641 */
4642
4643 if (s->check.state & CHK_ST_ENABLED) {
4644 s->check.state &= ~CHK_ST_PAUSED;
4645 check->health = check->rise; /* start OK but check immediately */
4646 }
4647
4648 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4649 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4650 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4651 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4652 s->next_state = SRV_ST_STOPPING;
4653 }
4654 else {
4655 s->next_state = SRV_ST_STARTING;
4656 if (s->slowstart > 0)
4657 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4658 else
4659 s->next_state = SRV_ST_RUNNING;
4660 }
4661
4662 }
4663
4664 tmptrash = alloc_trash_chunk();
4665 if (tmptrash) {
4666 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4667 chunk_printf(tmptrash,
4668 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4669 s->flags & SRV_F_BACKUP ? "Backup " : "",
4670 s->proxy->id, s->id,
4671 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4672 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4673 }
4674 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4675 chunk_printf(tmptrash,
4676 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4677 s->flags & SRV_F_BACKUP ? "Backup " : "",
4678 s->proxy->id, s->id, s->hostname,
4679 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4680 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4681 }
4682 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4683 chunk_printf(tmptrash,
4684 "%sServer %s/%s is %s/%s (leaving maintenance)",
4685 s->flags & SRV_F_BACKUP ? "Backup " : "",
4686 s->proxy->id, s->id,
4687 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4688 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4689 }
4690 Warning("%s.\n", tmptrash->str);
4691 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4692 free_trash_chunk(tmptrash);
4693 tmptrash = NULL;
4694 }
4695
4696 server_recalc_eweight(s);
4697 /* now propagate the status change to any LB algorithms */
4698 if (px->lbprm.update_server_eweight)
4699 px->lbprm.update_server_eweight(s);
4700 else if (srv_willbe_usable(s)) {
4701 if (px->lbprm.set_server_status_up)
4702 px->lbprm.set_server_status_up(s);
4703 }
4704 else {
4705 if (px->lbprm.set_server_status_down)
4706 px->lbprm.set_server_status_down(s);
4707 }
4708
4709 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4710 set_backend_down(s->proxy);
4711
4712 }
4713 else if (s->next_admin & SRV_ADMF_MAINT) {
4714 /* remaining in maintenance mode, let's inform precisely about the
4715 * situation.
4716 */
4717 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4718 tmptrash = alloc_trash_chunk();
4719 if (tmptrash) {
4720 chunk_printf(tmptrash,
4721 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4722 s->flags & SRV_F_BACKUP ? "Backup " : "",
4723 s->proxy->id, s->id);
4724
4725 if (s->track) /* normally it's mandatory here */
4726 chunk_appendf(tmptrash, " via %s/%s",
4727 s->track->proxy->id, s->track->id);
4728 Warning("%s.\n", tmptrash->str);
4729 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4730 free_trash_chunk(tmptrash);
4731 tmptrash = NULL;
4732 }
4733 }
4734 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4735 tmptrash = alloc_trash_chunk();
4736 if (tmptrash) {
4737 chunk_printf(tmptrash,
4738 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4739 s->flags & SRV_F_BACKUP ? "Backup " : "",
4740 s->proxy->id, s->id, s->hostname);
4741
4742 if (s->track) /* normally it's mandatory here */
4743 chunk_appendf(tmptrash, " via %s/%s",
4744 s->track->proxy->id, s->track->id);
4745 Warning("%s.\n", tmptrash->str);
4746 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4747 free_trash_chunk(tmptrash);
4748 tmptrash = NULL;
4749 }
4750 }
4751 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4752 tmptrash = alloc_trash_chunk();
4753 if (tmptrash) {
4754 chunk_printf(tmptrash,
4755 "%sServer %s/%s remains in forced maintenance",
4756 s->flags & SRV_F_BACKUP ? "Backup " : "",
4757 s->proxy->id, s->id);
4758 Warning("%s.\n", tmptrash->str);
4759 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4760 free_trash_chunk(tmptrash);
4761 tmptrash = NULL;
4762 }
4763 }
4764 /* don't report anything when leaving drain mode and remaining in maintenance */
4765
4766 s->cur_admin = s->next_admin;
4767 }
4768
4769 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4770 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4771 /* drain state is applied only if not yet in maint */
4772
4773 s->last_change = now.tv_sec;
4774 if (px->lbprm.set_server_status_down)
4775 px->lbprm.set_server_status_down(s);
4776
4777 /* we might have streams queued on this server and waiting for
4778 * a connection. Those which are redispatchable will be queued
4779 * to another server or to the proxy itself.
4780 */
4781 xferred = pendconn_redistribute(s);
4782
4783 tmptrash = alloc_trash_chunk();
4784 if (tmptrash) {
4785 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4786 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4787 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4788
4789 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4790
4791 if (!(global.mode & MODE_STARTING)) {
4792 Warning("%s.\n", tmptrash->str);
4793 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4794 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4795 }
4796 free_trash_chunk(tmptrash);
4797 tmptrash = NULL;
4798 }
4799
4800 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4801 set_backend_down(s->proxy);
4802 }
4803 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4804 /* OK completely leaving drain mode */
4805 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4806 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4807 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4808 s->proxy->last_change = now.tv_sec;
4809 }
4810
4811 if (s->last_change < now.tv_sec) // ignore negative times
4812 s->down_time += now.tv_sec - s->last_change;
4813 s->last_change = now.tv_sec;
4814 server_recalc_eweight(s);
4815
4816 tmptrash = alloc_trash_chunk();
4817 if (tmptrash) {
4818 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4819 chunk_printf(tmptrash,
4820 "%sServer %s/%s is %s (leaving forced drain)",
4821 s->flags & SRV_F_BACKUP ? "Backup " : "",
4822 s->proxy->id, s->id,
4823 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4824 }
4825 else {
4826 chunk_printf(tmptrash,
4827 "%sServer %s/%s is %s (leaving drain)",
4828 s->flags & SRV_F_BACKUP ? "Backup " : "",
4829 s->proxy->id, s->id,
4830 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4831 if (s->track) /* normally it's mandatory here */
4832 chunk_appendf(tmptrash, " via %s/%s",
4833 s->track->proxy->id, s->track->id);
4834 }
4835
4836 Warning("%s.\n", tmptrash->str);
4837 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4838 free_trash_chunk(tmptrash);
4839 tmptrash = NULL;
4840 }
4841
4842 /* now propagate the status change to any LB algorithms */
4843 if (px->lbprm.update_server_eweight)
4844 px->lbprm.update_server_eweight(s);
4845 else if (srv_willbe_usable(s)) {
4846 if (px->lbprm.set_server_status_up)
4847 px->lbprm.set_server_status_up(s);
4848 }
4849 else {
4850 if (px->lbprm.set_server_status_down)
4851 px->lbprm.set_server_status_down(s);
4852 }
4853 }
4854 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4855 /* remaining in drain mode after removing one of its flags */
4856
4857 tmptrash = alloc_trash_chunk();
4858 if (tmptrash) {
4859 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4860 chunk_printf(tmptrash,
4861 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4862 s->flags & SRV_F_BACKUP ? "Backup " : "",
4863 s->proxy->id, s->id);
4864
4865 if (s->track) /* normally it's mandatory here */
4866 chunk_appendf(tmptrash, " via %s/%s",
4867 s->track->proxy->id, s->track->id);
4868 }
4869 else {
4870 chunk_printf(tmptrash,
4871 "%sServer %s/%s remains in forced drain mode",
4872 s->flags & SRV_F_BACKUP ? "Backup " : "",
4873 s->proxy->id, s->id);
4874 }
4875 Warning("%s.\n", tmptrash->str);
4876 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4877 free_trash_chunk(tmptrash);
4878 tmptrash = NULL;
4879 }
4880
4881 /* commit new admin status */
4882
4883 s->cur_admin = s->next_admin;
4884 }
4885 }
4886
4887 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004888 *s->adm_st_chg_cause = 0;
4889}
4890/*
4891 * This function loops on servers registered for asynchronous
4892 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004893 *
4894 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4895 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004896 */
4897void servers_update_status(void) {
4898 struct server *s, *stmp;
4899
4900 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4901 srv_update_status(s);
4902 LIST_DEL(&s->update_status);
4903 LIST_INIT(&s->update_status);
4904 }
4905}
4906
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004907/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004908 * Local variables:
4909 * c-indent-level: 8
4910 * c-basic-offset: 8
4911 * End:
4912 */