blob: 37f90d8c9238d3bf739239fd15235c94ba902f39 [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);
Olivier Houchardd16bfe62017-10-31 15:21:19 +010056static int srv_set_fqdn(struct server *srv, const char *fqdn, int dns_locked);
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);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200883 for (srv = s->trackers; srv; srv = srv->tracknext) {
884 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200885 srv_set_stopped(srv, NULL, NULL);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200886 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
887 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200888}
889
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200890/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200891 * in maintenance. The server is registered in a list to postpone the counting
892 * of the remaining servers on the proxy and tries to grab requests from the
893 * proxy at a sync point. Maintenance servers are ignored. It stores the
894 * <reason> if non-null as the reason for going down or the available data
895 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200896 */
Emeric Brun5a133512017-10-19 14:42:30 +0200897void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200898{
899 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200900
Emeric Brun64cc49c2017-10-03 14:46:45 +0200901 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200902 return;
903
Emeric Brun52a91d32017-08-31 14:41:55 +0200904 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200905 return;
906
Emeric Brun52a91d32017-08-31 14:41:55 +0200907 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200908 *s->op_st_chg.reason = 0;
909 s->op_st_chg.status = -1;
910 if (reason) {
911 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
912 }
913 else if (check) {
914 if (check->desc)
915 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
916 s->op_st_chg.code = check->code;
917 s->op_st_chg.status = check->status;
918 s->op_st_chg.duration = check->duration;
919 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200920
Emeric Brun64cc49c2017-10-03 14:46:45 +0200921 if (s->slowstart <= 0)
922 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200923
Christopher Faulet5d42e092017-10-16 12:00:40 +0200924 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200925 for (srv = s->trackers; srv; srv = srv->tracknext) {
926 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200927 srv_set_running(srv, NULL, NULL);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200928 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
929 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200930}
931
Willy Tarreau8eb77842014-05-21 13:54:57 +0200932/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200933 * isn't in maintenance. The server is registered in a list to postpone the
934 * counting of the remaining servers on the proxy and tries to grab requests
935 * from the proxy. Maintenance servers are ignored. It stores the
936 * <reason> if non-null as the reason for going down or the available data
937 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200938 * up. Note that it makes use of the trash to build the log strings, so <reason>
939 * must not be placed there.
940 */
Emeric Brun5a133512017-10-19 14:42:30 +0200941void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200942{
943 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944
Emeric Brun64cc49c2017-10-03 14:46:45 +0200945 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200946 return;
947
Emeric Brun52a91d32017-08-31 14:41:55 +0200948 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200949 return;
950
Emeric Brun52a91d32017-08-31 14:41:55 +0200951 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200952 *s->op_st_chg.reason = 0;
953 s->op_st_chg.status = -1;
954 if (reason) {
955 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
956 }
957 else if (check) {
958 if (check->desc)
959 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
960 s->op_st_chg.code = check->code;
961 s->op_st_chg.status = check->status;
962 s->op_st_chg.duration = check->duration;
963 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200964
Christopher Faulet5d42e092017-10-16 12:00:40 +0200965 srv_register_update(s);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200966 for (srv = s->trackers; srv; srv = srv->tracknext) {
967 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Emeric Brun5a133512017-10-19 14:42:30 +0200968 srv_set_stopping(srv, NULL, NULL);
Emeric Brun9f0b4582017-10-23 14:39:51 +0200969 SPIN_LOCK(SERVER_LOCK, &srv->lock);
970 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200971}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200972
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200973/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
974 * enforce either maint mode or drain mode. It is not allowed to set more than
975 * one flag at once. The equivalent "inherited" flag is propagated to all
976 * tracking servers. Maintenance mode disables health checks (but not agent
977 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100978 * is done. If <cause> is non-null, it will be displayed at the end of the log
979 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200980 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100981void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200982{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200983 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200984
985 if (!mode)
986 return;
987
988 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200989 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200990 return;
991
Emeric Brun52a91d32017-08-31 14:41:55 +0200992 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200993 if (cause)
994 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
995
Christopher Faulet5d42e092017-10-16 12:00:40 +0200996 srv_register_update(s);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200997
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200998 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200999 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
1000 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001001 return;
1002
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001003 /* compute the inherited flag to propagate */
1004 if (mode & SRV_ADMF_MAINT)
1005 mode = SRV_ADMF_IMAINT;
1006 else if (mode & SRV_ADMF_DRAIN)
1007 mode = SRV_ADMF_IDRAIN;
1008
Emeric Brun9f0b4582017-10-23 14:39:51 +02001009 for (srv = s->trackers; srv; srv = srv->tracknext) {
1010 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau8b428482016-11-07 15:53:43 +01001011 srv_set_admin_flag(srv, mode, cause);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001012 SPIN_LOCK(SERVER_LOCK, &srv->lock);
1013 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001014}
1015
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001016/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1017 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1018 * than one flag at once. The equivalent "inherited" flag is propagated to all
1019 * tracking servers. Leaving maintenance mode re-enables health checks. When
1020 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001021 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001022void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001023{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001024 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025
1026 if (!mode)
1027 return;
1028
1029 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001030 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001031 return;
1032
Emeric Brun52a91d32017-08-31 14:41:55 +02001033 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001034
Christopher Faulet5d42e092017-10-16 12:00:40 +02001035 srv_register_update(s);
1036
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001038 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1039 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001040 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001041
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001042 if (mode & SRV_ADMF_MAINT)
1043 mode = SRV_ADMF_IMAINT;
1044 else if (mode & SRV_ADMF_DRAIN)
1045 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001046
Emeric Brun9f0b4582017-10-23 14:39:51 +02001047 for (srv = s->trackers; srv; srv = srv->tracknext) {
1048 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001049 srv_clr_admin_flag(srv, mode);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001050 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
1051 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001052}
1053
Willy Tarreau757478e2016-11-03 19:22:19 +01001054/* principle: propagate maint and drain to tracking servers. This is useful
1055 * upon startup so that inherited states are correct.
1056 */
1057static void srv_propagate_admin_state(struct server *srv)
1058{
1059 struct server *srv2;
1060
1061 if (!srv->trackers)
1062 return;
1063
1064 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun9f0b4582017-10-23 14:39:51 +02001065 SPIN_LOCK(SERVER_LOCK, &srv2->lock);
Emeric Brun52a91d32017-08-31 14:41:55 +02001066 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001067 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001068
Emeric Brun52a91d32017-08-31 14:41:55 +02001069 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001070 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Emeric Brun9f0b4582017-10-23 14:39:51 +02001071 SPIN_UNLOCK(SERVER_LOCK, &srv2->lock);
Willy Tarreau757478e2016-11-03 19:22:19 +01001072 }
1073}
1074
1075/* Compute and propagate the admin states for all servers in proxy <px>.
1076 * Only servers *not* tracking another one are considered, because other
1077 * ones will be handled when the server they track is visited.
1078 */
1079void srv_compute_all_admin_states(struct proxy *px)
1080{
1081 struct server *srv;
1082
1083 for (srv = px->srv; srv; srv = srv->next) {
1084 if (srv->track)
1085 continue;
1086 srv_propagate_admin_state(srv);
1087 }
1088}
1089
Willy Tarreaudff55432012-10-10 17:51:05 +02001090/* Note: must not be declared <const> as its list will be overwritten.
1091 * Please take care of keeping this list alphabetically sorted, doing so helps
1092 * all code contributors.
1093 * Optional keywords are also declared with a NULL ->parse() function so that
1094 * the config parser can report an appropriate error when a known keyword was
1095 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001096 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001097 */
1098static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001099 { "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 +01001100 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001101 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001102 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001103 { "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 +01001104 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001105 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1106 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001107 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001108 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001109 { "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 +01001110 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001111 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001112 { "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 +01001113 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1114 { "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 +01001115 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001116 { "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 +01001117 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001118 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1119 { "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 +02001120 { "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 +01001121 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001122 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001123 { NULL, NULL, 0 },
1124}};
1125
1126__attribute__((constructor))
1127static void __listener_init(void)
1128{
1129 srv_register_keywords(&srv_kws);
1130}
1131
Willy Tarreau004e0452013-11-21 11:22:01 +01001132/* Recomputes the server's eweight based on its state, uweight, the current time,
1133 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1134 * state is automatically disabled if the time is elapsed.
1135 */
1136void server_recalc_eweight(struct server *sv)
1137{
1138 struct proxy *px = sv->proxy;
1139 unsigned w;
1140
1141 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1142 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001143 if (sv->next_state == SRV_ST_STARTING)
1144 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001145 }
1146
1147 /* We must take care of not pushing the server to full throttle during slow starts.
1148 * It must also start immediately, at least at the minimal step when leaving maintenance.
1149 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001150 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001151 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1152 else
1153 w = px->lbprm.wdiv;
1154
Emeric Brun52a91d32017-08-31 14:41:55 +02001155 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001156
Christopher Faulet5d42e092017-10-16 12:00:40 +02001157 srv_register_update(sv);
Willy Tarreau004e0452013-11-21 11:22:01 +01001158}
1159
Willy Tarreaubaaee002006-06-26 02:48:02 +02001160/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001161 * Parses weight_str and configures sv accordingly.
1162 * Returns NULL on success, error message string otherwise.
1163 */
1164const char *server_parse_weight_change_request(struct server *sv,
1165 const char *weight_str)
1166{
1167 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001168 long int w;
1169 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001170
1171 px = sv->proxy;
1172
1173 /* if the weight is terminated with '%', it is set relative to
1174 * the initial weight, otherwise it is absolute.
1175 */
1176 if (!*weight_str)
1177 return "Require <weight> or <weight%>.\n";
1178
Simon Hormanb796afa2013-02-12 10:45:53 +09001179 w = strtol(weight_str, &end, 10);
1180 if (end == weight_str)
1181 return "Empty weight string empty or preceded by garbage";
1182 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001183 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001184 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001185 /* Avoid integer overflow */
1186 if (w > 25600)
1187 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001188 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001189 if (w > 256)
1190 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001191 }
1192 else if (w < 0 || w > 256)
1193 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001194 else if (end[0] != '\0')
1195 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001196
1197 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1198 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1199
1200 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001201 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001202
1203 return NULL;
1204}
1205
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001206/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001207 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1208 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001209 * Returns:
1210 * - error string on error
1211 * - NULL on success
1212 */
1213const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001214 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001215{
1216 unsigned char ip[INET6_ADDRSTRLEN];
1217
1218 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001219 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001220 return NULL;
1221 }
1222 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001223 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001224 return NULL;
1225 }
1226
1227 return "Could not understand IP address format.\n";
1228}
1229
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001230const char *server_parse_maxconn_change_request(struct server *sv,
1231 const char *maxconn_str)
1232{
1233 long int v;
1234 char *end;
1235
1236 if (!*maxconn_str)
1237 return "Require <maxconn>.\n";
1238
1239 v = strtol(maxconn_str, &end, 10);
1240 if (end == maxconn_str)
1241 return "maxconn string empty or preceded by garbage";
1242 else if (end[0] != '\0')
1243 return "Trailing garbage in maxconn string";
1244
1245 if (sv->maxconn == sv->minconn) { // static maxconn
1246 sv->maxconn = sv->minconn = v;
1247 } else { // dynamic maxconn
1248 sv->maxconn = v;
1249 }
1250
1251 if (may_dequeue_tasks(sv, sv->proxy))
1252 process_srv_queue(sv);
1253
1254 return NULL;
1255}
1256
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001257#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001258static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1259 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001260{
1261 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001262 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001263 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001264 NULL,
1265 };
1266
1267 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001268 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001269
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001270 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1271}
1272
1273static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1274{
1275 struct sample_expr *expr;
1276
1277 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 +01001278 if (!expr) {
1279 memprintf(err, "error detected while parsing sni expression : %s", *err);
1280 return ERR_ALERT | ERR_FATAL;
1281 }
1282
1283 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1284 memprintf(err, "error detected while parsing sni expression : "
1285 " fetch method '%s' extracts information from '%s', "
1286 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001287 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001288 return ERR_ALERT | ERR_FATAL;
1289 }
1290
1291 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1292 release_sample_expr(newsrv->ssl_ctx.sni);
1293 newsrv->ssl_ctx.sni = expr;
1294
1295 return 0;
1296}
1297#endif
1298
1299static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1300{
1301 if (err && *err) {
1302 indent_msg(err, 2);
1303 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1304 }
1305 else
1306 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1307 file, linenum, args[0], args[1], args[cur_arg]);
1308}
1309
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001310static void srv_conn_src_sport_range_cpy(struct server *srv,
1311 struct server *src)
1312{
1313 int range_sz;
1314
1315 range_sz = src->conn_src.sport_range->size;
1316 if (range_sz > 0) {
1317 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1318 if (srv->conn_src.sport_range != NULL) {
1319 int i;
1320
1321 for (i = 0; i < range_sz; i++) {
1322 srv->conn_src.sport_range->ports[i] =
1323 src->conn_src.sport_range->ports[i];
1324 }
1325 }
1326 }
1327}
1328
1329/*
1330 * Copy <src> server connection source settings to <srv> server everything needed.
1331 */
1332static void srv_conn_src_cpy(struct server *srv, struct server *src)
1333{
1334 srv->conn_src.opts = src->conn_src.opts;
1335 srv->conn_src.source_addr = src->conn_src.source_addr;
1336
1337 /* Source port range copy. */
1338 if (src->conn_src.sport_range != NULL)
1339 srv_conn_src_sport_range_cpy(srv, src);
1340
1341#ifdef CONFIG_HAP_TRANSPARENT
1342 if (src->conn_src.bind_hdr_name != NULL) {
1343 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1344 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1345 }
1346 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1347 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1348#endif
1349 if (src->conn_src.iface_name != NULL)
1350 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1351}
1352
1353/*
1354 * Copy <src> server SSL settings to <srv> server allocating
1355 * everything needed.
1356 */
1357#if defined(USE_OPENSSL)
1358static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1359{
1360 if (src->ssl_ctx.ca_file != NULL)
1361 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1362 if (src->ssl_ctx.crl_file != NULL)
1363 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1364 if (src->ssl_ctx.client_crt != NULL)
1365 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1366
1367 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1368
1369 if (src->ssl_ctx.verify_host != NULL)
1370 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1371 if (src->ssl_ctx.ciphers != NULL)
1372 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1373 if (src->sni_expr != NULL)
1374 srv->sni_expr = strdup(src->sni_expr);
1375}
1376#endif
1377
1378/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001379 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001380 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001381 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001382 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001383static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001384{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385 char *hostname_dn;
1386 int hostname_len, hostname_dn_len;
1387
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001388 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001389 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001390
Christopher Faulet67957bd2017-09-27 11:00:59 +02001391 hostname_len = strlen(hostname);
1392 hostname_dn = trash.str;
1393 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1394 hostname_dn, trash.size);
1395 if (hostname_dn_len == -1)
1396 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001397
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001398
Christopher Faulet67957bd2017-09-27 11:00:59 +02001399 free(srv->hostname);
1400 free(srv->hostname_dn);
1401 srv->hostname = strdup(hostname);
1402 srv->hostname_dn = strdup(hostname_dn);
1403 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001404 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001405 goto err;
1406
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001407 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001408
1409 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001410 free(srv->hostname); srv->hostname = NULL;
1411 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001412 return -1;
1413}
1414
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001415/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001416 * Copy <src> server settings to <srv> server allocating
1417 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001418 * This function is not supposed to be called at any time, but only
1419 * during server settings parsing or during server allocations from
1420 * a server template, and just after having calloc()'ed a new server.
1421 * So, <src> may only be a default server (when parsing server settings)
1422 * or a server template (during server allocations from a server template).
1423 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1424 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001425 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001426static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001427{
1428 /* Connection source settings copy */
1429 srv_conn_src_cpy(srv, src);
1430
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001431 if (srv_tmpl) {
1432 srv->addr = src->addr;
1433 srv->svc_port = src->svc_port;
1434 }
1435
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001436 srv->pp_opts = src->pp_opts;
1437 if (src->rdr_pfx != NULL) {
1438 srv->rdr_pfx = strdup(src->rdr_pfx);
1439 srv->rdr_len = src->rdr_len;
1440 }
1441 if (src->cookie != NULL) {
1442 srv->cookie = strdup(src->cookie);
1443 srv->cklen = src->cklen;
1444 }
1445 srv->use_ssl = src->use_ssl;
1446 srv->check.addr = srv->agent.addr = src->check.addr;
1447 srv->check.use_ssl = src->check.use_ssl;
1448 srv->check.port = src->check.port;
1449 /* Note: 'flags' field has potentially been already initialized. */
1450 srv->flags |= src->flags;
1451 srv->do_check = src->do_check;
1452 srv->do_agent = src->do_agent;
1453 if (srv->check.port)
1454 srv->flags |= SRV_F_CHECKPORT;
1455 srv->check.inter = src->check.inter;
1456 srv->check.fastinter = src->check.fastinter;
1457 srv->check.downinter = src->check.downinter;
1458 srv->agent.use_ssl = src->agent.use_ssl;
1459 srv->agent.port = src->agent.port;
1460 if (src->agent.send_string != NULL)
1461 srv->agent.send_string = strdup(src->agent.send_string);
1462 srv->agent.send_string_len = src->agent.send_string_len;
1463 srv->agent.inter = src->agent.inter;
1464 srv->agent.fastinter = src->agent.fastinter;
1465 srv->agent.downinter = src->agent.downinter;
1466 srv->maxqueue = src->maxqueue;
1467 srv->minconn = src->minconn;
1468 srv->maxconn = src->maxconn;
1469 srv->slowstart = src->slowstart;
1470 srv->observe = src->observe;
1471 srv->onerror = src->onerror;
1472 srv->onmarkeddown = src->onmarkeddown;
1473 srv->onmarkedup = src->onmarkedup;
1474 if (src->trackit != NULL)
1475 srv->trackit = strdup(src->trackit);
1476 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1477 srv->uweight = srv->iweight = src->iweight;
1478
1479 srv->check.send_proxy = src->check.send_proxy;
1480 /* health: up, but will fall down at first failure */
1481 srv->check.rise = srv->check.health = src->check.rise;
1482 srv->check.fall = src->check.fall;
1483
1484 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001485 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1486 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1487 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001488 srv->check.state |= CHK_ST_PAUSED;
1489 srv->check.health = 0;
1490 }
1491
1492 /* health: up but will fall down at first failure */
1493 srv->agent.rise = srv->agent.health = src->agent.rise;
1494 srv->agent.fall = src->agent.fall;
1495
1496 if (src->resolvers_id != NULL)
1497 srv->resolvers_id = strdup(src->resolvers_id);
1498 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1499 if (srv->dns_opts.family_prio == AF_UNSPEC)
1500 srv->dns_opts.family_prio = AF_INET6;
1501 memcpy(srv->dns_opts.pref_net,
1502 src->dns_opts.pref_net,
1503 sizeof srv->dns_opts.pref_net);
1504 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1505
1506 srv->init_addr_methods = src->init_addr_methods;
1507 srv->init_addr = src->init_addr;
1508#if defined(USE_OPENSSL)
1509 srv_ssl_settings_cpy(srv, src);
1510#endif
1511#ifdef TCP_USER_TIMEOUT
1512 srv->tcp_ut = src->tcp_ut;
1513#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001514 if (srv_tmpl)
1515 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001516}
1517
1518static struct server *new_server(struct proxy *proxy)
1519{
1520 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001521 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001522
1523 srv = calloc(1, sizeof *srv);
1524 if (!srv)
1525 return NULL;
1526
1527 srv->obj_type = OBJ_TYPE_SERVER;
1528 srv->proxy = proxy;
1529 LIST_INIT(&srv->actconns);
1530 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001531
1532 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1533 goto free_srv;
1534 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1535 goto free_priv_conns;
1536 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1537 goto free_idle_conns;
1538
1539 for (i = 0; i < global.nbthread; i++) {
1540 LIST_INIT(&srv->priv_conns[i]);
1541 LIST_INIT(&srv->idle_conns[i]);
1542 LIST_INIT(&srv->safe_conns[i]);
1543 }
1544
Emeric Brun64cc49c2017-10-03 14:46:45 +02001545 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001546
Emeric Brun52a91d32017-08-31 14:41:55 +02001547 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001548 srv->last_change = now.tv_sec;
1549
1550 srv->check.status = HCHK_STATUS_INI;
1551 srv->check.server = srv;
1552 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1553
1554 srv->agent.status = HCHK_STATUS_INI;
1555 srv->agent.server = srv;
1556 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1557
1558 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001559
1560 free_idle_conns:
1561 free(srv->idle_conns);
1562 free_priv_conns:
1563 free(srv->priv_conns);
1564 free_srv:
1565 free(srv);
1566 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001567}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001568
1569/*
1570 * Validate <srv> server health-check settings.
1571 * Returns 0 if everything is OK, -1 if not.
1572 */
1573static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1574{
1575 struct tcpcheck_rule *r = NULL;
1576 struct list *l;
1577
1578 /*
1579 * We need at least a service port, a check port or the first tcp-check rule must
1580 * be a 'connect' one when checking an IPv4/IPv6 server.
1581 */
1582 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1583 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1584 return 0;
1585
1586 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1587 if (!r) {
1588 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1589 "Check has been disabled.\n",
1590 file, linenum, srv->id);
1591 return -1;
1592 }
1593
1594 /* search the first action (connect / send / expect) in the list */
1595 l = &srv->proxy->tcpcheck_rules;
1596 list_for_each_entry(r, l, list) {
1597 if (r->action != TCPCHK_ACT_COMMENT)
1598 break;
1599 }
1600
1601 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1602 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1603 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1604 file, linenum, srv->id);
1605 return -1;
1606 }
1607
1608 /* scan the tcp-check ruleset to ensure a port has been configured */
1609 l = &srv->proxy->tcpcheck_rules;
1610 list_for_each_entry(r, l, list) {
1611 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1612 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1613 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1614 file, linenum, srv->id);
1615 return -1;
1616 }
1617 }
1618
1619 return 0;
1620}
1621
1622/*
1623 * Initialize <srv> health-check structure.
1624 * Returns the error string in case of memory allocation failure, NULL if not.
1625 */
1626static const char *do_health_check_init(struct server *srv, int check_type, int state)
1627{
1628 const char *ret;
1629
1630 if (!srv->do_check)
1631 return NULL;
1632
1633 ret = init_check(&srv->check, check_type);
1634 if (ret)
1635 return ret;
1636
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001637 srv->check.state |= state;
1638 global.maxsock++;
1639
1640 return NULL;
1641}
1642
1643static int server_health_check_init(const char *file, int linenum,
1644 struct server *srv, struct proxy *curproxy)
1645{
1646 const char *ret;
1647
1648 if (!srv->do_check)
1649 return 0;
1650
1651 if (srv->trackit) {
1652 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1653 file, linenum);
1654 return ERR_ALERT | ERR_FATAL;
1655 }
1656
1657 if (server_healthcheck_validate(file, linenum, srv) < 0)
1658 return ERR_ALERT | ERR_ABORT;
1659
1660 /* note: check type will be set during the config review phase */
1661 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1662 if (ret) {
1663 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1664 return ERR_ALERT | ERR_ABORT;
1665 }
1666
1667 return 0;
1668}
1669
1670/*
1671 * Initialize <srv> agent check structure.
1672 * Returns the error string in case of memory allocation failure, NULL if not.
1673 */
1674static const char *do_server_agent_check_init(struct server *srv, int state)
1675{
1676 const char *ret;
1677
1678 if (!srv->do_agent)
1679 return NULL;
1680
1681 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1682 if (ret)
1683 return ret;
1684
1685 if (!srv->agent.inter)
1686 srv->agent.inter = srv->check.inter;
1687
1688 srv->agent.state |= state;
1689 global.maxsock++;
1690
1691 return NULL;
1692}
1693
1694static int server_agent_check_init(const char *file, int linenum,
1695 struct server *srv, struct proxy *curproxy)
1696{
1697 const char *ret;
1698
1699 if (!srv->do_agent)
1700 return 0;
1701
1702 if (!srv->agent.port) {
1703 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1704 file, linenum, srv->id);
1705 return ERR_ALERT | ERR_FATAL;
1706 }
1707
1708 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1709 if (ret) {
1710 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1711 return ERR_ALERT | ERR_ABORT;
1712 }
1713
1714 return 0;
1715}
1716
1717#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1718static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1719 struct server *srv, struct proxy *proxy)
1720{
1721 int ret;
1722 char *err = NULL;
1723
1724 if (!srv->sni_expr)
1725 return 0;
1726
1727 ret = server_parse_sni_expr(srv, proxy, &err);
1728 if (!ret)
1729 return 0;
1730
1731 display_parser_err(file, linenum, args, cur_arg, &err);
1732 free(err);
1733
1734 return ret;
1735}
1736#endif
1737
1738/*
1739 * Server initializations finalization.
1740 * Initialize health check, agent check and SNI expression if enabled.
1741 * Must not be called for a default server instance.
1742 */
1743static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1744 struct server *srv, struct proxy *px)
1745{
1746 int ret;
1747
1748 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1749 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1750 return ret;
1751 }
1752
1753#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1754 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1755 return ret;
1756#endif
1757
1758 if (srv->flags & SRV_F_BACKUP)
1759 px->srv_bck++;
1760 else
1761 px->srv_act++;
1762 srv_lb_commit_status(srv);
1763
1764 return 0;
1765}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001766
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001767/*
1768 * Parse as much as possible such a range string argument: low[-high]
1769 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1770 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1771 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1772 * Returns 0 if succeeded, -1 if not.
1773 */
1774static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1775{
1776 char *nb_high_arg;
1777
1778 *nb_high = 0;
1779 chunk_printf(&trash, "%s", arg);
1780 *nb_low = atoi(trash.str);
1781
1782 if ((nb_high_arg = strchr(trash.str, '-'))) {
1783 *nb_high_arg++ = '\0';
1784 *nb_high = atoi(nb_high_arg);
1785 }
1786 else {
1787 *nb_high += *nb_low;
1788 *nb_low = 1;
1789 }
1790
1791 if (*nb_low < 0 || *nb_high < *nb_low)
1792 return -1;
1793
1794 return 0;
1795}
1796
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001797static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1798{
1799 chunk_printf(&trash, "%s%d", prefix, nb);
1800 free(srv->id);
1801 srv->id = strdup(trash.str);
1802}
1803
1804/*
1805 * Initialize as much as possible servers from <srv> server template.
1806 * Note that a server template is a special server with
1807 * a few different parameters than a server which has
1808 * been parsed mostly the same way as a server.
1809 * Returns the number of servers succesfully allocated,
1810 * 'srv' template included.
1811 */
1812static int server_template_init(struct server *srv, struct proxy *px)
1813{
1814 int i;
1815 struct server *newsrv;
1816
1817 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1818 int check_init_state;
1819 int agent_init_state;
1820
1821 newsrv = new_server(px);
1822 if (!newsrv)
1823 goto err;
1824
1825 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001826 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001827#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1828 if (newsrv->sni_expr) {
1829 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1830 if (!newsrv->ssl_ctx.sni)
1831 goto err;
1832 }
1833#endif
1834 /* Set this new server ID. */
1835 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1836
1837 /* Initial checks states. */
1838 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1839 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1840
1841 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1842 do_server_agent_check_init(newsrv, agent_init_state))
1843 goto err;
1844
1845 /* Linked backwards first. This will be restablished after parsing. */
1846 newsrv->next = px->srv;
1847 px->srv = newsrv;
1848 }
1849 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1850
1851 return i - srv->tmpl_info.nb_low;
1852
1853 err:
1854 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1855 if (newsrv) {
1856#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1857 release_sample_expr(newsrv->ssl_ctx.sni);
1858#endif
1859 free_check(&newsrv->agent);
1860 free_check(&newsrv->check);
1861 }
1862 free(newsrv);
1863 return i - srv->tmpl_info.nb_low;
1864}
1865
Willy Tarreau272adea2014-03-31 10:39:59 +02001866int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1867{
1868 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001869 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001870 char *errmsg = NULL;
1871 int err_code = 0;
1872 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001873 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001874
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001875 if (!strcmp(args[0], "server") ||
1876 !strcmp(args[0], "default-server") ||
1877 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001878 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001879 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001880 int srv = !defsrv && !strcmp(args[0], "server");
1881 int srv_tmpl = !defsrv && !srv;
1882 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001883
1884 if (!defsrv && curproxy == defproxy) {
1885 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1886 err_code |= ERR_ALERT | ERR_FATAL;
1887 goto out;
1888 }
1889 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1890 err_code |= ERR_ALERT | ERR_FATAL;
1891
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001892 /* There is no mandatory first arguments for default server. */
1893 if (srv) {
1894 if (!*args[2]) {
1895 /* 'server' line number of argument check. */
1896 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1897 file, linenum, args[0]);
1898 err_code |= ERR_ALERT | ERR_FATAL;
1899 goto out;
1900 }
1901
1902 err = invalid_char(args[1]);
1903 }
1904 else if (srv_tmpl) {
1905 if (!*args[3]) {
1906 /* 'server-template' line number of argument check. */
1907 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1908 file, linenum, args[0]);
1909 err_code |= ERR_ALERT | ERR_FATAL;
1910 goto out;
1911 }
1912
1913 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001914 }
1915
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001916 if (err) {
1917 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1918 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001919 err_code |= ERR_ALERT | ERR_FATAL;
1920 goto out;
1921 }
1922
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001923 cur_arg = 2;
1924 if (srv_tmpl) {
1925 /* Parse server-template <nb | range> arg. */
1926 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1927 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1928 file, linenum, args[0], args[cur_arg]);
1929 err_code |= ERR_ALERT | ERR_FATAL;
1930 goto out;
1931 }
1932 cur_arg++;
1933 }
1934
Willy Tarreau272adea2014-03-31 10:39:59 +02001935 if (!defsrv) {
1936 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001937 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001938 struct protocol *proto;
1939
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001940 newsrv = new_server(curproxy);
1941 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001942 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1943 err_code |= ERR_ALERT | ERR_ABORT;
1944 goto out;
1945 }
1946
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001947 if (srv_tmpl) {
1948 newsrv->tmpl_info.nb_low = tmpl_range_low;
1949 newsrv->tmpl_info.nb_high = tmpl_range_high;
1950 }
1951
Willy Tarreau272adea2014-03-31 10:39:59 +02001952 /* the servers are linked backwards first */
1953 newsrv->next = curproxy->srv;
1954 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001955 newsrv->conf.file = strdup(file);
1956 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001957 /* Note: for a server template, its id is its prefix.
1958 * This is a temporary id which will be used for server allocations to come
1959 * after parsing.
1960 */
1961 if (srv)
1962 newsrv->id = strdup(args[1]);
1963 else
1964 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001965
1966 /* several ways to check the port component :
1967 * - IP => port=+0, relative (IPv4 only)
1968 * - IP: => port=+0, relative
1969 * - IP:N => port=N, absolute
1970 * - IP:+N => port=+N, relative
1971 * - IP:-N => port=-N, relative
1972 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001973 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001974 if (!sk) {
1975 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1976 err_code |= ERR_ALERT | ERR_FATAL;
1977 goto out;
1978 }
1979
1980 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001981 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001982 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1983 file, linenum, args[0], args[1]);
1984 err_code |= ERR_ALERT | ERR_FATAL;
1985 goto out;
1986 }
1987
1988 if (!port1 || !port2) {
1989 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001990 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001991 }
1992 else if (port1 != port2) {
1993 /* port range */
1994 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1995 file, linenum, args[0], args[1], args[2]);
1996 err_code |= ERR_ALERT | ERR_FATAL;
1997 goto out;
1998 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001999
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002000 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002001 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02002002 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02002003 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02002004 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
2005 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2006 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002007 err_code |= ERR_ALERT | ERR_FATAL;
2008 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002009 }
2010 }
2011 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2012 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2013 file, linenum, newsrv->id);
2014 err_code |= ERR_ALERT | ERR_FATAL;
2015 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002016 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002017 }
2018
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002020 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002021
Olivier Houchard8da5f982017-08-04 18:35:36 +02002022 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002023 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002024 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002025 err_code |= ERR_ALERT | ERR_FATAL;
2026 goto out;
2027 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002028
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002029 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002030 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet29f77e82017-06-08 14:04:45 +02002031 SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002032 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002033 } else {
2034 newsrv = &curproxy->defsrv;
2035 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002036 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002037 }
2038
2039 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002040 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002041 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2042 if (err) {
2043 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2044 file, linenum, *err, newsrv->id);
2045 err_code |= ERR_ALERT | ERR_FATAL;
2046 goto out;
2047 }
2048 if (val <= 0) {
2049 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2050 file, linenum, val, args[cur_arg], newsrv->id);
2051 err_code |= ERR_ALERT | ERR_FATAL;
2052 goto out;
2053 }
2054 newsrv->agent.inter = val;
2055 cur_arg += 2;
2056 }
Misiekea849332017-01-09 09:39:51 +01002057 else if (!strcmp(args[cur_arg], "agent-addr")) {
2058 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2059 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2060 goto out;
2061 }
2062
2063 cur_arg += 2;
2064 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002065 else if (!strcmp(args[cur_arg], "agent-port")) {
2066 global.maxsock++;
2067 newsrv->agent.port = atol(args[cur_arg + 1]);
2068 cur_arg += 2;
2069 }
James Brown55f9ff12015-10-21 18:19:05 -07002070 else if (!strcmp(args[cur_arg], "agent-send")) {
2071 global.maxsock++;
2072 free(newsrv->agent.send_string);
2073 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2074 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2075 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2076 cur_arg += 2;
2077 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002078 else if (!strcmp(args[cur_arg], "init-addr")) {
2079 char *p, *end;
2080 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002081 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002082
2083 newsrv->init_addr_methods = 0;
2084 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2085
2086 for (p = args[cur_arg + 1]; *p; p = end) {
2087 /* cut on next comma */
2088 for (end = p; *end && *end != ','; end++);
2089 if (*end)
2090 *(end++) = 0;
2091
Willy Tarreau4310d362016-11-02 15:05:56 +01002092 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002093 if (!strcmp(p, "libc")) {
2094 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2095 }
2096 else if (!strcmp(p, "last")) {
2097 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2098 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002099 else if (!strcmp(p, "none")) {
2100 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2101 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002102 else if (str2ip2(p, &sa, 0)) {
2103 if (is_addr(&newsrv->init_addr)) {
2104 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2105 file, linenum, args[cur_arg], p);
2106 err_code |= ERR_ALERT | ERR_FATAL;
2107 goto out;
2108 }
2109 newsrv->init_addr = sa;
2110 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2111 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002112 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002113 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002114 file, linenum, args[cur_arg], p);
2115 err_code |= ERR_ALERT | ERR_FATAL;
2116 goto out;
2117 }
2118 if (!done) {
2119 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2120 file, linenum, args[cur_arg], p);
2121 err_code |= ERR_ALERT | ERR_FATAL;
2122 goto out;
2123 }
2124 }
2125 cur_arg += 2;
2126 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002127 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002128 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002129 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2130 cur_arg += 2;
2131 }
2132 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2133 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002134 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002135 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002136 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002137 else {
2138 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2139 file, linenum, args[cur_arg]);
2140 err_code |= ERR_ALERT | ERR_FATAL;
2141 goto out;
2142 }
2143 cur_arg += 2;
2144 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002145 else if (!strcmp(args[cur_arg], "resolve-net")) {
2146 char *p, *e;
2147 unsigned char mask;
2148 struct dns_options *opt;
2149
2150 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2151 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2152 file, linenum, args[cur_arg]);
2153 err_code |= ERR_ALERT | ERR_FATAL;
2154 goto out;
2155 }
2156
2157 opt = &newsrv->dns_opts;
2158
2159 /* Split arguments by comma, and convert it from ipv4 or ipv6
2160 * string network in in_addr or in6_addr.
2161 */
2162 p = args[cur_arg + 1];
2163 e = p;
2164 while (*p != '\0') {
2165 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002166 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002167 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2168 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2169 err_code |= ERR_ALERT | ERR_FATAL;
2170 goto out;
2171 }
2172 /* look for end or comma. */
2173 while (*e != ',' && *e != '\0')
2174 e++;
2175 if (*e == ',') {
2176 *e = '\0';
2177 e++;
2178 }
2179 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2180 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2181 /* Try to convert input string from ipv4 or ipv6 network. */
2182 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2183 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2184 &mask)) {
2185 /* Try to convert input string from ipv6 network. */
2186 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2187 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2188 } else {
2189 /* All network conversions fail, retrun error. */
2190 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2191 file, linenum, args[cur_arg], p);
2192 err_code |= ERR_ALERT | ERR_FATAL;
2193 goto out;
2194 }
2195 opt->pref_net_nb++;
2196 p = e;
2197 }
2198
2199 cur_arg += 2;
2200 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002201 else if (!strcmp(args[cur_arg], "rise")) {
2202 if (!*args[cur_arg + 1]) {
2203 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2204 file, linenum, args[cur_arg]);
2205 err_code |= ERR_ALERT | ERR_FATAL;
2206 goto out;
2207 }
2208
2209 newsrv->check.rise = atol(args[cur_arg + 1]);
2210 if (newsrv->check.rise <= 0) {
2211 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2212 file, linenum, args[cur_arg]);
2213 err_code |= ERR_ALERT | ERR_FATAL;
2214 goto out;
2215 }
2216
2217 if (newsrv->check.health)
2218 newsrv->check.health = newsrv->check.rise;
2219 cur_arg += 2;
2220 }
2221 else if (!strcmp(args[cur_arg], "fall")) {
2222 newsrv->check.fall = atol(args[cur_arg + 1]);
2223
2224 if (!*args[cur_arg + 1]) {
2225 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2226 file, linenum, args[cur_arg]);
2227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230
2231 if (newsrv->check.fall <= 0) {
2232 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2233 file, linenum, args[cur_arg]);
2234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
2236 }
2237
2238 cur_arg += 2;
2239 }
2240 else if (!strcmp(args[cur_arg], "inter")) {
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 'inter' 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.inter = val;
2255 cur_arg += 2;
2256 }
2257 else if (!strcmp(args[cur_arg], "fastinter")) {
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 'fastinter' 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.fastinter = val;
2272 cur_arg += 2;
2273 }
2274 else if (!strcmp(args[cur_arg], "downinter")) {
2275 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2276 if (err) {
2277 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2278 file, linenum, *err, newsrv->id);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
2281 }
2282 if (val <= 0) {
2283 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2284 file, linenum, val, args[cur_arg], newsrv->id);
2285 err_code |= ERR_ALERT | ERR_FATAL;
2286 goto out;
2287 }
2288 newsrv->check.downinter = val;
2289 cur_arg += 2;
2290 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002291 else if (!strcmp(args[cur_arg], "port")) {
2292 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002293 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002294 cur_arg += 2;
2295 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002296 else if (!strcmp(args[cur_arg], "weight")) {
2297 int w;
2298 w = atol(args[cur_arg + 1]);
2299 if (w < 0 || w > SRV_UWGHT_MAX) {
2300 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2301 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2302 err_code |= ERR_ALERT | ERR_FATAL;
2303 goto out;
2304 }
2305 newsrv->uweight = newsrv->iweight = w;
2306 cur_arg += 2;
2307 }
2308 else if (!strcmp(args[cur_arg], "minconn")) {
2309 newsrv->minconn = atol(args[cur_arg + 1]);
2310 cur_arg += 2;
2311 }
2312 else if (!strcmp(args[cur_arg], "maxconn")) {
2313 newsrv->maxconn = atol(args[cur_arg + 1]);
2314 cur_arg += 2;
2315 }
2316 else if (!strcmp(args[cur_arg], "maxqueue")) {
2317 newsrv->maxqueue = atol(args[cur_arg + 1]);
2318 cur_arg += 2;
2319 }
2320 else if (!strcmp(args[cur_arg], "slowstart")) {
2321 /* slowstart is stored in seconds */
2322 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2323 if (err) {
2324 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2325 file, linenum, *err, newsrv->id);
2326 err_code |= ERR_ALERT | ERR_FATAL;
2327 goto out;
2328 }
2329 newsrv->slowstart = (val + 999) / 1000;
2330 cur_arg += 2;
2331 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002332 else if (!strcmp(args[cur_arg], "on-error")) {
2333 if (!strcmp(args[cur_arg + 1], "fastinter"))
2334 newsrv->onerror = HANA_ONERR_FASTINTER;
2335 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2336 newsrv->onerror = HANA_ONERR_FAILCHK;
2337 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2338 newsrv->onerror = HANA_ONERR_SUDDTH;
2339 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2340 newsrv->onerror = HANA_ONERR_MARKDWN;
2341 else {
2342 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2343 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2344 file, linenum, args[cur_arg], args[cur_arg + 1]);
2345 err_code |= ERR_ALERT | ERR_FATAL;
2346 goto out;
2347 }
2348
2349 cur_arg += 2;
2350 }
2351 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2352 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2353 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2354 else {
2355 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2356 file, linenum, args[cur_arg], args[cur_arg + 1]);
2357 err_code |= ERR_ALERT | ERR_FATAL;
2358 goto out;
2359 }
2360
2361 cur_arg += 2;
2362 }
2363 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2364 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2365 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2366 else {
2367 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2368 file, linenum, args[cur_arg], args[cur_arg + 1]);
2369 err_code |= ERR_ALERT | ERR_FATAL;
2370 goto out;
2371 }
2372
2373 cur_arg += 2;
2374 }
2375 else if (!strcmp(args[cur_arg], "error-limit")) {
2376 if (!*args[cur_arg + 1]) {
2377 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2378 file, linenum, args[cur_arg]);
2379 err_code |= ERR_ALERT | ERR_FATAL;
2380 goto out;
2381 }
2382
2383 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2384
2385 if (newsrv->consecutive_errors_limit <= 0) {
2386 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2387 file, linenum, args[cur_arg]);
2388 err_code |= ERR_ALERT | ERR_FATAL;
2389 goto out;
2390 }
2391 cur_arg += 2;
2392 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002393 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002394 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2395 file, linenum, "usesrc", "source");
2396 err_code |= ERR_ALERT | ERR_FATAL;
2397 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002398 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002399 else {
2400 static int srv_dumped;
2401 struct srv_kw *kw;
2402 char *err;
2403
2404 kw = srv_find_kw(args[cur_arg]);
2405 if (kw) {
2406 char *err = NULL;
2407 int code;
2408
2409 if (!kw->parse) {
2410 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2411 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002412 if (kw->skip != -1)
2413 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002414 err_code |= ERR_ALERT | ERR_FATAL;
2415 goto out;
2416 }
2417
2418 if (defsrv && !kw->default_ok) {
2419 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2420 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002421 if (kw->skip != -1)
2422 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002423 err_code |= ERR_ALERT;
2424 continue;
2425 }
2426
2427 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2428 err_code |= code;
2429
2430 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002431 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002432 if (code & ERR_FATAL) {
2433 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002434 if (kw->skip != -1)
2435 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002436 goto out;
2437 }
2438 }
2439 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002440 if (kw->skip != -1)
2441 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002442 continue;
2443 }
2444
2445 err = NULL;
2446 if (!srv_dumped) {
2447 srv_dump_kws(&err);
2448 indent_msg(&err, 4);
2449 srv_dumped = 1;
2450 }
2451
2452 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2453 file, linenum, args[0], args[1], args[cur_arg],
2454 err ? " Registered keywords :" : "", err ? err : "");
2455 free(err);
2456
2457 err_code |= ERR_ALERT | ERR_FATAL;
2458 goto out;
2459 }
2460 }
2461
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002462 if (!defsrv)
2463 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2464 if (err_code & ERR_FATAL)
2465 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002466 if (srv_tmpl)
2467 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002468 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002469 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002470 return 0;
2471
2472 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002473 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002474 free(errmsg);
2475 return err_code;
2476}
2477
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002478/* Returns a pointer to the first server matching either id <id>.
2479 * NULL is returned if no match is found.
2480 * the lookup is performed in the backend <bk>
2481 */
2482struct server *server_find_by_id(struct proxy *bk, int id)
2483{
2484 struct eb32_node *eb32;
2485 struct server *curserver;
2486
2487 if (!bk || (id ==0))
2488 return NULL;
2489
2490 /* <bk> has no backend capabilities, so it can't have a server */
2491 if (!(bk->cap & PR_CAP_BE))
2492 return NULL;
2493
2494 curserver = NULL;
2495
2496 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2497 if (eb32)
2498 curserver = container_of(eb32, struct server, conf.id);
2499
2500 return curserver;
2501}
2502
2503/* Returns a pointer to the first server matching either name <name>, or id
2504 * if <name> starts with a '#'. NULL is returned if no match is found.
2505 * the lookup is performed in the backend <bk>
2506 */
2507struct server *server_find_by_name(struct proxy *bk, const char *name)
2508{
2509 struct server *curserver;
2510
2511 if (!bk || !name)
2512 return NULL;
2513
2514 /* <bk> has no backend capabilities, so it can't have a server */
2515 if (!(bk->cap & PR_CAP_BE))
2516 return NULL;
2517
2518 curserver = NULL;
2519 if (*name == '#') {
2520 curserver = server_find_by_id(bk, atoi(name + 1));
2521 if (curserver)
2522 return curserver;
2523 }
2524 else {
2525 curserver = bk->srv;
2526
2527 while (curserver && (strcmp(curserver->id, name) != 0))
2528 curserver = curserver->next;
2529
2530 if (curserver)
2531 return curserver;
2532 }
2533
2534 return NULL;
2535}
2536
2537struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2538{
2539 struct server *byname;
2540 struct server *byid;
2541
2542 if (!name && !id)
2543 return NULL;
2544
2545 if (diff)
2546 *diff = 0;
2547
2548 byname = byid = NULL;
2549
2550 if (name) {
2551 byname = server_find_by_name(bk, name);
2552 if (byname && (!id || byname->puid == id))
2553 return byname;
2554 }
2555
2556 /* remaining possibilities :
2557 * - name not set
2558 * - name set but not found
2559 * - name found but ID doesn't match
2560 */
2561 if (id) {
2562 byid = server_find_by_id(bk, id);
2563 if (byid) {
2564 if (byname) {
2565 /* use id only if forced by configuration */
2566 if (byid->flags & SRV_F_FORCED_ID) {
2567 if (diff)
2568 *diff |= 2;
2569 return byid;
2570 }
2571 else {
2572 if (diff)
2573 *diff |= 1;
2574 return byname;
2575 }
2576 }
2577
2578 /* remaining possibilities:
2579 * - name not set
2580 * - name set but not found
2581 */
2582 if (name && diff)
2583 *diff |= 2;
2584 return byid;
2585 }
2586
2587 /* id bot found */
2588 if (byname) {
2589 if (diff)
2590 *diff |= 1;
2591 return byname;
2592 }
2593 }
2594
2595 return NULL;
2596}
2597
Christopher Faulet5d42e092017-10-16 12:00:40 +02002598/* Registers changes to be applied asynchronously */
2599static void srv_register_update(struct server *srv)
2600{
2601 if (LIST_ISEMPTY(&srv->update_status)) {
2602 THREAD_WANT_SYNC();
2603 SPIN_LOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
2604 if (LIST_ISEMPTY(&srv->update_status))
2605 LIST_ADDQ(&updated_servers, &srv->update_status);
2606 SPIN_UNLOCK(UPDATED_SERVERS_LOCK, &updated_servers_lock);
2607 }
2608}
2609
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002610/* Update a server state using the parameters available in the params list */
2611static void srv_update_state(struct server *srv, int version, char **params)
2612{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002613 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002614 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002615
2616 /* fields since version 1
2617 * and common to all other upcoming versions
2618 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002619 enum srv_state srv_op_state;
2620 enum srv_admin srv_admin_state;
2621 unsigned srv_uweight, srv_iweight;
2622 unsigned long srv_last_time_change;
2623 short srv_check_status;
2624 enum chk_result srv_check_result;
2625 int srv_check_health;
2626 int srv_check_state, srv_agent_state;
2627 int bk_f_forced_id;
2628 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002629 int fqdn_set_by_cli;
2630 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002631 const char *port_str;
2632 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002633
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002634 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002635 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002636 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002637 switch (version) {
2638 case 1:
2639 /*
2640 * now we can proceed with server's state update:
2641 * srv_addr: params[0]
2642 * srv_op_state: params[1]
2643 * srv_admin_state: params[2]
2644 * srv_uweight: params[3]
2645 * srv_iweight: params[4]
2646 * srv_last_time_change: params[5]
2647 * srv_check_status: params[6]
2648 * srv_check_result: params[7]
2649 * srv_check_health: params[8]
2650 * srv_check_state: params[9]
2651 * srv_agent_state: params[10]
2652 * bk_f_forced_id: params[11]
2653 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002654 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002655 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002656 */
2657
2658 /* validating srv_op_state */
2659 p = NULL;
2660 errno = 0;
2661 srv_op_state = strtol(params[1], &p, 10);
2662 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2663 (srv_op_state != SRV_ST_STOPPED &&
2664 srv_op_state != SRV_ST_STARTING &&
2665 srv_op_state != SRV_ST_RUNNING &&
2666 srv_op_state != SRV_ST_STOPPING)) {
2667 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2668 }
2669
2670 /* validating srv_admin_state */
2671 p = NULL;
2672 errno = 0;
2673 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002674 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002675
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002676 /* inherited statuses will be recomputed later.
2677 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2678 */
2679 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002680
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002681 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2682 (srv_admin_state != 0 &&
2683 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002684 srv_admin_state != SRV_ADMF_CMAINT &&
2685 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002686 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002687 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002688 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2689 }
2690
2691 /* validating srv_uweight */
2692 p = NULL;
2693 errno = 0;
2694 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002695 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002696 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2697
2698 /* validating srv_iweight */
2699 p = NULL;
2700 errno = 0;
2701 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002702 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002703 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2704
2705 /* validating srv_last_time_change */
2706 p = NULL;
2707 errno = 0;
2708 srv_last_time_change = strtol(params[5], &p, 10);
2709 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2710 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2711
2712 /* validating srv_check_status */
2713 p = NULL;
2714 errno = 0;
2715 srv_check_status = strtol(params[6], &p, 10);
2716 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2717 (srv_check_status >= HCHK_STATUS_SIZE))
2718 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2719
2720 /* validating srv_check_result */
2721 p = NULL;
2722 errno = 0;
2723 srv_check_result = strtol(params[7], &p, 10);
2724 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2725 (srv_check_result != CHK_RES_UNKNOWN &&
2726 srv_check_result != CHK_RES_NEUTRAL &&
2727 srv_check_result != CHK_RES_FAILED &&
2728 srv_check_result != CHK_RES_PASSED &&
2729 srv_check_result != CHK_RES_CONDPASS)) {
2730 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2731 }
2732
2733 /* validating srv_check_health */
2734 p = NULL;
2735 errno = 0;
2736 srv_check_health = strtol(params[8], &p, 10);
2737 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2738 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2739
2740 /* validating srv_check_state */
2741 p = NULL;
2742 errno = 0;
2743 srv_check_state = strtol(params[9], &p, 10);
2744 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2745 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2746 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2747
2748 /* validating srv_agent_state */
2749 p = NULL;
2750 errno = 0;
2751 srv_agent_state = strtol(params[10], &p, 10);
2752 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2753 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2754 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2755
2756 /* validating bk_f_forced_id */
2757 p = NULL;
2758 errno = 0;
2759 bk_f_forced_id = strtol(params[11], &p, 10);
2760 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2761 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2762
2763 /* validating srv_f_forced_id */
2764 p = NULL;
2765 errno = 0;
2766 srv_f_forced_id = strtol(params[12], &p, 10);
2767 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2768 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2769
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002770 /* validating srv_fqdn */
2771 fqdn = params[13];
2772 if (fqdn && *fqdn == '-')
2773 fqdn = NULL;
2774 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2775 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2776 fqdn = NULL;
2777 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002778
Frédéric Lécaille31694712017-08-01 08:47:19 +02002779 port_str = params[14];
2780 if (port_str) {
2781 port = strl2uic(port_str, strlen(port_str));
2782 if (port > USHRT_MAX) {
2783 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2784 port_str = NULL;
2785 }
2786 }
2787
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002788 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002789 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002790 goto out;
2791
Emeric Brun9f0b4582017-10-23 14:39:51 +02002792 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 /* recover operational state and apply it to this server
2794 * and all servers tracking this one */
2795 switch (srv_op_state) {
2796 case SRV_ST_STOPPED:
2797 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002798 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002799 break;
2800 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002801 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002802 break;
2803 case SRV_ST_STOPPING:
2804 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002805 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002806 break;
2807 case SRV_ST_RUNNING:
2808 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002809 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002810 break;
2811 }
2812
2813 /* When applying server state, the following rules apply:
2814 * - in case of a configuration change, we apply the setting from the new
2815 * configuration, regardless of old running state
2816 * - if no configuration change, we apply old running state only if old running
2817 * state is different from new configuration state
2818 */
2819 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002820 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2821 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002822 srv_adm_set_maint(srv);
2823 else
2824 srv_adm_set_ready(srv);
2825 }
2826 /* configuration is the same, let's compate old running state and new conf state */
2827 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002828 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002829 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002830 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002831 srv_adm_set_ready(srv);
2832 }
2833 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002834 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002835 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002836 * (srv->iweight is the weight set up in configuration).
2837 * There are two possible reasons for FDRAIN to have been present :
2838 * - previous config weight was zero
2839 * - "set server b/s drain" was sent to the CLI
2840 *
2841 * In the first case, we simply want to drop this drain state
2842 * if the new weight is not zero anymore, meaning the administrator
2843 * has intentionally turned the weight back to a positive value to
2844 * enable the server again after an operation. In the second case,
2845 * the drain state was forced on the CLI regardless of the config's
2846 * weight so we don't want a change to the config weight to lose this
2847 * status. What this means is :
2848 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2849 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002850 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002851 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002852 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002853 }
2854
2855 srv->last_change = date.tv_sec - srv_last_time_change;
2856 srv->check.status = srv_check_status;
2857 srv->check.result = srv_check_result;
2858 srv->check.health = srv_check_health;
2859
2860 /* Only case we want to apply is removing ENABLED flag which could have been
2861 * done by the "disable health" command over the stats socket
2862 */
2863 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2864 (srv_check_state & CHK_ST_CONFIGURED) &&
2865 !(srv_check_state & CHK_ST_ENABLED))
2866 srv->check.state &= ~CHK_ST_ENABLED;
2867
2868 /* Only case we want to apply is removing ENABLED flag which could have been
2869 * done by the "disable agent" command over the stats socket
2870 */
2871 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2872 (srv_agent_state & CHK_ST_CONFIGURED) &&
2873 !(srv_agent_state & CHK_ST_ENABLED))
2874 srv->agent.state &= ~CHK_ST_ENABLED;
2875
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002876 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2877 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002878 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002879 * It means that a configuration file change has precedence over a unix socket change
2880 * for server's weight
2881 *
2882 * by default, HAProxy applies the following weight when parsing the configuration
2883 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002884 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002885 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002886 srv->uweight = srv_uweight;
2887 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002888 server_recalc_eweight(srv);
2889
Willy Tarreaue5a60682016-11-09 14:54:53 +01002890 /* load server IP address */
2891 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002892
2893 if (fqdn && srv->hostname) {
2894 if (!strcmp(srv->hostname, fqdn)) {
2895 /* Here we reset the 'set from stats socket FQDN' flag
2896 * to support such transitions:
2897 * Let's say initial FQDN value is foo1 (in configuration file).
2898 * - FQDN changed from stats socket, from foo1 to foo2 value,
2899 * - FQDN changed again from file configuration (with the same previous value
2900 set from stats socket, from foo1 to foo2 value),
2901 * - reload for any other reason than a FQDN modification,
2902 * the configuration file FQDN matches the fqdn server state file value.
2903 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2904 * any futher FQDN modification.
2905 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002906 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002907 }
2908 else {
2909 /* If the FDQN has been changed from stats socket,
2910 * apply fqdn state file value (which is the value set
2911 * from stats socket).
2912 */
2913 if (fqdn_set_by_cli) {
Olivier Houchardd16bfe62017-10-31 15:21:19 +01002914 srv_set_fqdn(srv, fqdn, 0);
Emeric Brun52a91d32017-08-31 14:41:55 +02002915 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002916 }
2917 }
2918 }
2919
Frédéric Lécaille31694712017-08-01 08:47:19 +02002920 if (port_str)
2921 srv->svc_port = port;
Emeric Brun9f0b4582017-10-23 14:39:51 +02002922 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Frédéric Lécaille31694712017-08-01 08:47:19 +02002923
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002924 break;
2925 default:
2926 chunk_appendf(msg, ", version '%d' not supported", version);
2927 }
2928
2929 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002930 if (msg->len) {
2931 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002932 Warning("server-state application failed for server '%s/%s'%s",
2933 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002934 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002935}
2936
2937/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2938 * For each proxy, it does the following:
2939 * - opens its server state file (either one or local one)
2940 * - read whole file, line by line
2941 * - analyse each line to check if it matches our current backend:
2942 * - backend name matches
2943 * - backend id matches if id is forced and name doesn't match
2944 * - if the server pointed by the line is found, then state is applied
2945 *
2946 * If the running backend uuid or id differs from the state file, then HAProxy reports
2947 * a warning.
2948 */
2949void apply_server_state(void)
2950{
2951 char *cur, *end;
2952 char mybuf[SRV_STATE_LINE_MAXLEN];
2953 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002954 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2955 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002956 int arg, srv_arg, version, diff;
2957 FILE *f;
2958 char *filepath;
2959 char globalfilepath[MAXPATHLEN + 1];
2960 char localfilepath[MAXPATHLEN + 1];
2961 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2962 extern struct proxy *proxy;
2963 struct proxy *curproxy, *bk;
2964 struct server *srv;
2965
2966 globalfilepathlen = 0;
2967 /* create the globalfilepath variable */
2968 if (global.server_state_file) {
2969 /* absolute path or no base directory provided */
2970 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2971 len = strlen(global.server_state_file);
2972 if (len > MAXPATHLEN) {
2973 globalfilepathlen = 0;
2974 goto globalfileerror;
2975 }
2976 memcpy(globalfilepath, global.server_state_file, len);
2977 globalfilepath[len] = '\0';
2978 globalfilepathlen = len;
2979 }
2980 else if (global.server_state_base) {
2981 len = strlen(global.server_state_base);
2982 globalfilepathlen += len;
2983
2984 if (globalfilepathlen > MAXPATHLEN) {
2985 globalfilepathlen = 0;
2986 goto globalfileerror;
2987 }
2988 strncpy(globalfilepath, global.server_state_base, len);
2989 globalfilepath[globalfilepathlen] = 0;
2990
2991 /* append a slash if needed */
2992 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2993 if (globalfilepathlen + 1 > MAXPATHLEN) {
2994 globalfilepathlen = 0;
2995 goto globalfileerror;
2996 }
2997 globalfilepath[globalfilepathlen++] = '/';
2998 }
2999
3000 len = strlen(global.server_state_file);
3001 if (globalfilepathlen + len > MAXPATHLEN) {
3002 globalfilepathlen = 0;
3003 goto globalfileerror;
3004 }
3005 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3006 globalfilepathlen += len;
3007 globalfilepath[globalfilepathlen++] = 0;
3008 }
3009 }
3010 globalfileerror:
3011 if (globalfilepathlen == 0)
3012 globalfilepath[0] = '\0';
3013
3014 /* read servers state from local file */
3015 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3016 /* servers are only in backends */
3017 if (!(curproxy->cap & PR_CAP_BE))
3018 continue;
3019 fileopenerr = 0;
3020 filepath = NULL;
3021
3022 /* search server state file path and name */
3023 switch (curproxy->load_server_state_from_file) {
3024 /* read servers state from global file */
3025 case PR_SRV_STATE_FILE_GLOBAL:
3026 /* there was an error while generating global server state file path */
3027 if (globalfilepathlen == 0)
3028 continue;
3029 filepath = globalfilepath;
3030 fileopenerr = 1;
3031 break;
3032 /* this backend has its own file */
3033 case PR_SRV_STATE_FILE_LOCAL:
3034 localfilepathlen = 0;
3035 localfilepath[0] = '\0';
3036 len = 0;
3037 /* create the localfilepath variable */
3038 /* absolute path or no base directory provided */
3039 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3040 len = strlen(curproxy->server_state_file_name);
3041 if (len > MAXPATHLEN) {
3042 localfilepathlen = 0;
3043 goto localfileerror;
3044 }
3045 memcpy(localfilepath, curproxy->server_state_file_name, len);
3046 localfilepath[len] = '\0';
3047 localfilepathlen = len;
3048 }
3049 else if (global.server_state_base) {
3050 len = strlen(global.server_state_base);
3051 localfilepathlen += len;
3052
3053 if (localfilepathlen > MAXPATHLEN) {
3054 localfilepathlen = 0;
3055 goto localfileerror;
3056 }
3057 strncpy(localfilepath, global.server_state_base, len);
3058 localfilepath[localfilepathlen] = 0;
3059
3060 /* append a slash if needed */
3061 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3062 if (localfilepathlen + 1 > MAXPATHLEN) {
3063 localfilepathlen = 0;
3064 goto localfileerror;
3065 }
3066 localfilepath[localfilepathlen++] = '/';
3067 }
3068
3069 len = strlen(curproxy->server_state_file_name);
3070 if (localfilepathlen + len > MAXPATHLEN) {
3071 localfilepathlen = 0;
3072 goto localfileerror;
3073 }
3074 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3075 localfilepathlen += len;
3076 localfilepath[localfilepathlen++] = 0;
3077 }
3078 filepath = localfilepath;
3079 localfileerror:
3080 if (localfilepathlen == 0)
3081 localfilepath[0] = '\0';
3082
3083 break;
3084 case PR_SRV_STATE_FILE_NONE:
3085 default:
3086 continue;
3087 }
3088
3089 /* preload global state file */
3090 errno = 0;
3091 f = fopen(filepath, "r");
3092 if (errno && fileopenerr)
3093 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3094 if (!f)
3095 continue;
3096
3097 mybuf[0] = '\0';
3098 mybuflen = 0;
3099 version = 0;
3100
3101 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003102 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3103 Warning("Can't read first line of the server state file '%s'\n", filepath);
3104 goto fileclose;
3105 }
3106
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003107 cur = mybuf;
3108 version = atoi(cur);
3109 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3110 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003111 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003112
3113 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3114 int bk_f_forced_id = 0;
3115 int check_id = 0;
3116 int check_name = 0;
3117
3118 mybuflen = strlen(mybuf);
3119 cur = mybuf;
3120 end = cur + mybuflen;
3121
3122 bk = NULL;
3123 srv = NULL;
3124
3125 /* we need at least one character */
3126 if (mybuflen == 0)
3127 continue;
3128
3129 /* ignore blank characters at the beginning of the line */
3130 while (isspace(*cur))
3131 ++cur;
3132
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003133 /* Ignore empty or commented lines */
3134 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003135 continue;
3136
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003137 /* truncated lines */
3138 if (mybuf[mybuflen - 1] != '\n') {
3139 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003140 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003141 }
3142
3143 /* Removes trailing '\n' */
3144 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003145
3146 /* we're now ready to move the line into *srv_params[] */
3147 params[0] = cur;
3148 arg = 1;
3149 srv_arg = 0;
3150 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3151 if (isspace(*cur)) {
3152 *cur = '\0';
3153 ++cur;
3154 while (isspace(*cur))
3155 ++cur;
3156 switch (version) {
3157 case 1:
3158 /*
3159 * srv_addr: params[4] => srv_params[0]
3160 * srv_op_state: params[5] => srv_params[1]
3161 * srv_admin_state: params[6] => srv_params[2]
3162 * srv_uweight: params[7] => srv_params[3]
3163 * srv_iweight: params[8] => srv_params[4]
3164 * srv_last_time_change: params[9] => srv_params[5]
3165 * srv_check_status: params[10] => srv_params[6]
3166 * srv_check_result: params[11] => srv_params[7]
3167 * srv_check_health: params[12] => srv_params[8]
3168 * srv_check_state: params[13] => srv_params[9]
3169 * srv_agent_state: params[14] => srv_params[10]
3170 * bk_f_forced_id: params[15] => srv_params[11]
3171 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003172 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003173 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003174 */
3175 if (arg >= 4) {
3176 srv_params[srv_arg] = cur;
3177 ++srv_arg;
3178 }
3179 break;
3180 }
3181
3182 params[arg] = cur;
3183 ++arg;
3184 }
3185 else {
3186 ++cur;
3187 }
3188 }
3189
3190 /* if line is incomplete line, then ignore it.
3191 * otherwise, update useful flags */
3192 switch (version) {
3193 case 1:
3194 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3195 continue;
3196 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3197 check_id = (atoi(params[0]) == curproxy->uuid);
3198 check_name = (strcmp(curproxy->id, params[1]) == 0);
3199 break;
3200 }
3201
3202 diff = 0;
3203 bk = curproxy;
3204
3205 /* if backend can't be found, let's continue */
3206 if (!check_id && !check_name)
3207 continue;
3208 else if (!check_id && check_name) {
3209 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3210 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3211 }
3212 else if (check_id && !check_name) {
3213 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3214 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3215 /* if name doesn't match, we still want to update curproxy if the backend id
3216 * was forced in previous the previous configuration */
3217 if (!bk_f_forced_id)
3218 continue;
3219 }
3220
3221 /* look for the server by its id: param[2] */
3222 /* else look for the server by its name: param[3] */
3223 diff = 0;
3224 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3225
3226 if (!srv) {
3227 /* if no server found, then warning and continue with next line */
3228 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3229 params[3], params[2], params[0], params[1]);
3230 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3231 params[3], params[2], params[0], params[1]);
3232 continue;
3233 }
3234 else if (diff & PR_FBM_MISMATCH_ID) {
3235 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);
3236 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 +02003237 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003238 }
3239 else if (diff & PR_FBM_MISMATCH_NAME) {
3240 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);
3241 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 +02003242 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003243 }
3244
3245 /* now we can proceed with server's state update */
3246 srv_update_state(srv, version, srv_params);
3247 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003248fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003249 fclose(f);
3250 }
3251}
3252
Simon Horman7d09b9a2013-02-12 10:45:51 +09003253/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003254 * update a server's current IP address.
3255 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3256 * ip is in network format.
3257 * updater is a string which contains an information about the requester of the update.
3258 * updater is used if not NULL.
3259 *
3260 * A log line and a stderr warning message is generated based on server's backend options.
3261 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003262int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003263{
3264 /* generates a log line and a warning on stderr */
3265 if (1) {
3266 /* book enough space for both IPv4 and IPv6 */
3267 char oldip[INET6_ADDRSTRLEN];
3268 char newip[INET6_ADDRSTRLEN];
3269
3270 memset(oldip, '\0', INET6_ADDRSTRLEN);
3271 memset(newip, '\0', INET6_ADDRSTRLEN);
3272
3273 /* copy old IP address in a string */
3274 switch (s->addr.ss_family) {
3275 case AF_INET:
3276 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3277 break;
3278 case AF_INET6:
3279 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3280 break;
3281 };
3282
3283 /* copy new IP address in a string */
3284 switch (ip_sin_family) {
3285 case AF_INET:
3286 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3287 break;
3288 case AF_INET6:
3289 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3290 break;
3291 };
3292
3293 /* save log line into a buffer */
3294 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3295 s->proxy->id, s->id, oldip, newip, updater);
3296
3297 /* write the buffer on stderr */
3298 Warning("%s.\n", trash.str);
3299
3300 /* send a log */
3301 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3302 }
3303
3304 /* save the new IP family */
3305 s->addr.ss_family = ip_sin_family;
3306 /* save the new IP address */
3307 switch (ip_sin_family) {
3308 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003309 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003310 break;
3311 case AF_INET6:
3312 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3313 break;
3314 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003315 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003316
3317 return 0;
3318}
3319
3320/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003321 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3322 *
3323 * Caller can pass its name through <updater> to get it integrated in the response message
3324 * returned by the function.
3325 *
3326 * The function first does the following, in that order:
3327 * - validates the new addr and/or port
3328 * - checks if an update is required (new IP or port is different than current ones)
3329 * - checks the update is allowed:
3330 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3331 * - allow all changes if no CHECKS are configured
3332 * - if CHECK is configured:
3333 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3334 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3335 * conditions are met
3336 */
3337const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3338{
3339 struct sockaddr_storage sa;
3340 int ret, port_change_required;
3341 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003342 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003343 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003344 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003345
3346 msg = get_trash_chunk();
3347 chunk_reset(msg);
3348
3349 if (addr) {
3350 memset(&sa, 0, sizeof(struct sockaddr_storage));
3351 if (str2ip2(addr, &sa, 0) == NULL) {
3352 chunk_printf(msg, "Invalid addr '%s'", addr);
3353 goto out;
3354 }
3355
3356 /* changes are allowed on AF_INET* families only */
3357 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3358 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3359 goto out;
3360 }
3361
3362 /* collecting data currently setup */
3363 memset(current_addr, '\0', sizeof(current_addr));
3364 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3365 /* changes are allowed on AF_INET* families only */
3366 if ((ret != AF_INET) && (ret != AF_INET6)) {
3367 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3368 goto out;
3369 }
3370
3371 /* applying ADDR changes if required and allowed
3372 * ipcmp returns 0 when both ADDR are the same
3373 */
3374 if (ipcmp(&s->addr, &sa) == 0) {
3375 chunk_appendf(msg, "no need to change the addr");
3376 goto port;
3377 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003378 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003379 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003380
3381 /* we also need to update check's ADDR only if it uses the server's one */
3382 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003383 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003384 }
3385
3386 /* we also need to update agent ADDR only if it use the server's one */
3387 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003388 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003389 }
3390
3391 /* update report for caller */
3392 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3393 }
3394
3395 port:
3396 if (port) {
3397 char sign = '\0';
3398 char *endptr;
3399
3400 if (addr)
3401 chunk_appendf(msg, ", ");
3402
3403 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003404 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003405
3406 /* check if PORT change is required */
3407 port_change_required = 0;
3408
3409 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003410 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003411 new_port = strtol(port, &endptr, 10);
3412 if ((errno != 0) || (port == endptr)) {
3413 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3414 goto out;
3415 }
3416
3417 /* check if caller triggers a port mapped or offset */
3418 if (sign == '-' || (sign == '+')) {
3419 /* check if server currently uses port map */
3420 if (!(s->flags & SRV_F_MAPPORTS)) {
3421 /* switch from fixed port to port map mandatorily triggers
3422 * a port change */
3423 port_change_required = 1;
3424 /* check is configured
3425 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3426 * prevent PORT change if check doesn't have it's dedicated port while switching
3427 * to port mapping */
3428 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3429 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.");
3430 goto out;
3431 }
3432 }
3433 /* we're already using port maps */
3434 else {
3435 port_change_required = current_port != new_port;
3436 }
3437 }
3438 /* fixed port */
3439 else {
3440 port_change_required = current_port != new_port;
3441 }
3442
3443 /* applying PORT changes if required and update response message */
3444 if (port_change_required) {
3445 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003446 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003447 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003448
3449 /* prepare message */
3450 chunk_appendf(msg, "port changed from '");
3451 if (s->flags & SRV_F_MAPPORTS)
3452 chunk_appendf(msg, "+");
3453 chunk_appendf(msg, "%d' to '", current_port);
3454
3455 if (sign == '-') {
3456 s->flags |= SRV_F_MAPPORTS;
3457 chunk_appendf(msg, "%c", sign);
3458 /* just use for result output */
3459 new_port = -new_port;
3460 }
3461 else if (sign == '+') {
3462 s->flags |= SRV_F_MAPPORTS;
3463 chunk_appendf(msg, "%c", sign);
3464 }
3465 else {
3466 s->flags &= ~SRV_F_MAPPORTS;
3467 }
3468
3469 chunk_appendf(msg, "%d'", new_port);
3470
3471 /* we also need to update health checks port only if it uses server's realport */
3472 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3473 s->check.port = new_port;
3474 }
3475 }
3476 else {
3477 chunk_appendf(msg, "no need to change the port");
3478 }
3479 }
3480
3481out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003482 if (changed)
3483 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003484 if (updater)
3485 chunk_appendf(msg, " by '%s'", updater);
3486 chunk_appendf(msg, "\n");
3487 return msg->str;
3488}
3489
3490
3491/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003492 * update server status based on result of name resolution
3493 * returns:
3494 * 0 if server status is updated
3495 * 1 if server status has not changed
3496 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003497int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003498{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003499 struct dns_resolvers *resolvers = s->resolvers;
3500 struct dns_resolution *resolution = s->dns_requester->resolution;
3501 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003502
3503 switch (resolution->status) {
3504 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003505 /* status when HAProxy has just (re)started.
3506 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003507 break;
3508
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003509 case RSLV_STATUS_VALID:
3510 /*
3511 * resume health checks
3512 * server will be turned back on if health check is safe
3513 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003514 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003515 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003516 return 1;
3517 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3518 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003519 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003520 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003521
Emeric Brun52a91d32017-08-31 14:41:55 +02003522 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003523 return 1;
3524 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3525 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3526 s->proxy->id, s->id);
3527
3528 Warning("%s.\n", trash.str);
3529 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3530 return 0;
3531
3532 case RSLV_STATUS_NX:
3533 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003534 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3535 if (!tick_is_expired(exp, now_ms))
3536 break;
3537
3538 if (s->next_admin & SRV_ADMF_RMAINT)
3539 return 1;
3540 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3541 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003542
3543 case RSLV_STATUS_TIMEOUT:
3544 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003545 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3546 if (!tick_is_expired(exp, now_ms))
3547 break;
3548
3549 if (s->next_admin & SRV_ADMF_RMAINT)
3550 return 1;
3551 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3552 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003553
3554 case RSLV_STATUS_REFUSED:
3555 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003556 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3557 if (!tick_is_expired(exp, now_ms))
3558 break;
3559
3560 if (s->next_admin & SRV_ADMF_RMAINT)
3561 return 1;
3562 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3563 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003564
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003565 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003566 /* stop server if resolution failed for a long enough period */
3567 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3568 if (!tick_is_expired(exp, now_ms))
3569 break;
3570
3571 if (s->next_admin & SRV_ADMF_RMAINT)
3572 return 1;
3573 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3574 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003575 }
3576
3577 return 1;
3578}
3579
3580/*
3581 * Server Name Resolution valid response callback
3582 * It expects:
3583 * - <nameserver>: the name server which answered the valid response
3584 * - <response>: buffer containing a valid DNS response
3585 * - <response_len>: size of <response>
3586 * It performs the following actions:
3587 * - ignore response if current ip found and server family not met
3588 * - update with first new ip found if family is met and current IP is not found
3589 * returns:
3590 * 0 on error
3591 * 1 when no error or safe ignore
3592 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003593int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003594{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003595 struct server *s = NULL;
3596 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003597 void *serverip, *firstip;
3598 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003599 int ret;
3600 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003601 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003602
Christopher Faulet67957bd2017-09-27 11:00:59 +02003603 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003604 if (!s)
3605 return 1;
3606
Christopher Faulet67957bd2017-09-27 11:00:59 +02003607 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003608
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003609 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003610 firstip = NULL; /* pointer to the first valid response found */
3611 /* it will be used as the new IP if a change is required */
3612 firstip_sin_family = AF_UNSPEC;
3613 serverip = NULL; /* current server IP address */
3614
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003615 /* initializing server IP pointer */
3616 server_sin_family = s->addr.ss_family;
3617 switch (server_sin_family) {
3618 case AF_INET:
3619 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3620 break;
3621
3622 case AF_INET6:
3623 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3624 break;
3625
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003626 case AF_UNSPEC:
3627 break;
3628
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003629 default:
3630 goto invalid;
3631 }
3632
Baptiste Assmann729c9012017-05-22 15:13:10 +02003633 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003634 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003635 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003636
3637 switch (ret) {
3638 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003639 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003640
3641 case DNS_UPD_SRVIP_NOT_FOUND:
3642 goto save_ip;
3643
3644 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003645 goto invalid;
3646
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003647 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003648 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003649 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003650
Baptiste Assmannfad03182015-10-28 02:03:32 +01003651 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003652 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003653 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003654 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003655
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003656 default:
3657 goto invalid;
3658
3659 }
3660
3661 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003662 if (nameserver) {
3663 nameserver->counters.update++;
3664 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003665 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003666 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003667 else
3668 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003669 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3670
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003671 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003672 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003673 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003674
3675 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003676 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003677 nameserver->counters.invalid++;
3678 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003679 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003680 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003681 return 0;
3682}
3683
3684/*
3685 * Server Name Resolution error management callback
3686 * returns:
3687 * 0 on error
3688 * 1 when no error or safe ignore
3689 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003690int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003691{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003692 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003693
Christopher Faulet67957bd2017-09-27 11:00:59 +02003694 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003695 if (!s)
3696 return 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003697 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003698 return 1;
3699}
3700
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003701/*
3702 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003703 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003704 * It returns a pointer to the first server found or NULL if <ip> is not yet
3705 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003706 */
3707struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3708{
3709 struct server *tmpsrv;
3710 struct proxy *be;
3711
3712 if (!srv)
3713 return NULL;
3714
3715 be = srv->proxy;
3716 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3717 /* We want to compare the IP in the record with the IP of the servers in the
3718 * same backend, only if:
3719 * * DNS resolution is enabled on the server
3720 * * the hostname used for the resolution by our server is the same than the
3721 * one used for the server found in the backend
3722 * * the server found in the backend is not our current server
3723 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003724 if ((tmpsrv->hostname_dn == NULL) ||
3725 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3726 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003727 (srv->puid == tmpsrv->puid))
3728 continue;
3729
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003730 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003731 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003732 continue;
3733
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003734 /* At this point, we have 2 different servers using the same DNS hostname
3735 * for their respective resolution.
3736 */
3737 if (*ip_family == tmpsrv->addr.ss_family &&
3738 ((tmpsrv->addr.ss_family == AF_INET &&
3739 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3740 (tmpsrv->addr.ss_family == AF_INET6 &&
3741 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3742 return tmpsrv;
3743 }
3744 }
3745
3746 return NULL;
3747}
3748
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003749/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3750 * resolver. This is suited for initial address configuration. Returns 0 on
3751 * success otherwise a non-zero error code. In case of error, *err_code, if
3752 * not NULL, is filled up.
3753 */
3754int srv_set_addr_via_libc(struct server *srv, int *err_code)
3755{
3756 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003757 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003758 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003759 return 1;
3760 }
3761 return 0;
3762}
3763
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003764/* Set the server's FDQN (->hostname) from <hostname>.
3765 * Returns -1 if failed, 0 if not.
3766 */
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003767int srv_set_fqdn(struct server *srv, const char *hostname, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003768{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003769 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003770 char *hostname_dn;
3771 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003772
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003773 if (!dns_locked)
3774 SPIN_LOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003775 /* run time DNS resolution was not active for this server
3776 * and we can't enable it at run time for now.
3777 */
3778 if (!srv->dns_requester)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003779 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003780
3781 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003782 hostname_len = strlen(hostname);
3783 hostname_dn = trash.str;
3784 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3785 hostname_dn, trash.size);
3786 if (hostname_dn_len == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003787 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003788
Christopher Faulet67957bd2017-09-27 11:00:59 +02003789 resolution = srv->dns_requester->resolution;
3790 if (resolution &&
3791 resolution->hostname_dn &&
3792 !strcmp(resolution->hostname_dn, hostname_dn))
Christopher Fauletb2812a62017-10-04 16:17:58 +02003793 goto end;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003794
Christopher Faulet67957bd2017-09-27 11:00:59 +02003795 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003796
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003797 free(srv->hostname);
3798 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003799 srv->hostname = strdup(hostname);
3800 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003801 srv->hostname_dn_len = hostname_dn_len;
3802 if (!srv->hostname || !srv->hostname_dn)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003803 goto err;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003804
Christopher Faulet67957bd2017-09-27 11:00:59 +02003805 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1)
Christopher Fauletb2812a62017-10-04 16:17:58 +02003806 goto err;
3807
3808 end:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003809 if (!dns_locked)
3810 SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003811 return 0;
Christopher Fauletb2812a62017-10-04 16:17:58 +02003812
3813 err:
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003814 if (!dns_locked)
3815 SPIN_UNLOCK(DNS_LOCK, &srv->resolvers->lock);
Christopher Fauletb2812a62017-10-04 16:17:58 +02003816 return -1;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003817}
3818
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003819/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3820 * from the state file. This is suited for initial address configuration.
3821 * Returns 0 on success otherwise a non-zero error code. In case of error,
3822 * *err_code, if not NULL, is filled up.
3823 */
3824static int srv_apply_lastaddr(struct server *srv, int *err_code)
3825{
3826 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3827 if (err_code)
3828 *err_code |= ERR_WARN;
3829 return 1;
3830 }
3831 return 0;
3832}
3833
Willy Tarreau25e51522016-11-04 15:10:17 +01003834/* returns 0 if no error, otherwise a combination of ERR_* flags */
3835static int srv_iterate_initaddr(struct server *srv)
3836{
3837 int return_code = 0;
3838 int err_code;
3839 unsigned int methods;
3840
3841 methods = srv->init_addr_methods;
3842 if (!methods) { // default to "last,libc"
3843 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3844 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3845 }
3846
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003847 /* "-dr" : always append "none" so that server addresses resolution
3848 * failures are silently ignored, this is convenient to validate some
3849 * configs out of their environment.
3850 */
3851 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3852 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3853
Willy Tarreau25e51522016-11-04 15:10:17 +01003854 while (methods) {
3855 err_code = 0;
3856 switch (srv_get_next_initaddr(&methods)) {
3857 case SRV_IADDR_LAST:
3858 if (!srv->lastaddr)
3859 continue;
3860 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003861 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003862 return_code |= err_code;
3863 break;
3864
3865 case SRV_IADDR_LIBC:
3866 if (!srv->hostname)
3867 continue;
3868 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003869 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003870 return_code |= err_code;
3871 break;
3872
Willy Tarreau37ebe122016-11-04 15:17:58 +01003873 case SRV_IADDR_NONE:
3874 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003875 if (return_code) {
3876 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3877 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3878 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003879 return return_code;
3880
Willy Tarreau4310d362016-11-02 15:05:56 +01003881 case SRV_IADDR_IP:
3882 ipcpy(&srv->init_addr, &srv->addr);
3883 if (return_code) {
3884 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3885 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3886 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003887 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003888
Willy Tarreau25e51522016-11-04 15:10:17 +01003889 default: /* unhandled method */
3890 break;
3891 }
3892 }
3893
3894 if (!return_code) {
3895 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3896 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3897 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003898 else {
3899 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3900 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3901 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003902
3903 return_code |= ERR_ALERT | ERR_FATAL;
3904 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003905out:
3906 srv_set_dyncookie(srv);
3907 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003908}
3909
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003910/*
3911 * This function parses all backends and all servers within each backend
3912 * and performs servers' addr resolution based on information provided by:
3913 * - configuration file
3914 * - server-state file (states provided by an 'old' haproxy process)
3915 *
3916 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3917 */
3918int srv_init_addr(void)
3919{
3920 struct proxy *curproxy;
3921 int return_code = 0;
3922
3923 curproxy = proxy;
3924 while (curproxy) {
3925 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003926
3927 /* servers are in backend only */
3928 if (!(curproxy->cap & PR_CAP_BE))
3929 goto srv_init_addr_next;
3930
Willy Tarreau25e51522016-11-04 15:10:17 +01003931 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003932 if (srv->hostname)
3933 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003934
3935 srv_init_addr_next:
3936 curproxy = curproxy->next;
3937 }
3938
3939 return return_code;
3940}
3941
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003942const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater, int dns_locked)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003943{
3944
3945 struct chunk *msg;
3946
3947 msg = get_trash_chunk();
3948 chunk_reset(msg);
3949
Olivier Houchard8da5f982017-08-04 18:35:36 +02003950 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003951 chunk_appendf(msg, "no need to change the FDQN");
3952 goto out;
3953 }
3954
3955 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3956 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3957 goto out;
3958 }
3959
3960 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3961 server->proxy->id, server->id, server->hostname, fqdn);
3962
Olivier Houchardd16bfe62017-10-31 15:21:19 +01003963 if (srv_set_fqdn(server, fqdn, dns_locked) < 0) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003964 chunk_reset(msg);
3965 chunk_appendf(msg, "could not update %s/%s FQDN",
3966 server->proxy->id, server->id);
3967 goto out;
3968 }
3969
3970 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003971 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003972
3973 out:
3974 if (updater)
3975 chunk_appendf(msg, " by '%s'", updater);
3976 chunk_appendf(msg, "\n");
3977
3978 return msg->str;
3979}
3980
3981
Willy Tarreau21b069d2016-11-23 17:15:08 +01003982/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3983 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003984 * 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 +01003985 * used for CLI commands requiring a server name.
3986 * Important: the <arg> is modified to remove the '/'.
3987 */
3988struct server *cli_find_server(struct appctx *appctx, char *arg)
3989{
3990 struct proxy *px;
3991 struct server *sv;
3992 char *line;
3993
3994 /* split "backend/server" and make <line> point to server */
3995 for (line = arg; *line; line++)
3996 if (*line == '/') {
3997 *line++ = '\0';
3998 break;
3999 }
4000
4001 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004002 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004003 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004004 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004005 return NULL;
4006 }
4007
4008 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004009 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004010 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004011 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004012 return NULL;
4013 }
4014
4015 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004016 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004017 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004018 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004019 return NULL;
4020 }
4021
4022 return sv;
4023}
4024
William Lallemand222baf22016-11-19 02:00:33 +01004025
4026static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4027{
4028 struct server *sv;
4029 const char *warning;
4030
4031 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4032 return 1;
4033
4034 sv = cli_find_server(appctx, args[2]);
4035 if (!sv)
4036 return 1;
4037
Emeric Brun9f0b4582017-10-23 14:39:51 +02004038 SPIN_LOCK(SERVER_LOCK, &sv->lock);
4039
William Lallemand222baf22016-11-19 02:00:33 +01004040 if (strcmp(args[3], "weight") == 0) {
4041 warning = server_parse_weight_change_request(sv, args[4]);
4042 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004043 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004044 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004045 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004046 }
4047 }
4048 else if (strcmp(args[3], "state") == 0) {
4049 if (strcmp(args[4], "ready") == 0)
4050 srv_adm_set_ready(sv);
4051 else if (strcmp(args[4], "drain") == 0)
4052 srv_adm_set_drain(sv);
4053 else if (strcmp(args[4], "maint") == 0)
4054 srv_adm_set_maint(sv);
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> state' expects 'ready', 'drain' and 'maint'.\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], "health") == 0) {
4062 if (sv->track) {
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 = "cannot change health on a tracking 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->check.health = sv->check.rise + sv->check.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], "stopping") == 0) {
4072 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004073 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004074 }
4075 else if (strcmp(args[4], "down") == 0) {
4076 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004077 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004078 }
4079 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004080 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004081 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004082 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004083 }
4084 }
4085 else if (strcmp(args[3], "agent") == 0) {
4086 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004087 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004088 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004089 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004090 }
4091 else if (strcmp(args[4], "up") == 0) {
4092 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004093 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004094 }
4095 else if (strcmp(args[4], "down") == 0) {
4096 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004097 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004098 }
4099 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004100 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004101 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004102 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004103 }
4104 }
Misiek2da082d2017-01-09 09:40:42 +01004105 else if (strcmp(args[3], "agent-addr") == 0) {
4106 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004107 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004108 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4109 appctx->st0 = CLI_ST_PRINT;
4110 } else {
4111 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004112 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004113 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4114 appctx->st0 = CLI_ST_PRINT;
4115 }
4116 }
4117 }
4118 else if (strcmp(args[3], "agent-send") == 0) {
4119 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004120 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004121 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4122 appctx->st0 = CLI_ST_PRINT;
4123 } else {
4124 char *nss = strdup(args[4]);
4125 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004126 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004127 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4128 appctx->st0 = CLI_ST_PRINT;
4129 } else {
4130 free(sv->agent.send_string);
4131 sv->agent.send_string = nss;
4132 sv->agent.send_string_len = strlen(args[4]);
4133 }
4134 }
4135 }
William Lallemand222baf22016-11-19 02:00:33 +01004136 else if (strcmp(args[3], "check-port") == 0) {
4137 int i = 0;
4138 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004139 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004140 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004141 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004142 }
4143 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004144 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004145 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004146 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004147 }
4148 /* prevent the update of port to 0 if MAPPORTS are in use */
4149 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004150 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004151 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004152 appctx->st0 = CLI_ST_PRINT;
Emeric Brun9f0b4582017-10-23 14:39:51 +02004153 SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004154 return 1;
4155 }
4156 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004157 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004158 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004159 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004160 }
4161 else if (strcmp(args[3], "addr") == 0) {
4162 char *addr = NULL;
4163 char *port = NULL;
4164 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004165 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004166 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004167 appctx->st0 = CLI_ST_PRINT;
Emeric Brun9f0b4582017-10-23 14:39:51 +02004168 SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004169 return 1;
4170 }
4171 else {
4172 addr = args[4];
4173 }
4174 if (strcmp(args[5], "port") == 0) {
4175 port = args[6];
4176 }
4177 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4178 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004179 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004180 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004181 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004182 }
4183 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4184 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004185 else if (strcmp(args[3], "fqdn") == 0) {
4186 if (!*args[4]) {
4187 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4188 appctx->st0 = CLI_ST_PRINT;
4189 return 1;
4190 }
Olivier Houchardd16bfe62017-10-31 15:21:19 +01004191 warning = update_server_fqdn(sv, args[4], "stats socket command", 0);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004192 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004193 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004194 appctx->ctx.cli.msg = warning;
4195 appctx->st0 = CLI_ST_PRINT;
4196 }
4197 }
William Lallemand222baf22016-11-19 02:00:33 +01004198 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004199 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004200 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 +01004201 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004202 }
Emeric Brun9f0b4582017-10-23 14:39:51 +02004203 SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
William Lallemand222baf22016-11-19 02:00:33 +01004204 return 1;
4205}
4206
William Lallemand6b160942016-11-22 12:34:35 +01004207static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4208{
4209 struct stream_interface *si = appctx->owner;
4210 struct proxy *px;
4211 struct server *sv;
4212 char *line;
4213
4214
4215 /* split "backend/server" and make <line> point to server */
4216 for (line = args[2]; *line; line++)
4217 if (*line == '/') {
4218 *line++ = '\0';
4219 break;
4220 }
4221
4222 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004223 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004224 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004225 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004226 return 1;
4227 }
4228
4229 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004230 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004231 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004232 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004233 return 1;
4234 }
4235
4236 /* return server's effective weight at the moment */
4237 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004238 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004239 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004240 return 0;
4241 }
William Lallemand6b160942016-11-22 12:34:35 +01004242 return 1;
4243}
4244
4245static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4246{
4247 struct server *sv;
4248 const char *warning;
4249
4250 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4251 return 1;
4252
4253 sv = cli_find_server(appctx, args[2]);
4254 if (!sv)
4255 return 1;
4256
4257 warning = server_parse_weight_change_request(sv, args[3]);
4258 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004259 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004260 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004261 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004262 }
4263 return 1;
4264}
4265
Willy Tarreaub8026272016-11-23 11:26:56 +01004266/* parse a "set maxconn server" command. It always returns 1. */
4267static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4268{
4269 struct server *sv;
4270 const char *warning;
4271
4272 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4273 return 1;
4274
4275 sv = cli_find_server(appctx, args[3]);
4276 if (!sv)
4277 return 1;
4278
4279 warning = server_parse_maxconn_change_request(sv, args[4]);
4280 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004281 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004282 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004283 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004284 }
4285 return 1;
4286}
William Lallemand6b160942016-11-22 12:34:35 +01004287
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004288/* parse a "disable agent" command. It always returns 1. */
4289static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4290{
4291 struct server *sv;
4292
4293 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4294 return 1;
4295
4296 sv = cli_find_server(appctx, args[2]);
4297 if (!sv)
4298 return 1;
4299
4300 sv->agent.state &= ~CHK_ST_ENABLED;
4301 return 1;
4302}
4303
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004304/* parse a "disable health" command. It always returns 1. */
4305static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4306{
4307 struct server *sv;
4308
4309 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4310 return 1;
4311
4312 sv = cli_find_server(appctx, args[2]);
4313 if (!sv)
4314 return 1;
4315
4316 sv->check.state &= ~CHK_ST_ENABLED;
4317 return 1;
4318}
4319
Willy Tarreauffb4d582016-11-24 12:47:00 +01004320/* parse a "disable server" command. It always returns 1. */
4321static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4322{
4323 struct server *sv;
4324
4325 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4326 return 1;
4327
4328 sv = cli_find_server(appctx, args[2]);
4329 if (!sv)
4330 return 1;
4331
4332 srv_adm_set_maint(sv);
4333 return 1;
4334}
4335
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004336/* parse a "enable agent" command. It always returns 1. */
4337static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4338{
4339 struct server *sv;
4340
4341 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4342 return 1;
4343
4344 sv = cli_find_server(appctx, args[2]);
4345 if (!sv)
4346 return 1;
4347
4348 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004349 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004350 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004351 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004352 return 1;
4353 }
4354
4355 sv->agent.state |= CHK_ST_ENABLED;
4356 return 1;
4357}
4358
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004359/* parse a "enable health" command. It always returns 1. */
4360static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4361{
4362 struct server *sv;
4363
4364 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4365 return 1;
4366
4367 sv = cli_find_server(appctx, args[2]);
4368 if (!sv)
4369 return 1;
4370
4371 sv->check.state |= CHK_ST_ENABLED;
4372 return 1;
4373}
4374
Willy Tarreauffb4d582016-11-24 12:47:00 +01004375/* parse a "enable server" command. It always returns 1. */
4376static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4377{
4378 struct server *sv;
4379
4380 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4381 return 1;
4382
4383 sv = cli_find_server(appctx, args[2]);
4384 if (!sv)
4385 return 1;
4386
4387 srv_adm_set_ready(sv);
4388 return 1;
4389}
4390
William Lallemand222baf22016-11-19 02:00:33 +01004391/* register cli keywords */
4392static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004393 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004394 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004395 { { "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 +01004396 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004397 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004398 { { "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 +01004399 { { "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 +01004400 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004401 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4402 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4403
William Lallemand222baf22016-11-19 02:00:33 +01004404 {{},}
4405}};
4406
4407__attribute__((constructor))
4408static void __server_init(void)
4409{
Christopher Faulet5d42e092017-10-16 12:00:40 +02004410 SPIN_INIT(&updated_servers_lock);
William Lallemand222baf22016-11-19 02:00:33 +01004411 cli_register_kw(&cli_kws);
4412}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004413
Emeric Brun64cc49c2017-10-03 14:46:45 +02004414/*
4415 * This function applies server's status changes, it is
4416 * is designed to be called asynchronously.
4417 *
4418 */
4419void srv_update_status(struct server *s)
4420{
4421 struct check *check = &s->check;
4422 int xferred;
4423 struct proxy *px = s->proxy;
4424 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4425 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4426 int log_level;
4427 struct chunk *tmptrash = NULL;
4428
4429
4430 /* If currently main is not set we try to apply pending state changes */
4431 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4432 int next_admin;
4433
4434 /* Backup next admin */
4435 next_admin = s->next_admin;
4436
4437 /* restore current admin state */
4438 s->next_admin = s->cur_admin;
4439
4440 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4441 s->last_change = now.tv_sec;
4442 if (s->proxy->lbprm.set_server_status_down)
4443 s->proxy->lbprm.set_server_status_down(s);
4444
4445 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4446 srv_shutdown_streams(s, SF_ERR_DOWN);
4447
4448 /* we might have streams queued on this server and waiting for
4449 * a connection. Those which are redispatchable will be queued
4450 * to another server or to the proxy itself.
4451 */
4452 xferred = pendconn_redistribute(s);
4453
4454 tmptrash = alloc_trash_chunk();
4455 if (tmptrash) {
4456 chunk_printf(tmptrash,
4457 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4458 s->proxy->id, s->id);
4459
Emeric Brun5a133512017-10-19 14:42:30 +02004460 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004461 Warning("%s.\n", tmptrash->str);
4462
4463 /* we don't send an alert if the server was previously paused */
4464 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4465 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4466 send_email_alert(s, log_level, "%s", tmptrash->str);
4467 free_trash_chunk(tmptrash);
4468 tmptrash = NULL;
4469 }
4470 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4471 set_backend_down(s->proxy);
4472
4473 s->counters.down_trans++;
4474 }
4475 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4476 s->last_change = now.tv_sec;
4477 if (s->proxy->lbprm.set_server_status_down)
4478 s->proxy->lbprm.set_server_status_down(s);
4479
4480 /* we might have streams queued on this server and waiting for
4481 * a connection. Those which are redispatchable will be queued
4482 * to another server or to the proxy itself.
4483 */
4484 xferred = pendconn_redistribute(s);
4485
4486 tmptrash = alloc_trash_chunk();
4487 if (tmptrash) {
4488 chunk_printf(tmptrash,
4489 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4490 s->proxy->id, s->id);
4491
Emeric Brun5a133512017-10-19 14:42:30 +02004492 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004493
4494 Warning("%s.\n", tmptrash->str);
4495 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4496 free_trash_chunk(tmptrash);
4497 tmptrash = NULL;
4498 }
4499
4500 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4501 set_backend_down(s->proxy);
4502 }
4503 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4504 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4505 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4506 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4507 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4508 s->proxy->last_change = now.tv_sec;
4509 }
4510
4511 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4512 s->down_time += now.tv_sec - s->last_change;
4513
4514 s->last_change = now.tv_sec;
4515 if (s->next_state == SRV_ST_STARTING)
4516 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4517
4518 server_recalc_eweight(s);
4519 /* now propagate the status change to any LB algorithms */
4520 if (px->lbprm.update_server_eweight)
4521 px->lbprm.update_server_eweight(s);
4522 else if (srv_willbe_usable(s)) {
4523 if (px->lbprm.set_server_status_up)
4524 px->lbprm.set_server_status_up(s);
4525 }
4526 else {
4527 if (px->lbprm.set_server_status_down)
4528 px->lbprm.set_server_status_down(s);
4529 }
4530
4531 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4532 * and it's not a backup server and its effective weight is > 0,
4533 * then it can accept new connections, so we shut down all streams
4534 * on all backup servers.
4535 */
4536 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4537 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4538 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4539
4540 /* check if we can handle some connections queued at the proxy. We
4541 * will take as many as we can handle.
4542 */
4543 xferred = pendconn_grab_from_px(s);
4544
4545 tmptrash = alloc_trash_chunk();
4546 if (tmptrash) {
4547 chunk_printf(tmptrash,
4548 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4549 s->proxy->id, s->id);
4550
Emeric Brun5a133512017-10-19 14:42:30 +02004551 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004552 Warning("%s.\n", tmptrash->str);
4553 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4554 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4555 free_trash_chunk(tmptrash);
4556 tmptrash = NULL;
4557 }
4558
4559 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4560 set_backend_down(s->proxy);
4561 }
4562 else if (s->cur_eweight != s->next_eweight) {
4563 /* now propagate the status change to any LB algorithms */
4564 if (px->lbprm.update_server_eweight)
4565 px->lbprm.update_server_eweight(s);
4566 else if (srv_willbe_usable(s)) {
4567 if (px->lbprm.set_server_status_up)
4568 px->lbprm.set_server_status_up(s);
4569 }
4570 else {
4571 if (px->lbprm.set_server_status_down)
4572 px->lbprm.set_server_status_down(s);
4573 }
4574
4575 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4576 set_backend_down(s->proxy);
4577 }
4578
4579 s->next_admin = next_admin;
4580 }
4581
Emeric Brun5a133512017-10-19 14:42:30 +02004582 /* reset operational state change */
4583 *s->op_st_chg.reason = 0;
4584 s->op_st_chg.status = s->op_st_chg.code = -1;
4585 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004586
4587 /* Now we try to apply pending admin changes */
4588
4589 /* Maintenance must also disable health checks */
4590 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4591 if (s->check.state & CHK_ST_ENABLED) {
4592 s->check.state |= CHK_ST_PAUSED;
4593 check->health = 0;
4594 }
4595
4596 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004597 tmptrash = alloc_trash_chunk();
4598 if (tmptrash) {
4599 chunk_printf(tmptrash,
4600 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4601 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4602 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004603
Olivier Houchard796a2b32017-10-24 17:42:47 +02004604 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004605
Olivier Houchard796a2b32017-10-24 17:42:47 +02004606 if (!(global.mode & MODE_STARTING)) {
4607 Warning("%s.\n", tmptrash->str);
4608 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4609 }
4610 free_trash_chunk(tmptrash);
4611 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004612 }
4613 }
4614 else { /* server was still running */
4615 check->health = 0; /* failure */
4616 s->last_change = now.tv_sec;
4617 if (s->proxy->lbprm.set_server_status_down)
4618 s->proxy->lbprm.set_server_status_down(s);
4619
4620 s->next_state = SRV_ST_STOPPED;
4621 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4622 srv_shutdown_streams(s, SF_ERR_DOWN);
4623
4624 /* we might have streams queued on this server and waiting for
4625 * a connection. Those which are redispatchable will be queued
4626 * to another server or to the proxy itself.
4627 */
4628 xferred = pendconn_redistribute(s);
4629
4630 tmptrash = alloc_trash_chunk();
4631 if (tmptrash) {
4632 chunk_printf(tmptrash,
4633 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4634 s->flags & SRV_F_BACKUP ? "Backup " : "",
4635 s->proxy->id, s->id,
4636 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4637
4638 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4639
4640 if (!(global.mode & MODE_STARTING)) {
4641 Warning("%s.\n", tmptrash->str);
4642 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4643 }
4644 free_trash_chunk(tmptrash);
4645 tmptrash = NULL;
4646 }
4647 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4648 set_backend_down(s->proxy);
4649
4650 s->counters.down_trans++;
4651 }
4652 }
4653 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4654 /* OK here we're leaving maintenance, we have many things to check,
4655 * because the server might possibly be coming back up depending on
4656 * its state. In practice, leaving maintenance means that we should
4657 * immediately turn to UP (more or less the slowstart) under the
4658 * following conditions :
4659 * - server is neither checked nor tracked
4660 * - server tracks another server which is not checked
4661 * - server tracks another server which is already up
4662 * Which sums up as something simpler :
4663 * "either the tracking server is up or the server's checks are disabled
4664 * or up". Otherwise we only re-enable health checks. There's a special
4665 * case associated to the stopping state which can be inherited. Note
4666 * that the server might still be in drain mode, which is naturally dealt
4667 * with by the lower level functions.
4668 */
4669
4670 if (s->check.state & CHK_ST_ENABLED) {
4671 s->check.state &= ~CHK_ST_PAUSED;
4672 check->health = check->rise; /* start OK but check immediately */
4673 }
4674
4675 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4676 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4677 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4678 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4679 s->next_state = SRV_ST_STOPPING;
4680 }
4681 else {
4682 s->next_state = SRV_ST_STARTING;
4683 if (s->slowstart > 0)
4684 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4685 else
4686 s->next_state = SRV_ST_RUNNING;
4687 }
4688
4689 }
4690
4691 tmptrash = alloc_trash_chunk();
4692 if (tmptrash) {
4693 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4694 chunk_printf(tmptrash,
4695 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4696 s->flags & SRV_F_BACKUP ? "Backup " : "",
4697 s->proxy->id, s->id,
4698 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4699 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4700 }
4701 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4702 chunk_printf(tmptrash,
4703 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4704 s->flags & SRV_F_BACKUP ? "Backup " : "",
4705 s->proxy->id, s->id, s->hostname,
4706 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4707 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4708 }
4709 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4710 chunk_printf(tmptrash,
4711 "%sServer %s/%s is %s/%s (leaving maintenance)",
4712 s->flags & SRV_F_BACKUP ? "Backup " : "",
4713 s->proxy->id, s->id,
4714 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4715 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4716 }
4717 Warning("%s.\n", tmptrash->str);
4718 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4719 free_trash_chunk(tmptrash);
4720 tmptrash = NULL;
4721 }
4722
4723 server_recalc_eweight(s);
4724 /* now propagate the status change to any LB algorithms */
4725 if (px->lbprm.update_server_eweight)
4726 px->lbprm.update_server_eweight(s);
4727 else if (srv_willbe_usable(s)) {
4728 if (px->lbprm.set_server_status_up)
4729 px->lbprm.set_server_status_up(s);
4730 }
4731 else {
4732 if (px->lbprm.set_server_status_down)
4733 px->lbprm.set_server_status_down(s);
4734 }
4735
4736 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4737 set_backend_down(s->proxy);
4738
4739 }
4740 else if (s->next_admin & SRV_ADMF_MAINT) {
4741 /* remaining in maintenance mode, let's inform precisely about the
4742 * situation.
4743 */
4744 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4745 tmptrash = alloc_trash_chunk();
4746 if (tmptrash) {
4747 chunk_printf(tmptrash,
4748 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4749 s->flags & SRV_F_BACKUP ? "Backup " : "",
4750 s->proxy->id, s->id);
4751
4752 if (s->track) /* normally it's mandatory here */
4753 chunk_appendf(tmptrash, " via %s/%s",
4754 s->track->proxy->id, s->track->id);
4755 Warning("%s.\n", tmptrash->str);
4756 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4757 free_trash_chunk(tmptrash);
4758 tmptrash = NULL;
4759 }
4760 }
4761 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4762 tmptrash = alloc_trash_chunk();
4763 if (tmptrash) {
4764 chunk_printf(tmptrash,
4765 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4766 s->flags & SRV_F_BACKUP ? "Backup " : "",
4767 s->proxy->id, s->id, s->hostname);
4768
4769 if (s->track) /* normally it's mandatory here */
4770 chunk_appendf(tmptrash, " via %s/%s",
4771 s->track->proxy->id, s->track->id);
4772 Warning("%s.\n", tmptrash->str);
4773 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4774 free_trash_chunk(tmptrash);
4775 tmptrash = NULL;
4776 }
4777 }
4778 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4779 tmptrash = alloc_trash_chunk();
4780 if (tmptrash) {
4781 chunk_printf(tmptrash,
4782 "%sServer %s/%s remains in forced maintenance",
4783 s->flags & SRV_F_BACKUP ? "Backup " : "",
4784 s->proxy->id, s->id);
4785 Warning("%s.\n", tmptrash->str);
4786 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4787 free_trash_chunk(tmptrash);
4788 tmptrash = NULL;
4789 }
4790 }
4791 /* don't report anything when leaving drain mode and remaining in maintenance */
4792
4793 s->cur_admin = s->next_admin;
4794 }
4795
4796 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4797 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4798 /* drain state is applied only if not yet in maint */
4799
4800 s->last_change = now.tv_sec;
4801 if (px->lbprm.set_server_status_down)
4802 px->lbprm.set_server_status_down(s);
4803
4804 /* we might have streams queued on this server and waiting for
4805 * a connection. Those which are redispatchable will be queued
4806 * to another server or to the proxy itself.
4807 */
4808 xferred = pendconn_redistribute(s);
4809
4810 tmptrash = alloc_trash_chunk();
4811 if (tmptrash) {
4812 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4813 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4814 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4815
4816 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4817
4818 if (!(global.mode & MODE_STARTING)) {
4819 Warning("%s.\n", tmptrash->str);
4820 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4821 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4822 }
4823 free_trash_chunk(tmptrash);
4824 tmptrash = NULL;
4825 }
4826
4827 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4828 set_backend_down(s->proxy);
4829 }
4830 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4831 /* OK completely leaving drain mode */
4832 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4833 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4834 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4835 s->proxy->last_change = now.tv_sec;
4836 }
4837
4838 if (s->last_change < now.tv_sec) // ignore negative times
4839 s->down_time += now.tv_sec - s->last_change;
4840 s->last_change = now.tv_sec;
4841 server_recalc_eweight(s);
4842
4843 tmptrash = alloc_trash_chunk();
4844 if (tmptrash) {
4845 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4846 chunk_printf(tmptrash,
4847 "%sServer %s/%s is %s (leaving forced drain)",
4848 s->flags & SRV_F_BACKUP ? "Backup " : "",
4849 s->proxy->id, s->id,
4850 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4851 }
4852 else {
4853 chunk_printf(tmptrash,
4854 "%sServer %s/%s is %s (leaving drain)",
4855 s->flags & SRV_F_BACKUP ? "Backup " : "",
4856 s->proxy->id, s->id,
4857 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4858 if (s->track) /* normally it's mandatory here */
4859 chunk_appendf(tmptrash, " via %s/%s",
4860 s->track->proxy->id, s->track->id);
4861 }
4862
4863 Warning("%s.\n", tmptrash->str);
4864 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4865 free_trash_chunk(tmptrash);
4866 tmptrash = NULL;
4867 }
4868
4869 /* now propagate the status change to any LB algorithms */
4870 if (px->lbprm.update_server_eweight)
4871 px->lbprm.update_server_eweight(s);
4872 else if (srv_willbe_usable(s)) {
4873 if (px->lbprm.set_server_status_up)
4874 px->lbprm.set_server_status_up(s);
4875 }
4876 else {
4877 if (px->lbprm.set_server_status_down)
4878 px->lbprm.set_server_status_down(s);
4879 }
4880 }
4881 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4882 /* remaining in drain mode after removing one of its flags */
4883
4884 tmptrash = alloc_trash_chunk();
4885 if (tmptrash) {
4886 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4887 chunk_printf(tmptrash,
4888 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4889 s->flags & SRV_F_BACKUP ? "Backup " : "",
4890 s->proxy->id, s->id);
4891
4892 if (s->track) /* normally it's mandatory here */
4893 chunk_appendf(tmptrash, " via %s/%s",
4894 s->track->proxy->id, s->track->id);
4895 }
4896 else {
4897 chunk_printf(tmptrash,
4898 "%sServer %s/%s remains in forced drain mode",
4899 s->flags & SRV_F_BACKUP ? "Backup " : "",
4900 s->proxy->id, s->id);
4901 }
4902 Warning("%s.\n", tmptrash->str);
4903 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4904 free_trash_chunk(tmptrash);
4905 tmptrash = NULL;
4906 }
4907
4908 /* commit new admin status */
4909
4910 s->cur_admin = s->next_admin;
4911 }
4912 }
4913
4914 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004915 *s->adm_st_chg_cause = 0;
4916}
4917/*
4918 * This function loops on servers registered for asynchronous
4919 * status changes
Christopher Faulet5d42e092017-10-16 12:00:40 +02004920 *
4921 * NOTE: No needs to lock <updated_servers> list because it is called inside the
4922 * sync point.
Emeric Brun64cc49c2017-10-03 14:46:45 +02004923 */
4924void servers_update_status(void) {
4925 struct server *s, *stmp;
4926
4927 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4928 srv_update_status(s);
4929 LIST_DEL(&s->update_status);
4930 LIST_INIT(&s->update_status);
4931 }
4932}
4933
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004934/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004935 * Local variables:
4936 * c-indent-level: 8
4937 * c-basic-offset: 8
4938 * End:
4939 */