blob: 01a7d9b670d4702706a92d7023d6729d9389511e [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
Willy Tarreau87b09662015-04-03 00:22:06 +0200764 list_for_each_entry_safe(stream, stream_bck, &srv->actconns, by_srv)
765 if (stream->srv_conn == srv)
766 stream_shutdown(stream, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200767}
768
769/* Shutdown all connections of all backup servers of a proxy. The caller must
Willy Tarreaue7dff022015-04-03 01:14:29 +0200770 * pass a termination code in <why>, which must be one of SF_ERR_* indicating
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200771 * the reason for the shutdown.
772 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200773void srv_shutdown_backup_streams(struct proxy *px, int why)
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200774{
775 struct server *srv;
776
777 for (srv = px->srv; srv != NULL; srv = srv->next)
778 if (srv->flags & SRV_F_BACKUP)
Willy Tarreau87b09662015-04-03 00:22:06 +0200779 srv_shutdown_streams(srv, why);
Willy Tarreau4aac7db2014-05-16 11:48:10 +0200780}
781
Willy Tarreaubda92272014-05-20 21:55:30 +0200782/* Appends some information to a message string related to a server going UP or
783 * DOWN. If both <forced> and <reason> are null and the server tracks another
784 * one, a "via" information will be provided to know where the status came from.
Emeric Brun5a133512017-10-19 14:42:30 +0200785 * If <check> is non-null, an entire string describing the check result will be
786 * appended after a comma and a space (eg: to report some information from the
787 * check that changed the state). In the other case, the string will be built
788 * using the check results stored into the struct server if present.
789 * If <xferred> is non-negative, some information about requeued sessions are
Willy Tarreaubda92272014-05-20 21:55:30 +0200790 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200791 */
Emeric Brun5a133512017-10-19 14:42:30 +0200792void srv_append_status(struct chunk *msg, struct server *s, struct check *check, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200793{
Emeric Brun5a133512017-10-19 14:42:30 +0200794 short status = s->op_st_chg.status;
795 short code = s->op_st_chg.code;
796 long duration = s->op_st_chg.duration;
797 char *desc = s->op_st_chg.reason;
798
799 if (check) {
800 status = check->status;
801 code = check->code;
802 duration = check->duration;
803 desc = check->desc;
804 }
805
806 if (status != -1) {
807 chunk_appendf(msg, ", reason: %s", get_check_status_description(status));
808
809 if (status >= HCHK_STATUS_L57DATA)
810 chunk_appendf(msg, ", code: %d", code);
811
812 if (desc && *desc) {
813 struct chunk src;
814
815 chunk_appendf(msg, ", info: \"");
816
817 chunk_initlen(&src, desc, 0, strlen(desc));
818 chunk_asciiencode(msg, &src, '"');
819
820 chunk_appendf(msg, "\"");
821 }
822
823 if (duration >= 0)
824 chunk_appendf(msg, ", check duration: %ldms", duration);
825 }
826 else if (desc && *desc) {
827 chunk_appendf(msg, ", %s", desc);
828 }
829 else if (!forced && s->track) {
Willy Tarreaubda92272014-05-20 21:55:30 +0200830 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Emeric Brun5a133512017-10-19 14:42:30 +0200831 }
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200832
833 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200834 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200835 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
836 " %d sessions active, %d requeued, %d remaining in queue",
837 s->proxy->srv_act, s->proxy->srv_bck,
838 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
839 s->cur_sess, xferred, s->nbpend);
840 else
841 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
842 " %d sessions requeued, %d total in queue",
843 s->proxy->srv_act, s->proxy->srv_bck,
844 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
845 xferred, s->nbpend);
846 }
847}
848
Emeric Brun5a133512017-10-19 14:42:30 +0200849/* Marks server <s> down, regardless of its checks' statuses. The server is
850 * registered in a list to postpone the counting of the remaining servers on
851 * the proxy and transfers queued streams whenever possible to other servers at
852 * a sync point. Maintenance servers are ignored. It stores the <reason> if
853 * non-null as the reason for going down or the available data from the check
854 * struct to recompute this reason later.
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200855 */
Emeric Brun5a133512017-10-19 14:42:30 +0200856void srv_set_stopped(struct server *s, const char *reason, struct check *check)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200857{
858 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200859
Emeric Brun64cc49c2017-10-03 14:46:45 +0200860 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200861 return;
862
Emeric Brun52a91d32017-08-31 14:41:55 +0200863 s->next_state = SRV_ST_STOPPED;
Emeric Brun5a133512017-10-19 14:42:30 +0200864 *s->op_st_chg.reason = 0;
865 s->op_st_chg.status = -1;
866 if (reason) {
867 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
868 }
869 else if (check) {
870 if (check->desc)
871 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
872 s->op_st_chg.code = check->code;
873 s->op_st_chg.status = check->status;
874 s->op_st_chg.duration = check->duration;
875 }
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200876
Emeric Brun64cc49c2017-10-03 14:46:45 +0200877 /* Register changes to be applied asynchronously */
878 if (LIST_ISEMPTY(&s->update_status))
879 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200880
881 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200882 srv_set_stopped(srv, NULL, NULL);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200883}
884
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200885/* Marks server <s> up regardless of its checks' statuses and provided it isn't
Emeric Brun5a133512017-10-19 14:42:30 +0200886 * in maintenance. The server is registered in a list to postpone the counting
887 * of the remaining servers on the proxy and tries to grab requests from the
888 * proxy at a sync point. Maintenance servers are ignored. It stores the
889 * <reason> if non-null as the reason for going down or the available data
890 * from the check struct to recompute this reason later.
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200891 */
Emeric Brun5a133512017-10-19 14:42:30 +0200892void srv_set_running(struct server *s, const char *reason, struct check *check)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200893{
894 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200895
Emeric Brun64cc49c2017-10-03 14:46:45 +0200896 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200897 return;
898
Emeric Brun52a91d32017-08-31 14:41:55 +0200899 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200900 return;
901
Emeric Brun52a91d32017-08-31 14:41:55 +0200902 s->next_state = SRV_ST_STARTING;
Emeric Brun5a133512017-10-19 14:42:30 +0200903 *s->op_st_chg.reason = 0;
904 s->op_st_chg.status = -1;
905 if (reason) {
906 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
907 }
908 else if (check) {
909 if (check->desc)
910 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
911 s->op_st_chg.code = check->code;
912 s->op_st_chg.status = check->status;
913 s->op_st_chg.duration = check->duration;
914 }
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200915
Emeric Brun64cc49c2017-10-03 14:46:45 +0200916 if (s->slowstart <= 0)
917 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200918
Emeric Brun64cc49c2017-10-03 14:46:45 +0200919 /* Register changes to be applied asynchronously */
920 if (LIST_ISEMPTY(&s->update_status))
921 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200922
923 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200924 srv_set_running(srv, NULL, NULL);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200925}
926
Willy Tarreau8eb77842014-05-21 13:54:57 +0200927/* Marks server <s> stopping regardless of its checks' statuses and provided it
Emeric Brun5a133512017-10-19 14:42:30 +0200928 * isn't in maintenance. The server is registered in a list to postpone the
929 * counting of the remaining servers on the proxy and tries to grab requests
930 * from the proxy. Maintenance servers are ignored. It stores the
931 * <reason> if non-null as the reason for going down or the available data
932 * from the check struct to recompute this reason later.
Willy Tarreau8eb77842014-05-21 13:54:57 +0200933 * up. Note that it makes use of the trash to build the log strings, so <reason>
934 * must not be placed there.
935 */
Emeric Brun5a133512017-10-19 14:42:30 +0200936void srv_set_stopping(struct server *s, const char *reason, struct check *check)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200937{
938 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200939
Emeric Brun64cc49c2017-10-03 14:46:45 +0200940 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200941 return;
942
Emeric Brun52a91d32017-08-31 14:41:55 +0200943 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200944 return;
945
Emeric Brun52a91d32017-08-31 14:41:55 +0200946 s->next_state = SRV_ST_STOPPING;
Emeric Brun5a133512017-10-19 14:42:30 +0200947 *s->op_st_chg.reason = 0;
948 s->op_st_chg.status = -1;
949 if (reason) {
950 strlcpy2(s->op_st_chg.reason, reason, sizeof(s->op_st_chg.reason));
951 }
952 else if (check) {
953 if (check->desc)
954 strlcpy2(s->op_st_chg.reason, check->desc, sizeof(s->op_st_chg.reason));
955 s->op_st_chg.code = check->code;
956 s->op_st_chg.status = check->status;
957 s->op_st_chg.duration = check->duration;
958 }
Willy Tarreau8eb77842014-05-21 13:54:57 +0200959
Emeric Brun64cc49c2017-10-03 14:46:45 +0200960 /* Register changes to be applied asynchronously */
961 if (LIST_ISEMPTY(&s->update_status))
962 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200963
964 for (srv = s->trackers; srv; srv = srv->tracknext)
Emeric Brun5a133512017-10-19 14:42:30 +0200965 srv_set_stopping(srv, NULL, NULL);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200966}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200967
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200968/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
969 * enforce either maint mode or drain mode. It is not allowed to set more than
970 * one flag at once. The equivalent "inherited" flag is propagated to all
971 * tracking servers. Maintenance mode disables health checks (but not agent
972 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100973 * is done. If <cause> is non-null, it will be displayed at the end of the log
974 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200975 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100976void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200978 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200979
980 if (!mode)
981 return;
982
983 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200984 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200985 return;
986
Emeric Brun52a91d32017-08-31 14:41:55 +0200987 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200988 if (cause)
989 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
990
991 /* Register changes to be applied asynchronously */
992 if (LIST_ISEMPTY(&s->update_status))
993 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200994
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200995 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200996 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
997 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200998 return;
999
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001000 /* compute the inherited flag to propagate */
1001 if (mode & SRV_ADMF_MAINT)
1002 mode = SRV_ADMF_IMAINT;
1003 else if (mode & SRV_ADMF_DRAIN)
1004 mode = SRV_ADMF_IDRAIN;
1005
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001006 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +01001007 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001008}
1009
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001010/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
1011 * stop enforcing either maint mode or drain mode. It is not allowed to set more
1012 * than one flag at once. The equivalent "inherited" flag is propagated to all
1013 * tracking servers. Leaving maintenance mode re-enables health checks. When
1014 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001015 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001016void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001017{
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001018 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001019
1020 if (!mode)
1021 return;
1022
1023 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +02001024 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001025 return;
1026
Emeric Brun52a91d32017-08-31 14:41:55 +02001027 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001028
Emeric Brun64cc49c2017-10-03 14:46:45 +02001029 /* Register changes to be applied asynchronously */
1030 if (LIST_ISEMPTY(&s->update_status))
1031 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001032
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001033 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +02001034 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
1035 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001036 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001037
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001038 if (mode & SRV_ADMF_MAINT)
1039 mode = SRV_ADMF_IMAINT;
1040 else if (mode & SRV_ADMF_DRAIN)
1041 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001042
1043 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +02001044 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +02001045}
1046
Willy Tarreau757478e2016-11-03 19:22:19 +01001047/* principle: propagate maint and drain to tracking servers. This is useful
1048 * upon startup so that inherited states are correct.
1049 */
1050static void srv_propagate_admin_state(struct server *srv)
1051{
1052 struct server *srv2;
1053
1054 if (!srv->trackers)
1055 return;
1056
1057 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +02001058 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +01001059 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001060
Emeric Brun52a91d32017-08-31 14:41:55 +02001061 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +01001062 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +01001063 }
1064}
1065
1066/* Compute and propagate the admin states for all servers in proxy <px>.
1067 * Only servers *not* tracking another one are considered, because other
1068 * ones will be handled when the server they track is visited.
1069 */
1070void srv_compute_all_admin_states(struct proxy *px)
1071{
1072 struct server *srv;
1073
1074 for (srv = px->srv; srv; srv = srv->next) {
1075 if (srv->track)
1076 continue;
1077 srv_propagate_admin_state(srv);
1078 }
1079}
1080
Willy Tarreaudff55432012-10-10 17:51:05 +02001081/* Note: must not be declared <const> as its list will be overwritten.
1082 * Please take care of keeping this list alphabetically sorted, doing so helps
1083 * all code contributors.
1084 * Optional keywords are also declared with a NULL ->parse() function so that
1085 * the config parser can report an appropriate error when a known keyword was
1086 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001087 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001088 */
1089static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001090 { "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 +01001091 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001092 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001093 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001094 { "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 +01001095 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001096 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1097 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001098 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001099 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001100 { "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 +01001101 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001102 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001103 { "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 +01001104 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1105 { "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 +01001106 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001107 { "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 +01001108 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001109 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1110 { "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 +02001111 { "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 +01001112 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001113 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001114 { NULL, NULL, 0 },
1115}};
1116
1117__attribute__((constructor))
1118static void __listener_init(void)
1119{
1120 srv_register_keywords(&srv_kws);
1121}
1122
Willy Tarreau004e0452013-11-21 11:22:01 +01001123/* Recomputes the server's eweight based on its state, uweight, the current time,
1124 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1125 * state is automatically disabled if the time is elapsed.
1126 */
1127void server_recalc_eweight(struct server *sv)
1128{
1129 struct proxy *px = sv->proxy;
1130 unsigned w;
1131
1132 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1133 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001134 if (sv->next_state == SRV_ST_STARTING)
1135 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001136 }
1137
1138 /* We must take care of not pushing the server to full throttle during slow starts.
1139 * It must also start immediately, at least at the minimal step when leaving maintenance.
1140 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001141 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001142 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1143 else
1144 w = px->lbprm.wdiv;
1145
Emeric Brun52a91d32017-08-31 14:41:55 +02001146 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001147
Emeric Brun64cc49c2017-10-03 14:46:45 +02001148 /* Register changes to be applied asynchronously */
1149 if (LIST_ISEMPTY(&sv->update_status))
1150 LIST_ADDQ(&updated_servers, &sv->update_status);
Willy Tarreau004e0452013-11-21 11:22:01 +01001151}
1152
Willy Tarreaubaaee002006-06-26 02:48:02 +02001153/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001154 * Parses weight_str and configures sv accordingly.
1155 * Returns NULL on success, error message string otherwise.
1156 */
1157const char *server_parse_weight_change_request(struct server *sv,
1158 const char *weight_str)
1159{
1160 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001161 long int w;
1162 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001163
1164 px = sv->proxy;
1165
1166 /* if the weight is terminated with '%', it is set relative to
1167 * the initial weight, otherwise it is absolute.
1168 */
1169 if (!*weight_str)
1170 return "Require <weight> or <weight%>.\n";
1171
Simon Hormanb796afa2013-02-12 10:45:53 +09001172 w = strtol(weight_str, &end, 10);
1173 if (end == weight_str)
1174 return "Empty weight string empty or preceded by garbage";
1175 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001176 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001177 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001178 /* Avoid integer overflow */
1179 if (w > 25600)
1180 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001181 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001182 if (w > 256)
1183 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001184 }
1185 else if (w < 0 || w > 256)
1186 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001187 else if (end[0] != '\0')
1188 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001189
1190 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1191 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1192
1193 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001194 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001195
1196 return NULL;
1197}
1198
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001199/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001200 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1201 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001202 * Returns:
1203 * - error string on error
1204 * - NULL on success
1205 */
1206const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001207 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001208{
1209 unsigned char ip[INET6_ADDRSTRLEN];
1210
1211 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001212 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001213 return NULL;
1214 }
1215 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001216 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001217 return NULL;
1218 }
1219
1220 return "Could not understand IP address format.\n";
1221}
1222
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001223const char *server_parse_maxconn_change_request(struct server *sv,
1224 const char *maxconn_str)
1225{
1226 long int v;
1227 char *end;
1228
1229 if (!*maxconn_str)
1230 return "Require <maxconn>.\n";
1231
1232 v = strtol(maxconn_str, &end, 10);
1233 if (end == maxconn_str)
1234 return "maxconn string empty or preceded by garbage";
1235 else if (end[0] != '\0')
1236 return "Trailing garbage in maxconn string";
1237
1238 if (sv->maxconn == sv->minconn) { // static maxconn
1239 sv->maxconn = sv->minconn = v;
1240 } else { // dynamic maxconn
1241 sv->maxconn = v;
1242 }
1243
1244 if (may_dequeue_tasks(sv, sv->proxy))
1245 process_srv_queue(sv);
1246
1247 return NULL;
1248}
1249
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001250#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001251static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1252 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001253{
1254 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001255 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001256 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001257 NULL,
1258 };
1259
1260 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001261 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001262
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001263 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1264}
1265
1266static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1267{
1268 struct sample_expr *expr;
1269
1270 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 +01001271 if (!expr) {
1272 memprintf(err, "error detected while parsing sni expression : %s", *err);
1273 return ERR_ALERT | ERR_FATAL;
1274 }
1275
1276 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1277 memprintf(err, "error detected while parsing sni expression : "
1278 " fetch method '%s' extracts information from '%s', "
1279 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001280 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001281 return ERR_ALERT | ERR_FATAL;
1282 }
1283
1284 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1285 release_sample_expr(newsrv->ssl_ctx.sni);
1286 newsrv->ssl_ctx.sni = expr;
1287
1288 return 0;
1289}
1290#endif
1291
1292static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1293{
1294 if (err && *err) {
1295 indent_msg(err, 2);
1296 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1297 }
1298 else
1299 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1300 file, linenum, args[0], args[1], args[cur_arg]);
1301}
1302
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001303static void srv_conn_src_sport_range_cpy(struct server *srv,
1304 struct server *src)
1305{
1306 int range_sz;
1307
1308 range_sz = src->conn_src.sport_range->size;
1309 if (range_sz > 0) {
1310 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1311 if (srv->conn_src.sport_range != NULL) {
1312 int i;
1313
1314 for (i = 0; i < range_sz; i++) {
1315 srv->conn_src.sport_range->ports[i] =
1316 src->conn_src.sport_range->ports[i];
1317 }
1318 }
1319 }
1320}
1321
1322/*
1323 * Copy <src> server connection source settings to <srv> server everything needed.
1324 */
1325static void srv_conn_src_cpy(struct server *srv, struct server *src)
1326{
1327 srv->conn_src.opts = src->conn_src.opts;
1328 srv->conn_src.source_addr = src->conn_src.source_addr;
1329
1330 /* Source port range copy. */
1331 if (src->conn_src.sport_range != NULL)
1332 srv_conn_src_sport_range_cpy(srv, src);
1333
1334#ifdef CONFIG_HAP_TRANSPARENT
1335 if (src->conn_src.bind_hdr_name != NULL) {
1336 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1337 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1338 }
1339 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1340 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1341#endif
1342 if (src->conn_src.iface_name != NULL)
1343 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1344}
1345
1346/*
1347 * Copy <src> server SSL settings to <srv> server allocating
1348 * everything needed.
1349 */
1350#if defined(USE_OPENSSL)
1351static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1352{
1353 if (src->ssl_ctx.ca_file != NULL)
1354 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1355 if (src->ssl_ctx.crl_file != NULL)
1356 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1357 if (src->ssl_ctx.client_crt != NULL)
1358 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1359
1360 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1361
1362 if (src->ssl_ctx.verify_host != NULL)
1363 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1364 if (src->ssl_ctx.ciphers != NULL)
1365 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1366 if (src->sni_expr != NULL)
1367 srv->sni_expr = strdup(src->sni_expr);
1368}
1369#endif
1370
1371/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001372 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001373 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001374 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001375 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001376static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001377{
Christopher Faulet67957bd2017-09-27 11:00:59 +02001378 char *hostname_dn;
1379 int hostname_len, hostname_dn_len;
1380
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001381 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001382 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001383
Christopher Faulet67957bd2017-09-27 11:00:59 +02001384 hostname_len = strlen(hostname);
1385 hostname_dn = trash.str;
1386 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
1387 hostname_dn, trash.size);
1388 if (hostname_dn_len == -1)
1389 goto err;
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001390
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001391
Christopher Faulet67957bd2017-09-27 11:00:59 +02001392 free(srv->hostname);
1393 free(srv->hostname_dn);
1394 srv->hostname = strdup(hostname);
1395 srv->hostname_dn = strdup(hostname_dn);
1396 srv->hostname_dn_len = hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001397 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001398 goto err;
1399
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001400 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001401
1402 err:
Christopher Faulet67957bd2017-09-27 11:00:59 +02001403 free(srv->hostname); srv->hostname = NULL;
1404 free(srv->hostname_dn); srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001405 return -1;
1406}
1407
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001408/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001409 * Copy <src> server settings to <srv> server allocating
1410 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001411 * This function is not supposed to be called at any time, but only
1412 * during server settings parsing or during server allocations from
1413 * a server template, and just after having calloc()'ed a new server.
1414 * So, <src> may only be a default server (when parsing server settings)
1415 * or a server template (during server allocations from a server template).
1416 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1417 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001418 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001419static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001420{
1421 /* Connection source settings copy */
1422 srv_conn_src_cpy(srv, src);
1423
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001424 if (srv_tmpl) {
1425 srv->addr = src->addr;
1426 srv->svc_port = src->svc_port;
1427 }
1428
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001429 srv->pp_opts = src->pp_opts;
1430 if (src->rdr_pfx != NULL) {
1431 srv->rdr_pfx = strdup(src->rdr_pfx);
1432 srv->rdr_len = src->rdr_len;
1433 }
1434 if (src->cookie != NULL) {
1435 srv->cookie = strdup(src->cookie);
1436 srv->cklen = src->cklen;
1437 }
1438 srv->use_ssl = src->use_ssl;
1439 srv->check.addr = srv->agent.addr = src->check.addr;
1440 srv->check.use_ssl = src->check.use_ssl;
1441 srv->check.port = src->check.port;
1442 /* Note: 'flags' field has potentially been already initialized. */
1443 srv->flags |= src->flags;
1444 srv->do_check = src->do_check;
1445 srv->do_agent = src->do_agent;
1446 if (srv->check.port)
1447 srv->flags |= SRV_F_CHECKPORT;
1448 srv->check.inter = src->check.inter;
1449 srv->check.fastinter = src->check.fastinter;
1450 srv->check.downinter = src->check.downinter;
1451 srv->agent.use_ssl = src->agent.use_ssl;
1452 srv->agent.port = src->agent.port;
1453 if (src->agent.send_string != NULL)
1454 srv->agent.send_string = strdup(src->agent.send_string);
1455 srv->agent.send_string_len = src->agent.send_string_len;
1456 srv->agent.inter = src->agent.inter;
1457 srv->agent.fastinter = src->agent.fastinter;
1458 srv->agent.downinter = src->agent.downinter;
1459 srv->maxqueue = src->maxqueue;
1460 srv->minconn = src->minconn;
1461 srv->maxconn = src->maxconn;
1462 srv->slowstart = src->slowstart;
1463 srv->observe = src->observe;
1464 srv->onerror = src->onerror;
1465 srv->onmarkeddown = src->onmarkeddown;
1466 srv->onmarkedup = src->onmarkedup;
1467 if (src->trackit != NULL)
1468 srv->trackit = strdup(src->trackit);
1469 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1470 srv->uweight = srv->iweight = src->iweight;
1471
1472 srv->check.send_proxy = src->check.send_proxy;
1473 /* health: up, but will fall down at first failure */
1474 srv->check.rise = srv->check.health = src->check.rise;
1475 srv->check.fall = src->check.fall;
1476
1477 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001478 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1479 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1480 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001481 srv->check.state |= CHK_ST_PAUSED;
1482 srv->check.health = 0;
1483 }
1484
1485 /* health: up but will fall down at first failure */
1486 srv->agent.rise = srv->agent.health = src->agent.rise;
1487 srv->agent.fall = src->agent.fall;
1488
1489 if (src->resolvers_id != NULL)
1490 srv->resolvers_id = strdup(src->resolvers_id);
1491 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1492 if (srv->dns_opts.family_prio == AF_UNSPEC)
1493 srv->dns_opts.family_prio = AF_INET6;
1494 memcpy(srv->dns_opts.pref_net,
1495 src->dns_opts.pref_net,
1496 sizeof srv->dns_opts.pref_net);
1497 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1498
1499 srv->init_addr_methods = src->init_addr_methods;
1500 srv->init_addr = src->init_addr;
1501#if defined(USE_OPENSSL)
1502 srv_ssl_settings_cpy(srv, src);
1503#endif
1504#ifdef TCP_USER_TIMEOUT
1505 srv->tcp_ut = src->tcp_ut;
1506#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001507 if (srv_tmpl)
1508 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001509}
1510
1511static struct server *new_server(struct proxy *proxy)
1512{
1513 struct server *srv;
1514
1515 srv = calloc(1, sizeof *srv);
1516 if (!srv)
1517 return NULL;
1518
1519 srv->obj_type = OBJ_TYPE_SERVER;
1520 srv->proxy = proxy;
1521 LIST_INIT(&srv->actconns);
1522 LIST_INIT(&srv->pendconns);
1523 LIST_INIT(&srv->priv_conns);
1524 LIST_INIT(&srv->idle_conns);
1525 LIST_INIT(&srv->safe_conns);
Emeric Brun64cc49c2017-10-03 14:46:45 +02001526 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001527
Emeric Brun52a91d32017-08-31 14:41:55 +02001528 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001529 srv->last_change = now.tv_sec;
1530
1531 srv->check.status = HCHK_STATUS_INI;
1532 srv->check.server = srv;
1533 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1534
1535 srv->agent.status = HCHK_STATUS_INI;
1536 srv->agent.server = srv;
1537 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1538
1539 return srv;
1540}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001541
1542/*
1543 * Validate <srv> server health-check settings.
1544 * Returns 0 if everything is OK, -1 if not.
1545 */
1546static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1547{
1548 struct tcpcheck_rule *r = NULL;
1549 struct list *l;
1550
1551 /*
1552 * We need at least a service port, a check port or the first tcp-check rule must
1553 * be a 'connect' one when checking an IPv4/IPv6 server.
1554 */
1555 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1556 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1557 return 0;
1558
1559 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1560 if (!r) {
1561 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1562 "Check has been disabled.\n",
1563 file, linenum, srv->id);
1564 return -1;
1565 }
1566
1567 /* search the first action (connect / send / expect) in the list */
1568 l = &srv->proxy->tcpcheck_rules;
1569 list_for_each_entry(r, l, list) {
1570 if (r->action != TCPCHK_ACT_COMMENT)
1571 break;
1572 }
1573
1574 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1575 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1576 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1577 file, linenum, srv->id);
1578 return -1;
1579 }
1580
1581 /* scan the tcp-check ruleset to ensure a port has been configured */
1582 l = &srv->proxy->tcpcheck_rules;
1583 list_for_each_entry(r, l, list) {
1584 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1585 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1586 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1587 file, linenum, srv->id);
1588 return -1;
1589 }
1590 }
1591
1592 return 0;
1593}
1594
1595/*
1596 * Initialize <srv> health-check structure.
1597 * Returns the error string in case of memory allocation failure, NULL if not.
1598 */
1599static const char *do_health_check_init(struct server *srv, int check_type, int state)
1600{
1601 const char *ret;
1602
1603 if (!srv->do_check)
1604 return NULL;
1605
1606 ret = init_check(&srv->check, check_type);
1607 if (ret)
1608 return ret;
1609
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001610 srv->check.state |= state;
1611 global.maxsock++;
1612
1613 return NULL;
1614}
1615
1616static int server_health_check_init(const char *file, int linenum,
1617 struct server *srv, struct proxy *curproxy)
1618{
1619 const char *ret;
1620
1621 if (!srv->do_check)
1622 return 0;
1623
1624 if (srv->trackit) {
1625 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1626 file, linenum);
1627 return ERR_ALERT | ERR_FATAL;
1628 }
1629
1630 if (server_healthcheck_validate(file, linenum, srv) < 0)
1631 return ERR_ALERT | ERR_ABORT;
1632
1633 /* note: check type will be set during the config review phase */
1634 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1635 if (ret) {
1636 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1637 return ERR_ALERT | ERR_ABORT;
1638 }
1639
1640 return 0;
1641}
1642
1643/*
1644 * Initialize <srv> agent check structure.
1645 * Returns the error string in case of memory allocation failure, NULL if not.
1646 */
1647static const char *do_server_agent_check_init(struct server *srv, int state)
1648{
1649 const char *ret;
1650
1651 if (!srv->do_agent)
1652 return NULL;
1653
1654 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1655 if (ret)
1656 return ret;
1657
1658 if (!srv->agent.inter)
1659 srv->agent.inter = srv->check.inter;
1660
1661 srv->agent.state |= state;
1662 global.maxsock++;
1663
1664 return NULL;
1665}
1666
1667static int server_agent_check_init(const char *file, int linenum,
1668 struct server *srv, struct proxy *curproxy)
1669{
1670 const char *ret;
1671
1672 if (!srv->do_agent)
1673 return 0;
1674
1675 if (!srv->agent.port) {
1676 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1677 file, linenum, srv->id);
1678 return ERR_ALERT | ERR_FATAL;
1679 }
1680
1681 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1682 if (ret) {
1683 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1684 return ERR_ALERT | ERR_ABORT;
1685 }
1686
1687 return 0;
1688}
1689
1690#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1691static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1692 struct server *srv, struct proxy *proxy)
1693{
1694 int ret;
1695 char *err = NULL;
1696
1697 if (!srv->sni_expr)
1698 return 0;
1699
1700 ret = server_parse_sni_expr(srv, proxy, &err);
1701 if (!ret)
1702 return 0;
1703
1704 display_parser_err(file, linenum, args, cur_arg, &err);
1705 free(err);
1706
1707 return ret;
1708}
1709#endif
1710
1711/*
1712 * Server initializations finalization.
1713 * Initialize health check, agent check and SNI expression if enabled.
1714 * Must not be called for a default server instance.
1715 */
1716static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1717 struct server *srv, struct proxy *px)
1718{
1719 int ret;
1720
1721 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1722 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1723 return ret;
1724 }
1725
1726#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1727 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1728 return ret;
1729#endif
1730
1731 if (srv->flags & SRV_F_BACKUP)
1732 px->srv_bck++;
1733 else
1734 px->srv_act++;
1735 srv_lb_commit_status(srv);
1736
1737 return 0;
1738}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001739
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001740/*
1741 * Parse as much as possible such a range string argument: low[-high]
1742 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1743 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1744 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1745 * Returns 0 if succeeded, -1 if not.
1746 */
1747static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1748{
1749 char *nb_high_arg;
1750
1751 *nb_high = 0;
1752 chunk_printf(&trash, "%s", arg);
1753 *nb_low = atoi(trash.str);
1754
1755 if ((nb_high_arg = strchr(trash.str, '-'))) {
1756 *nb_high_arg++ = '\0';
1757 *nb_high = atoi(nb_high_arg);
1758 }
1759 else {
1760 *nb_high += *nb_low;
1761 *nb_low = 1;
1762 }
1763
1764 if (*nb_low < 0 || *nb_high < *nb_low)
1765 return -1;
1766
1767 return 0;
1768}
1769
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001770static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1771{
1772 chunk_printf(&trash, "%s%d", prefix, nb);
1773 free(srv->id);
1774 srv->id = strdup(trash.str);
1775}
1776
1777/*
1778 * Initialize as much as possible servers from <srv> server template.
1779 * Note that a server template is a special server with
1780 * a few different parameters than a server which has
1781 * been parsed mostly the same way as a server.
1782 * Returns the number of servers succesfully allocated,
1783 * 'srv' template included.
1784 */
1785static int server_template_init(struct server *srv, struct proxy *px)
1786{
1787 int i;
1788 struct server *newsrv;
1789
1790 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1791 int check_init_state;
1792 int agent_init_state;
1793
1794 newsrv = new_server(px);
1795 if (!newsrv)
1796 goto err;
1797
1798 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001799 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001800#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1801 if (newsrv->sni_expr) {
1802 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1803 if (!newsrv->ssl_ctx.sni)
1804 goto err;
1805 }
1806#endif
1807 /* Set this new server ID. */
1808 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1809
1810 /* Initial checks states. */
1811 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1812 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1813
1814 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1815 do_server_agent_check_init(newsrv, agent_init_state))
1816 goto err;
1817
1818 /* Linked backwards first. This will be restablished after parsing. */
1819 newsrv->next = px->srv;
1820 px->srv = newsrv;
1821 }
1822 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1823
1824 return i - srv->tmpl_info.nb_low;
1825
1826 err:
1827 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1828 if (newsrv) {
1829#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1830 release_sample_expr(newsrv->ssl_ctx.sni);
1831#endif
1832 free_check(&newsrv->agent);
1833 free_check(&newsrv->check);
1834 }
1835 free(newsrv);
1836 return i - srv->tmpl_info.nb_low;
1837}
1838
Willy Tarreau272adea2014-03-31 10:39:59 +02001839int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1840{
1841 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001842 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001843 char *errmsg = NULL;
1844 int err_code = 0;
1845 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001846 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001847
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001848 if (!strcmp(args[0], "server") ||
1849 !strcmp(args[0], "default-server") ||
1850 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001851 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001852 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001853 int srv = !defsrv && !strcmp(args[0], "server");
1854 int srv_tmpl = !defsrv && !srv;
1855 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001856
1857 if (!defsrv && curproxy == defproxy) {
1858 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1859 err_code |= ERR_ALERT | ERR_FATAL;
1860 goto out;
1861 }
1862 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1863 err_code |= ERR_ALERT | ERR_FATAL;
1864
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001865 /* There is no mandatory first arguments for default server. */
1866 if (srv) {
1867 if (!*args[2]) {
1868 /* 'server' line number of argument check. */
1869 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1870 file, linenum, args[0]);
1871 err_code |= ERR_ALERT | ERR_FATAL;
1872 goto out;
1873 }
1874
1875 err = invalid_char(args[1]);
1876 }
1877 else if (srv_tmpl) {
1878 if (!*args[3]) {
1879 /* 'server-template' line number of argument check. */
1880 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1881 file, linenum, args[0]);
1882 err_code |= ERR_ALERT | ERR_FATAL;
1883 goto out;
1884 }
1885
1886 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001887 }
1888
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001889 if (err) {
1890 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1891 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001892 err_code |= ERR_ALERT | ERR_FATAL;
1893 goto out;
1894 }
1895
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001896 cur_arg = 2;
1897 if (srv_tmpl) {
1898 /* Parse server-template <nb | range> arg. */
1899 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1900 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1901 file, linenum, args[0], args[cur_arg]);
1902 err_code |= ERR_ALERT | ERR_FATAL;
1903 goto out;
1904 }
1905 cur_arg++;
1906 }
1907
Willy Tarreau272adea2014-03-31 10:39:59 +02001908 if (!defsrv) {
1909 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001910 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001911 struct protocol *proto;
1912
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001913 newsrv = new_server(curproxy);
1914 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001915 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1916 err_code |= ERR_ALERT | ERR_ABORT;
1917 goto out;
1918 }
1919
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001920 if (srv_tmpl) {
1921 newsrv->tmpl_info.nb_low = tmpl_range_low;
1922 newsrv->tmpl_info.nb_high = tmpl_range_high;
1923 }
1924
Willy Tarreau272adea2014-03-31 10:39:59 +02001925 /* the servers are linked backwards first */
1926 newsrv->next = curproxy->srv;
1927 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001928 newsrv->conf.file = strdup(file);
1929 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 /* Note: for a server template, its id is its prefix.
1931 * This is a temporary id which will be used for server allocations to come
1932 * after parsing.
1933 */
1934 if (srv)
1935 newsrv->id = strdup(args[1]);
1936 else
1937 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001938
1939 /* several ways to check the port component :
1940 * - IP => port=+0, relative (IPv4 only)
1941 * - IP: => port=+0, relative
1942 * - IP:N => port=N, absolute
1943 * - IP:+N => port=+N, relative
1944 * - IP:-N => port=-N, relative
1945 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001946 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001947 if (!sk) {
1948 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1949 err_code |= ERR_ALERT | ERR_FATAL;
1950 goto out;
1951 }
1952
1953 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001954 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001955 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1956 file, linenum, args[0], args[1]);
1957 err_code |= ERR_ALERT | ERR_FATAL;
1958 goto out;
1959 }
1960
1961 if (!port1 || !port2) {
1962 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001963 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001964 }
1965 else if (port1 != port2) {
1966 /* port range */
1967 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1968 file, linenum, args[0], args[1], args[2]);
1969 err_code |= ERR_ALERT | ERR_FATAL;
1970 goto out;
1971 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001972
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001973 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001974 if (fqdn) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02001975 if (fqdn[0] == '_') { /* SRV record */
Olivier Houchard8da5f982017-08-04 18:35:36 +02001976 /* Check if a SRV request already exists, and if not, create it */
Christopher Faulet67957bd2017-09-27 11:00:59 +02001977 if ((newsrv->srvrq = find_srvrq_by_name(fqdn, curproxy)) == NULL)
1978 newsrv->srvrq = new_dns_srvrq(newsrv, fqdn);
1979 if (newsrv->srvrq == NULL) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02001980 err_code |= ERR_ALERT | ERR_FATAL;
1981 goto out;
Christopher Faulet67957bd2017-09-27 11:00:59 +02001982 }
1983 }
1984 else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
1985 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
1986 file, linenum, newsrv->id);
1987 err_code |= ERR_ALERT | ERR_FATAL;
1988 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001989 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001990 }
1991
Willy Tarreau272adea2014-03-31 10:39:59 +02001992 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001993 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001994
Olivier Houchard8da5f982017-08-04 18:35:36 +02001995 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001996 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001997 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001998 err_code |= ERR_ALERT | ERR_FATAL;
1999 goto out;
2000 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002001
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002002 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002003 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002004 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002005 } else {
2006 newsrv = &curproxy->defsrv;
2007 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002008 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002009 }
2010
2011 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002012 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002013 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2014 if (err) {
2015 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2016 file, linenum, *err, newsrv->id);
2017 err_code |= ERR_ALERT | ERR_FATAL;
2018 goto out;
2019 }
2020 if (val <= 0) {
2021 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2022 file, linenum, val, args[cur_arg], newsrv->id);
2023 err_code |= ERR_ALERT | ERR_FATAL;
2024 goto out;
2025 }
2026 newsrv->agent.inter = val;
2027 cur_arg += 2;
2028 }
Misiekea849332017-01-09 09:39:51 +01002029 else if (!strcmp(args[cur_arg], "agent-addr")) {
2030 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2031 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2032 goto out;
2033 }
2034
2035 cur_arg += 2;
2036 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002037 else if (!strcmp(args[cur_arg], "agent-port")) {
2038 global.maxsock++;
2039 newsrv->agent.port = atol(args[cur_arg + 1]);
2040 cur_arg += 2;
2041 }
James Brown55f9ff12015-10-21 18:19:05 -07002042 else if (!strcmp(args[cur_arg], "agent-send")) {
2043 global.maxsock++;
2044 free(newsrv->agent.send_string);
2045 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2046 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2047 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2048 cur_arg += 2;
2049 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002050 else if (!strcmp(args[cur_arg], "init-addr")) {
2051 char *p, *end;
2052 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002053 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002054
2055 newsrv->init_addr_methods = 0;
2056 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2057
2058 for (p = args[cur_arg + 1]; *p; p = end) {
2059 /* cut on next comma */
2060 for (end = p; *end && *end != ','; end++);
2061 if (*end)
2062 *(end++) = 0;
2063
Willy Tarreau4310d362016-11-02 15:05:56 +01002064 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002065 if (!strcmp(p, "libc")) {
2066 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2067 }
2068 else if (!strcmp(p, "last")) {
2069 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2070 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002071 else if (!strcmp(p, "none")) {
2072 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2073 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002074 else if (str2ip2(p, &sa, 0)) {
2075 if (is_addr(&newsrv->init_addr)) {
2076 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2077 file, linenum, args[cur_arg], p);
2078 err_code |= ERR_ALERT | ERR_FATAL;
2079 goto out;
2080 }
2081 newsrv->init_addr = sa;
2082 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2083 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002084 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002085 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002086 file, linenum, args[cur_arg], p);
2087 err_code |= ERR_ALERT | ERR_FATAL;
2088 goto out;
2089 }
2090 if (!done) {
2091 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2092 file, linenum, args[cur_arg], p);
2093 err_code |= ERR_ALERT | ERR_FATAL;
2094 goto out;
2095 }
2096 }
2097 cur_arg += 2;
2098 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002099 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002100 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002101 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2102 cur_arg += 2;
2103 }
2104 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2105 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002106 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002107 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002108 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002109 else {
2110 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2111 file, linenum, args[cur_arg]);
2112 err_code |= ERR_ALERT | ERR_FATAL;
2113 goto out;
2114 }
2115 cur_arg += 2;
2116 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002117 else if (!strcmp(args[cur_arg], "resolve-net")) {
2118 char *p, *e;
2119 unsigned char mask;
2120 struct dns_options *opt;
2121
2122 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2123 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2124 file, linenum, args[cur_arg]);
2125 err_code |= ERR_ALERT | ERR_FATAL;
2126 goto out;
2127 }
2128
2129 opt = &newsrv->dns_opts;
2130
2131 /* Split arguments by comma, and convert it from ipv4 or ipv6
2132 * string network in in_addr or in6_addr.
2133 */
2134 p = args[cur_arg + 1];
2135 e = p;
2136 while (*p != '\0') {
2137 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002138 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002139 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2140 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2141 err_code |= ERR_ALERT | ERR_FATAL;
2142 goto out;
2143 }
2144 /* look for end or comma. */
2145 while (*e != ',' && *e != '\0')
2146 e++;
2147 if (*e == ',') {
2148 *e = '\0';
2149 e++;
2150 }
2151 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2152 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2153 /* Try to convert input string from ipv4 or ipv6 network. */
2154 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2155 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2156 &mask)) {
2157 /* Try to convert input string from ipv6 network. */
2158 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2159 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2160 } else {
2161 /* All network conversions fail, retrun error. */
2162 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2163 file, linenum, args[cur_arg], p);
2164 err_code |= ERR_ALERT | ERR_FATAL;
2165 goto out;
2166 }
2167 opt->pref_net_nb++;
2168 p = e;
2169 }
2170
2171 cur_arg += 2;
2172 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002173 else if (!strcmp(args[cur_arg], "rise")) {
2174 if (!*args[cur_arg + 1]) {
2175 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2176 file, linenum, args[cur_arg]);
2177 err_code |= ERR_ALERT | ERR_FATAL;
2178 goto out;
2179 }
2180
2181 newsrv->check.rise = atol(args[cur_arg + 1]);
2182 if (newsrv->check.rise <= 0) {
2183 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2184 file, linenum, args[cur_arg]);
2185 err_code |= ERR_ALERT | ERR_FATAL;
2186 goto out;
2187 }
2188
2189 if (newsrv->check.health)
2190 newsrv->check.health = newsrv->check.rise;
2191 cur_arg += 2;
2192 }
2193 else if (!strcmp(args[cur_arg], "fall")) {
2194 newsrv->check.fall = atol(args[cur_arg + 1]);
2195
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 if (newsrv->check.fall <= 0) {
2204 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2205 file, linenum, args[cur_arg]);
2206 err_code |= ERR_ALERT | ERR_FATAL;
2207 goto out;
2208 }
2209
2210 cur_arg += 2;
2211 }
2212 else if (!strcmp(args[cur_arg], "inter")) {
2213 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2214 if (err) {
2215 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2216 file, linenum, *err, newsrv->id);
2217 err_code |= ERR_ALERT | ERR_FATAL;
2218 goto out;
2219 }
2220 if (val <= 0) {
2221 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2222 file, linenum, val, args[cur_arg], newsrv->id);
2223 err_code |= ERR_ALERT | ERR_FATAL;
2224 goto out;
2225 }
2226 newsrv->check.inter = val;
2227 cur_arg += 2;
2228 }
2229 else if (!strcmp(args[cur_arg], "fastinter")) {
2230 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2231 if (err) {
2232 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2233 file, linenum, *err, newsrv->id);
2234 err_code |= ERR_ALERT | ERR_FATAL;
2235 goto out;
2236 }
2237 if (val <= 0) {
2238 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2239 file, linenum, val, args[cur_arg], newsrv->id);
2240 err_code |= ERR_ALERT | ERR_FATAL;
2241 goto out;
2242 }
2243 newsrv->check.fastinter = val;
2244 cur_arg += 2;
2245 }
2246 else if (!strcmp(args[cur_arg], "downinter")) {
2247 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2248 if (err) {
2249 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2250 file, linenum, *err, newsrv->id);
2251 err_code |= ERR_ALERT | ERR_FATAL;
2252 goto out;
2253 }
2254 if (val <= 0) {
2255 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2256 file, linenum, val, args[cur_arg], newsrv->id);
2257 err_code |= ERR_ALERT | ERR_FATAL;
2258 goto out;
2259 }
2260 newsrv->check.downinter = val;
2261 cur_arg += 2;
2262 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002263 else if (!strcmp(args[cur_arg], "port")) {
2264 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002265 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002266 cur_arg += 2;
2267 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002268 else if (!strcmp(args[cur_arg], "weight")) {
2269 int w;
2270 w = atol(args[cur_arg + 1]);
2271 if (w < 0 || w > SRV_UWGHT_MAX) {
2272 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2273 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2274 err_code |= ERR_ALERT | ERR_FATAL;
2275 goto out;
2276 }
2277 newsrv->uweight = newsrv->iweight = w;
2278 cur_arg += 2;
2279 }
2280 else if (!strcmp(args[cur_arg], "minconn")) {
2281 newsrv->minconn = atol(args[cur_arg + 1]);
2282 cur_arg += 2;
2283 }
2284 else if (!strcmp(args[cur_arg], "maxconn")) {
2285 newsrv->maxconn = atol(args[cur_arg + 1]);
2286 cur_arg += 2;
2287 }
2288 else if (!strcmp(args[cur_arg], "maxqueue")) {
2289 newsrv->maxqueue = atol(args[cur_arg + 1]);
2290 cur_arg += 2;
2291 }
2292 else if (!strcmp(args[cur_arg], "slowstart")) {
2293 /* slowstart is stored in seconds */
2294 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2295 if (err) {
2296 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2297 file, linenum, *err, newsrv->id);
2298 err_code |= ERR_ALERT | ERR_FATAL;
2299 goto out;
2300 }
2301 newsrv->slowstart = (val + 999) / 1000;
2302 cur_arg += 2;
2303 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 else if (!strcmp(args[cur_arg], "on-error")) {
2305 if (!strcmp(args[cur_arg + 1], "fastinter"))
2306 newsrv->onerror = HANA_ONERR_FASTINTER;
2307 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2308 newsrv->onerror = HANA_ONERR_FAILCHK;
2309 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2310 newsrv->onerror = HANA_ONERR_SUDDTH;
2311 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2312 newsrv->onerror = HANA_ONERR_MARKDWN;
2313 else {
2314 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2315 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2316 file, linenum, args[cur_arg], args[cur_arg + 1]);
2317 err_code |= ERR_ALERT | ERR_FATAL;
2318 goto out;
2319 }
2320
2321 cur_arg += 2;
2322 }
2323 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2324 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2325 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2326 else {
2327 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2328 file, linenum, args[cur_arg], args[cur_arg + 1]);
2329 err_code |= ERR_ALERT | ERR_FATAL;
2330 goto out;
2331 }
2332
2333 cur_arg += 2;
2334 }
2335 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2336 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2337 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2338 else {
2339 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2340 file, linenum, args[cur_arg], args[cur_arg + 1]);
2341 err_code |= ERR_ALERT | ERR_FATAL;
2342 goto out;
2343 }
2344
2345 cur_arg += 2;
2346 }
2347 else if (!strcmp(args[cur_arg], "error-limit")) {
2348 if (!*args[cur_arg + 1]) {
2349 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2350 file, linenum, args[cur_arg]);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354
2355 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2356
2357 if (newsrv->consecutive_errors_limit <= 0) {
2358 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2359 file, linenum, args[cur_arg]);
2360 err_code |= ERR_ALERT | ERR_FATAL;
2361 goto out;
2362 }
2363 cur_arg += 2;
2364 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002365 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002366 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2367 file, linenum, "usesrc", "source");
2368 err_code |= ERR_ALERT | ERR_FATAL;
2369 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002370 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002371 else {
2372 static int srv_dumped;
2373 struct srv_kw *kw;
2374 char *err;
2375
2376 kw = srv_find_kw(args[cur_arg]);
2377 if (kw) {
2378 char *err = NULL;
2379 int code;
2380
2381 if (!kw->parse) {
2382 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2383 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002384 if (kw->skip != -1)
2385 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002386 err_code |= ERR_ALERT | ERR_FATAL;
2387 goto out;
2388 }
2389
2390 if (defsrv && !kw->default_ok) {
2391 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2392 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002393 if (kw->skip != -1)
2394 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002395 err_code |= ERR_ALERT;
2396 continue;
2397 }
2398
2399 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2400 err_code |= code;
2401
2402 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002403 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002404 if (code & ERR_FATAL) {
2405 free(err);
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 goto out;
2409 }
2410 }
2411 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002412 if (kw->skip != -1)
2413 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002414 continue;
2415 }
2416
2417 err = NULL;
2418 if (!srv_dumped) {
2419 srv_dump_kws(&err);
2420 indent_msg(&err, 4);
2421 srv_dumped = 1;
2422 }
2423
2424 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2425 file, linenum, args[0], args[1], args[cur_arg],
2426 err ? " Registered keywords :" : "", err ? err : "");
2427 free(err);
2428
2429 err_code |= ERR_ALERT | ERR_FATAL;
2430 goto out;
2431 }
2432 }
2433
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002434 if (!defsrv)
2435 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2436 if (err_code & ERR_FATAL)
2437 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002438 if (srv_tmpl)
2439 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002440 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002441 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002442 return 0;
2443
2444 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002445 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002446 free(errmsg);
2447 return err_code;
2448}
2449
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002450/* Returns a pointer to the first server matching either id <id>.
2451 * NULL is returned if no match is found.
2452 * the lookup is performed in the backend <bk>
2453 */
2454struct server *server_find_by_id(struct proxy *bk, int id)
2455{
2456 struct eb32_node *eb32;
2457 struct server *curserver;
2458
2459 if (!bk || (id ==0))
2460 return NULL;
2461
2462 /* <bk> has no backend capabilities, so it can't have a server */
2463 if (!(bk->cap & PR_CAP_BE))
2464 return NULL;
2465
2466 curserver = NULL;
2467
2468 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2469 if (eb32)
2470 curserver = container_of(eb32, struct server, conf.id);
2471
2472 return curserver;
2473}
2474
2475/* Returns a pointer to the first server matching either name <name>, or id
2476 * if <name> starts with a '#'. NULL is returned if no match is found.
2477 * the lookup is performed in the backend <bk>
2478 */
2479struct server *server_find_by_name(struct proxy *bk, const char *name)
2480{
2481 struct server *curserver;
2482
2483 if (!bk || !name)
2484 return NULL;
2485
2486 /* <bk> has no backend capabilities, so it can't have a server */
2487 if (!(bk->cap & PR_CAP_BE))
2488 return NULL;
2489
2490 curserver = NULL;
2491 if (*name == '#') {
2492 curserver = server_find_by_id(bk, atoi(name + 1));
2493 if (curserver)
2494 return curserver;
2495 }
2496 else {
2497 curserver = bk->srv;
2498
2499 while (curserver && (strcmp(curserver->id, name) != 0))
2500 curserver = curserver->next;
2501
2502 if (curserver)
2503 return curserver;
2504 }
2505
2506 return NULL;
2507}
2508
2509struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2510{
2511 struct server *byname;
2512 struct server *byid;
2513
2514 if (!name && !id)
2515 return NULL;
2516
2517 if (diff)
2518 *diff = 0;
2519
2520 byname = byid = NULL;
2521
2522 if (name) {
2523 byname = server_find_by_name(bk, name);
2524 if (byname && (!id || byname->puid == id))
2525 return byname;
2526 }
2527
2528 /* remaining possibilities :
2529 * - name not set
2530 * - name set but not found
2531 * - name found but ID doesn't match
2532 */
2533 if (id) {
2534 byid = server_find_by_id(bk, id);
2535 if (byid) {
2536 if (byname) {
2537 /* use id only if forced by configuration */
2538 if (byid->flags & SRV_F_FORCED_ID) {
2539 if (diff)
2540 *diff |= 2;
2541 return byid;
2542 }
2543 else {
2544 if (diff)
2545 *diff |= 1;
2546 return byname;
2547 }
2548 }
2549
2550 /* remaining possibilities:
2551 * - name not set
2552 * - name set but not found
2553 */
2554 if (name && diff)
2555 *diff |= 2;
2556 return byid;
2557 }
2558
2559 /* id bot found */
2560 if (byname) {
2561 if (diff)
2562 *diff |= 1;
2563 return byname;
2564 }
2565 }
2566
2567 return NULL;
2568}
2569
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002570/* Update a server state using the parameters available in the params list */
2571static void srv_update_state(struct server *srv, int version, char **params)
2572{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002573 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002574 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002575
2576 /* fields since version 1
2577 * and common to all other upcoming versions
2578 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002579 enum srv_state srv_op_state;
2580 enum srv_admin srv_admin_state;
2581 unsigned srv_uweight, srv_iweight;
2582 unsigned long srv_last_time_change;
2583 short srv_check_status;
2584 enum chk_result srv_check_result;
2585 int srv_check_health;
2586 int srv_check_state, srv_agent_state;
2587 int bk_f_forced_id;
2588 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002589 int fqdn_set_by_cli;
2590 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002591 const char *port_str;
2592 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002593
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002594 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002595 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002596 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002597 switch (version) {
2598 case 1:
2599 /*
2600 * now we can proceed with server's state update:
2601 * srv_addr: params[0]
2602 * srv_op_state: params[1]
2603 * srv_admin_state: params[2]
2604 * srv_uweight: params[3]
2605 * srv_iweight: params[4]
2606 * srv_last_time_change: params[5]
2607 * srv_check_status: params[6]
2608 * srv_check_result: params[7]
2609 * srv_check_health: params[8]
2610 * srv_check_state: params[9]
2611 * srv_agent_state: params[10]
2612 * bk_f_forced_id: params[11]
2613 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002614 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002615 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002616 */
2617
2618 /* validating srv_op_state */
2619 p = NULL;
2620 errno = 0;
2621 srv_op_state = strtol(params[1], &p, 10);
2622 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2623 (srv_op_state != SRV_ST_STOPPED &&
2624 srv_op_state != SRV_ST_STARTING &&
2625 srv_op_state != SRV_ST_RUNNING &&
2626 srv_op_state != SRV_ST_STOPPING)) {
2627 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2628 }
2629
2630 /* validating srv_admin_state */
2631 p = NULL;
2632 errno = 0;
2633 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002634 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002635
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002636 /* inherited statuses will be recomputed later.
2637 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2638 */
2639 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002640
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002641 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2642 (srv_admin_state != 0 &&
2643 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002644 srv_admin_state != SRV_ADMF_CMAINT &&
2645 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002646 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002647 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002648 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2649 }
2650
2651 /* validating srv_uweight */
2652 p = NULL;
2653 errno = 0;
2654 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002655 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002656 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2657
2658 /* validating srv_iweight */
2659 p = NULL;
2660 errno = 0;
2661 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002662 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002663 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2664
2665 /* validating srv_last_time_change */
2666 p = NULL;
2667 errno = 0;
2668 srv_last_time_change = strtol(params[5], &p, 10);
2669 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2670 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2671
2672 /* validating srv_check_status */
2673 p = NULL;
2674 errno = 0;
2675 srv_check_status = strtol(params[6], &p, 10);
2676 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2677 (srv_check_status >= HCHK_STATUS_SIZE))
2678 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2679
2680 /* validating srv_check_result */
2681 p = NULL;
2682 errno = 0;
2683 srv_check_result = strtol(params[7], &p, 10);
2684 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2685 (srv_check_result != CHK_RES_UNKNOWN &&
2686 srv_check_result != CHK_RES_NEUTRAL &&
2687 srv_check_result != CHK_RES_FAILED &&
2688 srv_check_result != CHK_RES_PASSED &&
2689 srv_check_result != CHK_RES_CONDPASS)) {
2690 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2691 }
2692
2693 /* validating srv_check_health */
2694 p = NULL;
2695 errno = 0;
2696 srv_check_health = strtol(params[8], &p, 10);
2697 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2698 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2699
2700 /* validating srv_check_state */
2701 p = NULL;
2702 errno = 0;
2703 srv_check_state = strtol(params[9], &p, 10);
2704 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2705 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2706 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2707
2708 /* validating srv_agent_state */
2709 p = NULL;
2710 errno = 0;
2711 srv_agent_state = strtol(params[10], &p, 10);
2712 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2713 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2714 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2715
2716 /* validating bk_f_forced_id */
2717 p = NULL;
2718 errno = 0;
2719 bk_f_forced_id = strtol(params[11], &p, 10);
2720 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2721 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2722
2723 /* validating srv_f_forced_id */
2724 p = NULL;
2725 errno = 0;
2726 srv_f_forced_id = strtol(params[12], &p, 10);
2727 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2728 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2729
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002730 /* validating srv_fqdn */
2731 fqdn = params[13];
2732 if (fqdn && *fqdn == '-')
2733 fqdn = NULL;
2734 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2735 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2736 fqdn = NULL;
2737 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002738
Frédéric Lécaille31694712017-08-01 08:47:19 +02002739 port_str = params[14];
2740 if (port_str) {
2741 port = strl2uic(port_str, strlen(port_str));
2742 if (port > USHRT_MAX) {
2743 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2744 port_str = NULL;
2745 }
2746 }
2747
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002748 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002749 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002750 goto out;
2751
2752 /* recover operational state and apply it to this server
2753 * and all servers tracking this one */
2754 switch (srv_op_state) {
2755 case SRV_ST_STOPPED:
2756 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002757 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002758 break;
2759 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002760 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002761 break;
2762 case SRV_ST_STOPPING:
2763 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002764 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002765 break;
2766 case SRV_ST_RUNNING:
2767 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002768 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002769 break;
2770 }
2771
2772 /* When applying server state, the following rules apply:
2773 * - in case of a configuration change, we apply the setting from the new
2774 * configuration, regardless of old running state
2775 * - if no configuration change, we apply old running state only if old running
2776 * state is different from new configuration state
2777 */
2778 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002779 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2780 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002781 srv_adm_set_maint(srv);
2782 else
2783 srv_adm_set_ready(srv);
2784 }
2785 /* configuration is the same, let's compate old running state and new conf state */
2786 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002787 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002788 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002789 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002790 srv_adm_set_ready(srv);
2791 }
2792 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002793 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002794 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002795 * (srv->iweight is the weight set up in configuration).
2796 * There are two possible reasons for FDRAIN to have been present :
2797 * - previous config weight was zero
2798 * - "set server b/s drain" was sent to the CLI
2799 *
2800 * In the first case, we simply want to drop this drain state
2801 * if the new weight is not zero anymore, meaning the administrator
2802 * has intentionally turned the weight back to a positive value to
2803 * enable the server again after an operation. In the second case,
2804 * the drain state was forced on the CLI regardless of the config's
2805 * weight so we don't want a change to the config weight to lose this
2806 * status. What this means is :
2807 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2808 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002809 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002810 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002811 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002812 }
2813
2814 srv->last_change = date.tv_sec - srv_last_time_change;
2815 srv->check.status = srv_check_status;
2816 srv->check.result = srv_check_result;
2817 srv->check.health = srv_check_health;
2818
2819 /* Only case we want to apply is removing ENABLED flag which could have been
2820 * done by the "disable health" command over the stats socket
2821 */
2822 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2823 (srv_check_state & CHK_ST_CONFIGURED) &&
2824 !(srv_check_state & CHK_ST_ENABLED))
2825 srv->check.state &= ~CHK_ST_ENABLED;
2826
2827 /* Only case we want to apply is removing ENABLED flag which could have been
2828 * done by the "disable agent" command over the stats socket
2829 */
2830 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2831 (srv_agent_state & CHK_ST_CONFIGURED) &&
2832 !(srv_agent_state & CHK_ST_ENABLED))
2833 srv->agent.state &= ~CHK_ST_ENABLED;
2834
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002835 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2836 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002837 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002838 * It means that a configuration file change has precedence over a unix socket change
2839 * for server's weight
2840 *
2841 * by default, HAProxy applies the following weight when parsing the configuration
2842 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002843 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002844 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002845 srv->uweight = srv_uweight;
2846 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002847 server_recalc_eweight(srv);
2848
Willy Tarreaue5a60682016-11-09 14:54:53 +01002849 /* load server IP address */
2850 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002851
2852 if (fqdn && srv->hostname) {
2853 if (!strcmp(srv->hostname, fqdn)) {
2854 /* Here we reset the 'set from stats socket FQDN' flag
2855 * to support such transitions:
2856 * Let's say initial FQDN value is foo1 (in configuration file).
2857 * - FQDN changed from stats socket, from foo1 to foo2 value,
2858 * - FQDN changed again from file configuration (with the same previous value
2859 set from stats socket, from foo1 to foo2 value),
2860 * - reload for any other reason than a FQDN modification,
2861 * the configuration file FQDN matches the fqdn server state file value.
2862 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2863 * any futher FQDN modification.
2864 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002865 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002866 }
2867 else {
2868 /* If the FDQN has been changed from stats socket,
2869 * apply fqdn state file value (which is the value set
2870 * from stats socket).
2871 */
2872 if (fqdn_set_by_cli) {
2873 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02002874 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002875 }
2876 }
2877 }
2878
Frédéric Lécaille31694712017-08-01 08:47:19 +02002879 if (port_str)
2880 srv->svc_port = port;
2881
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002882 break;
2883 default:
2884 chunk_appendf(msg, ", version '%d' not supported", version);
2885 }
2886
2887 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002888 if (msg->len) {
2889 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002890 Warning("server-state application failed for server '%s/%s'%s",
2891 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002892 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002893}
2894
2895/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2896 * For each proxy, it does the following:
2897 * - opens its server state file (either one or local one)
2898 * - read whole file, line by line
2899 * - analyse each line to check if it matches our current backend:
2900 * - backend name matches
2901 * - backend id matches if id is forced and name doesn't match
2902 * - if the server pointed by the line is found, then state is applied
2903 *
2904 * If the running backend uuid or id differs from the state file, then HAProxy reports
2905 * a warning.
2906 */
2907void apply_server_state(void)
2908{
2909 char *cur, *end;
2910 char mybuf[SRV_STATE_LINE_MAXLEN];
2911 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002912 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2913 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 int arg, srv_arg, version, diff;
2915 FILE *f;
2916 char *filepath;
2917 char globalfilepath[MAXPATHLEN + 1];
2918 char localfilepath[MAXPATHLEN + 1];
2919 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2920 extern struct proxy *proxy;
2921 struct proxy *curproxy, *bk;
2922 struct server *srv;
2923
2924 globalfilepathlen = 0;
2925 /* create the globalfilepath variable */
2926 if (global.server_state_file) {
2927 /* absolute path or no base directory provided */
2928 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2929 len = strlen(global.server_state_file);
2930 if (len > MAXPATHLEN) {
2931 globalfilepathlen = 0;
2932 goto globalfileerror;
2933 }
2934 memcpy(globalfilepath, global.server_state_file, len);
2935 globalfilepath[len] = '\0';
2936 globalfilepathlen = len;
2937 }
2938 else if (global.server_state_base) {
2939 len = strlen(global.server_state_base);
2940 globalfilepathlen += len;
2941
2942 if (globalfilepathlen > MAXPATHLEN) {
2943 globalfilepathlen = 0;
2944 goto globalfileerror;
2945 }
2946 strncpy(globalfilepath, global.server_state_base, len);
2947 globalfilepath[globalfilepathlen] = 0;
2948
2949 /* append a slash if needed */
2950 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2951 if (globalfilepathlen + 1 > MAXPATHLEN) {
2952 globalfilepathlen = 0;
2953 goto globalfileerror;
2954 }
2955 globalfilepath[globalfilepathlen++] = '/';
2956 }
2957
2958 len = strlen(global.server_state_file);
2959 if (globalfilepathlen + len > MAXPATHLEN) {
2960 globalfilepathlen = 0;
2961 goto globalfileerror;
2962 }
2963 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2964 globalfilepathlen += len;
2965 globalfilepath[globalfilepathlen++] = 0;
2966 }
2967 }
2968 globalfileerror:
2969 if (globalfilepathlen == 0)
2970 globalfilepath[0] = '\0';
2971
2972 /* read servers state from local file */
2973 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2974 /* servers are only in backends */
2975 if (!(curproxy->cap & PR_CAP_BE))
2976 continue;
2977 fileopenerr = 0;
2978 filepath = NULL;
2979
2980 /* search server state file path and name */
2981 switch (curproxy->load_server_state_from_file) {
2982 /* read servers state from global file */
2983 case PR_SRV_STATE_FILE_GLOBAL:
2984 /* there was an error while generating global server state file path */
2985 if (globalfilepathlen == 0)
2986 continue;
2987 filepath = globalfilepath;
2988 fileopenerr = 1;
2989 break;
2990 /* this backend has its own file */
2991 case PR_SRV_STATE_FILE_LOCAL:
2992 localfilepathlen = 0;
2993 localfilepath[0] = '\0';
2994 len = 0;
2995 /* create the localfilepath variable */
2996 /* absolute path or no base directory provided */
2997 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
2998 len = strlen(curproxy->server_state_file_name);
2999 if (len > MAXPATHLEN) {
3000 localfilepathlen = 0;
3001 goto localfileerror;
3002 }
3003 memcpy(localfilepath, curproxy->server_state_file_name, len);
3004 localfilepath[len] = '\0';
3005 localfilepathlen = len;
3006 }
3007 else if (global.server_state_base) {
3008 len = strlen(global.server_state_base);
3009 localfilepathlen += len;
3010
3011 if (localfilepathlen > MAXPATHLEN) {
3012 localfilepathlen = 0;
3013 goto localfileerror;
3014 }
3015 strncpy(localfilepath, global.server_state_base, len);
3016 localfilepath[localfilepathlen] = 0;
3017
3018 /* append a slash if needed */
3019 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3020 if (localfilepathlen + 1 > MAXPATHLEN) {
3021 localfilepathlen = 0;
3022 goto localfileerror;
3023 }
3024 localfilepath[localfilepathlen++] = '/';
3025 }
3026
3027 len = strlen(curproxy->server_state_file_name);
3028 if (localfilepathlen + len > MAXPATHLEN) {
3029 localfilepathlen = 0;
3030 goto localfileerror;
3031 }
3032 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3033 localfilepathlen += len;
3034 localfilepath[localfilepathlen++] = 0;
3035 }
3036 filepath = localfilepath;
3037 localfileerror:
3038 if (localfilepathlen == 0)
3039 localfilepath[0] = '\0';
3040
3041 break;
3042 case PR_SRV_STATE_FILE_NONE:
3043 default:
3044 continue;
3045 }
3046
3047 /* preload global state file */
3048 errno = 0;
3049 f = fopen(filepath, "r");
3050 if (errno && fileopenerr)
3051 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3052 if (!f)
3053 continue;
3054
3055 mybuf[0] = '\0';
3056 mybuflen = 0;
3057 version = 0;
3058
3059 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003060 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3061 Warning("Can't read first line of the server state file '%s'\n", filepath);
3062 goto fileclose;
3063 }
3064
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003065 cur = mybuf;
3066 version = atoi(cur);
3067 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3068 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003069 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003070
3071 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3072 int bk_f_forced_id = 0;
3073 int check_id = 0;
3074 int check_name = 0;
3075
3076 mybuflen = strlen(mybuf);
3077 cur = mybuf;
3078 end = cur + mybuflen;
3079
3080 bk = NULL;
3081 srv = NULL;
3082
3083 /* we need at least one character */
3084 if (mybuflen == 0)
3085 continue;
3086
3087 /* ignore blank characters at the beginning of the line */
3088 while (isspace(*cur))
3089 ++cur;
3090
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003091 /* Ignore empty or commented lines */
3092 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003093 continue;
3094
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003095 /* truncated lines */
3096 if (mybuf[mybuflen - 1] != '\n') {
3097 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003098 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003099 }
3100
3101 /* Removes trailing '\n' */
3102 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003103
3104 /* we're now ready to move the line into *srv_params[] */
3105 params[0] = cur;
3106 arg = 1;
3107 srv_arg = 0;
3108 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3109 if (isspace(*cur)) {
3110 *cur = '\0';
3111 ++cur;
3112 while (isspace(*cur))
3113 ++cur;
3114 switch (version) {
3115 case 1:
3116 /*
3117 * srv_addr: params[4] => srv_params[0]
3118 * srv_op_state: params[5] => srv_params[1]
3119 * srv_admin_state: params[6] => srv_params[2]
3120 * srv_uweight: params[7] => srv_params[3]
3121 * srv_iweight: params[8] => srv_params[4]
3122 * srv_last_time_change: params[9] => srv_params[5]
3123 * srv_check_status: params[10] => srv_params[6]
3124 * srv_check_result: params[11] => srv_params[7]
3125 * srv_check_health: params[12] => srv_params[8]
3126 * srv_check_state: params[13] => srv_params[9]
3127 * srv_agent_state: params[14] => srv_params[10]
3128 * bk_f_forced_id: params[15] => srv_params[11]
3129 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003130 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003131 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003132 */
3133 if (arg >= 4) {
3134 srv_params[srv_arg] = cur;
3135 ++srv_arg;
3136 }
3137 break;
3138 }
3139
3140 params[arg] = cur;
3141 ++arg;
3142 }
3143 else {
3144 ++cur;
3145 }
3146 }
3147
3148 /* if line is incomplete line, then ignore it.
3149 * otherwise, update useful flags */
3150 switch (version) {
3151 case 1:
3152 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3153 continue;
3154 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3155 check_id = (atoi(params[0]) == curproxy->uuid);
3156 check_name = (strcmp(curproxy->id, params[1]) == 0);
3157 break;
3158 }
3159
3160 diff = 0;
3161 bk = curproxy;
3162
3163 /* if backend can't be found, let's continue */
3164 if (!check_id && !check_name)
3165 continue;
3166 else if (!check_id && check_name) {
3167 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3168 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3169 }
3170 else if (check_id && !check_name) {
3171 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3172 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3173 /* if name doesn't match, we still want to update curproxy if the backend id
3174 * was forced in previous the previous configuration */
3175 if (!bk_f_forced_id)
3176 continue;
3177 }
3178
3179 /* look for the server by its id: param[2] */
3180 /* else look for the server by its name: param[3] */
3181 diff = 0;
3182 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3183
3184 if (!srv) {
3185 /* if no server found, then warning and continue with next line */
3186 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3187 params[3], params[2], params[0], params[1]);
3188 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3189 params[3], params[2], params[0], params[1]);
3190 continue;
3191 }
3192 else if (diff & PR_FBM_MISMATCH_ID) {
3193 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);
3194 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 +02003195 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003196 }
3197 else if (diff & PR_FBM_MISMATCH_NAME) {
3198 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);
3199 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 +02003200 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003201 }
3202
3203 /* now we can proceed with server's state update */
3204 srv_update_state(srv, version, srv_params);
3205 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003206fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003207 fclose(f);
3208 }
3209}
3210
Simon Horman7d09b9a2013-02-12 10:45:51 +09003211/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003212 * update a server's current IP address.
3213 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3214 * ip is in network format.
3215 * updater is a string which contains an information about the requester of the update.
3216 * updater is used if not NULL.
3217 *
3218 * A log line and a stderr warning message is generated based on server's backend options.
3219 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003220int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003221{
3222 /* generates a log line and a warning on stderr */
3223 if (1) {
3224 /* book enough space for both IPv4 and IPv6 */
3225 char oldip[INET6_ADDRSTRLEN];
3226 char newip[INET6_ADDRSTRLEN];
3227
3228 memset(oldip, '\0', INET6_ADDRSTRLEN);
3229 memset(newip, '\0', INET6_ADDRSTRLEN);
3230
3231 /* copy old IP address in a string */
3232 switch (s->addr.ss_family) {
3233 case AF_INET:
3234 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3235 break;
3236 case AF_INET6:
3237 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3238 break;
3239 };
3240
3241 /* copy new IP address in a string */
3242 switch (ip_sin_family) {
3243 case AF_INET:
3244 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3245 break;
3246 case AF_INET6:
3247 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3248 break;
3249 };
3250
3251 /* save log line into a buffer */
3252 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3253 s->proxy->id, s->id, oldip, newip, updater);
3254
3255 /* write the buffer on stderr */
3256 Warning("%s.\n", trash.str);
3257
3258 /* send a log */
3259 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3260 }
3261
3262 /* save the new IP family */
3263 s->addr.ss_family = ip_sin_family;
3264 /* save the new IP address */
3265 switch (ip_sin_family) {
3266 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003267 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003268 break;
3269 case AF_INET6:
3270 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3271 break;
3272 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003273 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003274
3275 return 0;
3276}
3277
3278/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003279 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3280 *
3281 * Caller can pass its name through <updater> to get it integrated in the response message
3282 * returned by the function.
3283 *
3284 * The function first does the following, in that order:
3285 * - validates the new addr and/or port
3286 * - checks if an update is required (new IP or port is different than current ones)
3287 * - checks the update is allowed:
3288 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3289 * - allow all changes if no CHECKS are configured
3290 * - if CHECK is configured:
3291 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3292 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3293 * conditions are met
3294 */
3295const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3296{
3297 struct sockaddr_storage sa;
3298 int ret, port_change_required;
3299 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003300 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003301 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003302 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003303
3304 msg = get_trash_chunk();
3305 chunk_reset(msg);
3306
3307 if (addr) {
3308 memset(&sa, 0, sizeof(struct sockaddr_storage));
3309 if (str2ip2(addr, &sa, 0) == NULL) {
3310 chunk_printf(msg, "Invalid addr '%s'", addr);
3311 goto out;
3312 }
3313
3314 /* changes are allowed on AF_INET* families only */
3315 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3316 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3317 goto out;
3318 }
3319
3320 /* collecting data currently setup */
3321 memset(current_addr, '\0', sizeof(current_addr));
3322 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3323 /* changes are allowed on AF_INET* families only */
3324 if ((ret != AF_INET) && (ret != AF_INET6)) {
3325 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3326 goto out;
3327 }
3328
3329 /* applying ADDR changes if required and allowed
3330 * ipcmp returns 0 when both ADDR are the same
3331 */
3332 if (ipcmp(&s->addr, &sa) == 0) {
3333 chunk_appendf(msg, "no need to change the addr");
3334 goto port;
3335 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003336 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003337 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003338
3339 /* we also need to update check's ADDR only if it uses the server's one */
3340 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003341 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003342 }
3343
3344 /* we also need to update agent ADDR only if it use the server's one */
3345 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003346 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003347 }
3348
3349 /* update report for caller */
3350 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3351 }
3352
3353 port:
3354 if (port) {
3355 char sign = '\0';
3356 char *endptr;
3357
3358 if (addr)
3359 chunk_appendf(msg, ", ");
3360
3361 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003362 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003363
3364 /* check if PORT change is required */
3365 port_change_required = 0;
3366
3367 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003368 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003369 new_port = strtol(port, &endptr, 10);
3370 if ((errno != 0) || (port == endptr)) {
3371 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3372 goto out;
3373 }
3374
3375 /* check if caller triggers a port mapped or offset */
3376 if (sign == '-' || (sign == '+')) {
3377 /* check if server currently uses port map */
3378 if (!(s->flags & SRV_F_MAPPORTS)) {
3379 /* switch from fixed port to port map mandatorily triggers
3380 * a port change */
3381 port_change_required = 1;
3382 /* check is configured
3383 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3384 * prevent PORT change if check doesn't have it's dedicated port while switching
3385 * to port mapping */
3386 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3387 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.");
3388 goto out;
3389 }
3390 }
3391 /* we're already using port maps */
3392 else {
3393 port_change_required = current_port != new_port;
3394 }
3395 }
3396 /* fixed port */
3397 else {
3398 port_change_required = current_port != new_port;
3399 }
3400
3401 /* applying PORT changes if required and update response message */
3402 if (port_change_required) {
3403 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003404 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003405 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003406
3407 /* prepare message */
3408 chunk_appendf(msg, "port changed from '");
3409 if (s->flags & SRV_F_MAPPORTS)
3410 chunk_appendf(msg, "+");
3411 chunk_appendf(msg, "%d' to '", current_port);
3412
3413 if (sign == '-') {
3414 s->flags |= SRV_F_MAPPORTS;
3415 chunk_appendf(msg, "%c", sign);
3416 /* just use for result output */
3417 new_port = -new_port;
3418 }
3419 else if (sign == '+') {
3420 s->flags |= SRV_F_MAPPORTS;
3421 chunk_appendf(msg, "%c", sign);
3422 }
3423 else {
3424 s->flags &= ~SRV_F_MAPPORTS;
3425 }
3426
3427 chunk_appendf(msg, "%d'", new_port);
3428
3429 /* we also need to update health checks port only if it uses server's realport */
3430 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3431 s->check.port = new_port;
3432 }
3433 }
3434 else {
3435 chunk_appendf(msg, "no need to change the port");
3436 }
3437 }
3438
3439out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003440 if (changed)
3441 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003442 if (updater)
3443 chunk_appendf(msg, " by '%s'", updater);
3444 chunk_appendf(msg, "\n");
3445 return msg->str;
3446}
3447
3448
3449/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003450 * update server status based on result of name resolution
3451 * returns:
3452 * 0 if server status is updated
3453 * 1 if server status has not changed
3454 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003455int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003456{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003457 struct dns_resolvers *resolvers = s->resolvers;
3458 struct dns_resolution *resolution = s->dns_requester->resolution;
3459 int exp;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003460
3461 switch (resolution->status) {
3462 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003463 /* status when HAProxy has just (re)started.
3464 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003465 break;
3466
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003467 case RSLV_STATUS_VALID:
3468 /*
3469 * resume health checks
3470 * server will be turned back on if health check is safe
3471 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003472 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003473 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003474 return 1;
3475 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3476 "No IP for server ");
Christopher Faulet67957bd2017-09-27 11:00:59 +02003477 return 0;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003478 }
Christopher Faulet67957bd2017-09-27 11:00:59 +02003479
Emeric Brun52a91d32017-08-31 14:41:55 +02003480 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003481 return 1;
3482 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3483 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3484 s->proxy->id, s->id);
3485
3486 Warning("%s.\n", trash.str);
3487 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3488 return 0;
3489
3490 case RSLV_STATUS_NX:
3491 /* stop server if resolution is NX for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003492 exp = tick_add(resolution->last_valid, resolvers->hold.nx);
3493 if (!tick_is_expired(exp, now_ms))
3494 break;
3495
3496 if (s->next_admin & SRV_ADMF_RMAINT)
3497 return 1;
3498 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3499 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003500
3501 case RSLV_STATUS_TIMEOUT:
3502 /* stop server if resolution is TIMEOUT for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003503 exp = tick_add(resolution->last_valid, resolvers->hold.timeout);
3504 if (!tick_is_expired(exp, now_ms))
3505 break;
3506
3507 if (s->next_admin & SRV_ADMF_RMAINT)
3508 return 1;
3509 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3510 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003511
3512 case RSLV_STATUS_REFUSED:
3513 /* stop server if resolution is REFUSED for a long enough period */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003514 exp = tick_add(resolution->last_valid, resolvers->hold.refused);
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 refused status");
3521 return 0;
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003522
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003523 default:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003524 /* stop server if resolution failed for a long enough period */
3525 exp = tick_add(resolution->last_valid, resolvers->hold.other);
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, "unspecified DNS error");
3532 return 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003533 }
3534
3535 return 1;
3536}
3537
3538/*
3539 * Server Name Resolution valid response callback
3540 * It expects:
3541 * - <nameserver>: the name server which answered the valid response
3542 * - <response>: buffer containing a valid DNS response
3543 * - <response_len>: size of <response>
3544 * It performs the following actions:
3545 * - ignore response if current ip found and server family not met
3546 * - update with first new ip found if family is met and current IP is not found
3547 * returns:
3548 * 0 on error
3549 * 1 when no error or safe ignore
3550 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003551int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003552{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003553 struct server *s = NULL;
3554 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003555 void *serverip, *firstip;
3556 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003557 int ret;
3558 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003559 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003560
Christopher Faulet67957bd2017-09-27 11:00:59 +02003561 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003562 if (!s)
3563 return 1;
3564
Christopher Faulet67957bd2017-09-27 11:00:59 +02003565 resolution = s->dns_requester->resolution;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003566
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003567 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003568 firstip = NULL; /* pointer to the first valid response found */
3569 /* it will be used as the new IP if a change is required */
3570 firstip_sin_family = AF_UNSPEC;
3571 serverip = NULL; /* current server IP address */
3572
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003573 /* initializing server IP pointer */
3574 server_sin_family = s->addr.ss_family;
3575 switch (server_sin_family) {
3576 case AF_INET:
3577 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3578 break;
3579
3580 case AF_INET6:
3581 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3582 break;
3583
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003584 case AF_UNSPEC:
3585 break;
3586
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003587 default:
3588 goto invalid;
3589 }
3590
Baptiste Assmann729c9012017-05-22 15:13:10 +02003591 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003592 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003593 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003594
3595 switch (ret) {
3596 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003597 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003598
3599 case DNS_UPD_SRVIP_NOT_FOUND:
3600 goto save_ip;
3601
3602 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003603 goto invalid;
3604
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003605 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003606 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003607 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003608
Baptiste Assmannfad03182015-10-28 02:03:32 +01003609 case DNS_UPD_NAME_ERROR:
Baptiste Assmannfad03182015-10-28 02:03:32 +01003610 /* update resolution status to OTHER error type */
Christopher Faulet67957bd2017-09-27 11:00:59 +02003611 resolution->status = RSLV_STATUS_OTHER;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003612 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003613
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003614 default:
3615 goto invalid;
3616
3617 }
3618
3619 save_ip:
Christopher Faulet67957bd2017-09-27 11:00:59 +02003620 if (nameserver) {
3621 nameserver->counters.update++;
3622 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003623 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003624 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003625 else
3626 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003627 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3628
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003629 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003630 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003631 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003632
3633 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003634 if (nameserver) {
Christopher Faulet67957bd2017-09-27 11:00:59 +02003635 nameserver->counters.invalid++;
3636 goto update_status;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003637 }
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003638 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003639 return 0;
3640}
3641
3642/*
3643 * Server Name Resolution error management callback
3644 * returns:
3645 * 0 on error
3646 * 1 when no error or safe ignore
3647 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003648int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003649{
Christopher Faulet67957bd2017-09-27 11:00:59 +02003650 struct server *s;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003651
Christopher Faulet67957bd2017-09-27 11:00:59 +02003652 s = objt_server(requester->owner);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003653 if (!s)
3654 return 1;
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003655 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003656 return 1;
3657}
3658
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003659/*
3660 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003661 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003662 * It returns a pointer to the first server found or NULL if <ip> is not yet
3663 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003664 */
3665struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3666{
3667 struct server *tmpsrv;
3668 struct proxy *be;
3669
3670 if (!srv)
3671 return NULL;
3672
3673 be = srv->proxy;
3674 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3675 /* We want to compare the IP in the record with the IP of the servers in the
3676 * same backend, only if:
3677 * * DNS resolution is enabled on the server
3678 * * the hostname used for the resolution by our server is the same than the
3679 * one used for the server found in the backend
3680 * * the server found in the backend is not our current server
3681 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003682 if ((tmpsrv->hostname_dn == NULL) ||
3683 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3684 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003685 (srv->puid == tmpsrv->puid))
3686 continue;
3687
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003688 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003689 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003690 continue;
3691
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003692 /* At this point, we have 2 different servers using the same DNS hostname
3693 * for their respective resolution.
3694 */
3695 if (*ip_family == tmpsrv->addr.ss_family &&
3696 ((tmpsrv->addr.ss_family == AF_INET &&
3697 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3698 (tmpsrv->addr.ss_family == AF_INET6 &&
3699 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3700 return tmpsrv;
3701 }
3702 }
3703
3704 return NULL;
3705}
3706
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003707/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3708 * resolver. This is suited for initial address configuration. Returns 0 on
3709 * success otherwise a non-zero error code. In case of error, *err_code, if
3710 * not NULL, is filled up.
3711 */
3712int srv_set_addr_via_libc(struct server *srv, int *err_code)
3713{
3714 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003715 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003716 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003717 return 1;
3718 }
3719 return 0;
3720}
3721
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003722/* Set the server's FDQN (->hostname) from <hostname>.
3723 * Returns -1 if failed, 0 if not.
3724 */
3725int srv_set_fqdn(struct server *srv, const char *hostname)
3726{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003727 struct dns_resolution *resolution;
Christopher Faulet67957bd2017-09-27 11:00:59 +02003728 char *hostname_dn;
3729 int hostname_len, hostname_dn_len;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003730
3731 /* run time DNS resolution was not active for this server
3732 * and we can't enable it at run time for now.
3733 */
3734 if (!srv->dns_requester)
3735 return -1;
3736
3737 chunk_reset(&trash);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003738 hostname_len = strlen(hostname);
3739 hostname_dn = trash.str;
3740 hostname_dn_len = dns_str_to_dn_label(hostname, hostname_len + 1,
3741 hostname_dn, trash.size);
3742 if (hostname_dn_len == -1)
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003743 return -1;
3744
Christopher Faulet67957bd2017-09-27 11:00:59 +02003745 resolution = srv->dns_requester->resolution;
3746 if (resolution &&
3747 resolution->hostname_dn &&
3748 !strcmp(resolution->hostname_dn, hostname_dn))
3749 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003750
Christopher Faulet67957bd2017-09-27 11:00:59 +02003751 dns_unlink_resolution(srv->dns_requester);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003752
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003753 free(srv->hostname);
3754 free(srv->hostname_dn);
Christopher Faulet67957bd2017-09-27 11:00:59 +02003755 srv->hostname = strdup(hostname);
3756 srv->hostname_dn = strdup(hostname_dn);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003757 srv->hostname_dn_len = hostname_dn_len;
3758 if (!srv->hostname || !srv->hostname_dn)
3759 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003760
Christopher Faulet67957bd2017-09-27 11:00:59 +02003761 if (dns_link_resolution(srv, OBJ_TYPE_SERVER) == -1)
3762 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003763 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003764}
3765
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003766/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3767 * from the state file. This is suited for initial address configuration.
3768 * Returns 0 on success otherwise a non-zero error code. In case of error,
3769 * *err_code, if not NULL, is filled up.
3770 */
3771static int srv_apply_lastaddr(struct server *srv, int *err_code)
3772{
3773 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3774 if (err_code)
3775 *err_code |= ERR_WARN;
3776 return 1;
3777 }
3778 return 0;
3779}
3780
Willy Tarreau25e51522016-11-04 15:10:17 +01003781/* returns 0 if no error, otherwise a combination of ERR_* flags */
3782static int srv_iterate_initaddr(struct server *srv)
3783{
3784 int return_code = 0;
3785 int err_code;
3786 unsigned int methods;
3787
3788 methods = srv->init_addr_methods;
3789 if (!methods) { // default to "last,libc"
3790 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3791 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3792 }
3793
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003794 /* "-dr" : always append "none" so that server addresses resolution
3795 * failures are silently ignored, this is convenient to validate some
3796 * configs out of their environment.
3797 */
3798 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3799 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3800
Willy Tarreau25e51522016-11-04 15:10:17 +01003801 while (methods) {
3802 err_code = 0;
3803 switch (srv_get_next_initaddr(&methods)) {
3804 case SRV_IADDR_LAST:
3805 if (!srv->lastaddr)
3806 continue;
3807 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003808 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003809 return_code |= err_code;
3810 break;
3811
3812 case SRV_IADDR_LIBC:
3813 if (!srv->hostname)
3814 continue;
3815 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003816 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003817 return_code |= err_code;
3818 break;
3819
Willy Tarreau37ebe122016-11-04 15:17:58 +01003820 case SRV_IADDR_NONE:
3821 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003822 if (return_code) {
3823 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3824 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3825 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003826 return return_code;
3827
Willy Tarreau4310d362016-11-02 15:05:56 +01003828 case SRV_IADDR_IP:
3829 ipcpy(&srv->init_addr, &srv->addr);
3830 if (return_code) {
3831 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3832 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3833 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003834 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003835
Willy Tarreau25e51522016-11-04 15:10:17 +01003836 default: /* unhandled method */
3837 break;
3838 }
3839 }
3840
3841 if (!return_code) {
3842 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3843 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3844 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003845 else {
3846 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3847 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3848 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003849
3850 return_code |= ERR_ALERT | ERR_FATAL;
3851 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003852out:
3853 srv_set_dyncookie(srv);
3854 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003855}
3856
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003857/*
3858 * This function parses all backends and all servers within each backend
3859 * and performs servers' addr resolution based on information provided by:
3860 * - configuration file
3861 * - server-state file (states provided by an 'old' haproxy process)
3862 *
3863 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3864 */
3865int srv_init_addr(void)
3866{
3867 struct proxy *curproxy;
3868 int return_code = 0;
3869
3870 curproxy = proxy;
3871 while (curproxy) {
3872 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003873
3874 /* servers are in backend only */
3875 if (!(curproxy->cap & PR_CAP_BE))
3876 goto srv_init_addr_next;
3877
Willy Tarreau25e51522016-11-04 15:10:17 +01003878 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003879 if (srv->hostname)
3880 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003881
3882 srv_init_addr_next:
3883 curproxy = curproxy->next;
3884 }
3885
3886 return return_code;
3887}
3888
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003889const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
3890{
3891
3892 struct chunk *msg;
3893
3894 msg = get_trash_chunk();
3895 chunk_reset(msg);
3896
Olivier Houchard8da5f982017-08-04 18:35:36 +02003897 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003898 chunk_appendf(msg, "no need to change the FDQN");
3899 goto out;
3900 }
3901
3902 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3903 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3904 goto out;
3905 }
3906
3907 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3908 server->proxy->id, server->id, server->hostname, fqdn);
3909
3910 if (srv_set_fqdn(server, fqdn) < 0) {
3911 chunk_reset(msg);
3912 chunk_appendf(msg, "could not update %s/%s FQDN",
3913 server->proxy->id, server->id);
3914 goto out;
3915 }
3916
3917 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003918 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003919
3920 out:
3921 if (updater)
3922 chunk_appendf(msg, " by '%s'", updater);
3923 chunk_appendf(msg, "\n");
3924
3925 return msg->str;
3926}
3927
3928
Willy Tarreau21b069d2016-11-23 17:15:08 +01003929/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3930 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003931 * 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 +01003932 * used for CLI commands requiring a server name.
3933 * Important: the <arg> is modified to remove the '/'.
3934 */
3935struct server *cli_find_server(struct appctx *appctx, char *arg)
3936{
3937 struct proxy *px;
3938 struct server *sv;
3939 char *line;
3940
3941 /* split "backend/server" and make <line> point to server */
3942 for (line = arg; *line; line++)
3943 if (*line == '/') {
3944 *line++ = '\0';
3945 break;
3946 }
3947
3948 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003949 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003950 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003951 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003952 return NULL;
3953 }
3954
3955 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003956 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003957 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003958 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003959 return NULL;
3960 }
3961
3962 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003963 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003964 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003965 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01003966 return NULL;
3967 }
3968
3969 return sv;
3970}
3971
William Lallemand222baf22016-11-19 02:00:33 +01003972
3973static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
3974{
3975 struct server *sv;
3976 const char *warning;
3977
3978 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3979 return 1;
3980
3981 sv = cli_find_server(appctx, args[2]);
3982 if (!sv)
3983 return 1;
3984
3985 if (strcmp(args[3], "weight") == 0) {
3986 warning = server_parse_weight_change_request(sv, args[4]);
3987 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003988 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01003989 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003990 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01003991 }
3992 }
3993 else if (strcmp(args[3], "state") == 0) {
3994 if (strcmp(args[4], "ready") == 0)
3995 srv_adm_set_ready(sv);
3996 else if (strcmp(args[4], "drain") == 0)
3997 srv_adm_set_drain(sv);
3998 else if (strcmp(args[4], "maint") == 0)
3999 srv_adm_set_maint(sv);
4000 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004001 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004002 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004003 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004004 }
4005 }
4006 else if (strcmp(args[3], "health") == 0) {
4007 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004008 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004009 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004010 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004011 }
4012 else if (strcmp(args[4], "up") == 0) {
4013 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004014 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004015 }
4016 else if (strcmp(args[4], "stopping") == 0) {
4017 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004018 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004019 }
4020 else if (strcmp(args[4], "down") == 0) {
4021 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004022 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004023 }
4024 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004025 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004026 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004027 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004028 }
4029 }
4030 else if (strcmp(args[3], "agent") == 0) {
4031 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004032 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004033 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004034 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004035 }
4036 else if (strcmp(args[4], "up") == 0) {
4037 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004038 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004039 }
4040 else if (strcmp(args[4], "down") == 0) {
4041 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004042 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004043 }
4044 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004045 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004046 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004047 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004048 }
4049 }
Misiek2da082d2017-01-09 09:40:42 +01004050 else if (strcmp(args[3], "agent-addr") == 0) {
4051 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004052 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004053 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4054 appctx->st0 = CLI_ST_PRINT;
4055 } else {
4056 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004057 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004058 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4059 appctx->st0 = CLI_ST_PRINT;
4060 }
4061 }
4062 }
4063 else if (strcmp(args[3], "agent-send") == 0) {
4064 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004065 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004066 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4067 appctx->st0 = CLI_ST_PRINT;
4068 } else {
4069 char *nss = strdup(args[4]);
4070 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004071 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004072 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4073 appctx->st0 = CLI_ST_PRINT;
4074 } else {
4075 free(sv->agent.send_string);
4076 sv->agent.send_string = nss;
4077 sv->agent.send_string_len = strlen(args[4]);
4078 }
4079 }
4080 }
William Lallemand222baf22016-11-19 02:00:33 +01004081 else if (strcmp(args[3], "check-port") == 0) {
4082 int i = 0;
4083 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004084 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004085 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004086 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004087 }
4088 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004089 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004090 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004091 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004092 }
4093 /* prevent the update of port to 0 if MAPPORTS are in use */
4094 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004095 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004096 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004097 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004098 return 1;
4099 }
4100 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004101 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004102 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004103 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004104 }
4105 else if (strcmp(args[3], "addr") == 0) {
4106 char *addr = NULL;
4107 char *port = NULL;
4108 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004109 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004110 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004111 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004112 return 1;
4113 }
4114 else {
4115 addr = args[4];
4116 }
4117 if (strcmp(args[5], "port") == 0) {
4118 port = args[6];
4119 }
4120 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4121 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004122 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004123 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004124 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004125 }
4126 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4127 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004128 else if (strcmp(args[3], "fqdn") == 0) {
4129 if (!*args[4]) {
4130 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4131 appctx->st0 = CLI_ST_PRINT;
4132 return 1;
4133 }
4134 warning = update_server_fqdn(sv, args[4], "stats socket command");
4135 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004136 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004137 appctx->ctx.cli.msg = warning;
4138 appctx->st0 = CLI_ST_PRINT;
4139 }
4140 }
William Lallemand222baf22016-11-19 02:00:33 +01004141 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004142 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004143 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 +01004144 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004145 }
4146 return 1;
4147}
4148
William Lallemand6b160942016-11-22 12:34:35 +01004149static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4150{
4151 struct stream_interface *si = appctx->owner;
4152 struct proxy *px;
4153 struct server *sv;
4154 char *line;
4155
4156
4157 /* split "backend/server" and make <line> point to server */
4158 for (line = args[2]; *line; line++)
4159 if (*line == '/') {
4160 *line++ = '\0';
4161 break;
4162 }
4163
4164 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004165 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004166 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004167 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004168 return 1;
4169 }
4170
4171 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004172 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004173 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004174 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004175 return 1;
4176 }
4177
4178 /* return server's effective weight at the moment */
4179 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004180 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004181 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004182 return 0;
4183 }
William Lallemand6b160942016-11-22 12:34:35 +01004184 return 1;
4185}
4186
4187static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4188{
4189 struct server *sv;
4190 const char *warning;
4191
4192 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4193 return 1;
4194
4195 sv = cli_find_server(appctx, args[2]);
4196 if (!sv)
4197 return 1;
4198
4199 warning = server_parse_weight_change_request(sv, args[3]);
4200 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004201 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004202 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004203 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004204 }
4205 return 1;
4206}
4207
Willy Tarreaub8026272016-11-23 11:26:56 +01004208/* parse a "set maxconn server" command. It always returns 1. */
4209static int cli_parse_set_maxconn_server(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[3]);
4218 if (!sv)
4219 return 1;
4220
4221 warning = server_parse_maxconn_change_request(sv, args[4]);
4222 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004223 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004224 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004225 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004226 }
4227 return 1;
4228}
William Lallemand6b160942016-11-22 12:34:35 +01004229
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004230/* parse a "disable agent" command. It always returns 1. */
4231static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4232{
4233 struct server *sv;
4234
4235 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4236 return 1;
4237
4238 sv = cli_find_server(appctx, args[2]);
4239 if (!sv)
4240 return 1;
4241
4242 sv->agent.state &= ~CHK_ST_ENABLED;
4243 return 1;
4244}
4245
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004246/* parse a "disable health" command. It always returns 1. */
4247static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4248{
4249 struct server *sv;
4250
4251 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4252 return 1;
4253
4254 sv = cli_find_server(appctx, args[2]);
4255 if (!sv)
4256 return 1;
4257
4258 sv->check.state &= ~CHK_ST_ENABLED;
4259 return 1;
4260}
4261
Willy Tarreauffb4d582016-11-24 12:47:00 +01004262/* parse a "disable server" command. It always returns 1. */
4263static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4264{
4265 struct server *sv;
4266
4267 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4268 return 1;
4269
4270 sv = cli_find_server(appctx, args[2]);
4271 if (!sv)
4272 return 1;
4273
4274 srv_adm_set_maint(sv);
4275 return 1;
4276}
4277
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004278/* parse a "enable agent" command. It always returns 1. */
4279static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4280{
4281 struct server *sv;
4282
4283 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4284 return 1;
4285
4286 sv = cli_find_server(appctx, args[2]);
4287 if (!sv)
4288 return 1;
4289
4290 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004291 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004292 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004293 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004294 return 1;
4295 }
4296
4297 sv->agent.state |= CHK_ST_ENABLED;
4298 return 1;
4299}
4300
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004301/* parse a "enable health" command. It always returns 1. */
4302static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4303{
4304 struct server *sv;
4305
4306 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4307 return 1;
4308
4309 sv = cli_find_server(appctx, args[2]);
4310 if (!sv)
4311 return 1;
4312
4313 sv->check.state |= CHK_ST_ENABLED;
4314 return 1;
4315}
4316
Willy Tarreauffb4d582016-11-24 12:47:00 +01004317/* parse a "enable server" command. It always returns 1. */
4318static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4319{
4320 struct server *sv;
4321
4322 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4323 return 1;
4324
4325 sv = cli_find_server(appctx, args[2]);
4326 if (!sv)
4327 return 1;
4328
4329 srv_adm_set_ready(sv);
4330 return 1;
4331}
4332
William Lallemand222baf22016-11-19 02:00:33 +01004333/* register cli keywords */
4334static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004335 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004336 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004337 { { "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 +01004338 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004339 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004340 { { "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 +01004341 { { "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 +01004342 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004343 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4344 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4345
William Lallemand222baf22016-11-19 02:00:33 +01004346 {{},}
4347}};
4348
4349__attribute__((constructor))
4350static void __server_init(void)
4351{
4352 cli_register_kw(&cli_kws);
4353}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004354
Emeric Brun64cc49c2017-10-03 14:46:45 +02004355/*
4356 * This function applies server's status changes, it is
4357 * is designed to be called asynchronously.
4358 *
4359 */
4360void srv_update_status(struct server *s)
4361{
4362 struct check *check = &s->check;
4363 int xferred;
4364 struct proxy *px = s->proxy;
4365 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4366 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4367 int log_level;
4368 struct chunk *tmptrash = NULL;
4369
4370
4371 /* If currently main is not set we try to apply pending state changes */
4372 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4373 int next_admin;
4374
4375 /* Backup next admin */
4376 next_admin = s->next_admin;
4377
4378 /* restore current admin state */
4379 s->next_admin = s->cur_admin;
4380
4381 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4382 s->last_change = now.tv_sec;
4383 if (s->proxy->lbprm.set_server_status_down)
4384 s->proxy->lbprm.set_server_status_down(s);
4385
4386 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4387 srv_shutdown_streams(s, SF_ERR_DOWN);
4388
4389 /* we might have streams queued on this server and waiting for
4390 * a connection. Those which are redispatchable will be queued
4391 * to another server or to the proxy itself.
4392 */
4393 xferred = pendconn_redistribute(s);
4394
4395 tmptrash = alloc_trash_chunk();
4396 if (tmptrash) {
4397 chunk_printf(tmptrash,
4398 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4399 s->proxy->id, s->id);
4400
Emeric Brun5a133512017-10-19 14:42:30 +02004401 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004402 Warning("%s.\n", tmptrash->str);
4403
4404 /* we don't send an alert if the server was previously paused */
4405 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4406 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4407 send_email_alert(s, log_level, "%s", tmptrash->str);
4408 free_trash_chunk(tmptrash);
4409 tmptrash = NULL;
4410 }
4411 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4412 set_backend_down(s->proxy);
4413
4414 s->counters.down_trans++;
4415 }
4416 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4417 s->last_change = now.tv_sec;
4418 if (s->proxy->lbprm.set_server_status_down)
4419 s->proxy->lbprm.set_server_status_down(s);
4420
4421 /* we might have streams queued on this server and waiting for
4422 * a connection. Those which are redispatchable will be queued
4423 * to another server or to the proxy itself.
4424 */
4425 xferred = pendconn_redistribute(s);
4426
4427 tmptrash = alloc_trash_chunk();
4428 if (tmptrash) {
4429 chunk_printf(tmptrash,
4430 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4431 s->proxy->id, s->id);
4432
Emeric Brun5a133512017-10-19 14:42:30 +02004433 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004434
4435 Warning("%s.\n", tmptrash->str);
4436 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4437 free_trash_chunk(tmptrash);
4438 tmptrash = NULL;
4439 }
4440
4441 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4442 set_backend_down(s->proxy);
4443 }
4444 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4445 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4446 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4447 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4448 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4449 s->proxy->last_change = now.tv_sec;
4450 }
4451
4452 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4453 s->down_time += now.tv_sec - s->last_change;
4454
4455 s->last_change = now.tv_sec;
4456 if (s->next_state == SRV_ST_STARTING)
4457 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4458
4459 server_recalc_eweight(s);
4460 /* now propagate the status change to any LB algorithms */
4461 if (px->lbprm.update_server_eweight)
4462 px->lbprm.update_server_eweight(s);
4463 else if (srv_willbe_usable(s)) {
4464 if (px->lbprm.set_server_status_up)
4465 px->lbprm.set_server_status_up(s);
4466 }
4467 else {
4468 if (px->lbprm.set_server_status_down)
4469 px->lbprm.set_server_status_down(s);
4470 }
4471
4472 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4473 * and it's not a backup server and its effective weight is > 0,
4474 * then it can accept new connections, so we shut down all streams
4475 * on all backup servers.
4476 */
4477 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4478 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4479 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4480
4481 /* check if we can handle some connections queued at the proxy. We
4482 * will take as many as we can handle.
4483 */
4484 xferred = pendconn_grab_from_px(s);
4485
4486 tmptrash = alloc_trash_chunk();
4487 if (tmptrash) {
4488 chunk_printf(tmptrash,
4489 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4490 s->proxy->id, s->id);
4491
Emeric Brun5a133512017-10-19 14:42:30 +02004492 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004493 Warning("%s.\n", tmptrash->str);
4494 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4495 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4496 free_trash_chunk(tmptrash);
4497 tmptrash = NULL;
4498 }
4499
4500 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4501 set_backend_down(s->proxy);
4502 }
4503 else if (s->cur_eweight != s->next_eweight) {
4504 /* now propagate the status change to any LB algorithms */
4505 if (px->lbprm.update_server_eweight)
4506 px->lbprm.update_server_eweight(s);
4507 else if (srv_willbe_usable(s)) {
4508 if (px->lbprm.set_server_status_up)
4509 px->lbprm.set_server_status_up(s);
4510 }
4511 else {
4512 if (px->lbprm.set_server_status_down)
4513 px->lbprm.set_server_status_down(s);
4514 }
4515
4516 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4517 set_backend_down(s->proxy);
4518 }
4519
4520 s->next_admin = next_admin;
4521 }
4522
Emeric Brun5a133512017-10-19 14:42:30 +02004523 /* reset operational state change */
4524 *s->op_st_chg.reason = 0;
4525 s->op_st_chg.status = s->op_st_chg.code = -1;
4526 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004527
4528 /* Now we try to apply pending admin changes */
4529
4530 /* Maintenance must also disable health checks */
4531 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4532 if (s->check.state & CHK_ST_ENABLED) {
4533 s->check.state |= CHK_ST_PAUSED;
4534 check->health = 0;
4535 }
4536
4537 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004538 tmptrash = alloc_trash_chunk();
4539 if (tmptrash) {
4540 chunk_printf(tmptrash,
4541 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4542 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4543 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004544
Olivier Houchard796a2b32017-10-24 17:42:47 +02004545 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004546
Olivier Houchard796a2b32017-10-24 17:42:47 +02004547 if (!(global.mode & MODE_STARTING)) {
4548 Warning("%s.\n", tmptrash->str);
4549 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4550 }
4551 free_trash_chunk(tmptrash);
4552 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004553 }
4554 }
4555 else { /* server was still running */
4556 check->health = 0; /* failure */
4557 s->last_change = now.tv_sec;
4558 if (s->proxy->lbprm.set_server_status_down)
4559 s->proxy->lbprm.set_server_status_down(s);
4560
4561 s->next_state = SRV_ST_STOPPED;
4562 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4563 srv_shutdown_streams(s, SF_ERR_DOWN);
4564
4565 /* we might have streams queued on this server and waiting for
4566 * a connection. Those which are redispatchable will be queued
4567 * to another server or to the proxy itself.
4568 */
4569 xferred = pendconn_redistribute(s);
4570
4571 tmptrash = alloc_trash_chunk();
4572 if (tmptrash) {
4573 chunk_printf(tmptrash,
4574 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4575 s->flags & SRV_F_BACKUP ? "Backup " : "",
4576 s->proxy->id, s->id,
4577 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4578
4579 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4580
4581 if (!(global.mode & MODE_STARTING)) {
4582 Warning("%s.\n", tmptrash->str);
4583 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4584 }
4585 free_trash_chunk(tmptrash);
4586 tmptrash = NULL;
4587 }
4588 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4589 set_backend_down(s->proxy);
4590
4591 s->counters.down_trans++;
4592 }
4593 }
4594 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4595 /* OK here we're leaving maintenance, we have many things to check,
4596 * because the server might possibly be coming back up depending on
4597 * its state. In practice, leaving maintenance means that we should
4598 * immediately turn to UP (more or less the slowstart) under the
4599 * following conditions :
4600 * - server is neither checked nor tracked
4601 * - server tracks another server which is not checked
4602 * - server tracks another server which is already up
4603 * Which sums up as something simpler :
4604 * "either the tracking server is up or the server's checks are disabled
4605 * or up". Otherwise we only re-enable health checks. There's a special
4606 * case associated to the stopping state which can be inherited. Note
4607 * that the server might still be in drain mode, which is naturally dealt
4608 * with by the lower level functions.
4609 */
4610
4611 if (s->check.state & CHK_ST_ENABLED) {
4612 s->check.state &= ~CHK_ST_PAUSED;
4613 check->health = check->rise; /* start OK but check immediately */
4614 }
4615
4616 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4617 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4618 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4619 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4620 s->next_state = SRV_ST_STOPPING;
4621 }
4622 else {
4623 s->next_state = SRV_ST_STARTING;
4624 if (s->slowstart > 0)
4625 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4626 else
4627 s->next_state = SRV_ST_RUNNING;
4628 }
4629
4630 }
4631
4632 tmptrash = alloc_trash_chunk();
4633 if (tmptrash) {
4634 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4635 chunk_printf(tmptrash,
4636 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4637 s->flags & SRV_F_BACKUP ? "Backup " : "",
4638 s->proxy->id, s->id,
4639 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4640 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4641 }
4642 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4643 chunk_printf(tmptrash,
4644 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4645 s->flags & SRV_F_BACKUP ? "Backup " : "",
4646 s->proxy->id, s->id, s->hostname,
4647 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4648 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4649 }
4650 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4651 chunk_printf(tmptrash,
4652 "%sServer %s/%s is %s/%s (leaving maintenance)",
4653 s->flags & SRV_F_BACKUP ? "Backup " : "",
4654 s->proxy->id, s->id,
4655 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4656 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4657 }
4658 Warning("%s.\n", tmptrash->str);
4659 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4660 free_trash_chunk(tmptrash);
4661 tmptrash = NULL;
4662 }
4663
4664 server_recalc_eweight(s);
4665 /* now propagate the status change to any LB algorithms */
4666 if (px->lbprm.update_server_eweight)
4667 px->lbprm.update_server_eweight(s);
4668 else if (srv_willbe_usable(s)) {
4669 if (px->lbprm.set_server_status_up)
4670 px->lbprm.set_server_status_up(s);
4671 }
4672 else {
4673 if (px->lbprm.set_server_status_down)
4674 px->lbprm.set_server_status_down(s);
4675 }
4676
4677 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4678 set_backend_down(s->proxy);
4679
4680 }
4681 else if (s->next_admin & SRV_ADMF_MAINT) {
4682 /* remaining in maintenance mode, let's inform precisely about the
4683 * situation.
4684 */
4685 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4686 tmptrash = alloc_trash_chunk();
4687 if (tmptrash) {
4688 chunk_printf(tmptrash,
4689 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4690 s->flags & SRV_F_BACKUP ? "Backup " : "",
4691 s->proxy->id, s->id);
4692
4693 if (s->track) /* normally it's mandatory here */
4694 chunk_appendf(tmptrash, " via %s/%s",
4695 s->track->proxy->id, s->track->id);
4696 Warning("%s.\n", tmptrash->str);
4697 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4698 free_trash_chunk(tmptrash);
4699 tmptrash = NULL;
4700 }
4701 }
4702 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4703 tmptrash = alloc_trash_chunk();
4704 if (tmptrash) {
4705 chunk_printf(tmptrash,
4706 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4707 s->flags & SRV_F_BACKUP ? "Backup " : "",
4708 s->proxy->id, s->id, s->hostname);
4709
4710 if (s->track) /* normally it's mandatory here */
4711 chunk_appendf(tmptrash, " via %s/%s",
4712 s->track->proxy->id, s->track->id);
4713 Warning("%s.\n", tmptrash->str);
4714 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4715 free_trash_chunk(tmptrash);
4716 tmptrash = NULL;
4717 }
4718 }
4719 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4720 tmptrash = alloc_trash_chunk();
4721 if (tmptrash) {
4722 chunk_printf(tmptrash,
4723 "%sServer %s/%s remains in forced maintenance",
4724 s->flags & SRV_F_BACKUP ? "Backup " : "",
4725 s->proxy->id, s->id);
4726 Warning("%s.\n", tmptrash->str);
4727 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4728 free_trash_chunk(tmptrash);
4729 tmptrash = NULL;
4730 }
4731 }
4732 /* don't report anything when leaving drain mode and remaining in maintenance */
4733
4734 s->cur_admin = s->next_admin;
4735 }
4736
4737 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4738 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4739 /* drain state is applied only if not yet in maint */
4740
4741 s->last_change = now.tv_sec;
4742 if (px->lbprm.set_server_status_down)
4743 px->lbprm.set_server_status_down(s);
4744
4745 /* we might have streams queued on this server and waiting for
4746 * a connection. Those which are redispatchable will be queued
4747 * to another server or to the proxy itself.
4748 */
4749 xferred = pendconn_redistribute(s);
4750
4751 tmptrash = alloc_trash_chunk();
4752 if (tmptrash) {
4753 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4754 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4755 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4756
4757 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4758
4759 if (!(global.mode & MODE_STARTING)) {
4760 Warning("%s.\n", tmptrash->str);
4761 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4762 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4763 }
4764 free_trash_chunk(tmptrash);
4765 tmptrash = NULL;
4766 }
4767
4768 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4769 set_backend_down(s->proxy);
4770 }
4771 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4772 /* OK completely leaving drain mode */
4773 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4774 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4775 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4776 s->proxy->last_change = now.tv_sec;
4777 }
4778
4779 if (s->last_change < now.tv_sec) // ignore negative times
4780 s->down_time += now.tv_sec - s->last_change;
4781 s->last_change = now.tv_sec;
4782 server_recalc_eweight(s);
4783
4784 tmptrash = alloc_trash_chunk();
4785 if (tmptrash) {
4786 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4787 chunk_printf(tmptrash,
4788 "%sServer %s/%s is %s (leaving forced drain)",
4789 s->flags & SRV_F_BACKUP ? "Backup " : "",
4790 s->proxy->id, s->id,
4791 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4792 }
4793 else {
4794 chunk_printf(tmptrash,
4795 "%sServer %s/%s is %s (leaving drain)",
4796 s->flags & SRV_F_BACKUP ? "Backup " : "",
4797 s->proxy->id, s->id,
4798 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4799 if (s->track) /* normally it's mandatory here */
4800 chunk_appendf(tmptrash, " via %s/%s",
4801 s->track->proxy->id, s->track->id);
4802 }
4803
4804 Warning("%s.\n", tmptrash->str);
4805 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4806 free_trash_chunk(tmptrash);
4807 tmptrash = NULL;
4808 }
4809
4810 /* now propagate the status change to any LB algorithms */
4811 if (px->lbprm.update_server_eweight)
4812 px->lbprm.update_server_eweight(s);
4813 else if (srv_willbe_usable(s)) {
4814 if (px->lbprm.set_server_status_up)
4815 px->lbprm.set_server_status_up(s);
4816 }
4817 else {
4818 if (px->lbprm.set_server_status_down)
4819 px->lbprm.set_server_status_down(s);
4820 }
4821 }
4822 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4823 /* remaining in drain mode after removing one of its flags */
4824
4825 tmptrash = alloc_trash_chunk();
4826 if (tmptrash) {
4827 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4828 chunk_printf(tmptrash,
4829 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4830 s->flags & SRV_F_BACKUP ? "Backup " : "",
4831 s->proxy->id, s->id);
4832
4833 if (s->track) /* normally it's mandatory here */
4834 chunk_appendf(tmptrash, " via %s/%s",
4835 s->track->proxy->id, s->track->id);
4836 }
4837 else {
4838 chunk_printf(tmptrash,
4839 "%sServer %s/%s remains in forced drain mode",
4840 s->flags & SRV_F_BACKUP ? "Backup " : "",
4841 s->proxy->id, s->id);
4842 }
4843 Warning("%s.\n", tmptrash->str);
4844 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4845 free_trash_chunk(tmptrash);
4846 tmptrash = NULL;
4847 }
4848
4849 /* commit new admin status */
4850
4851 s->cur_admin = s->next_admin;
4852 }
4853 }
4854
4855 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004856 *s->adm_st_chg_cause = 0;
4857}
4858/*
4859 * This function loops on servers registered for asynchronous
4860 * status changes
4861 */
4862void servers_update_status(void) {
4863 struct server *s, *stmp;
4864
4865 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4866 srv_update_status(s);
4867 LIST_DEL(&s->update_status);
4868 LIST_INIT(&s->update_status);
4869 }
4870}
4871
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004872/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004873 * Local variables:
4874 * c-indent-level: 8
4875 * c-basic-offset: 8
4876 * End:
4877 */