blob: b6986a9805abb922275ffd38979fdd1dea47f0ac [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
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +020049static void srv_update_state(struct server *srv, int version, char **params);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +010050static int srv_apply_lastaddr(struct server *srv, int *err_code);
Frédéric Lécailleb418c122017-04-26 11:24:02 +020051static int srv_set_fqdn(struct server *srv, const char *fqdn);
Willy Tarreaubaaee002006-06-26 02:48:02 +020052
Willy Tarreau21faa912012-10-10 08:27:36 +020053/* List head of all known server keywords */
54static struct srv_kw_list srv_keywords = {
55 .list = LIST_HEAD_INIT(srv_keywords.list)
56};
Krzysztof Oledzki85130942007-10-22 16:21:10 +020057
Simon Hormana3608442013-11-01 16:46:15 +090058int srv_downtime(const struct server *s)
Willy Tarreau21faa912012-10-10 08:27:36 +020059{
Emeric Brun52a91d32017-08-31 14:41:55 +020060 if ((s->cur_state != SRV_ST_STOPPED) && s->last_change < now.tv_sec) // ignore negative time
Krzysztof Oledzki85130942007-10-22 16:21:10 +020061 return s->down_time;
62
63 return now.tv_sec - s->last_change + s->down_time;
64}
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -050066int srv_lastsession(const struct server *s)
67{
68 if (s->counters.last_sess)
69 return now.tv_sec - s->counters.last_sess;
70
71 return -1;
72}
73
Simon Horman4a741432013-02-23 15:35:38 +090074int srv_getinter(const struct check *check)
Willy Tarreau21faa912012-10-10 08:27:36 +020075{
Simon Horman4a741432013-02-23 15:35:38 +090076 const struct server *s = check->server;
77
Willy Tarreauff5ae352013-12-11 20:36:34 +010078 if ((check->state & CHK_ST_CONFIGURED) && (check->health == check->rise + check->fall - 1))
Simon Horman4a741432013-02-23 15:35:38 +090079 return check->inter;
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010080
Emeric Brun52a91d32017-08-31 14:41:55 +020081 if ((s->next_state == SRV_ST_STOPPED) && check->health == 0)
Simon Horman4a741432013-02-23 15:35:38 +090082 return (check->downinter)?(check->downinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010083
Simon Horman4a741432013-02-23 15:35:38 +090084 return (check->fastinter)?(check->fastinter):(check->inter);
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +010085}
86
Olivier Houchard4e694042017-03-14 20:01:29 +010087void srv_set_dyncookie(struct server *s)
88{
89 struct proxy *p = s->proxy;
90 struct server *tmpserv;
91 char *tmpbuf;
92 unsigned long long hash_value;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +010093 size_t key_len;
Olivier Houchard4e694042017-03-14 20:01:29 +010094 size_t buffer_len;
95 int addr_len;
96 int port;
97
98 if ((s->flags & SRV_F_COOKIESET) ||
99 !(s->proxy->ck_opts & PR_CK_DYNAMIC) ||
100 s->proxy->dyncookie_key == NULL)
101 return;
Olivier Houchard2cb49eb2017-03-15 15:11:06 +0100102 key_len = strlen(p->dyncookie_key);
Olivier Houchard4e694042017-03-14 20:01:29 +0100103
104 if (s->addr.ss_family != AF_INET &&
105 s->addr.ss_family != AF_INET6)
106 return;
107 /*
108 * Buffer to calculate the cookie value.
109 * The buffer contains the secret key + the server IP address
110 * + the TCP port.
111 */
112 addr_len = (s->addr.ss_family == AF_INET) ? 4 : 16;
113 /*
114 * The TCP port should use only 2 bytes, but is stored in
115 * an unsigned int in struct server, so let's use 4, to be
116 * on the safe side.
117 */
118 buffer_len = key_len + addr_len + 4;
119 tmpbuf = trash.str;
120 memcpy(tmpbuf, p->dyncookie_key, key_len);
121 memcpy(&(tmpbuf[key_len]),
122 s->addr.ss_family == AF_INET ?
123 (void *)&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr :
124 (void *)&(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr),
125 addr_len);
126 /*
127 * Make sure it's the same across all the load balancers,
128 * no matter their endianness.
129 */
130 port = htonl(s->svc_port);
131 memcpy(&tmpbuf[key_len + addr_len], &port, 4);
132 hash_value = XXH64(tmpbuf, buffer_len, 0);
133 memprintf(&s->cookie, "%016llx", hash_value);
134 if (!s->cookie)
135 return;
136 s->cklen = 16;
137 /*
138 * Check that we did not get a hash collision.
139 * Unlikely, but it can happen.
140 */
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200141 for (tmpserv = p->srv; tmpserv != NULL;
142 tmpserv = tmpserv->next) {
143 if (tmpserv == s)
144 continue;
145 if (tmpserv->cookie &&
146 strcmp(tmpserv->cookie, s->cookie) == 0) {
147 Warning("We generated two equal cookies for two different servers.\n"
148 "Please change the secret key for '%s'.\n",
149 s->proxy->id);
Olivier Houchard4e694042017-03-14 20:01:29 +0100150 }
Olivier Houchardb4a2d5e2017-04-04 22:10:36 +0200151 }
Olivier Houchard4e694042017-03-14 20:01:29 +0100152}
153
Willy Tarreau21faa912012-10-10 08:27:36 +0200154/*
155 * Registers the server keyword list <kwl> as a list of valid keywords for next
156 * parsing sessions.
157 */
158void srv_register_keywords(struct srv_kw_list *kwl)
159{
160 LIST_ADDQ(&srv_keywords.list, &kwl->list);
161}
162
163/* Return a pointer to the server keyword <kw>, or NULL if not found. If the
164 * keyword is found with a NULL ->parse() function, then an attempt is made to
165 * find one with a valid ->parse() function. This way it is possible to declare
166 * platform-dependant, known keywords as NULL, then only declare them as valid
167 * if some options are met. Note that if the requested keyword contains an
168 * opening parenthesis, everything from this point is ignored.
169 */
170struct srv_kw *srv_find_kw(const char *kw)
171{
172 int index;
173 const char *kwend;
174 struct srv_kw_list *kwl;
175 struct srv_kw *ret = NULL;
176
177 kwend = strchr(kw, '(');
178 if (!kwend)
179 kwend = kw + strlen(kw);
180
181 list_for_each_entry(kwl, &srv_keywords.list, list) {
182 for (index = 0; kwl->kw[index].kw != NULL; index++) {
183 if ((strncmp(kwl->kw[index].kw, kw, kwend - kw) == 0) &&
184 kwl->kw[index].kw[kwend-kw] == 0) {
185 if (kwl->kw[index].parse)
186 return &kwl->kw[index]; /* found it !*/
187 else
188 ret = &kwl->kw[index]; /* may be OK */
189 }
190 }
191 }
192 return ret;
193}
194
195/* Dumps all registered "server" keywords to the <out> string pointer. The
196 * unsupported keywords are only dumped if their supported form was not
197 * found.
198 */
199void srv_dump_kws(char **out)
200{
201 struct srv_kw_list *kwl;
202 int index;
203
204 *out = NULL;
205 list_for_each_entry(kwl, &srv_keywords.list, list) {
206 for (index = 0; kwl->kw[index].kw != NULL; index++) {
207 if (kwl->kw[index].parse ||
208 srv_find_kw(kwl->kw[index].kw) == &kwl->kw[index]) {
209 memprintf(out, "%s[%4s] %s%s%s%s\n", *out ? *out : "",
210 kwl->scope,
211 kwl->kw[index].kw,
212 kwl->kw[index].skip ? " <arg>" : "",
213 kwl->kw[index].default_ok ? " [dflt_ok]" : "",
214 kwl->kw[index].parse ? "" : " (not supported)");
215 }
216 }
217 }
218}
Krzysztof Piotr Oledzki5259dfe2008-01-21 01:54:06 +0100219
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +0100220/* Parse the "addr" server keyword */
221static int srv_parse_addr(char **args, int *cur_arg,
222 struct proxy *curproxy, struct server *newsrv, char **err)
223{
224 char *errmsg, *arg;
225 struct sockaddr_storage *sk;
226 int port1, port2;
227 struct protocol *proto;
228
229 errmsg = NULL;
230 arg = args[*cur_arg + 1];
231
232 if (!*arg) {
233 memprintf(err, "'%s' expects <ipv4|ipv6> as argument.\n", args[*cur_arg]);
234 goto err;
235 }
236
237 sk = str2sa_range(arg, NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
238 if (!sk) {
239 memprintf(err, "'%s' : %s", args[*cur_arg], errmsg);
240 goto err;
241 }
242
243 proto = protocol_by_family(sk->ss_family);
244 if (!proto || !proto->connect) {
245 memprintf(err, "'%s %s' : connect() not supported for this address family.\n",
246 args[*cur_arg], arg);
247 goto err;
248 }
249
250 if (port1 != port2) {
251 memprintf(err, "'%s' : port ranges and offsets are not allowed in '%s'\n",
252 args[*cur_arg], arg);
253 goto err;
254 }
255
256 newsrv->check.addr = newsrv->agent.addr = *sk;
257 newsrv->flags |= SRV_F_CHECKADDR;
258 newsrv->flags |= SRV_F_AGENTADDR;
259
260 return 0;
261
262 err:
263 free(errmsg);
264 return ERR_ALERT | ERR_FATAL;
265}
266
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100267/* Parse the "agent-check" server keyword */
268static int srv_parse_agent_check(char **args, int *cur_arg,
269 struct proxy *curproxy, struct server *newsrv, char **err)
270{
271 newsrv->do_agent = 1;
272 return 0;
273}
274
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100275/* Parse the "backup" server keyword */
276static int srv_parse_backup(char **args, int *cur_arg,
277 struct proxy *curproxy, struct server *newsrv, char **err)
278{
279 newsrv->flags |= SRV_F_BACKUP;
280 return 0;
281}
282
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100283/* Parse the "check" server keyword */
284static int srv_parse_check(char **args, int *cur_arg,
285 struct proxy *curproxy, struct server *newsrv, char **err)
286{
287 newsrv->do_check = 1;
288 return 0;
289}
290
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100291/* Parse the "check-send-proxy" server keyword */
292static int srv_parse_check_send_proxy(char **args, int *cur_arg,
293 struct proxy *curproxy, struct server *newsrv, char **err)
294{
295 newsrv->check.send_proxy = 1;
296 return 0;
297}
298
Frédéric Lécaille9d1b95b2017-03-15 09:13:33 +0100299/* Parse the "cookie" server keyword */
300static int srv_parse_cookie(char **args, int *cur_arg,
301 struct proxy *curproxy, struct server *newsrv, char **err)
302{
303 char *arg;
304
305 arg = args[*cur_arg + 1];
306 if (!*arg) {
307 memprintf(err, "'%s' expects <value> as argument.\n", args[*cur_arg]);
308 return ERR_ALERT | ERR_FATAL;
309 }
310
311 free(newsrv->cookie);
312 newsrv->cookie = strdup(arg);
313 newsrv->cklen = strlen(arg);
314 newsrv->flags |= SRV_F_COOKIESET;
315 return 0;
316}
317
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100318/* Parse the "disabled" server keyword */
319static int srv_parse_disabled(char **args, int *cur_arg,
320 struct proxy *curproxy, struct server *newsrv, char **err)
321{
Emeric Brun52a91d32017-08-31 14:41:55 +0200322 newsrv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
323 newsrv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100324 newsrv->check.state |= CHK_ST_PAUSED;
325 newsrv->check.health = 0;
326 return 0;
327}
328
329/* Parse the "enabled" server keyword */
330static int srv_parse_enabled(char **args, int *cur_arg,
331 struct proxy *curproxy, struct server *newsrv, char **err)
332{
Emeric Brun52a91d32017-08-31 14:41:55 +0200333 newsrv->next_admin &= ~SRV_ADMF_CMAINT & ~SRV_ADMF_FMAINT;
334 newsrv->next_state = SRV_ST_RUNNING;
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +0100335 newsrv->check.state &= ~CHK_ST_PAUSED;
336 newsrv->check.health = newsrv->check.rise;
337 return 0;
338}
339
Willy Tarreaudff55432012-10-10 17:51:05 +0200340/* parse the "id" server keyword */
341static int srv_parse_id(char **args, int *cur_arg, struct proxy *curproxy, struct server *newsrv, char **err)
342{
343 struct eb32_node *node;
344
345 if (!*args[*cur_arg + 1]) {
346 memprintf(err, "'%s' : expects an integer argument", args[*cur_arg]);
347 return ERR_ALERT | ERR_FATAL;
348 }
349
350 newsrv->puid = atol(args[*cur_arg + 1]);
351 newsrv->conf.id.key = newsrv->puid;
352
353 if (newsrv->puid <= 0) {
354 memprintf(err, "'%s' : custom id has to be > 0", args[*cur_arg]);
355 return ERR_ALERT | ERR_FATAL;
356 }
357
358 node = eb32_lookup(&curproxy->conf.used_server_id, newsrv->puid);
359 if (node) {
360 struct server *target = container_of(node, struct server, conf.id);
361 memprintf(err, "'%s' : custom id %d already used at %s:%d ('server %s')",
362 args[*cur_arg], newsrv->puid, target->conf.file, target->conf.line,
363 target->id);
364 return ERR_ALERT | ERR_FATAL;
365 }
366
367 eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
Baptiste Assmann7cc419a2015-07-07 22:02:20 +0200368 newsrv->flags |= SRV_F_FORCED_ID;
Willy Tarreaudff55432012-10-10 17:51:05 +0200369 return 0;
370}
371
Frédéric Lécaille22f41a22017-03-16 17:17:36 +0100372/* Parse the "namespace" server keyword */
373static int srv_parse_namespace(char **args, int *cur_arg,
374 struct proxy *curproxy, struct server *newsrv, char **err)
375{
376#ifdef CONFIG_HAP_NS
377 char *arg;
378
379 arg = args[*cur_arg + 1];
380 if (!*arg) {
381 memprintf(err, "'%s' : expects <name> as argument", args[*cur_arg]);
382 return ERR_ALERT | ERR_FATAL;
383 }
384
385 if (!strcmp(arg, "*")) {
386 /* Use the namespace associated with the connection (if present). */
387 newsrv->flags |= SRV_F_USE_NS_FROM_PP;
388 return 0;
389 }
390
391 /*
392 * As this parser may be called several times for the same 'default-server'
393 * object, or for a new 'server' instance deriving from a 'default-server'
394 * one with SRV_F_USE_NS_FROM_PP flag enabled, let's reset it.
395 */
396 newsrv->flags &= ~SRV_F_USE_NS_FROM_PP;
397
398 newsrv->netns = netns_store_lookup(arg, strlen(arg));
399 if (!newsrv->netns)
400 newsrv->netns = netns_store_insert(arg);
401
402 if (!newsrv->netns) {
403 memprintf(err, "Cannot open namespace '%s'", arg);
404 return ERR_ALERT | ERR_FATAL;
405 }
406
407 return 0;
408#else
409 memprintf(err, "'%s': '%s' option not implemented", args[0], args[*cur_arg]);
410 return ERR_ALERT | ERR_FATAL;
411#endif
412}
413
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +0100414/* Parse the "no-agent-check" server keyword */
415static int srv_parse_no_agent_check(char **args, int *cur_arg,
416 struct proxy *curproxy, struct server *newsrv, char **err)
417{
418 free_check(&newsrv->agent);
419 newsrv->agent.inter = 0;
420 newsrv->agent.port = 0;
421 newsrv->agent.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED & ~CHK_ST_AGENT;
422 newsrv->do_agent = 0;
423 return 0;
424}
425
Frédéric Lécaillef5bf9032017-03-10 11:51:05 +0100426/* Parse the "no-backup" server keyword */
427static int srv_parse_no_backup(char **args, int *cur_arg,
428 struct proxy *curproxy, struct server *newsrv, char **err)
429{
430 newsrv->flags &= ~SRV_F_BACKUP;
431 return 0;
432}
433
Frédéric Lécaille65aa3562017-03-14 11:20:13 +0100434/* Parse the "no-check" server keyword */
435static int srv_parse_no_check(char **args, int *cur_arg,
436 struct proxy *curproxy, struct server *newsrv, char **err)
437{
438 free_check(&newsrv->check);
439 newsrv->check.state &= ~CHK_ST_CONFIGURED & ~CHK_ST_ENABLED;
440 newsrv->do_check = 0;
441 return 0;
442}
443
Frédéric Lécaille25df8902017-03-10 14:04:31 +0100444/* Parse the "no-check-send-proxy" server keyword */
445static int srv_parse_no_check_send_proxy(char **args, int *cur_arg,
446 struct proxy *curproxy, struct server *newsrv, char **err)
447{
448 newsrv->check.send_proxy = 0;
449 return 0;
450}
451
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100452/* Disable server PROXY protocol flags. */
453static int inline srv_disable_pp_flags(struct server *srv, unsigned int flags)
454{
455 srv->pp_opts &= ~flags;
456 return 0;
457}
458
459/* Parse the "no-send-proxy" server keyword */
460static int srv_parse_no_send_proxy(char **args, int *cur_arg,
461 struct proxy *curproxy, struct server *newsrv, char **err)
462{
463 return srv_disable_pp_flags(newsrv, SRV_PP_V1);
464}
465
466/* Parse the "no-send-proxy-v2" server keyword */
467static int srv_parse_no_send_proxy_v2(char **args, int *cur_arg,
468 struct proxy *curproxy, struct server *newsrv, char **err)
469{
470 return srv_disable_pp_flags(newsrv, SRV_PP_V2);
471}
472
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100473/* Parse the "non-stick" server keyword */
474static int srv_parse_non_stick(char **args, int *cur_arg,
475 struct proxy *curproxy, struct server *newsrv, char **err)
476{
477 newsrv->flags |= SRV_F_NON_STICK;
478 return 0;
479}
480
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100481/* Enable server PROXY protocol flags. */
482static int inline srv_enable_pp_flags(struct server *srv, unsigned int flags)
483{
484 srv->pp_opts |= flags;
485 return 0;
486}
487
Frédéric Lécaille547356e2017-03-15 08:55:39 +0100488/* Parse the "observe" server keyword */
489static int srv_parse_observe(char **args, int *cur_arg,
490 struct proxy *curproxy, struct server *newsrv, char **err)
491{
492 char *arg;
493
494 arg = args[*cur_arg + 1];
495 if (!*arg) {
496 memprintf(err, "'%s' expects <mode> as argument.\n", args[*cur_arg]);
497 return ERR_ALERT | ERR_FATAL;
498 }
499
500 if (!strcmp(arg, "none")) {
501 newsrv->observe = HANA_OBS_NONE;
502 }
503 else if (!strcmp(arg, "layer4")) {
504 newsrv->observe = HANA_OBS_LAYER4;
505 }
506 else if (!strcmp(arg, "layer7")) {
507 if (curproxy->mode != PR_MODE_HTTP) {
508 memprintf(err, "'%s' can only be used in http proxies.\n", arg);
509 return ERR_ALERT;
510 }
511 newsrv->observe = HANA_OBS_LAYER7;
512 }
513 else {
514 memprintf(err, "'%s' expects one of 'none', 'layer4', 'layer7' "
515 "but got '%s'\n", args[*cur_arg], arg);
516 return ERR_ALERT | ERR_FATAL;
517 }
518
519 return 0;
520}
521
Frédéric Lécaille16186232017-03-14 16:42:49 +0100522/* Parse the "redir" server keyword */
523static int srv_parse_redir(char **args, int *cur_arg,
524 struct proxy *curproxy, struct server *newsrv, char **err)
525{
526 char *arg;
527
528 arg = args[*cur_arg + 1];
529 if (!*arg) {
530 memprintf(err, "'%s' expects <prefix> as argument.\n", args[*cur_arg]);
531 return ERR_ALERT | ERR_FATAL;
532 }
533
534 free(newsrv->rdr_pfx);
535 newsrv->rdr_pfx = strdup(arg);
536 newsrv->rdr_len = strlen(arg);
537
538 return 0;
539}
540
Frédéric Lécaille31045e42017-03-10 16:40:00 +0100541/* Parse the "send-proxy" server keyword */
542static int srv_parse_send_proxy(char **args, int *cur_arg,
543 struct proxy *curproxy, struct server *newsrv, char **err)
544{
545 return srv_enable_pp_flags(newsrv, SRV_PP_V1);
546}
547
548/* Parse the "send-proxy-v2" server keyword */
549static int srv_parse_send_proxy_v2(char **args, int *cur_arg,
550 struct proxy *curproxy, struct server *newsrv, char **err)
551{
552 return srv_enable_pp_flags(newsrv, SRV_PP_V2);
553}
554
Frédéric Lécailledba97072017-03-17 15:33:50 +0100555
556/* Parse the "source" server keyword */
557static int srv_parse_source(char **args, int *cur_arg,
558 struct proxy *curproxy, struct server *newsrv, char **err)
559{
560 char *errmsg;
561 int port_low, port_high;
562 struct sockaddr_storage *sk;
563 struct protocol *proto;
564
565 errmsg = NULL;
566
567 if (!*args[*cur_arg + 1]) {
568 memprintf(err, "'%s' expects <addr>[:<port>[-<port>]], and optionally '%s' <addr>, "
569 "and '%s' <name> as argument.\n", args[*cur_arg], "usesrc", "interface");
570 goto err;
571 }
572
573 /* 'sk' is statically allocated (no need to be freed). */
574 sk = str2sa_range(args[*cur_arg + 1], NULL, &port_low, &port_high, &errmsg, NULL, NULL, 1);
575 if (!sk) {
576 memprintf(err, "'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
577 goto err;
578 }
579
580 proto = protocol_by_family(sk->ss_family);
581 if (!proto || !proto->connect) {
582 Alert("'%s %s' : connect() not supported for this address family.\n",
583 args[*cur_arg], args[*cur_arg + 1]);
584 goto err;
585 }
586
587 newsrv->conn_src.opts |= CO_SRC_BIND;
588 newsrv->conn_src.source_addr = *sk;
589
590 if (port_low != port_high) {
591 int i;
592
593 if (!port_low || !port_high) {
594 Alert("'%s' does not support port offsets (found '%s').\n",
595 args[*cur_arg], args[*cur_arg + 1]);
596 goto err;
597 }
598
599 if (port_low <= 0 || port_low > 65535 ||
600 port_high <= 0 || port_high > 65535 ||
601 port_low > port_high) {
602 Alert("'%s': invalid source port range %d-%d.\n", args[*cur_arg], port_low, port_high);
603 goto err;
604 }
605 newsrv->conn_src.sport_range = port_range_alloc_range(port_high - port_low + 1);
606 for (i = 0; i < newsrv->conn_src.sport_range->size; i++)
607 newsrv->conn_src.sport_range->ports[i] = port_low + i;
608 }
609
610 *cur_arg += 2;
611 while (*(args[*cur_arg])) {
612 if (!strcmp(args[*cur_arg], "usesrc")) { /* address to use outside */
613#if defined(CONFIG_HAP_TRANSPARENT)
614 if (!*args[*cur_arg + 1]) {
615 Alert("'usesrc' expects <addr>[:<port>], 'client', 'clientip', "
616 "or 'hdr_ip(name,#)' as argument.\n");
617 goto err;
618 }
619 if (!strcmp(args[*cur_arg + 1], "client")) {
620 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
621 newsrv->conn_src.opts |= CO_SRC_TPROXY_CLI;
622 }
623 else if (!strcmp(args[*cur_arg + 1], "clientip")) {
624 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
625 newsrv->conn_src.opts |= CO_SRC_TPROXY_CIP;
626 }
627 else if (!strncmp(args[*cur_arg + 1], "hdr_ip(", 7)) {
628 char *name, *end;
629
630 name = args[*cur_arg + 1] + 7;
631 while (isspace(*name))
632 name++;
633
634 end = name;
635 while (*end && !isspace(*end) && *end != ',' && *end != ')')
636 end++;
637
638 newsrv->conn_src.opts &= ~CO_SRC_TPROXY_MASK;
639 newsrv->conn_src.opts |= CO_SRC_TPROXY_DYN;
640 free(newsrv->conn_src.bind_hdr_name);
641 newsrv->conn_src.bind_hdr_name = calloc(1, end - name + 1);
642 newsrv->conn_src.bind_hdr_len = end - name;
643 memcpy(newsrv->conn_src.bind_hdr_name, name, end - name);
644 newsrv->conn_src.bind_hdr_name[end - name] = '\0';
645 newsrv->conn_src.bind_hdr_occ = -1;
646
647 /* now look for an occurrence number */
648 while (isspace(*end))
649 end++;
650 if (*end == ',') {
651 end++;
652 name = end;
653 if (*end == '-')
654 end++;
655 while (isdigit((int)*end))
656 end++;
657 newsrv->conn_src.bind_hdr_occ = strl2ic(name, end - name);
658 }
659
660 if (newsrv->conn_src.bind_hdr_occ < -MAX_HDR_HISTORY) {
661 Alert("usesrc hdr_ip(name,num) does not support negative"
662 " occurrences values smaller than %d.\n", MAX_HDR_HISTORY);
663 goto err;
664 }
665 }
666 else {
667 struct sockaddr_storage *sk;
668 int port1, port2;
669
670 /* 'sk' is statically allocated (no need to be freed). */
671 sk = str2sa_range(args[*cur_arg + 1], NULL, &port1, &port2, &errmsg, NULL, NULL, 1);
672 if (!sk) {
673 Alert("'%s %s' : %s\n", args[*cur_arg], args[*cur_arg + 1], errmsg);
674 goto err;
675 }
676
677 proto = protocol_by_family(sk->ss_family);
678 if (!proto || !proto->connect) {
679 Alert("'%s %s' : connect() not supported for this address family.\n",
680 args[*cur_arg], args[*cur_arg + 1]);
681 goto err;
682 }
683
684 if (port1 != port2) {
685 Alert("'%s' : port ranges and offsets are not allowed in '%s'\n",
686 args[*cur_arg], args[*cur_arg + 1]);
687 goto err;
688 }
689 newsrv->conn_src.tproxy_addr = *sk;
690 newsrv->conn_src.opts |= CO_SRC_TPROXY_ADDR;
691 }
692 global.last_checks |= LSTCHK_NETADM;
693 *cur_arg += 2;
694 continue;
695#else /* no TPROXY support */
696 Alert("'usesrc' not allowed here because support for TPROXY was not compiled in.\n");
697 goto err;
698#endif /* defined(CONFIG_HAP_TRANSPARENT) */
699 } /* "usesrc" */
700
701 if (!strcmp(args[*cur_arg], "interface")) { /* specifically bind to this interface */
702#ifdef SO_BINDTODEVICE
703 if (!*args[*cur_arg + 1]) {
704 Alert("'%s' : missing interface name.\n", args[0]);
705 goto err;
706 }
707 free(newsrv->conn_src.iface_name);
708 newsrv->conn_src.iface_name = strdup(args[*cur_arg + 1]);
709 newsrv->conn_src.iface_len = strlen(newsrv->conn_src.iface_name);
710 global.last_checks |= LSTCHK_NETADM;
711#else
712 Alert("'%s' : '%s' option not implemented.\n", args[0], args[*cur_arg]);
713 goto err;
714#endif
715 *cur_arg += 2;
716 continue;
717 }
718 /* this keyword in not an option of "source" */
719 break;
720 } /* while */
721
722 return 0;
723
724 err:
725 free(errmsg);
726 return ERR_ALERT | ERR_FATAL;
727}
728
Frédéric Lécaillef9bc1d62017-03-10 15:50:49 +0100729/* Parse the "stick" server keyword */
730static int srv_parse_stick(char **args, int *cur_arg,
731 struct proxy *curproxy, struct server *newsrv, char **err)
732{
733 newsrv->flags &= ~SRV_F_NON_STICK;
734 return 0;
735}
736
Frédéric Lécaille67e0e612017-03-14 15:21:31 +0100737/* Parse the "track" server keyword */
738static int srv_parse_track(char **args, int *cur_arg,
739 struct proxy *curproxy, struct server *newsrv, char **err)
740{
741 char *arg;
742
743 arg = args[*cur_arg + 1];
744 if (!*arg) {
745 memprintf(err, "'track' expects [<proxy>/]<server> as argument.\n");
746 return ERR_ALERT | ERR_FATAL;
747 }
748
749 free(newsrv->trackit);
750 newsrv->trackit = strdup(arg);
751
752 return 0;
753}
754
Frédéric Lécailledba97072017-03-17 15:33:50 +0100755
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200756/* Shutdown all connections of a server. The caller must pass a termination
Willy Tarreaue7dff022015-04-03 01:14:29 +0200757 * code in <why>, which must be one of SF_ERR_* indicating the reason for the
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200758 * shutdown.
759 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200760void srv_shutdown_streams(struct server *srv, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200761{
Willy Tarreau87b09662015-04-03 00:22:06 +0200762 struct stream *stream, *stream_bck;
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200763
Christopher Faulet29f77e82017-06-08 14:04:45 +0200764 SPIN_LOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau87b09662015-04-03 00:22:06 +0200765 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
766 if (stream->srv_conn == srv)
767 stream_shutdown(stream, why);
Christopher Faulet29f77e82017-06-08 14:04:45 +0200768 SPIN_UNLOCK(SERVER_LOCK, &srv->lock);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200769}
770
771/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200772 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200773 * the reason for the shutdown.
774 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200775void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200776{
777 struct server *srv;
778
779 for (srv = px->srv; srv != NULL; srv = srv->next)
780 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200781 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200782}
783
Willy Tarreaubda92272014-05-20 21:55:30 +0200784/* Appends some information to a message string related to a server going UP or
785 * DOWN. If both <forced> and <reason> are null and the server tracks another
786 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200787 * If <check> is non-null, an entire string describing the check result will be
788 * appended after a comma and a space (eg: to report some information from the
789 * check that changed the state). In the other case, the string will be built
790 * using the check results stored into the struct server if present.
791 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200792 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200793 */
Emeric Brun5a133512017-10-19 14:42:30 +0200794void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200795{
Emeric Brun5a133512017-10-19 14:42:30 +0200796 short status = s->op_st_chg.status;
797 short code = s->op_st_chg.code;
798 long duration = s->op_st_chg.duration;
799 char *desc = s->op_st_chg.reason;
800
801 if (check) {
802 status = check->status;
803 code = check->code;
804 duration = check->duration;
805 desc = check->desc;
806 }
807
808 if (status != -1) {
809 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
810
811 if (status >= HCHK_STATUS_L57DATA)
812 chunk_appendf(msg, ", code: %d", code);
813
814 if (desc && *desc) {
815 struct chunk src;
816
817 chunk_appendf(msg, ", info: \"");
818
819 chunk_initlen(&src, desc, 0, strlen(desc));
820 chunk_asciiencode(msg, &src, '"');
821
822 chunk_appendf(msg, "\"");
823 }
824
825 if (duration >= 0)
826 chunk_appendf(msg, ", check duration: %ldms", duration);
827 }
828 else if (desc && *desc) {
829 chunk_appendf(msg, ", %s", desc);
830 }
831 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200832 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200833 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200834
835 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200836 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200837 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
838 " %d sessions active, %d requeued, %d remaining in queue",
839 s->proxy->srv_act, s->proxy->srv_bck,
840 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
841 s->cur_sess, xferred, s->nbpend);
842 else
843 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
844 " %d sessions requeued, %d total in queue",
845 s->proxy->srv_act, s->proxy->srv_bck,
846 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
847 xferred, s->nbpend);
848 }
849}
850
Emeric Brun5a133512017-10-19 14:42:30 +0200851/* Marks server <s> down, regardless of its checks' statuses. The server is
852 * registered in a list to postpone the counting of the remaining servers on
853 * the proxy and transfers queued streams whenever possible to other servers at
854 * a sync point. Maintenance servers are ignored. It stores the <reason> if
855 * non-null as the reason for going down or the available data from the check
856 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200857 */
Emeric Brun5a133512017-10-19 14:42:30 +0200858void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200859{
860 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200861
Emeric Brun64cc49c2017-10-03 14:46:45 +0200862 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200863 return;
864
Emeric Brun52a91d32017-08-31 14:41:55 +0200865 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200866 *s->op_st_chg.reason = 0;
867 s->op_st_chg.status = -1;
868 if (reason) {
869 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
870 }
871 else if (check) {
872 if (check->desc)
873 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
874 s->op_st_chg.code = check->code;
875 s->op_st_chg.status = check->status;
876 s->op_st_chg.duration = check->duration;
877 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200878
Emeric Brun64cc49c2017-10-03 14:46:45 +0200879 /* Register changes to be applied asynchronously */
880 if (LIST_ISEMPTY(&s->update_status))
881 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200882
883 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200884 srv_set_stopped(srv, NULL, NULL);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200885}
886
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200887/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200888 * in maintenance. The server is registered in a list to postpone the counting
889 * of the remaining servers on the proxy and tries to grab requests from the
890 * proxy at a sync point. Maintenance servers are ignored. It stores the
891 * <reason> if non-null as the reason for going down or the available data
892 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200893 */
Emeric Brun5a133512017-10-19 14:42:30 +0200894void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200895{
896 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200897
Emeric Brun64cc49c2017-10-03 14:46:45 +0200898 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200899 return;
900
Emeric Brun52a91d32017-08-31 14:41:55 +0200901 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200902 return;
903
Emeric Brun52a91d32017-08-31 14:41:55 +0200904 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200905 *s->op_st_chg.reason = 0;
906 s->op_st_chg.status = -1;
907 if (reason) {
908 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
909 }
910 else if (check) {
911 if (check->desc)
912 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
913 s->op_st_chg.code = check->code;
914 s->op_st_chg.status = check->status;
915 s->op_st_chg.duration = check->duration;
916 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200917
Emeric Brun64cc49c2017-10-03 14:46:45 +0200918 if (s->slowstart <= 0)
919 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200920
Emeric Brun64cc49c2017-10-03 14:46:45 +0200921 /* Register changes to be applied asynchronously */
922 if (LIST_ISEMPTY(&s->update_status))
923 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200924
925 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200926 srv_set_running(srv, NULL, NULL);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200927}
928
Willy Tarreau8eb77842014-05-21 13:54:57 +0200929/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200930 * isn't in maintenance. The server is registered in a list to postpone the
931 * counting of the remaining servers on the proxy and tries to grab requests
932 * from the proxy. Maintenance servers are ignored. It stores the
933 * <reason> if non-null as the reason for going down or the available data
934 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200935 * up. Note that it makes use of the trash to build the log strings, so <reason>
936 * must not be placed there.
937 */
Emeric Brun5a133512017-10-19 14:42:30 +0200938void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200939{
940 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200941
Emeric Brun64cc49c2017-10-03 14:46:45 +0200942 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200943 return;
944
Emeric Brun52a91d32017-08-31 14:41:55 +0200945 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200946 return;
947
Emeric Brun52a91d32017-08-31 14:41:55 +0200948 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200949 *s->op_st_chg.reason = 0;
950 s->op_st_chg.status = -1;
951 if (reason) {
952 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
953 }
954 else if (check) {
955 if (check->desc)
956 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
957 s->op_st_chg.code = check->code;
958 s->op_st_chg.status = check->status;
959 s->op_st_chg.duration = check->duration;
960 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200961
Emeric Brun64cc49c2017-10-03 14:46:45 +0200962 /* Register changes to be applied asynchronously */
963 if (LIST_ISEMPTY(&s->update_status))
964 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200965
966 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200967 srv_set_stopping(srv, NULL, NULL);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200968}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200969
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200970/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
971 * enforce either maint mode or drain mode. It is not allowed to set more than
972 * one flag at once. The equivalent "inherited" flag is propagated to all
973 * tracking servers. Maintenance mode disables health checks (but not agent
974 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100975 * is done. If <cause> is non-null, it will be displayed at the end of the log
976 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100978void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200979{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200980 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200981
982 if (!mode)
983 return;
984
985 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200986 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200987 return;
988
Emeric Brun52a91d32017-08-31 14:41:55 +0200989 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200990 if (cause)
991 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
992
993 /* Register changes to be applied asynchronously */
994 if (LIST_ISEMPTY(&s->update_status))
995 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200996
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200997 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200998 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
999 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001000 return;
1001
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001002 /* compute the inherited flag to propagate */
1003 if (mode & SRV_ADMF_MAINT)
1004 mode = SRV_ADMF_IMAINT;
1005 else if (mode & SRV_ADMF_DRAIN)
1006 mode = SRV_ADMF_IDRAIN;
1007
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001008 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001009 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001010}
1011
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001012/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1013 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1014 * than one flag at once. The equivalent "inherited" flag is propagated to all
1015 * tracking servers. Leaving maintenance mode re-enables health checks. When
1016 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001018void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001020 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001021
1022 if (!mode)
1023 return;
1024
1025 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001026 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001027 return;
1028
Emeric Brun52a91d32017-08-31 14:41:55 +02001029 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001030
Emeric Brun64cc49c2017-10-03 14:46:45 +02001031 /* Register changes to be applied asynchronously */
1032 if (LIST_ISEMPTY(&s->update_status))
1033 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001034 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001035 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1036 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001037 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001038
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001039 if (mode & SRV_ADMF_MAINT)
1040 mode = SRV_ADMF_IMAINT;
1041 else if (mode & SRV_ADMF_DRAIN)
1042 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001043
1044 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001045 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001046}
1047
Willy Tarreau757478e2016-11-03 19:22:19 +01001048/* principle: propagate maint and drain to tracking servers. This is useful
1049 * upon startup so that inherited states are correct.
1050 */
1051static void srv_propagate_admin_state(struct server *srv)
1052{
1053 struct server *srv2;
1054
1055 if (!srv->trackers)
1056 return;
1057
1058 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001059 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001060 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001061
Emeric Brun52a91d32017-08-31 14:41:55 +02001062 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001063 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001064 }
1065}
1066
1067/* Compute and propagate the admin states for all servers in proxy <px>.
1068 * Only servers *not* tracking another one are considered, because other
1069 * ones will be handled when the server they track is visited.
1070 */
1071void srv_compute_all_admin_states(struct proxy *px)
1072{
1073 struct server *srv;
1074
1075 for (srv = px->srv; srv; srv = srv->next) {
1076 if (srv->track)
1077 continue;
1078 srv_propagate_admin_state(srv);
1079 }
1080}
1081
Willy Tarreaudff55432012-10-10 17:51:05 +02001082/* Note: must not be declared <const> as its list will be overwritten.
1083 * Please take care of keeping this list alphabetically sorted, doing so helps
1084 * all code contributors.
1085 * Optional keywords are also declared with a NULL ->parse() function so that
1086 * the config parser can report an appropriate error when a known keyword was
1087 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001088 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001089 */
1090static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001091 { "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 +01001092 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001093 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001094 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001095 { "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 +01001096 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001097 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1098 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001099 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001100 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001101 { "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 +01001102 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001103 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001104 { "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 +01001105 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1106 { "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 +01001107 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001108 { "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 +01001109 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001110 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1111 { "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 +02001112 { "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 +01001113 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001114 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001115 { NULL, NULL, 0 },
1116}};
1117
1118__attribute__((constructor))
1119static void __listener_init(void)
1120{
1121 srv_register_keywords(&srv_kws);
1122}
1123
Willy Tarreau004e0452013-11-21 11:22:01 +01001124/* Recomputes the server's eweight based on its state, uweight, the current time,
1125 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1126 * state is automatically disabled if the time is elapsed.
1127 */
1128void server_recalc_eweight(struct server *sv)
1129{
1130 struct proxy *px = sv->proxy;
1131 unsigned w;
1132
1133 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1134 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001135 if (sv->next_state == SRV_ST_STARTING)
1136 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001137 }
1138
1139 /* We must take care of not pushing the server to full throttle during slow starts.
1140 * It must also start immediately, at least at the minimal step when leaving maintenance.
1141 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001142 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001143 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1144 else
1145 w = px->lbprm.wdiv;
1146
Emeric Brun52a91d32017-08-31 14:41:55 +02001147 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001148
Emeric Brun64cc49c2017-10-03 14:46:45 +02001149 /* Register changes to be applied asynchronously */
1150 if (LIST_ISEMPTY(&sv->update_status))
1151 LIST_ADDQ(&updated_servers, &sv->update_status);
Willy Tarreau004e0452013-11-21 11:22:01 +01001152}
1153
Willy Tarreaubaaee002006-06-26 02:48:02 +02001154/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001155 * Parses weight_str and configures sv accordingly.
1156 * Returns NULL on success, error message string otherwise.
1157 */
1158const char *server_parse_weight_change_request(struct server *sv,
1159 const char *weight_str)
1160{
1161 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001162 long int w;
1163 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001164
1165 px = sv->proxy;
1166
1167 /* if the weight is terminated with '%', it is set relative to
1168 * the initial weight, otherwise it is absolute.
1169 */
1170 if (!*weight_str)
1171 return "Require <weight> or <weight%>.\n";
1172
Simon Hormanb796afa2013-02-12 10:45:53 +09001173 w = strtol(weight_str, &end, 10);
1174 if (end == weight_str)
1175 return "Empty weight string empty or preceded by garbage";
1176 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001177 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001178 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001179 /* Avoid integer overflow */
1180 if (w > 25600)
1181 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001182 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001183 if (w > 256)
1184 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001185 }
1186 else if (w < 0 || w > 256)
1187 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001188 else if (end[0] != '\0')
1189 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001190
1191 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1192 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1193
1194 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001195 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001196
1197 return NULL;
1198}
1199
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001200/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001201 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1202 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001203 * Returns:
1204 * - error string on error
1205 * - NULL on success
1206 */
1207const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001208 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001209{
1210 unsigned char ip[INET6_ADDRSTRLEN];
1211
1212 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001213 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001214 return NULL;
1215 }
1216 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001217 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001218 return NULL;
1219 }
1220
1221 return "Could not understand IP address format.\n";
1222}
1223
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001224const char *server_parse_maxconn_change_request(struct server *sv,
1225 const char *maxconn_str)
1226{
1227 long int v;
1228 char *end;
1229
1230 if (!*maxconn_str)
1231 return "Require <maxconn>.\n";
1232
1233 v = strtol(maxconn_str, &end, 10);
1234 if (end == maxconn_str)
1235 return "maxconn string empty or preceded by garbage";
1236 else if (end[0] != '\0')
1237 return "Trailing garbage in maxconn string";
1238
1239 if (sv->maxconn == sv->minconn) { // static maxconn
1240 sv->maxconn = sv->minconn = v;
1241 } else { // dynamic maxconn
1242 sv->maxconn = v;
1243 }
1244
1245 if (may_dequeue_tasks(sv, sv->proxy))
1246 process_srv_queue(sv);
1247
1248 return NULL;
1249}
1250
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001251#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001252static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1253 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001254{
1255 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001256 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001257 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001258 NULL,
1259 };
1260
1261 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001262 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001263
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001264 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1265}
1266
1267static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1268{
1269 struct sample_expr *expr;
1270
1271 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 +01001272 if (!expr) {
1273 memprintf(err, "error detected while parsing sni expression : %s", *err);
1274 return ERR_ALERT | ERR_FATAL;
1275 }
1276
1277 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1278 memprintf(err, "error detected while parsing sni expression : "
1279 " fetch method '%s' extracts information from '%s', "
1280 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001281 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001282 return ERR_ALERT | ERR_FATAL;
1283 }
1284
1285 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1286 release_sample_expr(newsrv->ssl_ctx.sni);
1287 newsrv->ssl_ctx.sni = expr;
1288
1289 return 0;
1290}
1291#endif
1292
1293static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1294{
1295 if (err && *err) {
1296 indent_msg(err, 2);
1297 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1298 }
1299 else
1300 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1301 file, linenum, args[0], args[1], args[cur_arg]);
1302}
1303
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001304static void srv_conn_src_sport_range_cpy(struct server *srv,
1305 struct server *src)
1306{
1307 int range_sz;
1308
1309 range_sz = src->conn_src.sport_range->size;
1310 if (range_sz > 0) {
1311 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1312 if (srv->conn_src.sport_range != NULL) {
1313 int i;
1314
1315 for (i = 0; i < range_sz; i++) {
1316 srv->conn_src.sport_range->ports[i] =
1317 src->conn_src.sport_range->ports[i];
1318 }
1319 }
1320 }
1321}
1322
1323/*
1324 * Copy <src> server connection source settings to <srv> server everything needed.
1325 */
1326static void srv_conn_src_cpy(struct server *srv, struct server *src)
1327{
1328 srv->conn_src.opts = src->conn_src.opts;
1329 srv->conn_src.source_addr = src->conn_src.source_addr;
1330
1331 /* Source port range copy. */
1332 if (src->conn_src.sport_range != NULL)
1333 srv_conn_src_sport_range_cpy(srv, src);
1334
1335#ifdef CONFIG_HAP_TRANSPARENT
1336 if (src->conn_src.bind_hdr_name != NULL) {
1337 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1338 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1339 }
1340 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1341 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1342#endif
1343 if (src->conn_src.iface_name != NULL)
1344 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1345}
1346
1347/*
1348 * Copy <src> server SSL settings to <srv> server allocating
1349 * everything needed.
1350 */
1351#if defined(USE_OPENSSL)
1352static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1353{
1354 if (src->ssl_ctx.ca_file != NULL)
1355 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1356 if (src->ssl_ctx.crl_file != NULL)
1357 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1358 if (src->ssl_ctx.client_crt != NULL)
1359 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1360
1361 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1362
1363 if (src->ssl_ctx.verify_host != NULL)
1364 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1365 if (src->ssl_ctx.ciphers != NULL)
1366 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1367 if (src->sni_expr != NULL)
1368 srv->sni_expr = strdup(src->sni_expr);
1369}
1370#endif
1371
1372/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001373 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001374 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001375 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001376 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001377static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001378{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001379 char *hostname_dn;
1380 int hostname_len, hostname_dn_len;
1381
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001382 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001383 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001384
Christopher Faulet67957bd2017-09-27 11:00:59 +02001385 hostname_len = strlen(hostname);
1386 hostname_dn = trash.str;
1387 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1388 hostname_dn, trash.size);
1389 if (hostname_dn_len == -1)
1390 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001391
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001392
Christopher Faulet67957bd2017-09-27 11:00:59 +02001393 free(srv->hostname);
1394 free(srv->hostname_dn);
1395 srv->hostname = strdup(hostname);
1396 srv->hostname_dn = strdup(hostname_dn);
1397 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001398 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001399 goto err;
1400
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001401 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001402
1403 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001404 free(srv->hostname); srv->hostname = NULL;
1405 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001406 return -1;
1407}
1408
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001409/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001410 * Copy <src> server settings to <srv> server allocating
1411 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001412 * This function is not supposed to be called at any time, but only
1413 * during server settings parsing or during server allocations from
1414 * a server template, and just after having calloc()'ed a new server.
1415 * So, <src> may only be a default server (when parsing server settings)
1416 * or a server template (during server allocations from a server template).
1417 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1418 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001419 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001420static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001421{
1422 /* Connection source settings copy */
1423 srv_conn_src_cpy(srv, src);
1424
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001425 if (srv_tmpl) {
1426 srv->addr = src->addr;
1427 srv->svc_port = src->svc_port;
1428 }
1429
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001430 srv->pp_opts = src->pp_opts;
1431 if (src->rdr_pfx != NULL) {
1432 srv->rdr_pfx = strdup(src->rdr_pfx);
1433 srv->rdr_len = src->rdr_len;
1434 }
1435 if (src->cookie != NULL) {
1436 srv->cookie = strdup(src->cookie);
1437 srv->cklen = src->cklen;
1438 }
1439 srv->use_ssl = src->use_ssl;
1440 srv->check.addr = srv->agent.addr = src->check.addr;
1441 srv->check.use_ssl = src->check.use_ssl;
1442 srv->check.port = src->check.port;
1443 /* Note: 'flags' field has potentially been already initialized. */
1444 srv->flags |= src->flags;
1445 srv->do_check = src->do_check;
1446 srv->do_agent = src->do_agent;
1447 if (srv->check.port)
1448 srv->flags |= SRV_F_CHECKPORT;
1449 srv->check.inter = src->check.inter;
1450 srv->check.fastinter = src->check.fastinter;
1451 srv->check.downinter = src->check.downinter;
1452 srv->agent.use_ssl = src->agent.use_ssl;
1453 srv->agent.port = src->agent.port;
1454 if (src->agent.send_string != NULL)
1455 srv->agent.send_string = strdup(src->agent.send_string);
1456 srv->agent.send_string_len = src->agent.send_string_len;
1457 srv->agent.inter = src->agent.inter;
1458 srv->agent.fastinter = src->agent.fastinter;
1459 srv->agent.downinter = src->agent.downinter;
1460 srv->maxqueue = src->maxqueue;
1461 srv->minconn = src->minconn;
1462 srv->maxconn = src->maxconn;
1463 srv->slowstart = src->slowstart;
1464 srv->observe = src->observe;
1465 srv->onerror = src->onerror;
1466 srv->onmarkeddown = src->onmarkeddown;
1467 srv->onmarkedup = src->onmarkedup;
1468 if (src->trackit != NULL)
1469 srv->trackit = strdup(src->trackit);
1470 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1471 srv->uweight = srv->iweight = src->iweight;
1472
1473 srv->check.send_proxy = src->check.send_proxy;
1474 /* health: up, but will fall down at first failure */
1475 srv->check.rise = srv->check.health = src->check.rise;
1476 srv->check.fall = src->check.fall;
1477
1478 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001479 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1480 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1481 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001482 srv->check.state |= CHK_ST_PAUSED;
1483 srv->check.health = 0;
1484 }
1485
1486 /* health: up but will fall down at first failure */
1487 srv->agent.rise = srv->agent.health = src->agent.rise;
1488 srv->agent.fall = src->agent.fall;
1489
1490 if (src->resolvers_id != NULL)
1491 srv->resolvers_id = strdup(src->resolvers_id);
1492 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1493 if (srv->dns_opts.family_prio == AF_UNSPEC)
1494 srv->dns_opts.family_prio = AF_INET6;
1495 memcpy(srv->dns_opts.pref_net,
1496 src->dns_opts.pref_net,
1497 sizeof srv->dns_opts.pref_net);
1498 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1499
1500 srv->init_addr_methods = src->init_addr_methods;
1501 srv->init_addr = src->init_addr;
1502#if defined(USE_OPENSSL)
1503 srv_ssl_settings_cpy(srv, src);
1504#endif
1505#ifdef TCP_USER_TIMEOUT
1506 srv->tcp_ut = src->tcp_ut;
1507#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001508 if (srv_tmpl)
1509 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001510}
1511
1512static struct server *new_server(struct proxy *proxy)
1513{
1514 struct server *srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001515 int i;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001516
1517 srv = calloc(1, sizeof *srv);
1518 if (!srv)
1519 return NULL;
1520
1521 srv->obj_type = OBJ_TYPE_SERVER;
1522 srv->proxy = proxy;
1523 LIST_INIT(&srv->actconns);
1524 LIST_INIT(&srv->pendconns);
Christopher Faulet40a007c2017-07-03 15:41:01 +02001525
1526 if ((srv->priv_conns = calloc(global.nbthread, sizeof(*srv->priv_conns))) == NULL)
1527 goto free_srv;
1528 if ((srv->idle_conns = calloc(global.nbthread, sizeof(*srv->idle_conns))) == NULL)
1529 goto free_priv_conns;
1530 if ((srv->safe_conns = calloc(global.nbthread, sizeof(*srv->safe_conns))) == NULL)
1531 goto free_idle_conns;
1532
1533 for (i = 0; i < global.nbthread; i++) {
1534 LIST_INIT(&srv->priv_conns[i]);
1535 LIST_INIT(&srv->idle_conns[i]);
1536 LIST_INIT(&srv->safe_conns[i]);
1537 }
1538
Emeric Brun64cc49c2017-10-03 14:46:45 +02001539 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001540
Emeric Brun52a91d32017-08-31 14:41:55 +02001541 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001542 srv->last_change = now.tv_sec;
1543
1544 srv->check.status = HCHK_STATUS_INI;
1545 srv->check.server = srv;
1546 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1547
1548 srv->agent.status = HCHK_STATUS_INI;
1549 srv->agent.server = srv;
1550 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1551
1552 return srv;
Christopher Faulet40a007c2017-07-03 15:41:01 +02001553
1554 free_idle_conns:
1555 free(srv->idle_conns);
1556 free_priv_conns:
1557 free(srv->priv_conns);
1558 free_srv:
1559 free(srv);
1560 return NULL;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001561}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001562
1563/*
1564 * Validate <srv> server health-check settings.
1565 * Returns 0 if everything is OK, -1 if not.
1566 */
1567static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1568{
1569 struct tcpcheck_rule *r = NULL;
1570 struct list *l;
1571
1572 /*
1573 * We need at least a service port, a check port or the first tcp-check rule must
1574 * be a 'connect' one when checking an IPv4/IPv6 server.
1575 */
1576 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1577 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1578 return 0;
1579
1580 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1581 if (!r) {
1582 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1583 "Check has been disabled.\n",
1584 file, linenum, srv->id);
1585 return -1;
1586 }
1587
1588 /* search the first action (connect / send / expect) in the list */
1589 l = &srv->proxy->tcpcheck_rules;
1590 list_for_each_entry(r, l, list) {
1591 if (r->action != TCPCHK_ACT_COMMENT)
1592 break;
1593 }
1594
1595 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1596 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1597 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1598 file, linenum, srv->id);
1599 return -1;
1600 }
1601
1602 /* scan the tcp-check ruleset to ensure a port has been configured */
1603 l = &srv->proxy->tcpcheck_rules;
1604 list_for_each_entry(r, l, list) {
1605 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1606 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1607 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1608 file, linenum, srv->id);
1609 return -1;
1610 }
1611 }
1612
1613 return 0;
1614}
1615
1616/*
1617 * Initialize <srv> health-check structure.
1618 * Returns the error string in case of memory allocation failure, NULL if not.
1619 */
1620static const char *do_health_check_init(struct server *srv, int check_type, int state)
1621{
1622 const char *ret;
1623
1624 if (!srv->do_check)
1625 return NULL;
1626
1627 ret = init_check(&srv->check, check_type);
1628 if (ret)
1629 return ret;
1630
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001631 srv->check.state |= state;
1632 global.maxsock++;
1633
1634 return NULL;
1635}
1636
1637static int server_health_check_init(const char *file, int linenum,
1638 struct server *srv, struct proxy *curproxy)
1639{
1640 const char *ret;
1641
1642 if (!srv->do_check)
1643 return 0;
1644
1645 if (srv->trackit) {
1646 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1647 file, linenum);
1648 return ERR_ALERT | ERR_FATAL;
1649 }
1650
1651 if (server_healthcheck_validate(file, linenum, srv) < 0)
1652 return ERR_ALERT | ERR_ABORT;
1653
1654 /* note: check type will be set during the config review phase */
1655 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1656 if (ret) {
1657 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1658 return ERR_ALERT | ERR_ABORT;
1659 }
1660
1661 return 0;
1662}
1663
1664/*
1665 * Initialize <srv> agent check structure.
1666 * Returns the error string in case of memory allocation failure, NULL if not.
1667 */
1668static const char *do_server_agent_check_init(struct server *srv, int state)
1669{
1670 const char *ret;
1671
1672 if (!srv->do_agent)
1673 return NULL;
1674
1675 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1676 if (ret)
1677 return ret;
1678
1679 if (!srv->agent.inter)
1680 srv->agent.inter = srv->check.inter;
1681
1682 srv->agent.state |= state;
1683 global.maxsock++;
1684
1685 return NULL;
1686}
1687
1688static int server_agent_check_init(const char *file, int linenum,
1689 struct server *srv, struct proxy *curproxy)
1690{
1691 const char *ret;
1692
1693 if (!srv->do_agent)
1694 return 0;
1695
1696 if (!srv->agent.port) {
1697 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1698 file, linenum, srv->id);
1699 return ERR_ALERT | ERR_FATAL;
1700 }
1701
1702 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1703 if (ret) {
1704 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1705 return ERR_ALERT | ERR_ABORT;
1706 }
1707
1708 return 0;
1709}
1710
1711#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1712static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1713 struct server *srv, struct proxy *proxy)
1714{
1715 int ret;
1716 char *err = NULL;
1717
1718 if (!srv->sni_expr)
1719 return 0;
1720
1721 ret = server_parse_sni_expr(srv, proxy, &err);
1722 if (!ret)
1723 return 0;
1724
1725 display_parser_err(file, linenum, args, cur_arg, &err);
1726 free(err);
1727
1728 return ret;
1729}
1730#endif
1731
1732/*
1733 * Server initializations finalization.
1734 * Initialize health check, agent check and SNI expression if enabled.
1735 * Must not be called for a default server instance.
1736 */
1737static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1738 struct server *srv, struct proxy *px)
1739{
1740 int ret;
1741
1742 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1743 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1744 return ret;
1745 }
1746
1747#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1748 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1749 return ret;
1750#endif
1751
1752 if (srv->flags & SRV_F_BACKUP)
1753 px->srv_bck++;
1754 else
1755 px->srv_act++;
1756 srv_lb_commit_status(srv);
1757
1758 return 0;
1759}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001760
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001761/*
1762 * Parse as much as possible such a range string argument: low[-high]
1763 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1764 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1765 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1766 * Returns 0 if succeeded, -1 if not.
1767 */
1768static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1769{
1770 char *nb_high_arg;
1771
1772 *nb_high = 0;
1773 chunk_printf(&trash, "%s", arg);
1774 *nb_low = atoi(trash.str);
1775
1776 if ((nb_high_arg = strchr(trash.str, '-'))) {
1777 *nb_high_arg++ = '\0';
1778 *nb_high = atoi(nb_high_arg);
1779 }
1780 else {
1781 *nb_high += *nb_low;
1782 *nb_low = 1;
1783 }
1784
1785 if (*nb_low < 0 || *nb_high < *nb_low)
1786 return -1;
1787
1788 return 0;
1789}
1790
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001791static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1792{
1793 chunk_printf(&trash, "%s%d", prefix, nb);
1794 free(srv->id);
1795 srv->id = strdup(trash.str);
1796}
1797
1798/*
1799 * Initialize as much as possible servers from <srv> server template.
1800 * Note that a server template is a special server with
1801 * a few different parameters than a server which has
1802 * been parsed mostly the same way as a server.
1803 * Returns the number of servers succesfully allocated,
1804 * 'srv' template included.
1805 */
1806static int server_template_init(struct server *srv, struct proxy *px)
1807{
1808 int i;
1809 struct server *newsrv;
1810
1811 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1812 int check_init_state;
1813 int agent_init_state;
1814
1815 newsrv = new_server(px);
1816 if (!newsrv)
1817 goto err;
1818
1819 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001820 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001821#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1822 if (newsrv->sni_expr) {
1823 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1824 if (!newsrv->ssl_ctx.sni)
1825 goto err;
1826 }
1827#endif
1828 /* Set this new server ID. */
1829 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1830
1831 /* Initial checks states. */
1832 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1833 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1834
1835 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1836 do_server_agent_check_init(newsrv, agent_init_state))
1837 goto err;
1838
1839 /* Linked backwards first. This will be restablished after parsing. */
1840 newsrv->next = px->srv;
1841 px->srv = newsrv;
1842 }
1843 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1844
1845 return i - srv->tmpl_info.nb_low;
1846
1847 err:
1848 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1849 if (newsrv) {
1850#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1851 release_sample_expr(newsrv->ssl_ctx.sni);
1852#endif
1853 free_check(&newsrv->agent);
1854 free_check(&newsrv->check);
1855 }
1856 free(newsrv);
1857 return i - srv->tmpl_info.nb_low;
1858}
1859
Willy Tarreau272adea2014-03-31 10:39:59 +02001860int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1861{
1862 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001863 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001864 char *errmsg = NULL;
1865 int err_code = 0;
1866 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001867 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001868
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001869 if (!strcmp(args[0], "server") ||
1870 !strcmp(args[0], "default-server") ||
1871 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001872 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001873 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001874 int srv = !defsrv && !strcmp(args[0], "server");
1875 int srv_tmpl = !defsrv && !srv;
1876 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001877
1878 if (!defsrv && curproxy == defproxy) {
1879 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1880 err_code |= ERR_ALERT | ERR_FATAL;
1881 goto out;
1882 }
1883 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1884 err_code |= ERR_ALERT | ERR_FATAL;
1885
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001886 /* There is no mandatory first arguments for default server. */
1887 if (srv) {
1888 if (!*args[2]) {
1889 /* 'server' line number of argument check. */
1890 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1891 file, linenum, args[0]);
1892 err_code |= ERR_ALERT | ERR_FATAL;
1893 goto out;
1894 }
1895
1896 err = invalid_char(args[1]);
1897 }
1898 else if (srv_tmpl) {
1899 if (!*args[3]) {
1900 /* 'server-template' line number of argument check. */
1901 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1902 file, linenum, args[0]);
1903 err_code |= ERR_ALERT | ERR_FATAL;
1904 goto out;
1905 }
1906
1907 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001908 }
1909
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001910 if (err) {
1911 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1912 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001913 err_code |= ERR_ALERT | ERR_FATAL;
1914 goto out;
1915 }
1916
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001917 cur_arg = 2;
1918 if (srv_tmpl) {
1919 /* Parse server-template <nb | range> arg. */
1920 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1921 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1922 file, linenum, args[0], args[cur_arg]);
1923 err_code |= ERR_ALERT | ERR_FATAL;
1924 goto out;
1925 }
1926 cur_arg++;
1927 }
1928
Willy Tarreau272adea2014-03-31 10:39:59 +02001929 if (!defsrv) {
1930 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001931 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001932 struct protocol *proto;
1933
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001934 newsrv = new_server(curproxy);
1935 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001936 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1937 err_code |= ERR_ALERT | ERR_ABORT;
1938 goto out;
1939 }
1940
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001941 if (srv_tmpl) {
1942 newsrv->tmpl_info.nb_low = tmpl_range_low;
1943 newsrv->tmpl_info.nb_high = tmpl_range_high;
1944 }
1945
Willy Tarreau272adea2014-03-31 10:39:59 +02001946 /* the servers are linked backwards first */
1947 newsrv->next = curproxy->srv;
1948 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 newsrv->conf.file = strdup(file);
1950 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001951 /* Note: for a server template, its id is its prefix.
1952 * This is a temporary id which will be used for server allocations to come
1953 * after parsing.
1954 */
1955 if (srv)
1956 newsrv->id = strdup(args[1]);
1957 else
1958 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001959
1960 /* several ways to check the port component :
1961 * - IP => port=+0, relative (IPv4 only)
1962 * - IP: => port=+0, relative
1963 * - IP:N => port=N, absolute
1964 * - IP:+N => port=+N, relative
1965 * - IP:-N => port=-N, relative
1966 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001967 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001968 if (!sk) {
1969 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1970 err_code |= ERR_ALERT | ERR_FATAL;
1971 goto out;
1972 }
1973
1974 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001975 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001976 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1977 file, linenum, args[0], args[1]);
1978 err_code |= ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
1981
1982 if (!port1 || !port2) {
1983 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001984 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001985 }
1986 else if (port1 != port2) {
1987 /* port range */
1988 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1989 file, linenum, args[0], args[1], args[2]);
1990 err_code |= ERR_ALERT | ERR_FATAL;
1991 goto out;
1992 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001993
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001994 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001995 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001996 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001997 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001998 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
1999 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
2000 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002001 err_code |= ERR_ALERT | ERR_FATAL;
2002 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02002003 }
2004 }
2005 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2006 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2007 file, linenum, newsrv->id);
2008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002010 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002011 }
2012
Willy Tarreau272adea2014-03-31 10:39:59 +02002013 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002014 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002015
Olivier Houchard8da5f982017-08-04 18:35:36 +02002016 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002017 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002018 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 err_code |= ERR_ALERT | ERR_FATAL;
2020 goto out;
2021 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002022
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002023 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002024 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Christopher Faulet29f77e82017-06-08 14:04:45 +02002025 SPIN_INIT(&newsrv->lock);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002026 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002027 } else {
2028 newsrv = &curproxy->defsrv;
2029 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002030 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002031 }
2032
2033 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002034 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002035 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2036 if (err) {
2037 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2038 file, linenum, *err, newsrv->id);
2039 err_code |= ERR_ALERT | ERR_FATAL;
2040 goto out;
2041 }
2042 if (val <= 0) {
2043 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2044 file, linenum, val, args[cur_arg], newsrv->id);
2045 err_code |= ERR_ALERT | ERR_FATAL;
2046 goto out;
2047 }
2048 newsrv->agent.inter = val;
2049 cur_arg += 2;
2050 }
Misiekea849332017-01-09 09:39:51 +01002051 else if (!strcmp(args[cur_arg], "agent-addr")) {
2052 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2053 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2054 goto out;
2055 }
2056
2057 cur_arg += 2;
2058 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002059 else if (!strcmp(args[cur_arg], "agent-port")) {
2060 global.maxsock++;
2061 newsrv->agent.port = atol(args[cur_arg + 1]);
2062 cur_arg += 2;
2063 }
James Brown55f9ff12015-10-21 18:19:05 -07002064 else if (!strcmp(args[cur_arg], "agent-send")) {
2065 global.maxsock++;
2066 free(newsrv->agent.send_string);
2067 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2068 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2069 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2070 cur_arg += 2;
2071 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002072 else if (!strcmp(args[cur_arg], "init-addr")) {
2073 char *p, *end;
2074 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002075 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002076
2077 newsrv->init_addr_methods = 0;
2078 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2079
2080 for (p = args[cur_arg + 1]; *p; p = end) {
2081 /* cut on next comma */
2082 for (end = p; *end && *end != ','; end++);
2083 if (*end)
2084 *(end++) = 0;
2085
Willy Tarreau4310d362016-11-02 15:05:56 +01002086 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002087 if (!strcmp(p, "libc")) {
2088 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2089 }
2090 else if (!strcmp(p, "last")) {
2091 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2092 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002093 else if (!strcmp(p, "none")) {
2094 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2095 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002096 else if (str2ip2(p, &sa, 0)) {
2097 if (is_addr(&newsrv->init_addr)) {
2098 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2099 file, linenum, args[cur_arg], p);
2100 err_code |= ERR_ALERT | ERR_FATAL;
2101 goto out;
2102 }
2103 newsrv->init_addr = sa;
2104 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2105 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002106 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002107 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002108 file, linenum, args[cur_arg], p);
2109 err_code |= ERR_ALERT | ERR_FATAL;
2110 goto out;
2111 }
2112 if (!done) {
2113 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2114 file, linenum, args[cur_arg], p);
2115 err_code |= ERR_ALERT | ERR_FATAL;
2116 goto out;
2117 }
2118 }
2119 cur_arg += 2;
2120 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002121 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002122 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002123 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2124 cur_arg += 2;
2125 }
2126 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2127 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002128 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002129 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002130 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002131 else {
2132 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2133 file, linenum, args[cur_arg]);
2134 err_code |= ERR_ALERT | ERR_FATAL;
2135 goto out;
2136 }
2137 cur_arg += 2;
2138 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002139 else if (!strcmp(args[cur_arg], "resolve-net")) {
2140 char *p, *e;
2141 unsigned char mask;
2142 struct dns_options *opt;
2143
2144 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2145 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2146 file, linenum, args[cur_arg]);
2147 err_code |= ERR_ALERT | ERR_FATAL;
2148 goto out;
2149 }
2150
2151 opt = &newsrv->dns_opts;
2152
2153 /* Split arguments by comma, and convert it from ipv4 or ipv6
2154 * string network in in_addr or in6_addr.
2155 */
2156 p = args[cur_arg + 1];
2157 e = p;
2158 while (*p != '\0') {
2159 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002160 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002161 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2162 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2163 err_code |= ERR_ALERT | ERR_FATAL;
2164 goto out;
2165 }
2166 /* look for end or comma. */
2167 while (*e != ',' && *e != '\0')
2168 e++;
2169 if (*e == ',') {
2170 *e = '\0';
2171 e++;
2172 }
2173 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2174 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2175 /* Try to convert input string from ipv4 or ipv6 network. */
2176 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2177 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2178 &mask)) {
2179 /* Try to convert input string from ipv6 network. */
2180 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2181 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2182 } else {
2183 /* All network conversions fail, retrun error. */
2184 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2185 file, linenum, args[cur_arg], p);
2186 err_code |= ERR_ALERT | ERR_FATAL;
2187 goto out;
2188 }
2189 opt->pref_net_nb++;
2190 p = e;
2191 }
2192
2193 cur_arg += 2;
2194 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002195 else if (!strcmp(args[cur_arg], "rise")) {
2196 if (!*args[cur_arg + 1]) {
2197 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2198 file, linenum, args[cur_arg]);
2199 err_code |= ERR_ALERT | ERR_FATAL;
2200 goto out;
2201 }
2202
2203 newsrv->check.rise = atol(args[cur_arg + 1]);
2204 if (newsrv->check.rise <= 0) {
2205 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2206 file, linenum, args[cur_arg]);
2207 err_code |= ERR_ALERT | ERR_FATAL;
2208 goto out;
2209 }
2210
2211 if (newsrv->check.health)
2212 newsrv->check.health = newsrv->check.rise;
2213 cur_arg += 2;
2214 }
2215 else if (!strcmp(args[cur_arg], "fall")) {
2216 newsrv->check.fall = atol(args[cur_arg + 1]);
2217
2218 if (!*args[cur_arg + 1]) {
2219 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2220 file, linenum, args[cur_arg]);
2221 err_code |= ERR_ALERT | ERR_FATAL;
2222 goto out;
2223 }
2224
2225 if (newsrv->check.fall <= 0) {
2226 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2227 file, linenum, args[cur_arg]);
2228 err_code |= ERR_ALERT | ERR_FATAL;
2229 goto out;
2230 }
2231
2232 cur_arg += 2;
2233 }
2234 else if (!strcmp(args[cur_arg], "inter")) {
2235 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2236 if (err) {
2237 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2238 file, linenum, *err, newsrv->id);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
2241 }
2242 if (val <= 0) {
2243 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2244 file, linenum, val, args[cur_arg], newsrv->id);
2245 err_code |= ERR_ALERT | ERR_FATAL;
2246 goto out;
2247 }
2248 newsrv->check.inter = val;
2249 cur_arg += 2;
2250 }
2251 else if (!strcmp(args[cur_arg], "fastinter")) {
2252 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2253 if (err) {
2254 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2255 file, linenum, *err, newsrv->id);
2256 err_code |= ERR_ALERT | ERR_FATAL;
2257 goto out;
2258 }
2259 if (val <= 0) {
2260 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2261 file, linenum, val, args[cur_arg], newsrv->id);
2262 err_code |= ERR_ALERT | ERR_FATAL;
2263 goto out;
2264 }
2265 newsrv->check.fastinter = val;
2266 cur_arg += 2;
2267 }
2268 else if (!strcmp(args[cur_arg], "downinter")) {
2269 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2270 if (err) {
2271 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2272 file, linenum, *err, newsrv->id);
2273 err_code |= ERR_ALERT | ERR_FATAL;
2274 goto out;
2275 }
2276 if (val <= 0) {
2277 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2278 file, linenum, val, args[cur_arg], newsrv->id);
2279 err_code |= ERR_ALERT | ERR_FATAL;
2280 goto out;
2281 }
2282 newsrv->check.downinter = val;
2283 cur_arg += 2;
2284 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002285 else if (!strcmp(args[cur_arg], "port")) {
2286 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002287 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002288 cur_arg += 2;
2289 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002290 else if (!strcmp(args[cur_arg], "weight")) {
2291 int w;
2292 w = atol(args[cur_arg + 1]);
2293 if (w < 0 || w > SRV_UWGHT_MAX) {
2294 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2295 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2296 err_code |= ERR_ALERT | ERR_FATAL;
2297 goto out;
2298 }
2299 newsrv->uweight = newsrv->iweight = w;
2300 cur_arg += 2;
2301 }
2302 else if (!strcmp(args[cur_arg], "minconn")) {
2303 newsrv->minconn = atol(args[cur_arg + 1]);
2304 cur_arg += 2;
2305 }
2306 else if (!strcmp(args[cur_arg], "maxconn")) {
2307 newsrv->maxconn = atol(args[cur_arg + 1]);
2308 cur_arg += 2;
2309 }
2310 else if (!strcmp(args[cur_arg], "maxqueue")) {
2311 newsrv->maxqueue = atol(args[cur_arg + 1]);
2312 cur_arg += 2;
2313 }
2314 else if (!strcmp(args[cur_arg], "slowstart")) {
2315 /* slowstart is stored in seconds */
2316 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2317 if (err) {
2318 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2319 file, linenum, *err, newsrv->id);
2320 err_code |= ERR_ALERT | ERR_FATAL;
2321 goto out;
2322 }
2323 newsrv->slowstart = (val + 999) / 1000;
2324 cur_arg += 2;
2325 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002326 else if (!strcmp(args[cur_arg], "on-error")) {
2327 if (!strcmp(args[cur_arg + 1], "fastinter"))
2328 newsrv->onerror = HANA_ONERR_FASTINTER;
2329 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2330 newsrv->onerror = HANA_ONERR_FAILCHK;
2331 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2332 newsrv->onerror = HANA_ONERR_SUDDTH;
2333 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2334 newsrv->onerror = HANA_ONERR_MARKDWN;
2335 else {
2336 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2337 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2338 file, linenum, args[cur_arg], args[cur_arg + 1]);
2339 err_code |= ERR_ALERT | ERR_FATAL;
2340 goto out;
2341 }
2342
2343 cur_arg += 2;
2344 }
2345 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2346 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2347 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2348 else {
2349 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2350 file, linenum, args[cur_arg], args[cur_arg + 1]);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354
2355 cur_arg += 2;
2356 }
2357 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2358 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2359 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2360 else {
2361 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2362 file, linenum, args[cur_arg], args[cur_arg + 1]);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366
2367 cur_arg += 2;
2368 }
2369 else if (!strcmp(args[cur_arg], "error-limit")) {
2370 if (!*args[cur_arg + 1]) {
2371 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2372 file, linenum, args[cur_arg]);
2373 err_code |= ERR_ALERT | ERR_FATAL;
2374 goto out;
2375 }
2376
2377 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2378
2379 if (newsrv->consecutive_errors_limit <= 0) {
2380 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2381 file, linenum, args[cur_arg]);
2382 err_code |= ERR_ALERT | ERR_FATAL;
2383 goto out;
2384 }
2385 cur_arg += 2;
2386 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002387 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002388 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2389 file, linenum, "usesrc", "source");
2390 err_code |= ERR_ALERT | ERR_FATAL;
2391 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002392 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002393 else {
2394 static int srv_dumped;
2395 struct srv_kw *kw;
2396 char *err;
2397
2398 kw = srv_find_kw(args[cur_arg]);
2399 if (kw) {
2400 char *err = NULL;
2401 int code;
2402
2403 if (!kw->parse) {
2404 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2405 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002406 if (kw->skip != -1)
2407 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002408 err_code |= ERR_ALERT | ERR_FATAL;
2409 goto out;
2410 }
2411
2412 if (defsrv && !kw->default_ok) {
2413 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2414 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002415 if (kw->skip != -1)
2416 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002417 err_code |= ERR_ALERT;
2418 continue;
2419 }
2420
2421 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2422 err_code |= code;
2423
2424 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002425 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 if (code & ERR_FATAL) {
2427 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002428 if (kw->skip != -1)
2429 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002430 goto out;
2431 }
2432 }
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 continue;
2437 }
2438
2439 err = NULL;
2440 if (!srv_dumped) {
2441 srv_dump_kws(&err);
2442 indent_msg(&err, 4);
2443 srv_dumped = 1;
2444 }
2445
2446 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2447 file, linenum, args[0], args[1], args[cur_arg],
2448 err ? " Registered keywords :" : "", err ? err : "");
2449 free(err);
2450
2451 err_code |= ERR_ALERT | ERR_FATAL;
2452 goto out;
2453 }
2454 }
2455
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002456 if (!defsrv)
2457 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2458 if (err_code & ERR_FATAL)
2459 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002460 if (srv_tmpl)
2461 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002462 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002463 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002464 return 0;
2465
2466 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002467 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002468 free(errmsg);
2469 return err_code;
2470}
2471
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002472/* Returns a pointer to the first server matching either id <id>.
2473 * NULL is returned if no match is found.
2474 * the lookup is performed in the backend <bk>
2475 */
2476struct server *server_find_by_id(struct proxy *bk, int id)
2477{
2478 struct eb32_node *eb32;
2479 struct server *curserver;
2480
2481 if (!bk || (id ==0))
2482 return NULL;
2483
2484 /* <bk> has no backend capabilities, so it can't have a server */
2485 if (!(bk->cap & PR_CAP_BE))
2486 return NULL;
2487
2488 curserver = NULL;
2489
2490 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2491 if (eb32)
2492 curserver = container_of(eb32, struct server, conf.id);
2493
2494 return curserver;
2495}
2496
2497/* Returns a pointer to the first server matching either name <name>, or id
2498 * if <name> starts with a '#'. NULL is returned if no match is found.
2499 * the lookup is performed in the backend <bk>
2500 */
2501struct server *server_find_by_name(struct proxy *bk, const char *name)
2502{
2503 struct server *curserver;
2504
2505 if (!bk || !name)
2506 return NULL;
2507
2508 /* <bk> has no backend capabilities, so it can't have a server */
2509 if (!(bk->cap & PR_CAP_BE))
2510 return NULL;
2511
2512 curserver = NULL;
2513 if (*name == '#') {
2514 curserver = server_find_by_id(bk, atoi(name + 1));
2515 if (curserver)
2516 return curserver;
2517 }
2518 else {
2519 curserver = bk->srv;
2520
2521 while (curserver && (strcmp(curserver->id, name) != 0))
2522 curserver = curserver->next;
2523
2524 if (curserver)
2525 return curserver;
2526 }
2527
2528 return NULL;
2529}
2530
2531struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2532{
2533 struct server *byname;
2534 struct server *byid;
2535
2536 if (!name && !id)
2537 return NULL;
2538
2539 if (diff)
2540 *diff = 0;
2541
2542 byname = byid = NULL;
2543
2544 if (name) {
2545 byname = server_find_by_name(bk, name);
2546 if (byname && (!id || byname->puid == id))
2547 return byname;
2548 }
2549
2550 /* remaining possibilities :
2551 * - name not set
2552 * - name set but not found
2553 * - name found but ID doesn't match
2554 */
2555 if (id) {
2556 byid = server_find_by_id(bk, id);
2557 if (byid) {
2558 if (byname) {
2559 /* use id only if forced by configuration */
2560 if (byid->flags & SRV_F_FORCED_ID) {
2561 if (diff)
2562 *diff |= 2;
2563 return byid;
2564 }
2565 else {
2566 if (diff)
2567 *diff |= 1;
2568 return byname;
2569 }
2570 }
2571
2572 /* remaining possibilities:
2573 * - name not set
2574 * - name set but not found
2575 */
2576 if (name && diff)
2577 *diff |= 2;
2578 return byid;
2579 }
2580
2581 /* id bot found */
2582 if (byname) {
2583 if (diff)
2584 *diff |= 1;
2585 return byname;
2586 }
2587 }
2588
2589 return NULL;
2590}
2591
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002592/* Update a server state using the parameters available in the params list */
2593static void srv_update_state(struct server *srv, int version, char **params)
2594{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002595 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002596 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002597
2598 /* fields since version 1
2599 * and common to all other upcoming versions
2600 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002601 enum srv_state srv_op_state;
2602 enum srv_admin srv_admin_state;
2603 unsigned srv_uweight, srv_iweight;
2604 unsigned long srv_last_time_change;
2605 short srv_check_status;
2606 enum chk_result srv_check_result;
2607 int srv_check_health;
2608 int srv_check_state, srv_agent_state;
2609 int bk_f_forced_id;
2610 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002611 int fqdn_set_by_cli;
2612 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002613 const char *port_str;
2614 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002615
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002616 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002617 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002618 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002619 switch (version) {
2620 case 1:
2621 /*
2622 * now we can proceed with server's state update:
2623 * srv_addr: params[0]
2624 * srv_op_state: params[1]
2625 * srv_admin_state: params[2]
2626 * srv_uweight: params[3]
2627 * srv_iweight: params[4]
2628 * srv_last_time_change: params[5]
2629 * srv_check_status: params[6]
2630 * srv_check_result: params[7]
2631 * srv_check_health: params[8]
2632 * srv_check_state: params[9]
2633 * srv_agent_state: params[10]
2634 * bk_f_forced_id: params[11]
2635 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002636 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002637 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002638 */
2639
2640 /* validating srv_op_state */
2641 p = NULL;
2642 errno = 0;
2643 srv_op_state = strtol(params[1], &p, 10);
2644 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2645 (srv_op_state != SRV_ST_STOPPED &&
2646 srv_op_state != SRV_ST_STARTING &&
2647 srv_op_state != SRV_ST_RUNNING &&
2648 srv_op_state != SRV_ST_STOPPING)) {
2649 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2650 }
2651
2652 /* validating srv_admin_state */
2653 p = NULL;
2654 errno = 0;
2655 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002656 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002657
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002658 /* inherited statuses will be recomputed later.
2659 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2660 */
2661 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002662
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002663 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2664 (srv_admin_state != 0 &&
2665 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002666 srv_admin_state != SRV_ADMF_CMAINT &&
2667 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002668 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002669 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002670 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2671 }
2672
2673 /* validating srv_uweight */
2674 p = NULL;
2675 errno = 0;
2676 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002677 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002678 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2679
2680 /* validating srv_iweight */
2681 p = NULL;
2682 errno = 0;
2683 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002684 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002685 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2686
2687 /* validating srv_last_time_change */
2688 p = NULL;
2689 errno = 0;
2690 srv_last_time_change = strtol(params[5], &p, 10);
2691 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2692 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2693
2694 /* validating srv_check_status */
2695 p = NULL;
2696 errno = 0;
2697 srv_check_status = strtol(params[6], &p, 10);
2698 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2699 (srv_check_status >= HCHK_STATUS_SIZE))
2700 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2701
2702 /* validating srv_check_result */
2703 p = NULL;
2704 errno = 0;
2705 srv_check_result = strtol(params[7], &p, 10);
2706 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2707 (srv_check_result != CHK_RES_UNKNOWN &&
2708 srv_check_result != CHK_RES_NEUTRAL &&
2709 srv_check_result != CHK_RES_FAILED &&
2710 srv_check_result != CHK_RES_PASSED &&
2711 srv_check_result != CHK_RES_CONDPASS)) {
2712 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2713 }
2714
2715 /* validating srv_check_health */
2716 p = NULL;
2717 errno = 0;
2718 srv_check_health = strtol(params[8], &p, 10);
2719 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2720 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2721
2722 /* validating srv_check_state */
2723 p = NULL;
2724 errno = 0;
2725 srv_check_state = strtol(params[9], &p, 10);
2726 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2727 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2728 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2729
2730 /* validating srv_agent_state */
2731 p = NULL;
2732 errno = 0;
2733 srv_agent_state = strtol(params[10], &p, 10);
2734 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2735 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2736 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2737
2738 /* validating bk_f_forced_id */
2739 p = NULL;
2740 errno = 0;
2741 bk_f_forced_id = strtol(params[11], &p, 10);
2742 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2743 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2744
2745 /* validating srv_f_forced_id */
2746 p = NULL;
2747 errno = 0;
2748 srv_f_forced_id = strtol(params[12], &p, 10);
2749 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2750 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2751
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002752 /* validating srv_fqdn */
2753 fqdn = params[13];
2754 if (fqdn && *fqdn == '-')
2755 fqdn = NULL;
2756 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2757 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2758 fqdn = NULL;
2759 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002760
Frédéric Lécaille31694712017-08-01 08:47:19 +02002761 port_str = params[14];
2762 if (port_str) {
2763 port = strl2uic(port_str, strlen(port_str));
2764 if (port > USHRT_MAX) {
2765 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2766 port_str = NULL;
2767 }
2768 }
2769
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002770 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002771 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002772 goto out;
2773
2774 /* recover operational state and apply it to this server
2775 * and all servers tracking this one */
2776 switch (srv_op_state) {
2777 case SRV_ST_STOPPED:
2778 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002779 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002780 break;
2781 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002782 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002783 break;
2784 case SRV_ST_STOPPING:
2785 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002786 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002787 break;
2788 case SRV_ST_RUNNING:
2789 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002790 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002791 break;
2792 }
2793
2794 /* When applying server state, the following rules apply:
2795 * - in case of a configuration change, we apply the setting from the new
2796 * configuration, regardless of old running state
2797 * - if no configuration change, we apply old running state only if old running
2798 * state is different from new configuration state
2799 */
2800 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002801 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2802 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002803 srv_adm_set_maint(srv);
2804 else
2805 srv_adm_set_ready(srv);
2806 }
2807 /* configuration is the same, let's compate old running state and new conf state */
2808 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002809 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002810 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002811 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002812 srv_adm_set_ready(srv);
2813 }
2814 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002815 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002816 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002817 * (srv->iweight is the weight set up in configuration).
2818 * There are two possible reasons for FDRAIN to have been present :
2819 * - previous config weight was zero
2820 * - "set server b/s drain" was sent to the CLI
2821 *
2822 * In the first case, we simply want to drop this drain state
2823 * if the new weight is not zero anymore, meaning the administrator
2824 * has intentionally turned the weight back to a positive value to
2825 * enable the server again after an operation. In the second case,
2826 * the drain state was forced on the CLI regardless of the config's
2827 * weight so we don't want a change to the config weight to lose this
2828 * status. What this means is :
2829 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2830 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002831 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002832 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002833 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002834 }
2835
2836 srv->last_change = date.tv_sec - srv_last_time_change;
2837 srv->check.status = srv_check_status;
2838 srv->check.result = srv_check_result;
2839 srv->check.health = srv_check_health;
2840
2841 /* Only case we want to apply is removing ENABLED flag which could have been
2842 * done by the "disable health" command over the stats socket
2843 */
2844 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2845 (srv_check_state & CHK_ST_CONFIGURED) &&
2846 !(srv_check_state & CHK_ST_ENABLED))
2847 srv->check.state &= ~CHK_ST_ENABLED;
2848
2849 /* Only case we want to apply is removing ENABLED flag which could have been
2850 * done by the "disable agent" command over the stats socket
2851 */
2852 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2853 (srv_agent_state & CHK_ST_CONFIGURED) &&
2854 !(srv_agent_state & CHK_ST_ENABLED))
2855 srv->agent.state &= ~CHK_ST_ENABLED;
2856
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002857 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2858 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002859 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002860 * It means that a configuration file change has precedence over a unix socket change
2861 * for server's weight
2862 *
2863 * by default, HAProxy applies the following weight when parsing the configuration
2864 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002865 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002866 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 srv->uweight = srv_uweight;
2868 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002869 server_recalc_eweight(srv);
2870
Willy Tarreaue5a60682016-11-09 14:54:53 +01002871 /* load server IP address */
2872 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002873
2874 if (fqdn && srv->hostname) {
2875 if (!strcmp(srv->hostname, fqdn)) {
2876 /* Here we reset the 'set from stats socket FQDN' flag
2877 * to support such transitions:
2878 * Let's say initial FQDN value is foo1 (in configuration file).
2879 * - FQDN changed from stats socket, from foo1 to foo2 value,
2880 * - FQDN changed again from file configuration (with the same previous value
2881 set from stats socket, from foo1 to foo2 value),
2882 * - reload for any other reason than a FQDN modification,
2883 * the configuration file FQDN matches the fqdn server state file value.
2884 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2885 * any futher FQDN modification.
2886 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002887 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002888 }
2889 else {
2890 /* If the FDQN has been changed from stats socket,
2891 * apply fqdn state file value (which is the value set
2892 * from stats socket).
2893 */
2894 if (fqdn_set_by_cli) {
2895 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02002896 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002897 }
2898 }
2899 }
2900
Frédéric Lécaille31694712017-08-01 08:47:19 +02002901 if (port_str)
2902 srv->svc_port = port;
2903
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002904 break;
2905 default:
2906 chunk_appendf(msg, ", version '%d' not supported", version);
2907 }
2908
2909 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002910 if (msg->len) {
2911 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002912 Warning("server-state application failed for server '%s/%s'%s",
2913 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002914 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002915}
2916
2917/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2918 * For each proxy, it does the following:
2919 * - opens its server state file (either one or local one)
2920 * - read whole file, line by line
2921 * - analyse each line to check if it matches our current backend:
2922 * - backend name matches
2923 * - backend id matches if id is forced and name doesn't match
2924 * - if the server pointed by the line is found, then state is applied
2925 *
2926 * If the running backend uuid or id differs from the state file, then HAProxy reports
2927 * a warning.
2928 */
2929void apply_server_state(void)
2930{
2931 char *cur, *end;
2932 char mybuf[SRV_STATE_LINE_MAXLEN];
2933 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002934 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2935 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002936 int arg, srv_arg, version, diff;
2937 FILE *f;
2938 char *filepath;
2939 char globalfilepath[MAXPATHLEN + 1];
2940 char localfilepath[MAXPATHLEN + 1];
2941 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2942 extern struct proxy *proxy;
2943 struct proxy *curproxy, *bk;
2944 struct server *srv;
2945
2946 globalfilepathlen = 0;
2947 /* create the globalfilepath variable */
2948 if (global.server_state_file) {
2949 /* absolute path or no base directory provided */
2950 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2951 len = strlen(global.server_state_file);
2952 if (len > MAXPATHLEN) {
2953 globalfilepathlen = 0;
2954 goto globalfileerror;
2955 }
2956 memcpy(globalfilepath, global.server_state_file, len);
2957 globalfilepath[len] = '\0';
2958 globalfilepathlen = len;
2959 }
2960 else if (global.server_state_base) {
2961 len = strlen(global.server_state_base);
2962 globalfilepathlen += len;
2963
2964 if (globalfilepathlen > MAXPATHLEN) {
2965 globalfilepathlen = 0;
2966 goto globalfileerror;
2967 }
2968 strncpy(globalfilepath, global.server_state_base, len);
2969 globalfilepath[globalfilepathlen] = 0;
2970
2971 /* append a slash if needed */
2972 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2973 if (globalfilepathlen + 1 > MAXPATHLEN) {
2974 globalfilepathlen = 0;
2975 goto globalfileerror;
2976 }
2977 globalfilepath[globalfilepathlen++] = '/';
2978 }
2979
2980 len = strlen(global.server_state_file);
2981 if (globalfilepathlen + len > MAXPATHLEN) {
2982 globalfilepathlen = 0;
2983 goto globalfileerror;
2984 }
2985 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2986 globalfilepathlen += len;
2987 globalfilepath[globalfilepathlen++] = 0;
2988 }
2989 }
2990 globalfileerror:
2991 if (globalfilepathlen == 0)
2992 globalfilepath[0] = '\0';
2993
2994 /* read servers state from local file */
2995 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2996 /* servers are only in backends */
2997 if (!(curproxy->cap & PR_CAP_BE))
2998 continue;
2999 fileopenerr = 0;
3000 filepath = NULL;
3001
3002 /* search server state file path and name */
3003 switch (curproxy->load_server_state_from_file) {
3004 /* read servers state from global file */
3005 case PR_SRV_STATE_FILE_GLOBAL:
3006 /* there was an error while generating global server state file path */
3007 if (globalfilepathlen == 0)
3008 continue;
3009 filepath = globalfilepath;
3010 fileopenerr = 1;
3011 break;
3012 /* this backend has its own file */
3013 case PR_SRV_STATE_FILE_LOCAL:
3014 localfilepathlen = 0;
3015 localfilepath[0] = '\0';
3016 len = 0;
3017 /* create the localfilepath variable */
3018 /* absolute path or no base directory provided */
3019 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3020 len = strlen(curproxy->server_state_file_name);
3021 if (len > MAXPATHLEN) {
3022 localfilepathlen = 0;
3023 goto localfileerror;
3024 }
3025 memcpy(localfilepath, curproxy->server_state_file_name, len);
3026 localfilepath[len] = '\0';
3027 localfilepathlen = len;
3028 }
3029 else if (global.server_state_base) {
3030 len = strlen(global.server_state_base);
3031 localfilepathlen += len;
3032
3033 if (localfilepathlen > MAXPATHLEN) {
3034 localfilepathlen = 0;
3035 goto localfileerror;
3036 }
3037 strncpy(localfilepath, global.server_state_base, len);
3038 localfilepath[localfilepathlen] = 0;
3039
3040 /* append a slash if needed */
3041 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3042 if (localfilepathlen + 1 > MAXPATHLEN) {
3043 localfilepathlen = 0;
3044 goto localfileerror;
3045 }
3046 localfilepath[localfilepathlen++] = '/';
3047 }
3048
3049 len = strlen(curproxy->server_state_file_name);
3050 if (localfilepathlen + len > MAXPATHLEN) {
3051 localfilepathlen = 0;
3052 goto localfileerror;
3053 }
3054 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3055 localfilepathlen += len;
3056 localfilepath[localfilepathlen++] = 0;
3057 }
3058 filepath = localfilepath;
3059 localfileerror:
3060 if (localfilepathlen == 0)
3061 localfilepath[0] = '\0';
3062
3063 break;
3064 case PR_SRV_STATE_FILE_NONE:
3065 default:
3066 continue;
3067 }
3068
3069 /* preload global state file */
3070 errno = 0;
3071 f = fopen(filepath, "r");
3072 if (errno && fileopenerr)
3073 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3074 if (!f)
3075 continue;
3076
3077 mybuf[0] = '\0';
3078 mybuflen = 0;
3079 version = 0;
3080
3081 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003082 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3083 Warning("Can't read first line of the server state file '%s'\n", filepath);
3084 goto fileclose;
3085 }
3086
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003087 cur = mybuf;
3088 version = atoi(cur);
3089 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3090 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003091 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003092
3093 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3094 int bk_f_forced_id = 0;
3095 int check_id = 0;
3096 int check_name = 0;
3097
3098 mybuflen = strlen(mybuf);
3099 cur = mybuf;
3100 end = cur + mybuflen;
3101
3102 bk = NULL;
3103 srv = NULL;
3104
3105 /* we need at least one character */
3106 if (mybuflen == 0)
3107 continue;
3108
3109 /* ignore blank characters at the beginning of the line */
3110 while (isspace(*cur))
3111 ++cur;
3112
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003113 /* Ignore empty or commented lines */
3114 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003115 continue;
3116
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003117 /* truncated lines */
3118 if (mybuf[mybuflen - 1] != '\n') {
3119 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003120 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003121 }
3122
3123 /* Removes trailing '\n' */
3124 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003125
3126 /* we're now ready to move the line into *srv_params[] */
3127 params[0] = cur;
3128 arg = 1;
3129 srv_arg = 0;
3130 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3131 if (isspace(*cur)) {
3132 *cur = '\0';
3133 ++cur;
3134 while (isspace(*cur))
3135 ++cur;
3136 switch (version) {
3137 case 1:
3138 /*
3139 * srv_addr: params[4] => srv_params[0]
3140 * srv_op_state: params[5] => srv_params[1]
3141 * srv_admin_state: params[6] => srv_params[2]
3142 * srv_uweight: params[7] => srv_params[3]
3143 * srv_iweight: params[8] => srv_params[4]
3144 * srv_last_time_change: params[9] => srv_params[5]
3145 * srv_check_status: params[10] => srv_params[6]
3146 * srv_check_result: params[11] => srv_params[7]
3147 * srv_check_health: params[12] => srv_params[8]
3148 * srv_check_state: params[13] => srv_params[9]
3149 * srv_agent_state: params[14] => srv_params[10]
3150 * bk_f_forced_id: params[15] => srv_params[11]
3151 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003152 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003153 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003154 */
3155 if (arg >= 4) {
3156 srv_params[srv_arg] = cur;
3157 ++srv_arg;
3158 }
3159 break;
3160 }
3161
3162 params[arg] = cur;
3163 ++arg;
3164 }
3165 else {
3166 ++cur;
3167 }
3168 }
3169
3170 /* if line is incomplete line, then ignore it.
3171 * otherwise, update useful flags */
3172 switch (version) {
3173 case 1:
3174 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3175 continue;
3176 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3177 check_id = (atoi(params[0]) == curproxy->uuid);
3178 check_name = (strcmp(curproxy->id, params[1]) == 0);
3179 break;
3180 }
3181
3182 diff = 0;
3183 bk = curproxy;
3184
3185 /* if backend can't be found, let's continue */
3186 if (!check_id && !check_name)
3187 continue;
3188 else if (!check_id && check_name) {
3189 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3190 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3191 }
3192 else if (check_id && !check_name) {
3193 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3194 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3195 /* if name doesn't match, we still want to update curproxy if the backend id
3196 * was forced in previous the previous configuration */
3197 if (!bk_f_forced_id)
3198 continue;
3199 }
3200
3201 /* look for the server by its id: param[2] */
3202 /* else look for the server by its name: param[3] */
3203 diff = 0;
3204 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3205
3206 if (!srv) {
3207 /* if no server found, then warning and continue with next line */
3208 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3209 params[3], params[2], params[0], params[1]);
3210 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3211 params[3], params[2], params[0], params[1]);
3212 continue;
3213 }
3214 else if (diff & PR_FBM_MISMATCH_ID) {
3215 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);
3216 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 +02003217 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003218 }
3219 else if (diff & PR_FBM_MISMATCH_NAME) {
3220 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);
3221 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 +02003222 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003223 }
3224
3225 /* now we can proceed with server's state update */
3226 srv_update_state(srv, version, srv_params);
3227 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003228fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003229 fclose(f);
3230 }
3231}
3232
Simon Horman7d09b9a2013-02-12 10:45:51 +09003233/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003234 * update a server's current IP address.
3235 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3236 * ip is in network format.
3237 * updater is a string which contains an information about the requester of the update.
3238 * updater is used if not NULL.
3239 *
3240 * A log line and a stderr warning message is generated based on server's backend options.
3241 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003242int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003243{
3244 /* generates a log line and a warning on stderr */
3245 if (1) {
3246 /* book enough space for both IPv4 and IPv6 */
3247 char oldip[INET6_ADDRSTRLEN];
3248 char newip[INET6_ADDRSTRLEN];
3249
3250 memset(oldip, '\0', INET6_ADDRSTRLEN);
3251 memset(newip, '\0', INET6_ADDRSTRLEN);
3252
3253 /* copy old IP address in a string */
3254 switch (s->addr.ss_family) {
3255 case AF_INET:
3256 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3257 break;
3258 case AF_INET6:
3259 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3260 break;
3261 };
3262
3263 /* copy new IP address in a string */
3264 switch (ip_sin_family) {
3265 case AF_INET:
3266 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3267 break;
3268 case AF_INET6:
3269 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3270 break;
3271 };
3272
3273 /* save log line into a buffer */
3274 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3275 s->proxy->id, s->id, oldip, newip, updater);
3276
3277 /* write the buffer on stderr */
3278 Warning("%s.\n", trash.str);
3279
3280 /* send a log */
3281 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3282 }
3283
3284 /* save the new IP family */
3285 s->addr.ss_family = ip_sin_family;
3286 /* save the new IP address */
3287 switch (ip_sin_family) {
3288 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003289 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003290 break;
3291 case AF_INET6:
3292 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3293 break;
3294 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003295 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003296
3297 return 0;
3298}
3299
3300/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003301 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3302 *
3303 * Caller can pass its name through <updater> to get it integrated in the response message
3304 * returned by the function.
3305 *
3306 * The function first does the following, in that order:
3307 * - validates the new addr and/or port
3308 * - checks if an update is required (new IP or port is different than current ones)
3309 * - checks the update is allowed:
3310 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3311 * - allow all changes if no CHECKS are configured
3312 * - if CHECK is configured:
3313 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3314 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3315 * conditions are met
3316 */
3317const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3318{
3319 struct sockaddr_storage sa;
3320 int ret, port_change_required;
3321 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003322 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003323 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003324 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003325
3326 msg = get_trash_chunk();
3327 chunk_reset(msg);
3328
3329 if (addr) {
3330 memset(&sa, 0, sizeof(struct sockaddr_storage));
3331 if (str2ip2(addr, &sa, 0) == NULL) {
3332 chunk_printf(msg, "Invalid addr '%s'", addr);
3333 goto out;
3334 }
3335
3336 /* changes are allowed on AF_INET* families only */
3337 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3338 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3339 goto out;
3340 }
3341
3342 /* collecting data currently setup */
3343 memset(current_addr, '\0', sizeof(current_addr));
3344 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3345 /* changes are allowed on AF_INET* families only */
3346 if ((ret != AF_INET) && (ret != AF_INET6)) {
3347 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3348 goto out;
3349 }
3350
3351 /* applying ADDR changes if required and allowed
3352 * ipcmp returns 0 when both ADDR are the same
3353 */
3354 if (ipcmp(&s->addr, &sa) == 0) {
3355 chunk_appendf(msg, "no need to change the addr");
3356 goto port;
3357 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003358 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003359 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003360
3361 /* we also need to update check's ADDR only if it uses the server's one */
3362 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003363 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003364 }
3365
3366 /* we also need to update agent ADDR only if it use the server's one */
3367 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003368 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003369 }
3370
3371 /* update report for caller */
3372 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3373 }
3374
3375 port:
3376 if (port) {
3377 char sign = '\0';
3378 char *endptr;
3379
3380 if (addr)
3381 chunk_appendf(msg, ", ");
3382
3383 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003384 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003385
3386 /* check if PORT change is required */
3387 port_change_required = 0;
3388
3389 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003390 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003391 new_port = strtol(port, &endptr, 10);
3392 if ((errno != 0) || (port == endptr)) {
3393 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3394 goto out;
3395 }
3396
3397 /* check if caller triggers a port mapped or offset */
3398 if (sign == '-' || (sign == '+')) {
3399 /* check if server currently uses port map */
3400 if (!(s->flags & SRV_F_MAPPORTS)) {
3401 /* switch from fixed port to port map mandatorily triggers
3402 * a port change */
3403 port_change_required = 1;
3404 /* check is configured
3405 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3406 * prevent PORT change if check doesn't have it's dedicated port while switching
3407 * to port mapping */
3408 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3409 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.");
3410 goto out;
3411 }
3412 }
3413 /* we're already using port maps */
3414 else {
3415 port_change_required = current_port != new_port;
3416 }
3417 }
3418 /* fixed port */
3419 else {
3420 port_change_required = current_port != new_port;
3421 }
3422
3423 /* applying PORT changes if required and update response message */
3424 if (port_change_required) {
3425 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003426 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003427 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003428
3429 /* prepare message */
3430 chunk_appendf(msg, "port changed from '");
3431 if (s->flags & SRV_F_MAPPORTS)
3432 chunk_appendf(msg, "+");
3433 chunk_appendf(msg, "%d' to '", current_port);
3434
3435 if (sign == '-') {
3436 s->flags |= SRV_F_MAPPORTS;
3437 chunk_appendf(msg, "%c", sign);
3438 /* just use for result output */
3439 new_port = -new_port;
3440 }
3441 else if (sign == '+') {
3442 s->flags |= SRV_F_MAPPORTS;
3443 chunk_appendf(msg, "%c", sign);
3444 }
3445 else {
3446 s->flags &= ~SRV_F_MAPPORTS;
3447 }
3448
3449 chunk_appendf(msg, "%d'", new_port);
3450
3451 /* we also need to update health checks port only if it uses server's realport */
3452 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3453 s->check.port = new_port;
3454 }
3455 }
3456 else {
3457 chunk_appendf(msg, "no need to change the port");
3458 }
3459 }
3460
3461out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003462 if (changed)
3463 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003464 if (updater)
3465 chunk_appendf(msg, " by '%s'", updater);
3466 chunk_appendf(msg, "\n");
3467 return msg->str;
3468}
3469
3470
3471/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003472 * update server status based on result of name resolution
3473 * returns:
3474 * 0 if server status is updated
3475 * 1 if server status has not changed
3476 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003477int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003478{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003479 struct dns_resolvers *resolvers = s->resolvers;
3480 struct dns_resolution *resolution = s->dns_requester->resolution;
3481 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003482
3483 switch (resolution->status) {
3484 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003485 /* status when HAProxy has just (re)started.
3486 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003487 break;
3488
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003489 case RSLV_STATUS_VALID:
3490 /*
3491 * resume health checks
3492 * server will be turned back on if health check is safe
3493 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003494 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003495 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003496 return 1;
3497 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3498 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003499 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003500 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003501
Emeric Brun52a91d32017-08-31 14:41:55 +02003502 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003503 return 1;
3504 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3505 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3506 s->proxy->id, s->id);
3507
3508 Warning("%s.\n", trash.str);
3509 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3510 return 0;
3511
3512 case RSLV_STATUS_NX:
3513 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003514 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3515 if (!tick_is_expired(exp, now_ms))
3516 break;
3517
3518 if (s->next_admin & SRV_ADMF_RMAINT)
3519 return 1;
3520 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3521 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003522
3523 case RSLV_STATUS_TIMEOUT:
3524 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003525 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3526 if (!tick_is_expired(exp, now_ms))
3527 break;
3528
3529 if (s->next_admin & SRV_ADMF_RMAINT)
3530 return 1;
3531 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3532 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003533
3534 case RSLV_STATUS_REFUSED:
3535 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003536 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
3537 if (!tick_is_expired(exp, now_ms))
3538 break;
3539
3540 if (s->next_admin & SRV_ADMF_RMAINT)
3541 return 1;
3542 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3543 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003544
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003545 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003546 /* stop server if resolution failed for a long enough period */
3547 exp = tick_add(resolution->last_valid, resolvers->hold.other);
3548 if (!tick_is_expired(exp, now_ms))
3549 break;
3550
3551 if (s->next_admin & SRV_ADMF_RMAINT)
3552 return 1;
3553 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3554 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003555 }
3556
3557 return 1;
3558}
3559
3560/*
3561 * Server Name Resolution valid response callback
3562 * It expects:
3563 * - <nameserver>: the name server which answered the valid response
3564 * - <response>: buffer containing a valid DNS response
3565 * - <response_len>: size of <response>
3566 * It performs the following actions:
3567 * - ignore response if current ip found and server family not met
3568 * - update with first new ip found if family is met and current IP is not found
3569 * returns:
3570 * 0 on error
3571 * 1 when no error or safe ignore
3572 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003573int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003574{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003575 struct server *s = NULL;
3576 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003577 void *serverip, *firstip;
3578 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003579 int ret;
3580 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003581 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003582
Christopher Faulet67957bd2017-09-27 11:00:59 +02003583 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003584 if (!s)
3585 return 1;
3586
Christopher Faulet67957bd2017-09-27 11:00:59 +02003587 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003588
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003589 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003590 firstip = NULL; /* pointer to the first valid response found */
3591 /* it will be used as the new IP if a change is required */
3592 firstip_sin_family = AF_UNSPEC;
3593 serverip = NULL; /* current server IP address */
3594
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003595 /* initializing server IP pointer */
3596 server_sin_family = s->addr.ss_family;
3597 switch (server_sin_family) {
3598 case AF_INET:
3599 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3600 break;
3601
3602 case AF_INET6:
3603 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3604 break;
3605
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003606 case AF_UNSPEC:
3607 break;
3608
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003609 default:
3610 goto invalid;
3611 }
3612
Baptiste Assmann729c9012017-05-22 15:13:10 +02003613 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003614 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003615 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003616
3617 switch (ret) {
3618 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003619 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003620
3621 case DNS_UPD_SRVIP_NOT_FOUND:
3622 goto save_ip;
3623
3624 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003625 goto invalid;
3626
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003627 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003628 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003629 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003630
Baptiste Assmannfad03182015-10-28 02:03:32 +01003631 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003632 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003633 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003634 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003635
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003636 default:
3637 goto invalid;
3638
3639 }
3640
3641 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003642 if (nameserver) {
3643 nameserver->counters.update++;
3644 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003645 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003646 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003647 else
3648 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003649 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3650
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003651 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003652 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003653 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003654
3655 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003656 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003657 nameserver->counters.invalid++;
3658 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003659 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003660 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003661 return 0;
3662}
3663
3664/*
3665 * Server Name Resolution error management callback
3666 * returns:
3667 * 0 on error
3668 * 1 when no error or safe ignore
3669 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003670int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003671{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003672 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003673
Christopher Faulet67957bd2017-09-27 11:00:59 +02003674 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003675 if (!s)
3676 return 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003677 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003678 return 1;
3679}
3680
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003681/*
3682 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003683 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003684 * It returns a pointer to the first server found or NULL if <ip> is not yet
3685 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003686 */
3687struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3688{
3689 struct server *tmpsrv;
3690 struct proxy *be;
3691
3692 if (!srv)
3693 return NULL;
3694
3695 be = srv->proxy;
3696 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3697 /* We want to compare the IP in the record with the IP of the servers in the
3698 * same backend, only if:
3699 * * DNS resolution is enabled on the server
3700 * * the hostname used for the resolution by our server is the same than the
3701 * one used for the server found in the backend
3702 * * the server found in the backend is not our current server
3703 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003704 if ((tmpsrv->hostname_dn == NULL) ||
3705 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3706 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003707 (srv->puid == tmpsrv->puid))
3708 continue;
3709
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003710 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003711 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003712 continue;
3713
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003714 /* At this point, we have 2 different servers using the same DNS hostname
3715 * for their respective resolution.
3716 */
3717 if (*ip_family == tmpsrv->addr.ss_family &&
3718 ((tmpsrv->addr.ss_family == AF_INET &&
3719 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3720 (tmpsrv->addr.ss_family == AF_INET6 &&
3721 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3722 return tmpsrv;
3723 }
3724 }
3725
3726 return NULL;
3727}
3728
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003729/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3730 * resolver. This is suited for initial address configuration. Returns 0 on
3731 * success otherwise a non-zero error code. In case of error, *err_code, if
3732 * not NULL, is filled up.
3733 */
3734int srv_set_addr_via_libc(struct server *srv, int *err_code)
3735{
3736 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003737 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003738 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003739 return 1;
3740 }
3741 return 0;
3742}
3743
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003744/* Set the server's FDQN (->hostname) from <hostname>.
3745 * Returns -1 if failed, 0 if not.
3746 */
3747int srv_set_fqdn(struct server *srv, const char *hostname)
3748{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003749 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003750 char *hostname_dn;
3751 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003752
3753 /* run time DNS resolution was not active for this server
3754 * and we can't enable it at run time for now.
3755 */
3756 if (!srv->dns_requester)
3757 return -1;
3758
3759 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003760 hostname_len = strlen(hostname);
3761 hostname_dn = trash.str;
3762 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3763 hostname_dn, trash.size);
3764 if (hostname_dn_len == -1)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003765 return -1;
3766
Christopher Faulet67957bd2017-09-27 11:00:59 +02003767 resolution = srv->dns_requester->resolution;
3768 if (resolution &&
3769 resolution->hostname_dn &&
3770 !strcmp(resolution->hostname_dn, hostname_dn))
3771 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003772
Christopher Faulet67957bd2017-09-27 11:00:59 +02003773 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003774
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003775 free(srv->hostname);
3776 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003777 srv->hostname = strdup(hostname);
3778 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003779 srv->hostname_dn_len = hostname_dn_len;
3780 if (!srv->hostname || !srv->hostname_dn)
3781 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003782
Christopher Faulet67957bd2017-09-27 11:00:59 +02003783 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1)
3784 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003785 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003786}
3787
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003788/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3789 * from the state file. This is suited for initial address configuration.
3790 * Returns 0 on success otherwise a non-zero error code. In case of error,
3791 * *err_code, if not NULL, is filled up.
3792 */
3793static int srv_apply_lastaddr(struct server *srv, int *err_code)
3794{
3795 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3796 if (err_code)
3797 *err_code |= ERR_WARN;
3798 return 1;
3799 }
3800 return 0;
3801}
3802
Willy Tarreau25e51522016-11-04 15:10:17 +01003803/* returns 0 if no error, otherwise a combination of ERR_* flags */
3804static int srv_iterate_initaddr(struct server *srv)
3805{
3806 int return_code = 0;
3807 int err_code;
3808 unsigned int methods;
3809
3810 methods = srv->init_addr_methods;
3811 if (!methods) { // default to "last,libc"
3812 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3813 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3814 }
3815
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003816 /* "-dr" : always append "none" so that server addresses resolution
3817 * failures are silently ignored, this is convenient to validate some
3818 * configs out of their environment.
3819 */
3820 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3821 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3822
Willy Tarreau25e51522016-11-04 15:10:17 +01003823 while (methods) {
3824 err_code = 0;
3825 switch (srv_get_next_initaddr(&methods)) {
3826 case SRV_IADDR_LAST:
3827 if (!srv->lastaddr)
3828 continue;
3829 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003830 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003831 return_code |= err_code;
3832 break;
3833
3834 case SRV_IADDR_LIBC:
3835 if (!srv->hostname)
3836 continue;
3837 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003838 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003839 return_code |= err_code;
3840 break;
3841
Willy Tarreau37ebe122016-11-04 15:17:58 +01003842 case SRV_IADDR_NONE:
3843 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003844 if (return_code) {
3845 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3846 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3847 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003848 return return_code;
3849
Willy Tarreau4310d362016-11-02 15:05:56 +01003850 case SRV_IADDR_IP:
3851 ipcpy(&srv->init_addr, &srv->addr);
3852 if (return_code) {
3853 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3854 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3855 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003856 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003857
Willy Tarreau25e51522016-11-04 15:10:17 +01003858 default: /* unhandled method */
3859 break;
3860 }
3861 }
3862
3863 if (!return_code) {
3864 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3865 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3866 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003867 else {
3868 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3869 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3870 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003871
3872 return_code |= ERR_ALERT | ERR_FATAL;
3873 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003874out:
3875 srv_set_dyncookie(srv);
3876 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003877}
3878
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003879/*
3880 * This function parses all backends and all servers within each backend
3881 * and performs servers' addr resolution based on information provided by:
3882 * - configuration file
3883 * - server-state file (states provided by an 'old' haproxy process)
3884 *
3885 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3886 */
3887int srv_init_addr(void)
3888{
3889 struct proxy *curproxy;
3890 int return_code = 0;
3891
3892 curproxy = proxy;
3893 while (curproxy) {
3894 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003895
3896 /* servers are in backend only */
3897 if (!(curproxy->cap & PR_CAP_BE))
3898 goto srv_init_addr_next;
3899
Willy Tarreau25e51522016-11-04 15:10:17 +01003900 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003901 if (srv->hostname)
3902 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003903
3904 srv_init_addr_next:
3905 curproxy = curproxy->next;
3906 }
3907
3908 return return_code;
3909}
3910
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003911const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
3912{
3913
3914 struct chunk *msg;
3915
3916 msg = get_trash_chunk();
3917 chunk_reset(msg);
3918
Olivier Houchard8da5f982017-08-04 18:35:36 +02003919 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003920 chunk_appendf(msg, "no need to change the FDQN");
3921 goto out;
3922 }
3923
3924 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3925 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3926 goto out;
3927 }
3928
3929 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3930 server->proxy->id, server->id, server->hostname, fqdn);
3931
3932 if (srv_set_fqdn(server, fqdn) < 0) {
3933 chunk_reset(msg);
3934 chunk_appendf(msg, "could not update %s/%s FQDN",
3935 server->proxy->id, server->id);
3936 goto out;
3937 }
3938
3939 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003940 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003941
3942 out:
3943 if (updater)
3944 chunk_appendf(msg, " by '%s'", updater);
3945 chunk_appendf(msg, "\n");
3946
3947 return msg->str;
3948}
3949
3950
Willy Tarreau21b069d2016-11-23 17:15:08 +01003951/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3952 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003953 * 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 +01003954 * used for CLI commands requiring a server name.
3955 * Important: the <arg> is modified to remove the '/'.
3956 */
3957struct server *cli_find_server(struct appctx *appctx, char *arg)
3958{
3959 struct proxy *px;
3960 struct server *sv;
3961 char *line;
3962
3963 /* split "backend/server" and make <line> point to server */
3964 for (line = arg; *line; line++)
3965 if (*line == '/') {
3966 *line++ = '\0';
3967 break;
3968 }
3969
3970 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003971 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003972 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003973 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003974 return NULL;
3975 }
3976
3977 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003978 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003979 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003980 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003981 return NULL;
3982 }
3983
3984 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003985 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003986 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003987 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003988 return NULL;
3989 }
3990
3991 return sv;
3992}
3993
William Lallemand222baf22016-11-19 02:00:33 +01003994
3995static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3996{
3997 struct server *sv;
3998 const char *warning;
3999
4000 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4001 return 1;
4002
4003 sv = cli_find_server(appctx, args[2]);
4004 if (!sv)
4005 return 1;
4006
4007 if (strcmp(args[3], "weight") == 0) {
4008 warning = server_parse_weight_change_request(sv, args[4]);
4009 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004010 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004011 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004012 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004013 }
4014 }
4015 else if (strcmp(args[3], "state") == 0) {
4016 if (strcmp(args[4], "ready") == 0)
4017 srv_adm_set_ready(sv);
4018 else if (strcmp(args[4], "drain") == 0)
4019 srv_adm_set_drain(sv);
4020 else if (strcmp(args[4], "maint") == 0)
4021 srv_adm_set_maint(sv);
4022 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004023 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004024 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004025 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004026 }
4027 }
4028 else if (strcmp(args[3], "health") == 0) {
4029 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004030 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004031 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004032 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004033 }
4034 else if (strcmp(args[4], "up") == 0) {
4035 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004036 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004037 }
4038 else if (strcmp(args[4], "stopping") == 0) {
4039 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004040 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004041 }
4042 else if (strcmp(args[4], "down") == 0) {
4043 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004044 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004045 }
4046 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004047 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004048 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004049 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004050 }
4051 }
4052 else if (strcmp(args[3], "agent") == 0) {
4053 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004054 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004055 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004056 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004057 }
4058 else if (strcmp(args[4], "up") == 0) {
4059 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004060 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004061 }
4062 else if (strcmp(args[4], "down") == 0) {
4063 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004064 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004065 }
4066 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004067 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004068 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004069 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004070 }
4071 }
Misiek2da082d2017-01-09 09:40:42 +01004072 else if (strcmp(args[3], "agent-addr") == 0) {
4073 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004074 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004075 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4076 appctx->st0 = CLI_ST_PRINT;
4077 } else {
4078 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004079 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004080 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4081 appctx->st0 = CLI_ST_PRINT;
4082 }
4083 }
4084 }
4085 else if (strcmp(args[3], "agent-send") == 0) {
4086 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004087 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004088 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4089 appctx->st0 = CLI_ST_PRINT;
4090 } else {
4091 char *nss = strdup(args[4]);
4092 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004093 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004094 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4095 appctx->st0 = CLI_ST_PRINT;
4096 } else {
4097 free(sv->agent.send_string);
4098 sv->agent.send_string = nss;
4099 sv->agent.send_string_len = strlen(args[4]);
4100 }
4101 }
4102 }
William Lallemand222baf22016-11-19 02:00:33 +01004103 else if (strcmp(args[3], "check-port") == 0) {
4104 int i = 0;
4105 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004106 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004107 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004108 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004109 }
4110 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004111 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004112 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004113 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004114 }
4115 /* prevent the update of port to 0 if MAPPORTS are in use */
4116 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004117 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004118 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004119 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004120 return 1;
4121 }
4122 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004123 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004124 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004125 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004126 }
4127 else if (strcmp(args[3], "addr") == 0) {
4128 char *addr = NULL;
4129 char *port = NULL;
4130 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004131 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004132 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004133 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004134 return 1;
4135 }
4136 else {
4137 addr = args[4];
4138 }
4139 if (strcmp(args[5], "port") == 0) {
4140 port = args[6];
4141 }
4142 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4143 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004144 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004145 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004146 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004147 }
4148 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4149 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004150 else if (strcmp(args[3], "fqdn") == 0) {
4151 if (!*args[4]) {
4152 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4153 appctx->st0 = CLI_ST_PRINT;
4154 return 1;
4155 }
4156 warning = update_server_fqdn(sv, args[4], "stats socket command");
4157 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004158 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004159 appctx->ctx.cli.msg = warning;
4160 appctx->st0 = CLI_ST_PRINT;
4161 }
4162 }
William Lallemand222baf22016-11-19 02:00:33 +01004163 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004164 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004165 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 +01004166 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004167 }
4168 return 1;
4169}
4170
William Lallemand6b160942016-11-22 12:34:35 +01004171static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4172{
4173 struct stream_interface *si = appctx->owner;
4174 struct proxy *px;
4175 struct server *sv;
4176 char *line;
4177
4178
4179 /* split "backend/server" and make <line> point to server */
4180 for (line = args[2]; *line; line++)
4181 if (*line == '/') {
4182 *line++ = '\0';
4183 break;
4184 }
4185
4186 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004187 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004188 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004189 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004190 return 1;
4191 }
4192
4193 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004194 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004195 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004196 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004197 return 1;
4198 }
4199
4200 /* return server's effective weight at the moment */
4201 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004202 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004203 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004204 return 0;
4205 }
William Lallemand6b160942016-11-22 12:34:35 +01004206 return 1;
4207}
4208
4209static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4210{
4211 struct server *sv;
4212 const char *warning;
4213
4214 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4215 return 1;
4216
4217 sv = cli_find_server(appctx, args[2]);
4218 if (!sv)
4219 return 1;
4220
4221 warning = server_parse_weight_change_request(sv, args[3]);
4222 if (warning) {
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 = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004225 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004226 }
4227 return 1;
4228}
4229
Willy Tarreaub8026272016-11-23 11:26:56 +01004230/* parse a "set maxconn server" command. It always returns 1. */
4231static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4232{
4233 struct server *sv;
4234 const char *warning;
4235
4236 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4237 return 1;
4238
4239 sv = cli_find_server(appctx, args[3]);
4240 if (!sv)
4241 return 1;
4242
4243 warning = server_parse_maxconn_change_request(sv, args[4]);
4244 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004245 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004246 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004247 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004248 }
4249 return 1;
4250}
William Lallemand6b160942016-11-22 12:34:35 +01004251
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004252/* parse a "disable agent" command. It always returns 1. */
4253static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4254{
4255 struct server *sv;
4256
4257 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4258 return 1;
4259
4260 sv = cli_find_server(appctx, args[2]);
4261 if (!sv)
4262 return 1;
4263
4264 sv->agent.state &= ~CHK_ST_ENABLED;
4265 return 1;
4266}
4267
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004268/* parse a "disable health" command. It always returns 1. */
4269static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4270{
4271 struct server *sv;
4272
4273 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4274 return 1;
4275
4276 sv = cli_find_server(appctx, args[2]);
4277 if (!sv)
4278 return 1;
4279
4280 sv->check.state &= ~CHK_ST_ENABLED;
4281 return 1;
4282}
4283
Willy Tarreauffb4d582016-11-24 12:47:00 +01004284/* parse a "disable server" command. It always returns 1. */
4285static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4286{
4287 struct server *sv;
4288
4289 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4290 return 1;
4291
4292 sv = cli_find_server(appctx, args[2]);
4293 if (!sv)
4294 return 1;
4295
4296 srv_adm_set_maint(sv);
4297 return 1;
4298}
4299
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004300/* parse a "enable agent" command. It always returns 1. */
4301static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4302{
4303 struct server *sv;
4304
4305 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4306 return 1;
4307
4308 sv = cli_find_server(appctx, args[2]);
4309 if (!sv)
4310 return 1;
4311
4312 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004313 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004314 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004315 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004316 return 1;
4317 }
4318
4319 sv->agent.state |= CHK_ST_ENABLED;
4320 return 1;
4321}
4322
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004323/* parse a "enable health" command. It always returns 1. */
4324static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4325{
4326 struct server *sv;
4327
4328 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4329 return 1;
4330
4331 sv = cli_find_server(appctx, args[2]);
4332 if (!sv)
4333 return 1;
4334
4335 sv->check.state |= CHK_ST_ENABLED;
4336 return 1;
4337}
4338
Willy Tarreauffb4d582016-11-24 12:47:00 +01004339/* parse a "enable server" command. It always returns 1. */
4340static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4341{
4342 struct server *sv;
4343
4344 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4345 return 1;
4346
4347 sv = cli_find_server(appctx, args[2]);
4348 if (!sv)
4349 return 1;
4350
4351 srv_adm_set_ready(sv);
4352 return 1;
4353}
4354
William Lallemand222baf22016-11-19 02:00:33 +01004355/* register cli keywords */
4356static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004357 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004358 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004359 { { "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 +01004360 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004361 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004362 { { "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 +01004363 { { "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 +01004364 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004365 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4366 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4367
William Lallemand222baf22016-11-19 02:00:33 +01004368 {{},}
4369}};
4370
4371__attribute__((constructor))
4372static void __server_init(void)
4373{
4374 cli_register_kw(&cli_kws);
4375}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004376
Emeric Brun64cc49c2017-10-03 14:46:45 +02004377/*
4378 * This function applies server's status changes, it is
4379 * is designed to be called asynchronously.
4380 *
4381 */
4382void srv_update_status(struct server *s)
4383{
4384 struct check *check = &s->check;
4385 int xferred;
4386 struct proxy *px = s->proxy;
4387 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4388 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4389 int log_level;
4390 struct chunk *tmptrash = NULL;
4391
4392
4393 /* If currently main is not set we try to apply pending state changes */
4394 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4395 int next_admin;
4396
4397 /* Backup next admin */
4398 next_admin = s->next_admin;
4399
4400 /* restore current admin state */
4401 s->next_admin = s->cur_admin;
4402
4403 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4404 s->last_change = now.tv_sec;
4405 if (s->proxy->lbprm.set_server_status_down)
4406 s->proxy->lbprm.set_server_status_down(s);
4407
4408 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4409 srv_shutdown_streams(s, SF_ERR_DOWN);
4410
4411 /* we might have streams queued on this server and waiting for
4412 * a connection. Those which are redispatchable will be queued
4413 * to another server or to the proxy itself.
4414 */
4415 xferred = pendconn_redistribute(s);
4416
4417 tmptrash = alloc_trash_chunk();
4418 if (tmptrash) {
4419 chunk_printf(tmptrash,
4420 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4421 s->proxy->id, s->id);
4422
Emeric Brun5a133512017-10-19 14:42:30 +02004423 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004424 Warning("%s.\n", tmptrash->str);
4425
4426 /* we don't send an alert if the server was previously paused */
4427 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4428 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4429 send_email_alert(s, log_level, "%s", tmptrash->str);
4430 free_trash_chunk(tmptrash);
4431 tmptrash = NULL;
4432 }
4433 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4434 set_backend_down(s->proxy);
4435
4436 s->counters.down_trans++;
4437 }
4438 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4439 s->last_change = now.tv_sec;
4440 if (s->proxy->lbprm.set_server_status_down)
4441 s->proxy->lbprm.set_server_status_down(s);
4442
4443 /* we might have streams queued on this server and waiting for
4444 * a connection. Those which are redispatchable will be queued
4445 * to another server or to the proxy itself.
4446 */
4447 xferred = pendconn_redistribute(s);
4448
4449 tmptrash = alloc_trash_chunk();
4450 if (tmptrash) {
4451 chunk_printf(tmptrash,
4452 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4453 s->proxy->id, s->id);
4454
Emeric Brun5a133512017-10-19 14:42:30 +02004455 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004456
4457 Warning("%s.\n", tmptrash->str);
4458 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4459 free_trash_chunk(tmptrash);
4460 tmptrash = NULL;
4461 }
4462
4463 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4464 set_backend_down(s->proxy);
4465 }
4466 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4467 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4468 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4469 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4470 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4471 s->proxy->last_change = now.tv_sec;
4472 }
4473
4474 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4475 s->down_time += now.tv_sec - s->last_change;
4476
4477 s->last_change = now.tv_sec;
4478 if (s->next_state == SRV_ST_STARTING)
4479 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4480
4481 server_recalc_eweight(s);
4482 /* now propagate the status change to any LB algorithms */
4483 if (px->lbprm.update_server_eweight)
4484 px->lbprm.update_server_eweight(s);
4485 else if (srv_willbe_usable(s)) {
4486 if (px->lbprm.set_server_status_up)
4487 px->lbprm.set_server_status_up(s);
4488 }
4489 else {
4490 if (px->lbprm.set_server_status_down)
4491 px->lbprm.set_server_status_down(s);
4492 }
4493
4494 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4495 * and it's not a backup server and its effective weight is > 0,
4496 * then it can accept new connections, so we shut down all streams
4497 * on all backup servers.
4498 */
4499 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4500 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4501 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4502
4503 /* check if we can handle some connections queued at the proxy. We
4504 * will take as many as we can handle.
4505 */
4506 xferred = pendconn_grab_from_px(s);
4507
4508 tmptrash = alloc_trash_chunk();
4509 if (tmptrash) {
4510 chunk_printf(tmptrash,
4511 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4512 s->proxy->id, s->id);
4513
Emeric Brun5a133512017-10-19 14:42:30 +02004514 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004515 Warning("%s.\n", tmptrash->str);
4516 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4517 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4518 free_trash_chunk(tmptrash);
4519 tmptrash = NULL;
4520 }
4521
4522 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4523 set_backend_down(s->proxy);
4524 }
4525 else if (s->cur_eweight != s->next_eweight) {
4526 /* now propagate the status change to any LB algorithms */
4527 if (px->lbprm.update_server_eweight)
4528 px->lbprm.update_server_eweight(s);
4529 else if (srv_willbe_usable(s)) {
4530 if (px->lbprm.set_server_status_up)
4531 px->lbprm.set_server_status_up(s);
4532 }
4533 else {
4534 if (px->lbprm.set_server_status_down)
4535 px->lbprm.set_server_status_down(s);
4536 }
4537
4538 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4539 set_backend_down(s->proxy);
4540 }
4541
4542 s->next_admin = next_admin;
4543 }
4544
Emeric Brun5a133512017-10-19 14:42:30 +02004545 /* reset operational state change */
4546 *s->op_st_chg.reason = 0;
4547 s->op_st_chg.status = s->op_st_chg.code = -1;
4548 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004549
4550 /* Now we try to apply pending admin changes */
4551
4552 /* Maintenance must also disable health checks */
4553 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4554 if (s->check.state & CHK_ST_ENABLED) {
4555 s->check.state |= CHK_ST_PAUSED;
4556 check->health = 0;
4557 }
4558
4559 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004560 tmptrash = alloc_trash_chunk();
4561 if (tmptrash) {
4562 chunk_printf(tmptrash,
4563 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4564 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4565 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004566
Olivier Houchard796a2b32017-10-24 17:42:47 +02004567 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004568
Olivier Houchard796a2b32017-10-24 17:42:47 +02004569 if (!(global.mode & MODE_STARTING)) {
4570 Warning("%s.\n", tmptrash->str);
4571 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4572 }
4573 free_trash_chunk(tmptrash);
4574 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004575 }
4576 }
4577 else { /* server was still running */
4578 check->health = 0; /* failure */
4579 s->last_change = now.tv_sec;
4580 if (s->proxy->lbprm.set_server_status_down)
4581 s->proxy->lbprm.set_server_status_down(s);
4582
4583 s->next_state = SRV_ST_STOPPED;
4584 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4585 srv_shutdown_streams(s, SF_ERR_DOWN);
4586
4587 /* we might have streams queued on this server and waiting for
4588 * a connection. Those which are redispatchable will be queued
4589 * to another server or to the proxy itself.
4590 */
4591 xferred = pendconn_redistribute(s);
4592
4593 tmptrash = alloc_trash_chunk();
4594 if (tmptrash) {
4595 chunk_printf(tmptrash,
4596 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4597 s->flags & SRV_F_BACKUP ? "Backup " : "",
4598 s->proxy->id, s->id,
4599 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4600
4601 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4602
4603 if (!(global.mode & MODE_STARTING)) {
4604 Warning("%s.\n", tmptrash->str);
4605 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4606 }
4607 free_trash_chunk(tmptrash);
4608 tmptrash = NULL;
4609 }
4610 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4611 set_backend_down(s->proxy);
4612
4613 s->counters.down_trans++;
4614 }
4615 }
4616 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4617 /* OK here we're leaving maintenance, we have many things to check,
4618 * because the server might possibly be coming back up depending on
4619 * its state. In practice, leaving maintenance means that we should
4620 * immediately turn to UP (more or less the slowstart) under the
4621 * following conditions :
4622 * - server is neither checked nor tracked
4623 * - server tracks another server which is not checked
4624 * - server tracks another server which is already up
4625 * Which sums up as something simpler :
4626 * "either the tracking server is up or the server's checks are disabled
4627 * or up". Otherwise we only re-enable health checks. There's a special
4628 * case associated to the stopping state which can be inherited. Note
4629 * that the server might still be in drain mode, which is naturally dealt
4630 * with by the lower level functions.
4631 */
4632
4633 if (s->check.state & CHK_ST_ENABLED) {
4634 s->check.state &= ~CHK_ST_PAUSED;
4635 check->health = check->rise; /* start OK but check immediately */
4636 }
4637
4638 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4639 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4640 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4641 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4642 s->next_state = SRV_ST_STOPPING;
4643 }
4644 else {
4645 s->next_state = SRV_ST_STARTING;
4646 if (s->slowstart > 0)
4647 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4648 else
4649 s->next_state = SRV_ST_RUNNING;
4650 }
4651
4652 }
4653
4654 tmptrash = alloc_trash_chunk();
4655 if (tmptrash) {
4656 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4657 chunk_printf(tmptrash,
4658 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4659 s->flags & SRV_F_BACKUP ? "Backup " : "",
4660 s->proxy->id, s->id,
4661 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4662 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4663 }
4664 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4665 chunk_printf(tmptrash,
4666 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4667 s->flags & SRV_F_BACKUP ? "Backup " : "",
4668 s->proxy->id, s->id, s->hostname,
4669 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4670 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4671 }
4672 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4673 chunk_printf(tmptrash,
4674 "%sServer %s/%s is %s/%s (leaving maintenance)",
4675 s->flags & SRV_F_BACKUP ? "Backup " : "",
4676 s->proxy->id, s->id,
4677 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4678 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4679 }
4680 Warning("%s.\n", tmptrash->str);
4681 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4682 free_trash_chunk(tmptrash);
4683 tmptrash = NULL;
4684 }
4685
4686 server_recalc_eweight(s);
4687 /* now propagate the status change to any LB algorithms */
4688 if (px->lbprm.update_server_eweight)
4689 px->lbprm.update_server_eweight(s);
4690 else if (srv_willbe_usable(s)) {
4691 if (px->lbprm.set_server_status_up)
4692 px->lbprm.set_server_status_up(s);
4693 }
4694 else {
4695 if (px->lbprm.set_server_status_down)
4696 px->lbprm.set_server_status_down(s);
4697 }
4698
4699 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4700 set_backend_down(s->proxy);
4701
4702 }
4703 else if (s->next_admin & SRV_ADMF_MAINT) {
4704 /* remaining in maintenance mode, let's inform precisely about the
4705 * situation.
4706 */
4707 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4708 tmptrash = alloc_trash_chunk();
4709 if (tmptrash) {
4710 chunk_printf(tmptrash,
4711 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4712 s->flags & SRV_F_BACKUP ? "Backup " : "",
4713 s->proxy->id, s->id);
4714
4715 if (s->track) /* normally it's mandatory here */
4716 chunk_appendf(tmptrash, " via %s/%s",
4717 s->track->proxy->id, s->track->id);
4718 Warning("%s.\n", tmptrash->str);
4719 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4720 free_trash_chunk(tmptrash);
4721 tmptrash = NULL;
4722 }
4723 }
4724 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4725 tmptrash = alloc_trash_chunk();
4726 if (tmptrash) {
4727 chunk_printf(tmptrash,
4728 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4729 s->flags & SRV_F_BACKUP ? "Backup " : "",
4730 s->proxy->id, s->id, s->hostname);
4731
4732 if (s->track) /* normally it's mandatory here */
4733 chunk_appendf(tmptrash, " via %s/%s",
4734 s->track->proxy->id, s->track->id);
4735 Warning("%s.\n", tmptrash->str);
4736 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4737 free_trash_chunk(tmptrash);
4738 tmptrash = NULL;
4739 }
4740 }
4741 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4742 tmptrash = alloc_trash_chunk();
4743 if (tmptrash) {
4744 chunk_printf(tmptrash,
4745 "%sServer %s/%s remains in forced maintenance",
4746 s->flags & SRV_F_BACKUP ? "Backup " : "",
4747 s->proxy->id, s->id);
4748 Warning("%s.\n", tmptrash->str);
4749 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4750 free_trash_chunk(tmptrash);
4751 tmptrash = NULL;
4752 }
4753 }
4754 /* don't report anything when leaving drain mode and remaining in maintenance */
4755
4756 s->cur_admin = s->next_admin;
4757 }
4758
4759 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4760 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4761 /* drain state is applied only if not yet in maint */
4762
4763 s->last_change = now.tv_sec;
4764 if (px->lbprm.set_server_status_down)
4765 px->lbprm.set_server_status_down(s);
4766
4767 /* we might have streams queued on this server and waiting for
4768 * a connection. Those which are redispatchable will be queued
4769 * to another server or to the proxy itself.
4770 */
4771 xferred = pendconn_redistribute(s);
4772
4773 tmptrash = alloc_trash_chunk();
4774 if (tmptrash) {
4775 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4776 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4777 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4778
4779 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4780
4781 if (!(global.mode & MODE_STARTING)) {
4782 Warning("%s.\n", tmptrash->str);
4783 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4784 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4785 }
4786 free_trash_chunk(tmptrash);
4787 tmptrash = NULL;
4788 }
4789
4790 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4791 set_backend_down(s->proxy);
4792 }
4793 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4794 /* OK completely leaving drain mode */
4795 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4796 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4797 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4798 s->proxy->last_change = now.tv_sec;
4799 }
4800
4801 if (s->last_change < now.tv_sec) // ignore negative times
4802 s->down_time += now.tv_sec - s->last_change;
4803 s->last_change = now.tv_sec;
4804 server_recalc_eweight(s);
4805
4806 tmptrash = alloc_trash_chunk();
4807 if (tmptrash) {
4808 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4809 chunk_printf(tmptrash,
4810 "%sServer %s/%s is %s (leaving forced drain)",
4811 s->flags & SRV_F_BACKUP ? "Backup " : "",
4812 s->proxy->id, s->id,
4813 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4814 }
4815 else {
4816 chunk_printf(tmptrash,
4817 "%sServer %s/%s is %s (leaving drain)",
4818 s->flags & SRV_F_BACKUP ? "Backup " : "",
4819 s->proxy->id, s->id,
4820 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4821 if (s->track) /* normally it's mandatory here */
4822 chunk_appendf(tmptrash, " via %s/%s",
4823 s->track->proxy->id, s->track->id);
4824 }
4825
4826 Warning("%s.\n", tmptrash->str);
4827 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4828 free_trash_chunk(tmptrash);
4829 tmptrash = NULL;
4830 }
4831
4832 /* now propagate the status change to any LB algorithms */
4833 if (px->lbprm.update_server_eweight)
4834 px->lbprm.update_server_eweight(s);
4835 else if (srv_willbe_usable(s)) {
4836 if (px->lbprm.set_server_status_up)
4837 px->lbprm.set_server_status_up(s);
4838 }
4839 else {
4840 if (px->lbprm.set_server_status_down)
4841 px->lbprm.set_server_status_down(s);
4842 }
4843 }
4844 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4845 /* remaining in drain mode after removing one of its flags */
4846
4847 tmptrash = alloc_trash_chunk();
4848 if (tmptrash) {
4849 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4850 chunk_printf(tmptrash,
4851 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4852 s->flags & SRV_F_BACKUP ? "Backup " : "",
4853 s->proxy->id, s->id);
4854
4855 if (s->track) /* normally it's mandatory here */
4856 chunk_appendf(tmptrash, " via %s/%s",
4857 s->track->proxy->id, s->track->id);
4858 }
4859 else {
4860 chunk_printf(tmptrash,
4861 "%sServer %s/%s remains in forced drain mode",
4862 s->flags & SRV_F_BACKUP ? "Backup " : "",
4863 s->proxy->id, s->id);
4864 }
4865 Warning("%s.\n", tmptrash->str);
4866 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4867 free_trash_chunk(tmptrash);
4868 tmptrash = NULL;
4869 }
4870
4871 /* commit new admin status */
4872
4873 s->cur_admin = s->next_admin;
4874 }
4875 }
4876
4877 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004878 *s->adm_st_chg_cause = 0;
4879}
4880/*
4881 * This function loops on servers registered for asynchronous
4882 * status changes
4883 */
4884void servers_update_status(void) {
4885 struct server *s, *stmp;
4886
4887 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4888 srv_update_status(s);
4889 LIST_DEL(&s->update_status);
4890 LIST_INIT(&s->update_status);
4891 }
4892}
4893
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004894/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004895 * Local variables:
4896 * c-indent-level: 8
4897 * c-basic-offset: 8
4898 * End:
4899 */