blob: 844d6963bdb5cb3b374ceba21e2199d168bbe398 [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{
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001378 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001379 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001380
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001381 free(srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001382 srv->hostname = strdup(hostname);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001383
Baptiste Assmann42746372017-05-03 12:12:02 +02001384 srv->hostname_dn_len = dns_str_to_dn_label_len(hostname);
1385 srv->hostname_dn = calloc(srv->hostname_dn_len + 1, sizeof(char));
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001386
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001387 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001388 goto err;
1389
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001390 if (!dns_str_to_dn_label(srv->hostname,
Baptiste Assmann42746372017-05-03 12:12:02 +02001391 srv->hostname_dn,
1392 srv->hostname_dn_len + 1))
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001393 goto err;
1394
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001395 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001396
1397 err:
1398 free(srv->hostname);
1399 srv->hostname = NULL;
Baptiste Assmann42746372017-05-03 12:12:02 +02001400 free(srv->hostname_dn);
1401 srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001402 return -1;
1403}
1404
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001405/*
1406 * Free the link between a server and its resolution.
1407 * It also performs the following tasks:
1408 * - check if resolution can be moved back in the resolvers' pool
1409 * (and do it)
1410 * - move resolution's hostname_dn and hostname_dn_len to the next requester
1411 * available (when applied)
1412 */
Baptiste Assmann747359e2017-08-14 10:37:46 +02001413void srv_free_from_resolution(struct server *srv)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001414{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001415 struct dns_requester *requester;
1416 int count;
1417
1418 /* check if we can move the resolution back to the pool.
1419 * if <count> is greater than 1, then we can't */
1420 count = 0;
1421 list_for_each_entry(requester, &srv->resolution->requester.wait, list) {
1422 ++count;
1423 if (count > 1)
1424 break;
1425 }
1426 list_for_each_entry(requester, &srv->resolution->requester.curr, list) {
1427 ++count;
1428 if (count > 1)
1429 break;
1430 }
1431 if (count <= 1) {
1432 /* move the resolution back to the pool */
1433 dns_resolution_free(srv->resolvers, srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001434 return;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001435 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001436
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001437 dns_rm_requester_from_resolution(srv->dns_requester, srv->resolution);
1438
1439 return;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001440}
1441
1442/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001443 * Copy <src> server settings to <srv> server allocating
1444 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001445 * This function is not supposed to be called at any time, but only
1446 * during server settings parsing or during server allocations from
1447 * a server template, and just after having calloc()'ed a new server.
1448 * So, <src> may only be a default server (when parsing server settings)
1449 * or a server template (during server allocations from a server template).
1450 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1451 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001452 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001453static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001454{
1455 /* Connection source settings copy */
1456 srv_conn_src_cpy(srv, src);
1457
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001458 if (srv_tmpl) {
1459 srv->addr = src->addr;
1460 srv->svc_port = src->svc_port;
1461 }
1462
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001463 srv->pp_opts = src->pp_opts;
1464 if (src->rdr_pfx != NULL) {
1465 srv->rdr_pfx = strdup(src->rdr_pfx);
1466 srv->rdr_len = src->rdr_len;
1467 }
1468 if (src->cookie != NULL) {
1469 srv->cookie = strdup(src->cookie);
1470 srv->cklen = src->cklen;
1471 }
1472 srv->use_ssl = src->use_ssl;
1473 srv->check.addr = srv->agent.addr = src->check.addr;
1474 srv->check.use_ssl = src->check.use_ssl;
1475 srv->check.port = src->check.port;
1476 /* Note: 'flags' field has potentially been already initialized. */
1477 srv->flags |= src->flags;
1478 srv->do_check = src->do_check;
1479 srv->do_agent = src->do_agent;
1480 if (srv->check.port)
1481 srv->flags |= SRV_F_CHECKPORT;
1482 srv->check.inter = src->check.inter;
1483 srv->check.fastinter = src->check.fastinter;
1484 srv->check.downinter = src->check.downinter;
1485 srv->agent.use_ssl = src->agent.use_ssl;
1486 srv->agent.port = src->agent.port;
1487 if (src->agent.send_string != NULL)
1488 srv->agent.send_string = strdup(src->agent.send_string);
1489 srv->agent.send_string_len = src->agent.send_string_len;
1490 srv->agent.inter = src->agent.inter;
1491 srv->agent.fastinter = src->agent.fastinter;
1492 srv->agent.downinter = src->agent.downinter;
1493 srv->maxqueue = src->maxqueue;
1494 srv->minconn = src->minconn;
1495 srv->maxconn = src->maxconn;
1496 srv->slowstart = src->slowstart;
1497 srv->observe = src->observe;
1498 srv->onerror = src->onerror;
1499 srv->onmarkeddown = src->onmarkeddown;
1500 srv->onmarkedup = src->onmarkedup;
1501 if (src->trackit != NULL)
1502 srv->trackit = strdup(src->trackit);
1503 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1504 srv->uweight = srv->iweight = src->iweight;
1505
1506 srv->check.send_proxy = src->check.send_proxy;
1507 /* health: up, but will fall down at first failure */
1508 srv->check.rise = srv->check.health = src->check.rise;
1509 srv->check.fall = src->check.fall;
1510
1511 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001512 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1513 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1514 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001515 srv->check.state |= CHK_ST_PAUSED;
1516 srv->check.health = 0;
1517 }
1518
1519 /* health: up but will fall down at first failure */
1520 srv->agent.rise = srv->agent.health = src->agent.rise;
1521 srv->agent.fall = src->agent.fall;
1522
1523 if (src->resolvers_id != NULL)
1524 srv->resolvers_id = strdup(src->resolvers_id);
1525 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1526 if (srv->dns_opts.family_prio == AF_UNSPEC)
1527 srv->dns_opts.family_prio = AF_INET6;
1528 memcpy(srv->dns_opts.pref_net,
1529 src->dns_opts.pref_net,
1530 sizeof srv->dns_opts.pref_net);
1531 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1532
1533 srv->init_addr_methods = src->init_addr_methods;
1534 srv->init_addr = src->init_addr;
1535#if defined(USE_OPENSSL)
1536 srv_ssl_settings_cpy(srv, src);
1537#endif
1538#ifdef TCP_USER_TIMEOUT
1539 srv->tcp_ut = src->tcp_ut;
1540#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001541 if (srv_tmpl)
1542 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001543}
1544
1545static struct server *new_server(struct proxy *proxy)
1546{
1547 struct server *srv;
1548
1549 srv = calloc(1, sizeof *srv);
1550 if (!srv)
1551 return NULL;
1552
1553 srv->obj_type = OBJ_TYPE_SERVER;
1554 srv->proxy = proxy;
1555 LIST_INIT(&srv->actconns);
1556 LIST_INIT(&srv->pendconns);
1557 LIST_INIT(&srv->priv_conns);
1558 LIST_INIT(&srv->idle_conns);
1559 LIST_INIT(&srv->safe_conns);
Emeric Brun64cc49c2017-10-03 14:46:45 +02001560 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001561
Emeric Brun52a91d32017-08-31 14:41:55 +02001562 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001563 srv->last_change = now.tv_sec;
1564
1565 srv->check.status = HCHK_STATUS_INI;
1566 srv->check.server = srv;
1567 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1568
1569 srv->agent.status = HCHK_STATUS_INI;
1570 srv->agent.server = srv;
1571 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1572
1573 return srv;
1574}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001575
1576/*
1577 * Validate <srv> server health-check settings.
1578 * Returns 0 if everything is OK, -1 if not.
1579 */
1580static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1581{
1582 struct tcpcheck_rule *r = NULL;
1583 struct list *l;
1584
1585 /*
1586 * We need at least a service port, a check port or the first tcp-check rule must
1587 * be a 'connect' one when checking an IPv4/IPv6 server.
1588 */
1589 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1590 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1591 return 0;
1592
1593 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1594 if (!r) {
1595 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1596 "Check has been disabled.\n",
1597 file, linenum, srv->id);
1598 return -1;
1599 }
1600
1601 /* search the first action (connect / send / expect) in the list */
1602 l = &srv->proxy->tcpcheck_rules;
1603 list_for_each_entry(r, l, list) {
1604 if (r->action != TCPCHK_ACT_COMMENT)
1605 break;
1606 }
1607
1608 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1609 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1610 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1611 file, linenum, srv->id);
1612 return -1;
1613 }
1614
1615 /* scan the tcp-check ruleset to ensure a port has been configured */
1616 l = &srv->proxy->tcpcheck_rules;
1617 list_for_each_entry(r, l, list) {
1618 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1619 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1620 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1621 file, linenum, srv->id);
1622 return -1;
1623 }
1624 }
1625
1626 return 0;
1627}
1628
1629/*
1630 * Initialize <srv> health-check structure.
1631 * Returns the error string in case of memory allocation failure, NULL if not.
1632 */
1633static const char *do_health_check_init(struct server *srv, int check_type, int state)
1634{
1635 const char *ret;
1636
1637 if (!srv->do_check)
1638 return NULL;
1639
1640 ret = init_check(&srv->check, check_type);
1641 if (ret)
1642 return ret;
1643
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001644 srv->check.state |= state;
1645 global.maxsock++;
1646
1647 return NULL;
1648}
1649
1650static int server_health_check_init(const char *file, int linenum,
1651 struct server *srv, struct proxy *curproxy)
1652{
1653 const char *ret;
1654
1655 if (!srv->do_check)
1656 return 0;
1657
1658 if (srv->trackit) {
1659 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1660 file, linenum);
1661 return ERR_ALERT | ERR_FATAL;
1662 }
1663
1664 if (server_healthcheck_validate(file, linenum, srv) < 0)
1665 return ERR_ALERT | ERR_ABORT;
1666
1667 /* note: check type will be set during the config review phase */
1668 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1669 if (ret) {
1670 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1671 return ERR_ALERT | ERR_ABORT;
1672 }
1673
1674 return 0;
1675}
1676
1677/*
1678 * Initialize <srv> agent check structure.
1679 * Returns the error string in case of memory allocation failure, NULL if not.
1680 */
1681static const char *do_server_agent_check_init(struct server *srv, int state)
1682{
1683 const char *ret;
1684
1685 if (!srv->do_agent)
1686 return NULL;
1687
1688 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1689 if (ret)
1690 return ret;
1691
1692 if (!srv->agent.inter)
1693 srv->agent.inter = srv->check.inter;
1694
1695 srv->agent.state |= state;
1696 global.maxsock++;
1697
1698 return NULL;
1699}
1700
1701static int server_agent_check_init(const char *file, int linenum,
1702 struct server *srv, struct proxy *curproxy)
1703{
1704 const char *ret;
1705
1706 if (!srv->do_agent)
1707 return 0;
1708
1709 if (!srv->agent.port) {
1710 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1711 file, linenum, srv->id);
1712 return ERR_ALERT | ERR_FATAL;
1713 }
1714
1715 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1716 if (ret) {
1717 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1718 return ERR_ALERT | ERR_ABORT;
1719 }
1720
1721 return 0;
1722}
1723
1724#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1725static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1726 struct server *srv, struct proxy *proxy)
1727{
1728 int ret;
1729 char *err = NULL;
1730
1731 if (!srv->sni_expr)
1732 return 0;
1733
1734 ret = server_parse_sni_expr(srv, proxy, &err);
1735 if (!ret)
1736 return 0;
1737
1738 display_parser_err(file, linenum, args, cur_arg, &err);
1739 free(err);
1740
1741 return ret;
1742}
1743#endif
1744
1745/*
1746 * Server initializations finalization.
1747 * Initialize health check, agent check and SNI expression if enabled.
1748 * Must not be called for a default server instance.
1749 */
1750static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1751 struct server *srv, struct proxy *px)
1752{
1753 int ret;
1754
1755 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1756 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1757 return ret;
1758 }
1759
1760#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1761 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1762 return ret;
1763#endif
1764
1765 if (srv->flags & SRV_F_BACKUP)
1766 px->srv_bck++;
1767 else
1768 px->srv_act++;
1769 srv_lb_commit_status(srv);
1770
1771 return 0;
1772}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001773
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001774/*
1775 * Parse as much as possible such a range string argument: low[-high]
1776 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1777 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1778 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1779 * Returns 0 if succeeded, -1 if not.
1780 */
1781static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1782{
1783 char *nb_high_arg;
1784
1785 *nb_high = 0;
1786 chunk_printf(&trash, "%s", arg);
1787 *nb_low = atoi(trash.str);
1788
1789 if ((nb_high_arg = strchr(trash.str, '-'))) {
1790 *nb_high_arg++ = '\0';
1791 *nb_high = atoi(nb_high_arg);
1792 }
1793 else {
1794 *nb_high += *nb_low;
1795 *nb_low = 1;
1796 }
1797
1798 if (*nb_low < 0 || *nb_high < *nb_low)
1799 return -1;
1800
1801 return 0;
1802}
1803
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001804static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1805{
1806 chunk_printf(&trash, "%s%d", prefix, nb);
1807 free(srv->id);
1808 srv->id = strdup(trash.str);
1809}
1810
1811/*
1812 * Initialize as much as possible servers from <srv> server template.
1813 * Note that a server template is a special server with
1814 * a few different parameters than a server which has
1815 * been parsed mostly the same way as a server.
1816 * Returns the number of servers succesfully allocated,
1817 * 'srv' template included.
1818 */
1819static int server_template_init(struct server *srv, struct proxy *px)
1820{
1821 int i;
1822 struct server *newsrv;
1823
1824 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1825 int check_init_state;
1826 int agent_init_state;
1827
1828 newsrv = new_server(px);
1829 if (!newsrv)
1830 goto err;
1831
1832 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001833 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001834#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1835 if (newsrv->sni_expr) {
1836 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1837 if (!newsrv->ssl_ctx.sni)
1838 goto err;
1839 }
1840#endif
1841 /* Set this new server ID. */
1842 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1843
1844 /* Initial checks states. */
1845 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1846 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1847
1848 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1849 do_server_agent_check_init(newsrv, agent_init_state))
1850 goto err;
1851
1852 /* Linked backwards first. This will be restablished after parsing. */
1853 newsrv->next = px->srv;
1854 px->srv = newsrv;
1855 }
1856 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1857
1858 return i - srv->tmpl_info.nb_low;
1859
1860 err:
1861 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1862 if (newsrv) {
1863#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1864 release_sample_expr(newsrv->ssl_ctx.sni);
1865#endif
1866 free_check(&newsrv->agent);
1867 free_check(&newsrv->check);
1868 }
1869 free(newsrv);
1870 return i - srv->tmpl_info.nb_low;
1871}
1872
Willy Tarreau272adea2014-03-31 10:39:59 +02001873int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1874{
1875 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001876 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001877 char *errmsg = NULL;
1878 int err_code = 0;
1879 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001880 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001881
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001882 if (!strcmp(args[0], "server") ||
1883 !strcmp(args[0], "default-server") ||
1884 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001885 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001886 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001887 int srv = !defsrv && !strcmp(args[0], "server");
1888 int srv_tmpl = !defsrv && !srv;
1889 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001890
1891 if (!defsrv && curproxy == defproxy) {
1892 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1893 err_code |= ERR_ALERT | ERR_FATAL;
1894 goto out;
1895 }
1896 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1897 err_code |= ERR_ALERT | ERR_FATAL;
1898
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001899 /* There is no mandatory first arguments for default server. */
1900 if (srv) {
1901 if (!*args[2]) {
1902 /* 'server' line number of argument check. */
1903 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1904 file, linenum, args[0]);
1905 err_code |= ERR_ALERT | ERR_FATAL;
1906 goto out;
1907 }
1908
1909 err = invalid_char(args[1]);
1910 }
1911 else if (srv_tmpl) {
1912 if (!*args[3]) {
1913 /* 'server-template' line number of argument check. */
1914 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1915 file, linenum, args[0]);
1916 err_code |= ERR_ALERT | ERR_FATAL;
1917 goto out;
1918 }
1919
1920 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001921 }
1922
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001923 if (err) {
1924 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1925 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001926 err_code |= ERR_ALERT | ERR_FATAL;
1927 goto out;
1928 }
1929
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001930 cur_arg = 2;
1931 if (srv_tmpl) {
1932 /* Parse server-template <nb | range> arg. */
1933 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1934 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1935 file, linenum, args[0], args[cur_arg]);
1936 err_code |= ERR_ALERT | ERR_FATAL;
1937 goto out;
1938 }
1939 cur_arg++;
1940 }
1941
Willy Tarreau272adea2014-03-31 10:39:59 +02001942 if (!defsrv) {
1943 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001944 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001945 struct protocol *proto;
1946
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001947 newsrv = new_server(curproxy);
1948 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001949 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1950 err_code |= ERR_ALERT | ERR_ABORT;
1951 goto out;
1952 }
1953
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001954 if (srv_tmpl) {
1955 newsrv->tmpl_info.nb_low = tmpl_range_low;
1956 newsrv->tmpl_info.nb_high = tmpl_range_high;
1957 }
1958
Willy Tarreau272adea2014-03-31 10:39:59 +02001959 /* the servers are linked backwards first */
1960 newsrv->next = curproxy->srv;
1961 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001962 newsrv->conf.file = strdup(file);
1963 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001964 /* Note: for a server template, its id is its prefix.
1965 * This is a temporary id which will be used for server allocations to come
1966 * after parsing.
1967 */
1968 if (srv)
1969 newsrv->id = strdup(args[1]);
1970 else
1971 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001972
1973 /* several ways to check the port component :
1974 * - IP => port=+0, relative (IPv4 only)
1975 * - IP: => port=+0, relative
1976 * - IP:N => port=N, absolute
1977 * - IP:+N => port=+N, relative
1978 * - IP:-N => port=-N, relative
1979 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001980 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001981 if (!sk) {
1982 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1983 err_code |= ERR_ALERT | ERR_FATAL;
1984 goto out;
1985 }
1986
1987 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001988 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001989 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1990 file, linenum, args[0], args[1]);
1991 err_code |= ERR_ALERT | ERR_FATAL;
1992 goto out;
1993 }
1994
1995 if (!port1 || !port2) {
1996 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001997 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001998 }
1999 else if (port1 != port2) {
2000 /* port range */
2001 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
2002 file, linenum, args[0], args[1], args[2]);
2003 err_code |= ERR_ALERT | ERR_FATAL;
2004 goto out;
2005 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002006
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002007 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002008 if (fqdn) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02002009 if (fqdn[0] == '_') {
2010 struct dns_srvrq *srvrq = NULL;
2011 int found = 0;
2012 /* SRV record */
2013 /* Check if a SRV request already exists, and if not, create it */
2014 list_for_each_entry(srvrq, &curproxy->srvrq_list, list) {
2015 if (!strcmp(srvrq->name, fqdn)) {
2016 found = 1;
2017 break;
2018 }
2019 }
2020 if (found == 0) {
2021 int hostname_dn_len;
2022
2023 srvrq = calloc(1, sizeof(*srvrq));
2024 if (!srvrq) {
2025 Alert("Failed to allocate memory");
2026 err_code = ERR_ALERT | ERR_FATAL;
2027 goto out;
2028 }
2029 srvrq->obj_type = OBJ_TYPE_SRVRQ;
2030 srvrq->proxy = proxy;
2031 srvrq->name = strdup(fqdn);
2032 srvrq->inter = 2000;
2033 hostname_dn_len = dns_str_to_dn_label_len(fqdn);
2034 if (hostname_dn_len == -1) {
2035 Alert("Failed to parse domaine name '%s'", fqdn);
2036 err_code = ERR_ALERT | ERR_FATAL;
2037 goto out;
2038 }
2039 srvrq->hostname_dn = malloc(hostname_dn_len + 1);
2040 srvrq->hostname_dn_len = hostname_dn_len;
2041 if (!srvrq->hostname_dn) {
2042 Alert("Failed to alloc memory");
2043 err_code = ERR_ALERT | ERR_FATAL;
2044 goto out;
2045 }
2046 if (!dns_str_to_dn_label(fqdn,
2047 srvrq->hostname_dn,
2048 hostname_dn_len + 1)) {
2049 Alert("Failed to parse domain name '%s'", fqdn);
2050 err_code = ERR_ALERT | ERR_FATAL;
2051 goto out;
2052 }
2053 LIST_ADDQ(&proxy->srvrq_list, &srvrq->list);
2054
2055 }
2056 newsrv->srvrq = srvrq;
2057
2058
2059 } else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
2060 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
2061 file, linenum, newsrv->id);
2062 err_code |= ERR_ALERT | ERR_FATAL;
2063 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02002064 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002065 }
2066
Willy Tarreau272adea2014-03-31 10:39:59 +02002067 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002068 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002069
Olivier Houchard8da5f982017-08-04 18:35:36 +02002070 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002071 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002072 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002073 err_code |= ERR_ALERT | ERR_FATAL;
2074 goto out;
2075 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002076
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002077 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002078 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002079 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002080 } else {
2081 newsrv = &curproxy->defsrv;
2082 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002083 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002084 }
2085
2086 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002087 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002088 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2089 if (err) {
2090 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2091 file, linenum, *err, newsrv->id);
2092 err_code |= ERR_ALERT | ERR_FATAL;
2093 goto out;
2094 }
2095 if (val <= 0) {
2096 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2097 file, linenum, val, args[cur_arg], newsrv->id);
2098 err_code |= ERR_ALERT | ERR_FATAL;
2099 goto out;
2100 }
2101 newsrv->agent.inter = val;
2102 cur_arg += 2;
2103 }
Misiekea849332017-01-09 09:39:51 +01002104 else if (!strcmp(args[cur_arg], "agent-addr")) {
2105 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2106 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2107 goto out;
2108 }
2109
2110 cur_arg += 2;
2111 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002112 else if (!strcmp(args[cur_arg], "agent-port")) {
2113 global.maxsock++;
2114 newsrv->agent.port = atol(args[cur_arg + 1]);
2115 cur_arg += 2;
2116 }
James Brown55f9ff12015-10-21 18:19:05 -07002117 else if (!strcmp(args[cur_arg], "agent-send")) {
2118 global.maxsock++;
2119 free(newsrv->agent.send_string);
2120 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2121 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2122 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2123 cur_arg += 2;
2124 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002125 else if (!strcmp(args[cur_arg], "init-addr")) {
2126 char *p, *end;
2127 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002128 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002129
2130 newsrv->init_addr_methods = 0;
2131 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2132
2133 for (p = args[cur_arg + 1]; *p; p = end) {
2134 /* cut on next comma */
2135 for (end = p; *end && *end != ','; end++);
2136 if (*end)
2137 *(end++) = 0;
2138
Willy Tarreau4310d362016-11-02 15:05:56 +01002139 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002140 if (!strcmp(p, "libc")) {
2141 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2142 }
2143 else if (!strcmp(p, "last")) {
2144 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2145 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002146 else if (!strcmp(p, "none")) {
2147 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2148 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002149 else if (str2ip2(p, &sa, 0)) {
2150 if (is_addr(&newsrv->init_addr)) {
2151 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2152 file, linenum, args[cur_arg], p);
2153 err_code |= ERR_ALERT | ERR_FATAL;
2154 goto out;
2155 }
2156 newsrv->init_addr = sa;
2157 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2158 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002159 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002160 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002161 file, linenum, args[cur_arg], p);
2162 err_code |= ERR_ALERT | ERR_FATAL;
2163 goto out;
2164 }
2165 if (!done) {
2166 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2167 file, linenum, args[cur_arg], p);
2168 err_code |= ERR_ALERT | ERR_FATAL;
2169 goto out;
2170 }
2171 }
2172 cur_arg += 2;
2173 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002174 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002175 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002176 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2177 cur_arg += 2;
2178 }
2179 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2180 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002181 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002182 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002183 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002184 else {
2185 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2186 file, linenum, args[cur_arg]);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190 cur_arg += 2;
2191 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002192 else if (!strcmp(args[cur_arg], "resolve-net")) {
2193 char *p, *e;
2194 unsigned char mask;
2195 struct dns_options *opt;
2196
2197 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2198 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2199 file, linenum, args[cur_arg]);
2200 err_code |= ERR_ALERT | ERR_FATAL;
2201 goto out;
2202 }
2203
2204 opt = &newsrv->dns_opts;
2205
2206 /* Split arguments by comma, and convert it from ipv4 or ipv6
2207 * string network in in_addr or in6_addr.
2208 */
2209 p = args[cur_arg + 1];
2210 e = p;
2211 while (*p != '\0') {
2212 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002213 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002214 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2215 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219 /* look for end or comma. */
2220 while (*e != ',' && *e != '\0')
2221 e++;
2222 if (*e == ',') {
2223 *e = '\0';
2224 e++;
2225 }
2226 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2227 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2228 /* Try to convert input string from ipv4 or ipv6 network. */
2229 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2230 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2231 &mask)) {
2232 /* Try to convert input string from ipv6 network. */
2233 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2234 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2235 } else {
2236 /* All network conversions fail, retrun error. */
2237 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2238 file, linenum, args[cur_arg], p);
2239 err_code |= ERR_ALERT | ERR_FATAL;
2240 goto out;
2241 }
2242 opt->pref_net_nb++;
2243 p = e;
2244 }
2245
2246 cur_arg += 2;
2247 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002248 else if (!strcmp(args[cur_arg], "rise")) {
2249 if (!*args[cur_arg + 1]) {
2250 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2251 file, linenum, args[cur_arg]);
2252 err_code |= ERR_ALERT | ERR_FATAL;
2253 goto out;
2254 }
2255
2256 newsrv->check.rise = atol(args[cur_arg + 1]);
2257 if (newsrv->check.rise <= 0) {
2258 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2259 file, linenum, args[cur_arg]);
2260 err_code |= ERR_ALERT | ERR_FATAL;
2261 goto out;
2262 }
2263
2264 if (newsrv->check.health)
2265 newsrv->check.health = newsrv->check.rise;
2266 cur_arg += 2;
2267 }
2268 else if (!strcmp(args[cur_arg], "fall")) {
2269 newsrv->check.fall = atol(args[cur_arg + 1]);
2270
2271 if (!*args[cur_arg + 1]) {
2272 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2273 file, linenum, args[cur_arg]);
2274 err_code |= ERR_ALERT | ERR_FATAL;
2275 goto out;
2276 }
2277
2278 if (newsrv->check.fall <= 0) {
2279 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2280 file, linenum, args[cur_arg]);
2281 err_code |= ERR_ALERT | ERR_FATAL;
2282 goto out;
2283 }
2284
2285 cur_arg += 2;
2286 }
2287 else if (!strcmp(args[cur_arg], "inter")) {
2288 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2289 if (err) {
2290 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2291 file, linenum, *err, newsrv->id);
2292 err_code |= ERR_ALERT | ERR_FATAL;
2293 goto out;
2294 }
2295 if (val <= 0) {
2296 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2297 file, linenum, val, args[cur_arg], newsrv->id);
2298 err_code |= ERR_ALERT | ERR_FATAL;
2299 goto out;
2300 }
2301 newsrv->check.inter = val;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002302 if (newsrv->srvrq)
2303 newsrv->srvrq->inter = val;
Willy Tarreau272adea2014-03-31 10:39:59 +02002304 cur_arg += 2;
2305 }
2306 else if (!strcmp(args[cur_arg], "fastinter")) {
2307 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2308 if (err) {
2309 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2310 file, linenum, *err, newsrv->id);
2311 err_code |= ERR_ALERT | ERR_FATAL;
2312 goto out;
2313 }
2314 if (val <= 0) {
2315 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2316 file, linenum, val, args[cur_arg], newsrv->id);
2317 err_code |= ERR_ALERT | ERR_FATAL;
2318 goto out;
2319 }
2320 newsrv->check.fastinter = val;
2321 cur_arg += 2;
2322 }
2323 else if (!strcmp(args[cur_arg], "downinter")) {
2324 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2325 if (err) {
2326 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2327 file, linenum, *err, newsrv->id);
2328 err_code |= ERR_ALERT | ERR_FATAL;
2329 goto out;
2330 }
2331 if (val <= 0) {
2332 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2333 file, linenum, val, args[cur_arg], newsrv->id);
2334 err_code |= ERR_ALERT | ERR_FATAL;
2335 goto out;
2336 }
2337 newsrv->check.downinter = val;
2338 cur_arg += 2;
2339 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002340 else if (!strcmp(args[cur_arg], "port")) {
2341 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002342 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002343 cur_arg += 2;
2344 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002345 else if (!strcmp(args[cur_arg], "weight")) {
2346 int w;
2347 w = atol(args[cur_arg + 1]);
2348 if (w < 0 || w > SRV_UWGHT_MAX) {
2349 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2350 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2351 err_code |= ERR_ALERT | ERR_FATAL;
2352 goto out;
2353 }
2354 newsrv->uweight = newsrv->iweight = w;
2355 cur_arg += 2;
2356 }
2357 else if (!strcmp(args[cur_arg], "minconn")) {
2358 newsrv->minconn = atol(args[cur_arg + 1]);
2359 cur_arg += 2;
2360 }
2361 else if (!strcmp(args[cur_arg], "maxconn")) {
2362 newsrv->maxconn = atol(args[cur_arg + 1]);
2363 cur_arg += 2;
2364 }
2365 else if (!strcmp(args[cur_arg], "maxqueue")) {
2366 newsrv->maxqueue = atol(args[cur_arg + 1]);
2367 cur_arg += 2;
2368 }
2369 else if (!strcmp(args[cur_arg], "slowstart")) {
2370 /* slowstart is stored in seconds */
2371 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2372 if (err) {
2373 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2374 file, linenum, *err, newsrv->id);
2375 err_code |= ERR_ALERT | ERR_FATAL;
2376 goto out;
2377 }
2378 newsrv->slowstart = (val + 999) / 1000;
2379 cur_arg += 2;
2380 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002381 else if (!strcmp(args[cur_arg], "on-error")) {
2382 if (!strcmp(args[cur_arg + 1], "fastinter"))
2383 newsrv->onerror = HANA_ONERR_FASTINTER;
2384 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2385 newsrv->onerror = HANA_ONERR_FAILCHK;
2386 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2387 newsrv->onerror = HANA_ONERR_SUDDTH;
2388 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2389 newsrv->onerror = HANA_ONERR_MARKDWN;
2390 else {
2391 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2392 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2393 file, linenum, args[cur_arg], args[cur_arg + 1]);
2394 err_code |= ERR_ALERT | ERR_FATAL;
2395 goto out;
2396 }
2397
2398 cur_arg += 2;
2399 }
2400 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2401 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2402 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2403 else {
2404 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2405 file, linenum, args[cur_arg], args[cur_arg + 1]);
2406 err_code |= ERR_ALERT | ERR_FATAL;
2407 goto out;
2408 }
2409
2410 cur_arg += 2;
2411 }
2412 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2413 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2414 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2415 else {
2416 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2417 file, linenum, args[cur_arg], args[cur_arg + 1]);
2418 err_code |= ERR_ALERT | ERR_FATAL;
2419 goto out;
2420 }
2421
2422 cur_arg += 2;
2423 }
2424 else if (!strcmp(args[cur_arg], "error-limit")) {
2425 if (!*args[cur_arg + 1]) {
2426 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2427 file, linenum, args[cur_arg]);
2428 err_code |= ERR_ALERT | ERR_FATAL;
2429 goto out;
2430 }
2431
2432 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2433
2434 if (newsrv->consecutive_errors_limit <= 0) {
2435 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2436 file, linenum, args[cur_arg]);
2437 err_code |= ERR_ALERT | ERR_FATAL;
2438 goto out;
2439 }
2440 cur_arg += 2;
2441 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002442 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002443 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2444 file, linenum, "usesrc", "source");
2445 err_code |= ERR_ALERT | ERR_FATAL;
2446 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002447 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002448 else {
2449 static int srv_dumped;
2450 struct srv_kw *kw;
2451 char *err;
2452
2453 kw = srv_find_kw(args[cur_arg]);
2454 if (kw) {
2455 char *err = NULL;
2456 int code;
2457
2458 if (!kw->parse) {
2459 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2460 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002461 if (kw->skip != -1)
2462 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002463 err_code |= ERR_ALERT | ERR_FATAL;
2464 goto out;
2465 }
2466
2467 if (defsrv && !kw->default_ok) {
2468 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2469 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002470 if (kw->skip != -1)
2471 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002472 err_code |= ERR_ALERT;
2473 continue;
2474 }
2475
2476 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2477 err_code |= code;
2478
2479 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002480 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002481 if (code & ERR_FATAL) {
2482 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002483 if (kw->skip != -1)
2484 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002485 goto out;
2486 }
2487 }
2488 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002489 if (kw->skip != -1)
2490 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002491 continue;
2492 }
2493
2494 err = NULL;
2495 if (!srv_dumped) {
2496 srv_dump_kws(&err);
2497 indent_msg(&err, 4);
2498 srv_dumped = 1;
2499 }
2500
2501 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2502 file, linenum, args[0], args[1], args[cur_arg],
2503 err ? " Registered keywords :" : "", err ? err : "");
2504 free(err);
2505
2506 err_code |= ERR_ALERT | ERR_FATAL;
2507 goto out;
2508 }
2509 }
2510
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002511 if (!defsrv)
2512 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2513 if (err_code & ERR_FATAL)
2514 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002515 if (srv_tmpl)
2516 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002517 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002518 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002519 return 0;
2520
2521 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002522 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002523 free(errmsg);
2524 return err_code;
2525}
2526
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002527/* Returns a pointer to the first server matching either id <id>.
2528 * NULL is returned if no match is found.
2529 * the lookup is performed in the backend <bk>
2530 */
2531struct server *server_find_by_id(struct proxy *bk, int id)
2532{
2533 struct eb32_node *eb32;
2534 struct server *curserver;
2535
2536 if (!bk || (id ==0))
2537 return NULL;
2538
2539 /* <bk> has no backend capabilities, so it can't have a server */
2540 if (!(bk->cap & PR_CAP_BE))
2541 return NULL;
2542
2543 curserver = NULL;
2544
2545 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2546 if (eb32)
2547 curserver = container_of(eb32, struct server, conf.id);
2548
2549 return curserver;
2550}
2551
2552/* Returns a pointer to the first server matching either name <name>, or id
2553 * if <name> starts with a '#'. NULL is returned if no match is found.
2554 * the lookup is performed in the backend <bk>
2555 */
2556struct server *server_find_by_name(struct proxy *bk, const char *name)
2557{
2558 struct server *curserver;
2559
2560 if (!bk || !name)
2561 return NULL;
2562
2563 /* <bk> has no backend capabilities, so it can't have a server */
2564 if (!(bk->cap & PR_CAP_BE))
2565 return NULL;
2566
2567 curserver = NULL;
2568 if (*name == '#') {
2569 curserver = server_find_by_id(bk, atoi(name + 1));
2570 if (curserver)
2571 return curserver;
2572 }
2573 else {
2574 curserver = bk->srv;
2575
2576 while (curserver && (strcmp(curserver->id, name) != 0))
2577 curserver = curserver->next;
2578
2579 if (curserver)
2580 return curserver;
2581 }
2582
2583 return NULL;
2584}
2585
2586struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2587{
2588 struct server *byname;
2589 struct server *byid;
2590
2591 if (!name && !id)
2592 return NULL;
2593
2594 if (diff)
2595 *diff = 0;
2596
2597 byname = byid = NULL;
2598
2599 if (name) {
2600 byname = server_find_by_name(bk, name);
2601 if (byname && (!id || byname->puid == id))
2602 return byname;
2603 }
2604
2605 /* remaining possibilities :
2606 * - name not set
2607 * - name set but not found
2608 * - name found but ID doesn't match
2609 */
2610 if (id) {
2611 byid = server_find_by_id(bk, id);
2612 if (byid) {
2613 if (byname) {
2614 /* use id only if forced by configuration */
2615 if (byid->flags & SRV_F_FORCED_ID) {
2616 if (diff)
2617 *diff |= 2;
2618 return byid;
2619 }
2620 else {
2621 if (diff)
2622 *diff |= 1;
2623 return byname;
2624 }
2625 }
2626
2627 /* remaining possibilities:
2628 * - name not set
2629 * - name set but not found
2630 */
2631 if (name && diff)
2632 *diff |= 2;
2633 return byid;
2634 }
2635
2636 /* id bot found */
2637 if (byname) {
2638 if (diff)
2639 *diff |= 1;
2640 return byname;
2641 }
2642 }
2643
2644 return NULL;
2645}
2646
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002647/* Update a server state using the parameters available in the params list */
2648static void srv_update_state(struct server *srv, int version, char **params)
2649{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002650 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002651 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002652
2653 /* fields since version 1
2654 * and common to all other upcoming versions
2655 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002656 enum srv_state srv_op_state;
2657 enum srv_admin srv_admin_state;
2658 unsigned srv_uweight, srv_iweight;
2659 unsigned long srv_last_time_change;
2660 short srv_check_status;
2661 enum chk_result srv_check_result;
2662 int srv_check_health;
2663 int srv_check_state, srv_agent_state;
2664 int bk_f_forced_id;
2665 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002666 int fqdn_set_by_cli;
2667 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002668 const char *port_str;
2669 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002670
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002671 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002672 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002673 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002674 switch (version) {
2675 case 1:
2676 /*
2677 * now we can proceed with server's state update:
2678 * srv_addr: params[0]
2679 * srv_op_state: params[1]
2680 * srv_admin_state: params[2]
2681 * srv_uweight: params[3]
2682 * srv_iweight: params[4]
2683 * srv_last_time_change: params[5]
2684 * srv_check_status: params[6]
2685 * srv_check_result: params[7]
2686 * srv_check_health: params[8]
2687 * srv_check_state: params[9]
2688 * srv_agent_state: params[10]
2689 * bk_f_forced_id: params[11]
2690 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002691 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002692 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002693 */
2694
2695 /* validating srv_op_state */
2696 p = NULL;
2697 errno = 0;
2698 srv_op_state = strtol(params[1], &p, 10);
2699 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2700 (srv_op_state != SRV_ST_STOPPED &&
2701 srv_op_state != SRV_ST_STARTING &&
2702 srv_op_state != SRV_ST_RUNNING &&
2703 srv_op_state != SRV_ST_STOPPING)) {
2704 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2705 }
2706
2707 /* validating srv_admin_state */
2708 p = NULL;
2709 errno = 0;
2710 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002711 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002712
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002713 /* inherited statuses will be recomputed later.
2714 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2715 */
2716 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002717
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002718 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2719 (srv_admin_state != 0 &&
2720 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002721 srv_admin_state != SRV_ADMF_CMAINT &&
2722 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002723 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002724 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002725 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2726 }
2727
2728 /* validating srv_uweight */
2729 p = NULL;
2730 errno = 0;
2731 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002732 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002733 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2734
2735 /* validating srv_iweight */
2736 p = NULL;
2737 errno = 0;
2738 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002739 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002740 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2741
2742 /* validating srv_last_time_change */
2743 p = NULL;
2744 errno = 0;
2745 srv_last_time_change = strtol(params[5], &p, 10);
2746 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2747 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2748
2749 /* validating srv_check_status */
2750 p = NULL;
2751 errno = 0;
2752 srv_check_status = strtol(params[6], &p, 10);
2753 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2754 (srv_check_status >= HCHK_STATUS_SIZE))
2755 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2756
2757 /* validating srv_check_result */
2758 p = NULL;
2759 errno = 0;
2760 srv_check_result = strtol(params[7], &p, 10);
2761 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2762 (srv_check_result != CHK_RES_UNKNOWN &&
2763 srv_check_result != CHK_RES_NEUTRAL &&
2764 srv_check_result != CHK_RES_FAILED &&
2765 srv_check_result != CHK_RES_PASSED &&
2766 srv_check_result != CHK_RES_CONDPASS)) {
2767 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2768 }
2769
2770 /* validating srv_check_health */
2771 p = NULL;
2772 errno = 0;
2773 srv_check_health = strtol(params[8], &p, 10);
2774 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2775 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2776
2777 /* validating srv_check_state */
2778 p = NULL;
2779 errno = 0;
2780 srv_check_state = strtol(params[9], &p, 10);
2781 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2782 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2783 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2784
2785 /* validating srv_agent_state */
2786 p = NULL;
2787 errno = 0;
2788 srv_agent_state = strtol(params[10], &p, 10);
2789 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2790 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2791 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2792
2793 /* validating bk_f_forced_id */
2794 p = NULL;
2795 errno = 0;
2796 bk_f_forced_id = strtol(params[11], &p, 10);
2797 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2798 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2799
2800 /* validating srv_f_forced_id */
2801 p = NULL;
2802 errno = 0;
2803 srv_f_forced_id = strtol(params[12], &p, 10);
2804 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2805 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2806
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002807 /* validating srv_fqdn */
2808 fqdn = params[13];
2809 if (fqdn && *fqdn == '-')
2810 fqdn = NULL;
2811 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2812 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2813 fqdn = NULL;
2814 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002815
Frédéric Lécaille31694712017-08-01 08:47:19 +02002816 port_str = params[14];
2817 if (port_str) {
2818 port = strl2uic(port_str, strlen(port_str));
2819 if (port > USHRT_MAX) {
2820 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2821 port_str = NULL;
2822 }
2823 }
2824
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002825 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002826 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002827 goto out;
2828
2829 /* recover operational state and apply it to this server
2830 * and all servers tracking this one */
2831 switch (srv_op_state) {
2832 case SRV_ST_STOPPED:
2833 srv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02002834 srv_set_stopped(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002835 break;
2836 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002837 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002838 break;
2839 case SRV_ST_STOPPING:
2840 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002841 srv_set_stopping(srv, "changed from server-state after a reload", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002842 break;
2843 case SRV_ST_RUNNING:
2844 srv->check.health = srv->check.rise + srv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02002845 srv_set_running(srv, "", NULL);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002846 break;
2847 }
2848
2849 /* When applying server state, the following rules apply:
2850 * - in case of a configuration change, we apply the setting from the new
2851 * configuration, regardless of old running state
2852 * - if no configuration change, we apply old running state only if old running
2853 * state is different from new configuration state
2854 */
2855 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002856 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2857 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002858 srv_adm_set_maint(srv);
2859 else
2860 srv_adm_set_ready(srv);
2861 }
2862 /* configuration is the same, let's compate old running state and new conf state */
2863 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002864 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002865 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002866 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002867 srv_adm_set_ready(srv);
2868 }
2869 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002870 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002871 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002872 * (srv->iweight is the weight set up in configuration).
2873 * There are two possible reasons for FDRAIN to have been present :
2874 * - previous config weight was zero
2875 * - "set server b/s drain" was sent to the CLI
2876 *
2877 * In the first case, we simply want to drop this drain state
2878 * if the new weight is not zero anymore, meaning the administrator
2879 * has intentionally turned the weight back to a positive value to
2880 * enable the server again after an operation. In the second case,
2881 * the drain state was forced on the CLI regardless of the config's
2882 * weight so we don't want a change to the config weight to lose this
2883 * status. What this means is :
2884 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2885 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002886 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002887 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002888 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002889 }
2890
2891 srv->last_change = date.tv_sec - srv_last_time_change;
2892 srv->check.status = srv_check_status;
2893 srv->check.result = srv_check_result;
2894 srv->check.health = srv_check_health;
2895
2896 /* Only case we want to apply is removing ENABLED flag which could have been
2897 * done by the "disable health" command over the stats socket
2898 */
2899 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2900 (srv_check_state & CHK_ST_CONFIGURED) &&
2901 !(srv_check_state & CHK_ST_ENABLED))
2902 srv->check.state &= ~CHK_ST_ENABLED;
2903
2904 /* Only case we want to apply is removing ENABLED flag which could have been
2905 * done by the "disable agent" command over the stats socket
2906 */
2907 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2908 (srv_agent_state & CHK_ST_CONFIGURED) &&
2909 !(srv_agent_state & CHK_ST_ENABLED))
2910 srv->agent.state &= ~CHK_ST_ENABLED;
2911
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002912 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2913 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002914 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002915 * It means that a configuration file change has precedence over a unix socket change
2916 * for server's weight
2917 *
2918 * by default, HAProxy applies the following weight when parsing the configuration
2919 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002920 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002921 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002922 srv->uweight = srv_uweight;
2923 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002924 server_recalc_eweight(srv);
2925
Willy Tarreaue5a60682016-11-09 14:54:53 +01002926 /* load server IP address */
2927 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002928
2929 if (fqdn && srv->hostname) {
2930 if (!strcmp(srv->hostname, fqdn)) {
2931 /* Here we reset the 'set from stats socket FQDN' flag
2932 * to support such transitions:
2933 * Let's say initial FQDN value is foo1 (in configuration file).
2934 * - FQDN changed from stats socket, from foo1 to foo2 value,
2935 * - FQDN changed again from file configuration (with the same previous value
2936 set from stats socket, from foo1 to foo2 value),
2937 * - reload for any other reason than a FQDN modification,
2938 * the configuration file FQDN matches the fqdn server state file value.
2939 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2940 * any futher FQDN modification.
2941 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002942 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002943 }
2944 else {
2945 /* If the FDQN has been changed from stats socket,
2946 * apply fqdn state file value (which is the value set
2947 * from stats socket).
2948 */
2949 if (fqdn_set_by_cli) {
2950 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02002951 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002952 }
2953 }
2954 }
2955
Frédéric Lécaille31694712017-08-01 08:47:19 +02002956 if (port_str)
2957 srv->svc_port = port;
2958
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002959 break;
2960 default:
2961 chunk_appendf(msg, ", version '%d' not supported", version);
2962 }
2963
2964 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002965 if (msg->len) {
2966 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002967 Warning("server-state application failed for server '%s/%s'%s",
2968 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002969 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002970}
2971
2972/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2973 * For each proxy, it does the following:
2974 * - opens its server state file (either one or local one)
2975 * - read whole file, line by line
2976 * - analyse each line to check if it matches our current backend:
2977 * - backend name matches
2978 * - backend id matches if id is forced and name doesn't match
2979 * - if the server pointed by the line is found, then state is applied
2980 *
2981 * If the running backend uuid or id differs from the state file, then HAProxy reports
2982 * a warning.
2983 */
2984void apply_server_state(void)
2985{
2986 char *cur, *end;
2987 char mybuf[SRV_STATE_LINE_MAXLEN];
2988 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002989 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2990 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002991 int arg, srv_arg, version, diff;
2992 FILE *f;
2993 char *filepath;
2994 char globalfilepath[MAXPATHLEN + 1];
2995 char localfilepath[MAXPATHLEN + 1];
2996 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2997 extern struct proxy *proxy;
2998 struct proxy *curproxy, *bk;
2999 struct server *srv;
3000
3001 globalfilepathlen = 0;
3002 /* create the globalfilepath variable */
3003 if (global.server_state_file) {
3004 /* absolute path or no base directory provided */
3005 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
3006 len = strlen(global.server_state_file);
3007 if (len > MAXPATHLEN) {
3008 globalfilepathlen = 0;
3009 goto globalfileerror;
3010 }
3011 memcpy(globalfilepath, global.server_state_file, len);
3012 globalfilepath[len] = '\0';
3013 globalfilepathlen = len;
3014 }
3015 else if (global.server_state_base) {
3016 len = strlen(global.server_state_base);
3017 globalfilepathlen += len;
3018
3019 if (globalfilepathlen > MAXPATHLEN) {
3020 globalfilepathlen = 0;
3021 goto globalfileerror;
3022 }
3023 strncpy(globalfilepath, global.server_state_base, len);
3024 globalfilepath[globalfilepathlen] = 0;
3025
3026 /* append a slash if needed */
3027 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
3028 if (globalfilepathlen + 1 > MAXPATHLEN) {
3029 globalfilepathlen = 0;
3030 goto globalfileerror;
3031 }
3032 globalfilepath[globalfilepathlen++] = '/';
3033 }
3034
3035 len = strlen(global.server_state_file);
3036 if (globalfilepathlen + len > MAXPATHLEN) {
3037 globalfilepathlen = 0;
3038 goto globalfileerror;
3039 }
3040 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
3041 globalfilepathlen += len;
3042 globalfilepath[globalfilepathlen++] = 0;
3043 }
3044 }
3045 globalfileerror:
3046 if (globalfilepathlen == 0)
3047 globalfilepath[0] = '\0';
3048
3049 /* read servers state from local file */
3050 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
3051 /* servers are only in backends */
3052 if (!(curproxy->cap & PR_CAP_BE))
3053 continue;
3054 fileopenerr = 0;
3055 filepath = NULL;
3056
3057 /* search server state file path and name */
3058 switch (curproxy->load_server_state_from_file) {
3059 /* read servers state from global file */
3060 case PR_SRV_STATE_FILE_GLOBAL:
3061 /* there was an error while generating global server state file path */
3062 if (globalfilepathlen == 0)
3063 continue;
3064 filepath = globalfilepath;
3065 fileopenerr = 1;
3066 break;
3067 /* this backend has its own file */
3068 case PR_SRV_STATE_FILE_LOCAL:
3069 localfilepathlen = 0;
3070 localfilepath[0] = '\0';
3071 len = 0;
3072 /* create the localfilepath variable */
3073 /* absolute path or no base directory provided */
3074 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3075 len = strlen(curproxy->server_state_file_name);
3076 if (len > MAXPATHLEN) {
3077 localfilepathlen = 0;
3078 goto localfileerror;
3079 }
3080 memcpy(localfilepath, curproxy->server_state_file_name, len);
3081 localfilepath[len] = '\0';
3082 localfilepathlen = len;
3083 }
3084 else if (global.server_state_base) {
3085 len = strlen(global.server_state_base);
3086 localfilepathlen += len;
3087
3088 if (localfilepathlen > MAXPATHLEN) {
3089 localfilepathlen = 0;
3090 goto localfileerror;
3091 }
3092 strncpy(localfilepath, global.server_state_base, len);
3093 localfilepath[localfilepathlen] = 0;
3094
3095 /* append a slash if needed */
3096 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3097 if (localfilepathlen + 1 > MAXPATHLEN) {
3098 localfilepathlen = 0;
3099 goto localfileerror;
3100 }
3101 localfilepath[localfilepathlen++] = '/';
3102 }
3103
3104 len = strlen(curproxy->server_state_file_name);
3105 if (localfilepathlen + len > MAXPATHLEN) {
3106 localfilepathlen = 0;
3107 goto localfileerror;
3108 }
3109 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3110 localfilepathlen += len;
3111 localfilepath[localfilepathlen++] = 0;
3112 }
3113 filepath = localfilepath;
3114 localfileerror:
3115 if (localfilepathlen == 0)
3116 localfilepath[0] = '\0';
3117
3118 break;
3119 case PR_SRV_STATE_FILE_NONE:
3120 default:
3121 continue;
3122 }
3123
3124 /* preload global state file */
3125 errno = 0;
3126 f = fopen(filepath, "r");
3127 if (errno && fileopenerr)
3128 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3129 if (!f)
3130 continue;
3131
3132 mybuf[0] = '\0';
3133 mybuflen = 0;
3134 version = 0;
3135
3136 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003137 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3138 Warning("Can't read first line of the server state file '%s'\n", filepath);
3139 goto fileclose;
3140 }
3141
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003142 cur = mybuf;
3143 version = atoi(cur);
3144 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3145 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003146 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003147
3148 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3149 int bk_f_forced_id = 0;
3150 int check_id = 0;
3151 int check_name = 0;
3152
3153 mybuflen = strlen(mybuf);
3154 cur = mybuf;
3155 end = cur + mybuflen;
3156
3157 bk = NULL;
3158 srv = NULL;
3159
3160 /* we need at least one character */
3161 if (mybuflen == 0)
3162 continue;
3163
3164 /* ignore blank characters at the beginning of the line */
3165 while (isspace(*cur))
3166 ++cur;
3167
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003168 /* Ignore empty or commented lines */
3169 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003170 continue;
3171
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003172 /* truncated lines */
3173 if (mybuf[mybuflen - 1] != '\n') {
3174 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003175 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003176 }
3177
3178 /* Removes trailing '\n' */
3179 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003180
3181 /* we're now ready to move the line into *srv_params[] */
3182 params[0] = cur;
3183 arg = 1;
3184 srv_arg = 0;
3185 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3186 if (isspace(*cur)) {
3187 *cur = '\0';
3188 ++cur;
3189 while (isspace(*cur))
3190 ++cur;
3191 switch (version) {
3192 case 1:
3193 /*
3194 * srv_addr: params[4] => srv_params[0]
3195 * srv_op_state: params[5] => srv_params[1]
3196 * srv_admin_state: params[6] => srv_params[2]
3197 * srv_uweight: params[7] => srv_params[3]
3198 * srv_iweight: params[8] => srv_params[4]
3199 * srv_last_time_change: params[9] => srv_params[5]
3200 * srv_check_status: params[10] => srv_params[6]
3201 * srv_check_result: params[11] => srv_params[7]
3202 * srv_check_health: params[12] => srv_params[8]
3203 * srv_check_state: params[13] => srv_params[9]
3204 * srv_agent_state: params[14] => srv_params[10]
3205 * bk_f_forced_id: params[15] => srv_params[11]
3206 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003207 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003208 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003209 */
3210 if (arg >= 4) {
3211 srv_params[srv_arg] = cur;
3212 ++srv_arg;
3213 }
3214 break;
3215 }
3216
3217 params[arg] = cur;
3218 ++arg;
3219 }
3220 else {
3221 ++cur;
3222 }
3223 }
3224
3225 /* if line is incomplete line, then ignore it.
3226 * otherwise, update useful flags */
3227 switch (version) {
3228 case 1:
3229 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3230 continue;
3231 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3232 check_id = (atoi(params[0]) == curproxy->uuid);
3233 check_name = (strcmp(curproxy->id, params[1]) == 0);
3234 break;
3235 }
3236
3237 diff = 0;
3238 bk = curproxy;
3239
3240 /* if backend can't be found, let's continue */
3241 if (!check_id && !check_name)
3242 continue;
3243 else if (!check_id && check_name) {
3244 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3245 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3246 }
3247 else if (check_id && !check_name) {
3248 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3249 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3250 /* if name doesn't match, we still want to update curproxy if the backend id
3251 * was forced in previous the previous configuration */
3252 if (!bk_f_forced_id)
3253 continue;
3254 }
3255
3256 /* look for the server by its id: param[2] */
3257 /* else look for the server by its name: param[3] */
3258 diff = 0;
3259 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3260
3261 if (!srv) {
3262 /* if no server found, then warning and continue with next line */
3263 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3264 params[3], params[2], params[0], params[1]);
3265 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3266 params[3], params[2], params[0], params[1]);
3267 continue;
3268 }
3269 else if (diff & PR_FBM_MISMATCH_ID) {
3270 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);
3271 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 +02003272 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003273 }
3274 else if (diff & PR_FBM_MISMATCH_NAME) {
3275 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);
3276 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 +02003277 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003278 }
3279
3280 /* now we can proceed with server's state update */
3281 srv_update_state(srv, version, srv_params);
3282 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003283fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003284 fclose(f);
3285 }
3286}
3287
Simon Horman7d09b9a2013-02-12 10:45:51 +09003288/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003289 * update a server's current IP address.
3290 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3291 * ip is in network format.
3292 * updater is a string which contains an information about the requester of the update.
3293 * updater is used if not NULL.
3294 *
3295 * A log line and a stderr warning message is generated based on server's backend options.
3296 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003297int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003298{
3299 /* generates a log line and a warning on stderr */
3300 if (1) {
3301 /* book enough space for both IPv4 and IPv6 */
3302 char oldip[INET6_ADDRSTRLEN];
3303 char newip[INET6_ADDRSTRLEN];
3304
3305 memset(oldip, '\0', INET6_ADDRSTRLEN);
3306 memset(newip, '\0', INET6_ADDRSTRLEN);
3307
3308 /* copy old IP address in a string */
3309 switch (s->addr.ss_family) {
3310 case AF_INET:
3311 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3312 break;
3313 case AF_INET6:
3314 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3315 break;
3316 };
3317
3318 /* copy new IP address in a string */
3319 switch (ip_sin_family) {
3320 case AF_INET:
3321 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3322 break;
3323 case AF_INET6:
3324 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3325 break;
3326 };
3327
3328 /* save log line into a buffer */
3329 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3330 s->proxy->id, s->id, oldip, newip, updater);
3331
3332 /* write the buffer on stderr */
3333 Warning("%s.\n", trash.str);
3334
3335 /* send a log */
3336 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3337 }
3338
3339 /* save the new IP family */
3340 s->addr.ss_family = ip_sin_family;
3341 /* save the new IP address */
3342 switch (ip_sin_family) {
3343 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003344 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003345 break;
3346 case AF_INET6:
3347 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3348 break;
3349 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003350 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003351
3352 return 0;
3353}
3354
3355/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003356 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3357 *
3358 * Caller can pass its name through <updater> to get it integrated in the response message
3359 * returned by the function.
3360 *
3361 * The function first does the following, in that order:
3362 * - validates the new addr and/or port
3363 * - checks if an update is required (new IP or port is different than current ones)
3364 * - checks the update is allowed:
3365 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3366 * - allow all changes if no CHECKS are configured
3367 * - if CHECK is configured:
3368 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3369 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3370 * conditions are met
3371 */
3372const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3373{
3374 struct sockaddr_storage sa;
3375 int ret, port_change_required;
3376 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003377 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003378 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003379 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003380
3381 msg = get_trash_chunk();
3382 chunk_reset(msg);
3383
3384 if (addr) {
3385 memset(&sa, 0, sizeof(struct sockaddr_storage));
3386 if (str2ip2(addr, &sa, 0) == NULL) {
3387 chunk_printf(msg, "Invalid addr '%s'", addr);
3388 goto out;
3389 }
3390
3391 /* changes are allowed on AF_INET* families only */
3392 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3393 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3394 goto out;
3395 }
3396
3397 /* collecting data currently setup */
3398 memset(current_addr, '\0', sizeof(current_addr));
3399 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3400 /* changes are allowed on AF_INET* families only */
3401 if ((ret != AF_INET) && (ret != AF_INET6)) {
3402 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3403 goto out;
3404 }
3405
3406 /* applying ADDR changes if required and allowed
3407 * ipcmp returns 0 when both ADDR are the same
3408 */
3409 if (ipcmp(&s->addr, &sa) == 0) {
3410 chunk_appendf(msg, "no need to change the addr");
3411 goto port;
3412 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003413 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003414 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003415
3416 /* we also need to update check's ADDR only if it uses the server's one */
3417 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003418 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003419 }
3420
3421 /* we also need to update agent ADDR only if it use the server's one */
3422 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003423 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003424 }
3425
3426 /* update report for caller */
3427 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3428 }
3429
3430 port:
3431 if (port) {
3432 char sign = '\0';
3433 char *endptr;
3434
3435 if (addr)
3436 chunk_appendf(msg, ", ");
3437
3438 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003439 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003440
3441 /* check if PORT change is required */
3442 port_change_required = 0;
3443
3444 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003445 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003446 new_port = strtol(port, &endptr, 10);
3447 if ((errno != 0) || (port == endptr)) {
3448 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3449 goto out;
3450 }
3451
3452 /* check if caller triggers a port mapped or offset */
3453 if (sign == '-' || (sign == '+')) {
3454 /* check if server currently uses port map */
3455 if (!(s->flags & SRV_F_MAPPORTS)) {
3456 /* switch from fixed port to port map mandatorily triggers
3457 * a port change */
3458 port_change_required = 1;
3459 /* check is configured
3460 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3461 * prevent PORT change if check doesn't have it's dedicated port while switching
3462 * to port mapping */
3463 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3464 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.");
3465 goto out;
3466 }
3467 }
3468 /* we're already using port maps */
3469 else {
3470 port_change_required = current_port != new_port;
3471 }
3472 }
3473 /* fixed port */
3474 else {
3475 port_change_required = current_port != new_port;
3476 }
3477
3478 /* applying PORT changes if required and update response message */
3479 if (port_change_required) {
3480 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003481 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003482 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003483
3484 /* prepare message */
3485 chunk_appendf(msg, "port changed from '");
3486 if (s->flags & SRV_F_MAPPORTS)
3487 chunk_appendf(msg, "+");
3488 chunk_appendf(msg, "%d' to '", current_port);
3489
3490 if (sign == '-') {
3491 s->flags |= SRV_F_MAPPORTS;
3492 chunk_appendf(msg, "%c", sign);
3493 /* just use for result output */
3494 new_port = -new_port;
3495 }
3496 else if (sign == '+') {
3497 s->flags |= SRV_F_MAPPORTS;
3498 chunk_appendf(msg, "%c", sign);
3499 }
3500 else {
3501 s->flags &= ~SRV_F_MAPPORTS;
3502 }
3503
3504 chunk_appendf(msg, "%d'", new_port);
3505
3506 /* we also need to update health checks port only if it uses server's realport */
3507 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3508 s->check.port = new_port;
3509 }
3510 }
3511 else {
3512 chunk_appendf(msg, "no need to change the port");
3513 }
3514 }
3515
3516out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003517 if (changed)
3518 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003519 if (updater)
3520 chunk_appendf(msg, " by '%s'", updater);
3521 chunk_appendf(msg, "\n");
3522 return msg->str;
3523}
3524
3525
3526/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003527 * update server status based on result of name resolution
3528 * returns:
3529 * 0 if server status is updated
3530 * 1 if server status has not changed
3531 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003532int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003533{
3534 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003535 struct dns_resolvers *resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003536
3537 switch (resolution->status) {
3538 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003539 /* status when HAProxy has just (re)started.
3540 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003541 break;
3542
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003543 case RSLV_STATUS_VALID:
3544 /*
3545 * resume health checks
3546 * server will be turned back on if health check is safe
3547 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003548 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003549 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003550 return 1;
3551 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3552 "No IP for server ");
3553 return (0);
3554 }
Emeric Brun52a91d32017-08-31 14:41:55 +02003555 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003556 return 1;
3557 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3558 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3559 s->proxy->id, s->id);
3560
3561 Warning("%s.\n", trash.str);
3562 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3563 return 0;
3564
3565 case RSLV_STATUS_NX:
3566 /* stop server if resolution is NX for a long enough period */
3567 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003568 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003569 return 1;
3570 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3571 return 0;
3572 }
3573 break;
3574
3575 case RSLV_STATUS_TIMEOUT:
3576 /* stop server if resolution is TIMEOUT for a long enough period */
3577 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003578 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003579 return 1;
3580 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3581 return 0;
3582 }
3583 break;
3584
3585 case RSLV_STATUS_REFUSED:
3586 /* stop server if resolution is REFUSED for a long enough period */
3587 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003588 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003589 return 1;
3590 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3591 return 0;
3592 }
3593 break;
3594
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003595 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003596 /* stop server if resolution is in unmatched error for a long enough period */
3597 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003598 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003599 return 1;
3600 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3601 return 0;
3602 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003603 break;
3604 }
3605
3606 return 1;
3607}
3608
3609/*
3610 * Server Name Resolution valid response callback
3611 * It expects:
3612 * - <nameserver>: the name server which answered the valid response
3613 * - <response>: buffer containing a valid DNS response
3614 * - <response_len>: size of <response>
3615 * It performs the following actions:
3616 * - ignore response if current ip found and server family not met
3617 * - update with first new ip found if family is met and current IP is not found
3618 * returns:
3619 * 0 on error
3620 * 1 when no error or safe ignore
3621 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003622int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003623{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003624 struct server *s = NULL;
3625 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003626 void *serverip, *firstip;
3627 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003628 int ret;
3629 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003630 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003631
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003632 s = objt_server(requester->requester);
3633 if (!s)
3634 return 1;
3635
3636 resolution = s->resolution;
3637
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003638 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003639 firstip = NULL; /* pointer to the first valid response found */
3640 /* it will be used as the new IP if a change is required */
3641 firstip_sin_family = AF_UNSPEC;
3642 serverip = NULL; /* current server IP address */
3643
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003644 /* initializing server IP pointer */
3645 server_sin_family = s->addr.ss_family;
3646 switch (server_sin_family) {
3647 case AF_INET:
3648 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3649 break;
3650
3651 case AF_INET6:
3652 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3653 break;
3654
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003655 case AF_UNSPEC:
3656 break;
3657
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003658 default:
3659 goto invalid;
3660 }
3661
Baptiste Assmann729c9012017-05-22 15:13:10 +02003662 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003663 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003664 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003665
3666 switch (ret) {
3667 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003668 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003669
3670 case DNS_UPD_SRVIP_NOT_FOUND:
3671 goto save_ip;
3672
3673 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003674 goto invalid;
3675
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003676 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003677 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003678 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003679
Baptiste Assmannfad03182015-10-28 02:03:32 +01003680 case DNS_UPD_NAME_ERROR:
3681 /* if this is not the last expected response, we ignore it */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003682 if (nameserver && (resolution->nb_responses < nameserver->resolvers->count_nameservers))
Baptiste Assmannfad03182015-10-28 02:03:32 +01003683 return 0;
3684 /* update resolution status to OTHER error type */
3685 if (resolution->status != RSLV_STATUS_OTHER) {
3686 resolution->status = RSLV_STATUS_OTHER;
3687 resolution->last_status_change = now_ms;
3688 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003689 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003690
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003691 default:
3692 goto invalid;
3693
3694 }
3695
3696 save_ip:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003697 if (nameserver)
3698 nameserver->counters.update += 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003699
3700 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003701 if (nameserver)
3702 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3703 else
3704 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003705 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3706
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003707 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003708 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003709 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003710
3711 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003712 if (nameserver) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003713 nameserver->counters.invalid += 1;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003714 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3715 goto update_status;
3716 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003717
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003718 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003719 return 0;
3720}
3721
3722/*
3723 * Server Name Resolution error management callback
3724 * returns:
3725 * 0 on error
3726 * 1 when no error or safe ignore
3727 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003728int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003729{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003730 struct server *s = NULL;
3731 struct dns_resolution *resolution = NULL;
3732 struct dns_resolvers *resolvers = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003733
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003734 s = objt_server(requester->requester);
3735 if (!s)
3736 return 1;
3737
3738 resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003739 resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003740
3741 /* can be ignored if this is not the last response */
3742 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3743 return 1;
3744 }
3745
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003746 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003747 return 1;
3748}
3749
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003750/*
3751 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003752 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003753 * It returns a pointer to the first server found or NULL if <ip> is not yet
3754 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003755 */
3756struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3757{
3758 struct server *tmpsrv;
3759 struct proxy *be;
3760
3761 if (!srv)
3762 return NULL;
3763
3764 be = srv->proxy;
3765 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3766 /* We want to compare the IP in the record with the IP of the servers in the
3767 * same backend, only if:
3768 * * DNS resolution is enabled on the server
3769 * * the hostname used for the resolution by our server is the same than the
3770 * one used for the server found in the backend
3771 * * the server found in the backend is not our current server
3772 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003773 if ((tmpsrv->hostname_dn == NULL) ||
3774 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3775 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003776 (srv->puid == tmpsrv->puid))
3777 continue;
3778
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003779 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003780 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003781 continue;
3782
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003783 /* At this point, we have 2 different servers using the same DNS hostname
3784 * for their respective resolution.
3785 */
3786 if (*ip_family == tmpsrv->addr.ss_family &&
3787 ((tmpsrv->addr.ss_family == AF_INET &&
3788 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3789 (tmpsrv->addr.ss_family == AF_INET6 &&
3790 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3791 return tmpsrv;
3792 }
3793 }
3794
3795 return NULL;
3796}
3797
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003798/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3799 * resolver. This is suited for initial address configuration. Returns 0 on
3800 * success otherwise a non-zero error code. In case of error, *err_code, if
3801 * not NULL, is filled up.
3802 */
3803int srv_set_addr_via_libc(struct server *srv, int *err_code)
3804{
3805 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003806 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003807 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003808 return 1;
3809 }
3810 return 0;
3811}
3812
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003813/* Set the server's FDQN (->hostname) from <hostname>.
3814 * Returns -1 if failed, 0 if not.
3815 */
3816int srv_set_fqdn(struct server *srv, const char *hostname)
3817{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003818 struct dns_resolution *resolution;
3819 int hostname_dn_len;
Olivier Houchard8da5f982017-08-04 18:35:36 +02003820 int did_set_reso = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003821
3822 /* run time DNS resolution was not active for this server
3823 * and we can't enable it at run time for now.
3824 */
3825 if (!srv->dns_requester)
3826 return -1;
3827
3828 chunk_reset(&trash);
3829
3830 /* check if hostname is really a hostname and if we have enough
3831 * room to save it in its domain name format
3832 */
3833 hostname_dn_len = dns_str_to_dn_label_len(hostname);
3834 if (hostname_dn_len == -1 || hostname_dn_len + 1 > trash.size)
3835 return -1;
3836
3837 if (!dns_str_to_dn_label(hostname,
3838 trash.str,
3839 hostname_dn_len + 1))
3840 return -1;
3841
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003842
Olivier Houchard8da5f982017-08-04 18:35:36 +02003843 if (srv->resolution->hostname_dn) {
3844 /* get a resolution from the curr or wait queues, or a brand new one from the pool */
3845 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
3846 if (!resolution)
3847 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003848
Olivier Houchard8da5f982017-08-04 18:35:36 +02003849 /* in this case, the new hostanme is the same than the old one */
3850 if (srv->resolution == resolution && srv->hostname)
3851 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003852
Olivier Houchard8da5f982017-08-04 18:35:36 +02003853 /* first, we need to unlink our server from its current resolution */
3854 srv_free_from_resolution(srv);
3855 } else {
Baptiste Assmann6fb81922017-08-14 16:35:45 +02003856 /* this server's fqdn has been set by a SRV record */
3857 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
3858 srv_free_from_resolution(srv);
3859 srv->resolution = resolution;
3860 if (resolution->hostname_dn == NULL) {
3861 resolution->last_resolution = now_ms;
3862 did_set_reso = 1;
3863 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02003864 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003865
3866 /* now we update server's parameters */
3867 free(srv->hostname);
3868 free(srv->hostname_dn);
3869 srv->hostname = strdup(hostname);
3870 srv->hostname_dn = strdup(trash.str);
3871 srv->hostname_dn_len = hostname_dn_len;
3872 if (!srv->hostname || !srv->hostname_dn)
3873 return -1;
Olivier Houchard8da5f982017-08-04 18:35:36 +02003874 if (did_set_reso) {
3875 resolution->query_type = srv->dns_requester->prefered_query_type;
3876 resolution->hostname_dn = srv->hostname_dn;
3877 resolution->hostname_dn_len = hostname_dn_len;
3878 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003879
3880 /* then we can link srv to its new resolution */
3881 dns_link_resolution(srv, OBJ_TYPE_SERVER, resolution);
3882
3883 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003884}
3885
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003886/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3887 * from the state file. This is suited for initial address configuration.
3888 * Returns 0 on success otherwise a non-zero error code. In case of error,
3889 * *err_code, if not NULL, is filled up.
3890 */
3891static int srv_apply_lastaddr(struct server *srv, int *err_code)
3892{
3893 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3894 if (err_code)
3895 *err_code |= ERR_WARN;
3896 return 1;
3897 }
3898 return 0;
3899}
3900
Willy Tarreau25e51522016-11-04 15:10:17 +01003901/* returns 0 if no error, otherwise a combination of ERR_* flags */
3902static int srv_iterate_initaddr(struct server *srv)
3903{
3904 int return_code = 0;
3905 int err_code;
3906 unsigned int methods;
3907
3908 methods = srv->init_addr_methods;
3909 if (!methods) { // default to "last,libc"
3910 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3911 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3912 }
3913
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003914 /* "-dr" : always append "none" so that server addresses resolution
3915 * failures are silently ignored, this is convenient to validate some
3916 * configs out of their environment.
3917 */
3918 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3919 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3920
Willy Tarreau25e51522016-11-04 15:10:17 +01003921 while (methods) {
3922 err_code = 0;
3923 switch (srv_get_next_initaddr(&methods)) {
3924 case SRV_IADDR_LAST:
3925 if (!srv->lastaddr)
3926 continue;
3927 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003928 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003929 return_code |= err_code;
3930 break;
3931
3932 case SRV_IADDR_LIBC:
3933 if (!srv->hostname)
3934 continue;
3935 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003936 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003937 return_code |= err_code;
3938 break;
3939
Willy Tarreau37ebe122016-11-04 15:17:58 +01003940 case SRV_IADDR_NONE:
3941 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003942 if (return_code) {
3943 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3944 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3945 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003946 return return_code;
3947
Willy Tarreau4310d362016-11-02 15:05:56 +01003948 case SRV_IADDR_IP:
3949 ipcpy(&srv->init_addr, &srv->addr);
3950 if (return_code) {
3951 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3952 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3953 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003954 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003955
Willy Tarreau25e51522016-11-04 15:10:17 +01003956 default: /* unhandled method */
3957 break;
3958 }
3959 }
3960
3961 if (!return_code) {
3962 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3963 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3964 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003965 else {
3966 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3967 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3968 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003969
3970 return_code |= ERR_ALERT | ERR_FATAL;
3971 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003972out:
3973 srv_set_dyncookie(srv);
3974 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003975}
3976
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003977/*
3978 * This function parses all backends and all servers within each backend
3979 * and performs servers' addr resolution based on information provided by:
3980 * - configuration file
3981 * - server-state file (states provided by an 'old' haproxy process)
3982 *
3983 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3984 */
3985int srv_init_addr(void)
3986{
3987 struct proxy *curproxy;
3988 int return_code = 0;
3989
3990 curproxy = proxy;
3991 while (curproxy) {
3992 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003993
3994 /* servers are in backend only */
3995 if (!(curproxy->cap & PR_CAP_BE))
3996 goto srv_init_addr_next;
3997
Willy Tarreau25e51522016-11-04 15:10:17 +01003998 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003999 if (srv->hostname)
4000 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004001
4002 srv_init_addr_next:
4003 curproxy = curproxy->next;
4004 }
4005
4006 return return_code;
4007}
4008
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004009const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
4010{
4011
4012 struct chunk *msg;
4013
4014 msg = get_trash_chunk();
4015 chunk_reset(msg);
4016
Olivier Houchard8da5f982017-08-04 18:35:36 +02004017 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004018 chunk_appendf(msg, "no need to change the FDQN");
4019 goto out;
4020 }
4021
4022 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
4023 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
4024 goto out;
4025 }
4026
4027 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
4028 server->proxy->id, server->id, server->hostname, fqdn);
4029
4030 if (srv_set_fqdn(server, fqdn) < 0) {
4031 chunk_reset(msg);
4032 chunk_appendf(msg, "could not update %s/%s FQDN",
4033 server->proxy->id, server->id);
4034 goto out;
4035 }
4036
4037 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02004038 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004039
4040 out:
4041 if (updater)
4042 chunk_appendf(msg, " by '%s'", updater);
4043 chunk_appendf(msg, "\n");
4044
4045 return msg->str;
4046}
4047
4048
Willy Tarreau21b069d2016-11-23 17:15:08 +01004049/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
4050 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004051 * 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 +01004052 * used for CLI commands requiring a server name.
4053 * Important: the <arg> is modified to remove the '/'.
4054 */
4055struct server *cli_find_server(struct appctx *appctx, char *arg)
4056{
4057 struct proxy *px;
4058 struct server *sv;
4059 char *line;
4060
4061 /* split "backend/server" and make <line> point to server */
4062 for (line = arg; *line; line++)
4063 if (*line == '/') {
4064 *line++ = '\0';
4065 break;
4066 }
4067
4068 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004069 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004070 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004071 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004072 return NULL;
4073 }
4074
4075 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004076 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004077 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004078 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004079 return NULL;
4080 }
4081
4082 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004083 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004084 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004085 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004086 return NULL;
4087 }
4088
4089 return sv;
4090}
4091
William Lallemand222baf22016-11-19 02:00:33 +01004092
4093static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4094{
4095 struct server *sv;
4096 const char *warning;
4097
4098 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4099 return 1;
4100
4101 sv = cli_find_server(appctx, args[2]);
4102 if (!sv)
4103 return 1;
4104
4105 if (strcmp(args[3], "weight") == 0) {
4106 warning = server_parse_weight_change_request(sv, args[4]);
4107 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004108 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004109 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004110 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004111 }
4112 }
4113 else if (strcmp(args[3], "state") == 0) {
4114 if (strcmp(args[4], "ready") == 0)
4115 srv_adm_set_ready(sv);
4116 else if (strcmp(args[4], "drain") == 0)
4117 srv_adm_set_drain(sv);
4118 else if (strcmp(args[4], "maint") == 0)
4119 srv_adm_set_maint(sv);
4120 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004121 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004122 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004123 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004124 }
4125 }
4126 else if (strcmp(args[3], "health") == 0) {
4127 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004128 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004129 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004130 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004131 }
4132 else if (strcmp(args[4], "up") == 0) {
4133 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004134 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004135 }
4136 else if (strcmp(args[4], "stopping") == 0) {
4137 sv->check.health = sv->check.rise + sv->check.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004138 srv_set_stopping(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004139 }
4140 else if (strcmp(args[4], "down") == 0) {
4141 sv->check.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004142 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004143 }
4144 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004145 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004146 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004147 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004148 }
4149 }
4150 else if (strcmp(args[3], "agent") == 0) {
4151 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004152 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004153 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004154 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004155 }
4156 else if (strcmp(args[4], "up") == 0) {
4157 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
Emeric Brun5a133512017-10-19 14:42:30 +02004158 srv_set_running(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004159 }
4160 else if (strcmp(args[4], "down") == 0) {
4161 sv->agent.health = 0;
Emeric Brun5a133512017-10-19 14:42:30 +02004162 srv_set_stopped(sv, "changed from CLI", NULL);
William Lallemand222baf22016-11-19 02:00:33 +01004163 }
4164 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004165 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004166 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004167 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004168 }
4169 }
Misiek2da082d2017-01-09 09:40:42 +01004170 else if (strcmp(args[3], "agent-addr") == 0) {
4171 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004172 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004173 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4174 appctx->st0 = CLI_ST_PRINT;
4175 } else {
4176 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004177 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004178 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4179 appctx->st0 = CLI_ST_PRINT;
4180 }
4181 }
4182 }
4183 else if (strcmp(args[3], "agent-send") == 0) {
4184 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004185 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004186 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4187 appctx->st0 = CLI_ST_PRINT;
4188 } else {
4189 char *nss = strdup(args[4]);
4190 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004191 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004192 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4193 appctx->st0 = CLI_ST_PRINT;
4194 } else {
4195 free(sv->agent.send_string);
4196 sv->agent.send_string = nss;
4197 sv->agent.send_string_len = strlen(args[4]);
4198 }
4199 }
4200 }
William Lallemand222baf22016-11-19 02:00:33 +01004201 else if (strcmp(args[3], "check-port") == 0) {
4202 int i = 0;
4203 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004204 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004205 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004206 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004207 }
4208 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004209 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004210 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004211 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004212 }
4213 /* prevent the update of port to 0 if MAPPORTS are in use */
4214 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004215 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004216 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004217 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004218 return 1;
4219 }
4220 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004221 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004222 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004223 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004224 }
4225 else if (strcmp(args[3], "addr") == 0) {
4226 char *addr = NULL;
4227 char *port = NULL;
4228 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004229 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004230 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004231 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004232 return 1;
4233 }
4234 else {
4235 addr = args[4];
4236 }
4237 if (strcmp(args[5], "port") == 0) {
4238 port = args[6];
4239 }
4240 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4241 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004242 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004243 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004244 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004245 }
4246 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4247 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004248 else if (strcmp(args[3], "fqdn") == 0) {
4249 if (!*args[4]) {
4250 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4251 appctx->st0 = CLI_ST_PRINT;
4252 return 1;
4253 }
4254 warning = update_server_fqdn(sv, args[4], "stats socket command");
4255 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004256 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004257 appctx->ctx.cli.msg = warning;
4258 appctx->st0 = CLI_ST_PRINT;
4259 }
4260 }
William Lallemand222baf22016-11-19 02:00:33 +01004261 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004262 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004263 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 +01004264 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004265 }
4266 return 1;
4267}
4268
William Lallemand6b160942016-11-22 12:34:35 +01004269static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4270{
4271 struct stream_interface *si = appctx->owner;
4272 struct proxy *px;
4273 struct server *sv;
4274 char *line;
4275
4276
4277 /* split "backend/server" and make <line> point to server */
4278 for (line = args[2]; *line; line++)
4279 if (*line == '/') {
4280 *line++ = '\0';
4281 break;
4282 }
4283
4284 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004285 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004286 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004287 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004288 return 1;
4289 }
4290
4291 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004292 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004293 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004294 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004295 return 1;
4296 }
4297
4298 /* return server's effective weight at the moment */
4299 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004300 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004301 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004302 return 0;
4303 }
William Lallemand6b160942016-11-22 12:34:35 +01004304 return 1;
4305}
4306
4307static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4308{
4309 struct server *sv;
4310 const char *warning;
4311
4312 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4313 return 1;
4314
4315 sv = cli_find_server(appctx, args[2]);
4316 if (!sv)
4317 return 1;
4318
4319 warning = server_parse_weight_change_request(sv, args[3]);
4320 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004321 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004322 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004323 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004324 }
4325 return 1;
4326}
4327
Willy Tarreaub8026272016-11-23 11:26:56 +01004328/* parse a "set maxconn server" command. It always returns 1. */
4329static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4330{
4331 struct server *sv;
4332 const char *warning;
4333
4334 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4335 return 1;
4336
4337 sv = cli_find_server(appctx, args[3]);
4338 if (!sv)
4339 return 1;
4340
4341 warning = server_parse_maxconn_change_request(sv, args[4]);
4342 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004343 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004344 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004345 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004346 }
4347 return 1;
4348}
William Lallemand6b160942016-11-22 12:34:35 +01004349
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004350/* parse a "disable agent" command. It always returns 1. */
4351static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4352{
4353 struct server *sv;
4354
4355 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4356 return 1;
4357
4358 sv = cli_find_server(appctx, args[2]);
4359 if (!sv)
4360 return 1;
4361
4362 sv->agent.state &= ~CHK_ST_ENABLED;
4363 return 1;
4364}
4365
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004366/* parse a "disable health" command. It always returns 1. */
4367static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4368{
4369 struct server *sv;
4370
4371 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4372 return 1;
4373
4374 sv = cli_find_server(appctx, args[2]);
4375 if (!sv)
4376 return 1;
4377
4378 sv->check.state &= ~CHK_ST_ENABLED;
4379 return 1;
4380}
4381
Willy Tarreauffb4d582016-11-24 12:47:00 +01004382/* parse a "disable server" command. It always returns 1. */
4383static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4384{
4385 struct server *sv;
4386
4387 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4388 return 1;
4389
4390 sv = cli_find_server(appctx, args[2]);
4391 if (!sv)
4392 return 1;
4393
4394 srv_adm_set_maint(sv);
4395 return 1;
4396}
4397
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004398/* parse a "enable agent" command. It always returns 1. */
4399static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4400{
4401 struct server *sv;
4402
4403 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4404 return 1;
4405
4406 sv = cli_find_server(appctx, args[2]);
4407 if (!sv)
4408 return 1;
4409
4410 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004411 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004412 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004413 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004414 return 1;
4415 }
4416
4417 sv->agent.state |= CHK_ST_ENABLED;
4418 return 1;
4419}
4420
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004421/* parse a "enable health" command. It always returns 1. */
4422static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4423{
4424 struct server *sv;
4425
4426 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4427 return 1;
4428
4429 sv = cli_find_server(appctx, args[2]);
4430 if (!sv)
4431 return 1;
4432
4433 sv->check.state |= CHK_ST_ENABLED;
4434 return 1;
4435}
4436
Willy Tarreauffb4d582016-11-24 12:47:00 +01004437/* parse a "enable server" command. It always returns 1. */
4438static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4439{
4440 struct server *sv;
4441
4442 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4443 return 1;
4444
4445 sv = cli_find_server(appctx, args[2]);
4446 if (!sv)
4447 return 1;
4448
4449 srv_adm_set_ready(sv);
4450 return 1;
4451}
4452
William Lallemand222baf22016-11-19 02:00:33 +01004453/* register cli keywords */
4454static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004455 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004456 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004457 { { "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 +01004458 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004459 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004460 { { "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 +01004461 { { "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 +01004462 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004463 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4464 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4465
William Lallemand222baf22016-11-19 02:00:33 +01004466 {{},}
4467}};
4468
4469__attribute__((constructor))
4470static void __server_init(void)
4471{
4472 cli_register_kw(&cli_kws);
4473}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004474
Emeric Brun64cc49c2017-10-03 14:46:45 +02004475/*
4476 * This function applies server's status changes, it is
4477 * is designed to be called asynchronously.
4478 *
4479 */
4480void srv_update_status(struct server *s)
4481{
4482 struct check *check = &s->check;
4483 int xferred;
4484 struct proxy *px = s->proxy;
4485 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4486 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4487 int log_level;
4488 struct chunk *tmptrash = NULL;
4489
4490
4491 /* If currently main is not set we try to apply pending state changes */
4492 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4493 int next_admin;
4494
4495 /* Backup next admin */
4496 next_admin = s->next_admin;
4497
4498 /* restore current admin state */
4499 s->next_admin = s->cur_admin;
4500
4501 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4502 s->last_change = now.tv_sec;
4503 if (s->proxy->lbprm.set_server_status_down)
4504 s->proxy->lbprm.set_server_status_down(s);
4505
4506 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4507 srv_shutdown_streams(s, SF_ERR_DOWN);
4508
4509 /* we might have streams queued on this server and waiting for
4510 * a connection. Those which are redispatchable will be queued
4511 * to another server or to the proxy itself.
4512 */
4513 xferred = pendconn_redistribute(s);
4514
4515 tmptrash = alloc_trash_chunk();
4516 if (tmptrash) {
4517 chunk_printf(tmptrash,
4518 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4519 s->proxy->id, s->id);
4520
Emeric Brun5a133512017-10-19 14:42:30 +02004521 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004522 Warning("%s.\n", tmptrash->str);
4523
4524 /* we don't send an alert if the server was previously paused */
4525 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4526 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4527 send_email_alert(s, log_level, "%s", tmptrash->str);
4528 free_trash_chunk(tmptrash);
4529 tmptrash = NULL;
4530 }
4531 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4532 set_backend_down(s->proxy);
4533
4534 s->counters.down_trans++;
4535 }
4536 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4537 s->last_change = now.tv_sec;
4538 if (s->proxy->lbprm.set_server_status_down)
4539 s->proxy->lbprm.set_server_status_down(s);
4540
4541 /* we might have streams queued on this server and waiting for
4542 * a connection. Those which are redispatchable will be queued
4543 * to another server or to the proxy itself.
4544 */
4545 xferred = pendconn_redistribute(s);
4546
4547 tmptrash = alloc_trash_chunk();
4548 if (tmptrash) {
4549 chunk_printf(tmptrash,
4550 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4551 s->proxy->id, s->id);
4552
Emeric Brun5a133512017-10-19 14:42:30 +02004553 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004554
4555 Warning("%s.\n", tmptrash->str);
4556 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4557 free_trash_chunk(tmptrash);
4558 tmptrash = NULL;
4559 }
4560
4561 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4562 set_backend_down(s->proxy);
4563 }
4564 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4565 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4566 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4567 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4568 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4569 s->proxy->last_change = now.tv_sec;
4570 }
4571
4572 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4573 s->down_time += now.tv_sec - s->last_change;
4574
4575 s->last_change = now.tv_sec;
4576 if (s->next_state == SRV_ST_STARTING)
4577 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4578
4579 server_recalc_eweight(s);
4580 /* now propagate the status change to any LB algorithms */
4581 if (px->lbprm.update_server_eweight)
4582 px->lbprm.update_server_eweight(s);
4583 else if (srv_willbe_usable(s)) {
4584 if (px->lbprm.set_server_status_up)
4585 px->lbprm.set_server_status_up(s);
4586 }
4587 else {
4588 if (px->lbprm.set_server_status_down)
4589 px->lbprm.set_server_status_down(s);
4590 }
4591
4592 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4593 * and it's not a backup server and its effective weight is > 0,
4594 * then it can accept new connections, so we shut down all streams
4595 * on all backup servers.
4596 */
4597 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4598 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4599 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4600
4601 /* check if we can handle some connections queued at the proxy. We
4602 * will take as many as we can handle.
4603 */
4604 xferred = pendconn_grab_from_px(s);
4605
4606 tmptrash = alloc_trash_chunk();
4607 if (tmptrash) {
4608 chunk_printf(tmptrash,
4609 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4610 s->proxy->id, s->id);
4611
Emeric Brun5a133512017-10-19 14:42:30 +02004612 srv_append_status(tmptrash, s, NULL, xferred, 0);
Emeric Brun64cc49c2017-10-03 14:46:45 +02004613 Warning("%s.\n", tmptrash->str);
4614 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4615 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4616 free_trash_chunk(tmptrash);
4617 tmptrash = NULL;
4618 }
4619
4620 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4621 set_backend_down(s->proxy);
4622 }
4623 else if (s->cur_eweight != s->next_eweight) {
4624 /* now propagate the status change to any LB algorithms */
4625 if (px->lbprm.update_server_eweight)
4626 px->lbprm.update_server_eweight(s);
4627 else if (srv_willbe_usable(s)) {
4628 if (px->lbprm.set_server_status_up)
4629 px->lbprm.set_server_status_up(s);
4630 }
4631 else {
4632 if (px->lbprm.set_server_status_down)
4633 px->lbprm.set_server_status_down(s);
4634 }
4635
4636 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4637 set_backend_down(s->proxy);
4638 }
4639
4640 s->next_admin = next_admin;
4641 }
4642
Emeric Brun5a133512017-10-19 14:42:30 +02004643 /* reset operational state change */
4644 *s->op_st_chg.reason = 0;
4645 s->op_st_chg.status = s->op_st_chg.code = -1;
4646 s->op_st_chg.duration = 0;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004647
4648 /* Now we try to apply pending admin changes */
4649
4650 /* Maintenance must also disable health checks */
4651 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4652 if (s->check.state & CHK_ST_ENABLED) {
4653 s->check.state |= CHK_ST_PAUSED;
4654 check->health = 0;
4655 }
4656
4657 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
Olivier Houchard796a2b32017-10-24 17:42:47 +02004658 tmptrash = alloc_trash_chunk();
4659 if (tmptrash) {
4660 chunk_printf(tmptrash,
4661 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4662 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4663 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
Emeric Brun64cc49c2017-10-03 14:46:45 +02004664
Olivier Houchard796a2b32017-10-24 17:42:47 +02004665 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
Emeric Brun64cc49c2017-10-03 14:46:45 +02004666
Olivier Houchard796a2b32017-10-24 17:42:47 +02004667 if (!(global.mode & MODE_STARTING)) {
4668 Warning("%s.\n", tmptrash->str);
4669 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4670 }
4671 free_trash_chunk(tmptrash);
4672 tmptrash = NULL;
Emeric Brun64cc49c2017-10-03 14:46:45 +02004673 }
4674 }
4675 else { /* server was still running */
4676 check->health = 0; /* failure */
4677 s->last_change = now.tv_sec;
4678 if (s->proxy->lbprm.set_server_status_down)
4679 s->proxy->lbprm.set_server_status_down(s);
4680
4681 s->next_state = SRV_ST_STOPPED;
4682 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4683 srv_shutdown_streams(s, SF_ERR_DOWN);
4684
4685 /* we might have streams queued on this server and waiting for
4686 * a connection. Those which are redispatchable will be queued
4687 * to another server or to the proxy itself.
4688 */
4689 xferred = pendconn_redistribute(s);
4690
4691 tmptrash = alloc_trash_chunk();
4692 if (tmptrash) {
4693 chunk_printf(tmptrash,
4694 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4695 s->flags & SRV_F_BACKUP ? "Backup " : "",
4696 s->proxy->id, s->id,
4697 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4698
4699 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4700
4701 if (!(global.mode & MODE_STARTING)) {
4702 Warning("%s.\n", tmptrash->str);
4703 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4704 }
4705 free_trash_chunk(tmptrash);
4706 tmptrash = NULL;
4707 }
4708 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4709 set_backend_down(s->proxy);
4710
4711 s->counters.down_trans++;
4712 }
4713 }
4714 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4715 /* OK here we're leaving maintenance, we have many things to check,
4716 * because the server might possibly be coming back up depending on
4717 * its state. In practice, leaving maintenance means that we should
4718 * immediately turn to UP (more or less the slowstart) under the
4719 * following conditions :
4720 * - server is neither checked nor tracked
4721 * - server tracks another server which is not checked
4722 * - server tracks another server which is already up
4723 * Which sums up as something simpler :
4724 * "either the tracking server is up or the server's checks are disabled
4725 * or up". Otherwise we only re-enable health checks. There's a special
4726 * case associated to the stopping state which can be inherited. Note
4727 * that the server might still be in drain mode, which is naturally dealt
4728 * with by the lower level functions.
4729 */
4730
4731 if (s->check.state & CHK_ST_ENABLED) {
4732 s->check.state &= ~CHK_ST_PAUSED;
4733 check->health = check->rise; /* start OK but check immediately */
4734 }
4735
4736 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4737 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4738 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4739 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4740 s->next_state = SRV_ST_STOPPING;
4741 }
4742 else {
4743 s->next_state = SRV_ST_STARTING;
4744 if (s->slowstart > 0)
4745 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4746 else
4747 s->next_state = SRV_ST_RUNNING;
4748 }
4749
4750 }
4751
4752 tmptrash = alloc_trash_chunk();
4753 if (tmptrash) {
4754 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4755 chunk_printf(tmptrash,
4756 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4757 s->flags & SRV_F_BACKUP ? "Backup " : "",
4758 s->proxy->id, s->id,
4759 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4760 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4761 }
4762 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4763 chunk_printf(tmptrash,
4764 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4765 s->flags & SRV_F_BACKUP ? "Backup " : "",
4766 s->proxy->id, s->id, s->hostname,
4767 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4768 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4769 }
4770 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4771 chunk_printf(tmptrash,
4772 "%sServer %s/%s is %s/%s (leaving maintenance)",
4773 s->flags & SRV_F_BACKUP ? "Backup " : "",
4774 s->proxy->id, s->id,
4775 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4776 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4777 }
4778 Warning("%s.\n", tmptrash->str);
4779 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4780 free_trash_chunk(tmptrash);
4781 tmptrash = NULL;
4782 }
4783
4784 server_recalc_eweight(s);
4785 /* now propagate the status change to any LB algorithms */
4786 if (px->lbprm.update_server_eweight)
4787 px->lbprm.update_server_eweight(s);
4788 else if (srv_willbe_usable(s)) {
4789 if (px->lbprm.set_server_status_up)
4790 px->lbprm.set_server_status_up(s);
4791 }
4792 else {
4793 if (px->lbprm.set_server_status_down)
4794 px->lbprm.set_server_status_down(s);
4795 }
4796
4797 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4798 set_backend_down(s->proxy);
4799
4800 }
4801 else if (s->next_admin & SRV_ADMF_MAINT) {
4802 /* remaining in maintenance mode, let's inform precisely about the
4803 * situation.
4804 */
4805 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4806 tmptrash = alloc_trash_chunk();
4807 if (tmptrash) {
4808 chunk_printf(tmptrash,
4809 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4810 s->flags & SRV_F_BACKUP ? "Backup " : "",
4811 s->proxy->id, s->id);
4812
4813 if (s->track) /* normally it's mandatory here */
4814 chunk_appendf(tmptrash, " via %s/%s",
4815 s->track->proxy->id, s->track->id);
4816 Warning("%s.\n", tmptrash->str);
4817 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4818 free_trash_chunk(tmptrash);
4819 tmptrash = NULL;
4820 }
4821 }
4822 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4823 tmptrash = alloc_trash_chunk();
4824 if (tmptrash) {
4825 chunk_printf(tmptrash,
4826 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4827 s->flags & SRV_F_BACKUP ? "Backup " : "",
4828 s->proxy->id, s->id, s->hostname);
4829
4830 if (s->track) /* normally it's mandatory here */
4831 chunk_appendf(tmptrash, " via %s/%s",
4832 s->track->proxy->id, s->track->id);
4833 Warning("%s.\n", tmptrash->str);
4834 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4835 free_trash_chunk(tmptrash);
4836 tmptrash = NULL;
4837 }
4838 }
4839 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4840 tmptrash = alloc_trash_chunk();
4841 if (tmptrash) {
4842 chunk_printf(tmptrash,
4843 "%sServer %s/%s remains in forced maintenance",
4844 s->flags & SRV_F_BACKUP ? "Backup " : "",
4845 s->proxy->id, s->id);
4846 Warning("%s.\n", tmptrash->str);
4847 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4848 free_trash_chunk(tmptrash);
4849 tmptrash = NULL;
4850 }
4851 }
4852 /* don't report anything when leaving drain mode and remaining in maintenance */
4853
4854 s->cur_admin = s->next_admin;
4855 }
4856
4857 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4858 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4859 /* drain state is applied only if not yet in maint */
4860
4861 s->last_change = now.tv_sec;
4862 if (px->lbprm.set_server_status_down)
4863 px->lbprm.set_server_status_down(s);
4864
4865 /* we might have streams queued on this server and waiting for
4866 * a connection. Those which are redispatchable will be queued
4867 * to another server or to the proxy itself.
4868 */
4869 xferred = pendconn_redistribute(s);
4870
4871 tmptrash = alloc_trash_chunk();
4872 if (tmptrash) {
4873 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4874 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4875 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4876
4877 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4878
4879 if (!(global.mode & MODE_STARTING)) {
4880 Warning("%s.\n", tmptrash->str);
4881 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4882 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4883 }
4884 free_trash_chunk(tmptrash);
4885 tmptrash = NULL;
4886 }
4887
4888 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4889 set_backend_down(s->proxy);
4890 }
4891 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4892 /* OK completely leaving drain mode */
4893 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4894 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4895 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4896 s->proxy->last_change = now.tv_sec;
4897 }
4898
4899 if (s->last_change < now.tv_sec) // ignore negative times
4900 s->down_time += now.tv_sec - s->last_change;
4901 s->last_change = now.tv_sec;
4902 server_recalc_eweight(s);
4903
4904 tmptrash = alloc_trash_chunk();
4905 if (tmptrash) {
4906 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4907 chunk_printf(tmptrash,
4908 "%sServer %s/%s is %s (leaving forced drain)",
4909 s->flags & SRV_F_BACKUP ? "Backup " : "",
4910 s->proxy->id, s->id,
4911 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4912 }
4913 else {
4914 chunk_printf(tmptrash,
4915 "%sServer %s/%s is %s (leaving drain)",
4916 s->flags & SRV_F_BACKUP ? "Backup " : "",
4917 s->proxy->id, s->id,
4918 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4919 if (s->track) /* normally it's mandatory here */
4920 chunk_appendf(tmptrash, " via %s/%s",
4921 s->track->proxy->id, s->track->id);
4922 }
4923
4924 Warning("%s.\n", tmptrash->str);
4925 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4926 free_trash_chunk(tmptrash);
4927 tmptrash = NULL;
4928 }
4929
4930 /* now propagate the status change to any LB algorithms */
4931 if (px->lbprm.update_server_eweight)
4932 px->lbprm.update_server_eweight(s);
4933 else if (srv_willbe_usable(s)) {
4934 if (px->lbprm.set_server_status_up)
4935 px->lbprm.set_server_status_up(s);
4936 }
4937 else {
4938 if (px->lbprm.set_server_status_down)
4939 px->lbprm.set_server_status_down(s);
4940 }
4941 }
4942 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4943 /* remaining in drain mode after removing one of its flags */
4944
4945 tmptrash = alloc_trash_chunk();
4946 if (tmptrash) {
4947 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4948 chunk_printf(tmptrash,
4949 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4950 s->flags & SRV_F_BACKUP ? "Backup " : "",
4951 s->proxy->id, s->id);
4952
4953 if (s->track) /* normally it's mandatory here */
4954 chunk_appendf(tmptrash, " via %s/%s",
4955 s->track->proxy->id, s->track->id);
4956 }
4957 else {
4958 chunk_printf(tmptrash,
4959 "%sServer %s/%s remains in forced drain mode",
4960 s->flags & SRV_F_BACKUP ? "Backup " : "",
4961 s->proxy->id, s->id);
4962 }
4963 Warning("%s.\n", tmptrash->str);
4964 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4965 free_trash_chunk(tmptrash);
4966 tmptrash = NULL;
4967 }
4968
4969 /* commit new admin status */
4970
4971 s->cur_admin = s->next_admin;
4972 }
4973 }
4974
4975 /* Re-set log strings to empty */
Emeric Brun64cc49c2017-10-03 14:46:45 +02004976 *s->adm_st_chg_cause = 0;
4977}
4978/*
4979 * This function loops on servers registered for asynchronous
4980 * status changes
4981 */
4982void servers_update_status(void) {
4983 struct server *s, *stmp;
4984
4985 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4986 srv_update_status(s);
4987 LIST_DEL(&s->update_status);
4988 LIST_INIT(&s->update_status);
4989 }
4990}
4991
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004992/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004993 * Local variables:
4994 * c-indent-level: 8
4995 * c-basic-offset: 8
4996 * End:
4997 */