blob: b937c1972f6a50637c6c36729e63c06319598310 [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.
785 * If <reason> is non-null, the entire string will be appended after a comma and
786 * a space (eg: to report some information from the check that changed the state).
Willy Tarreau87b09662015-04-03 00:22:06 +0200787 * If <xferred> is non-negative, some information about requeued streams are
Willy Tarreaubda92272014-05-20 21:55:30 +0200788 * provided.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200789 */
Willy Tarreaubda92272014-05-20 21:55:30 +0200790void srv_append_status(struct chunk *msg, struct server *s, const char *reason, int xferred, int forced)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200791{
Willy Tarreaubda92272014-05-20 21:55:30 +0200792 if (reason)
793 chunk_appendf(msg, ", %s", reason);
794 else if (!forced && s->track)
795 chunk_appendf(msg, " via %s/%s", s->track->proxy->id, s->track->id);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200796
797 if (xferred >= 0) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200798 if (s->next_state == SRV_ST_STOPPED)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200799 chunk_appendf(msg, ". %d active and %d backup servers left.%s"
800 " %d sessions active, %d requeued, %d remaining in queue",
801 s->proxy->srv_act, s->proxy->srv_bck,
802 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
803 s->cur_sess, xferred, s->nbpend);
804 else
805 chunk_appendf(msg, ". %d active and %d backup servers online.%s"
806 " %d sessions requeued, %d total in queue",
807 s->proxy->srv_act, s->proxy->srv_bck,
808 (s->proxy->srv_bck && !s->proxy->srv_act) ? " Running on backup." : "",
809 xferred, s->nbpend);
810 }
811}
812
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200813/* Marks server <s> down, regardless of its checks' statuses, notifies by all
814 * available means, recounts the remaining servers on the proxy and transfers
Willy Tarreau87b09662015-04-03 00:22:06 +0200815 * queued streams whenever possible to other servers. It automatically
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200816 * recomputes the number of servers, but not the map. Maintenance servers are
817 * ignored. It reports <reason> if non-null as the reason for going down. Note
818 * that it makes use of the trash to build the log strings, so <reason> must
819 * not be placed there.
820 */
821void srv_set_stopped(struct server *s, const char *reason)
822{
823 struct server *srv;
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200824
Emeric Brun64cc49c2017-10-03 14:46:45 +0200825 if ((s->cur_admin & SRV_ADMF_MAINT) || s->next_state == SRV_ST_STOPPED)
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200826 return;
827
Emeric Brun52a91d32017-08-31 14:41:55 +0200828 s->next_state = SRV_ST_STOPPED;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200829 if (reason)
830 strlcpy2(s->op_st_chg_reason, reason, sizeof(s->op_st_chg_reason));
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200831
Emeric Brun64cc49c2017-10-03 14:46:45 +0200832 /* Register changes to be applied asynchronously */
833 if (LIST_ISEMPTY(&s->update_status))
834 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaue7d1ef12014-05-20 22:25:12 +0200835
836 for (srv = s->trackers; srv; srv = srv->tracknext)
837 srv_set_stopped(srv, NULL);
838}
839
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200840/* Marks server <s> up regardless of its checks' statuses and provided it isn't
841 * in maintenance. Notifies by all available means, recounts the remaining
842 * servers on the proxy and tries to grab requests from the proxy. It
843 * automatically recomputes the number of servers, but not the map. Maintenance
844 * servers are ignored. It reports <reason> if non-null as the reason for going
845 * up. Note that it makes use of the trash to build the log strings, so <reason>
846 * must not be placed there.
847 */
848void srv_set_running(struct server *s, const char *reason)
849{
850 struct server *srv;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200851
Emeric Brun64cc49c2017-10-03 14:46:45 +0200852 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200853 return;
854
Emeric Brun52a91d32017-08-31 14:41:55 +0200855 if (s->next_state == SRV_ST_STARTING || s->next_state == SRV_ST_RUNNING)
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200856 return;
857
Emeric Brun52a91d32017-08-31 14:41:55 +0200858 s->next_state = SRV_ST_STARTING;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200859 if (reason)
860 strlcpy2(s->op_st_chg_reason, reason, sizeof(s->op_st_chg_reason));
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200861
Emeric Brun64cc49c2017-10-03 14:46:45 +0200862 if (s->slowstart <= 0)
863 s->next_state = SRV_ST_RUNNING;
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200864
Emeric Brun64cc49c2017-10-03 14:46:45 +0200865 /* Register changes to be applied asynchronously */
866 if (LIST_ISEMPTY(&s->update_status))
867 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200868
869 for (srv = s->trackers; srv; srv = srv->tracknext)
870 srv_set_running(srv, NULL);
871}
872
Willy Tarreau8eb77842014-05-21 13:54:57 +0200873/* Marks server <s> stopping regardless of its checks' statuses and provided it
874 * isn't in maintenance. Notifies by all available means, recounts the remaining
875 * servers on the proxy and tries to grab requests from the proxy. It
876 * automatically recomputes the number of servers, but not the map. Maintenance
877 * servers are ignored. It reports <reason> if non-null as the reason for going
878 * up. Note that it makes use of the trash to build the log strings, so <reason>
879 * must not be placed there.
880 */
881void srv_set_stopping(struct server *s, const char *reason)
882{
883 struct server *srv;
Willy Tarreau8eb77842014-05-21 13:54:57 +0200884
Emeric Brun64cc49c2017-10-03 14:46:45 +0200885 if (s->cur_admin & SRV_ADMF_MAINT)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200886 return;
887
Emeric Brun52a91d32017-08-31 14:41:55 +0200888 if (s->next_state == SRV_ST_STOPPING)
Willy Tarreau8eb77842014-05-21 13:54:57 +0200889 return;
890
Emeric Brun52a91d32017-08-31 14:41:55 +0200891 s->next_state = SRV_ST_STOPPING;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200892 if (reason)
893 strlcpy2(s->op_st_chg_reason, reason, sizeof(s->op_st_chg_reason));
Willy Tarreau8eb77842014-05-21 13:54:57 +0200894
Emeric Brun64cc49c2017-10-03 14:46:45 +0200895 /* Register changes to be applied asynchronously */
896 if (LIST_ISEMPTY(&s->update_status))
897 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreau8eb77842014-05-21 13:54:57 +0200898
899 for (srv = s->trackers; srv; srv = srv->tracknext)
900 srv_set_stopping(srv, NULL);
901}
Willy Tarreaudbd5e782014-05-20 22:46:35 +0200902
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200903/* Enables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
904 * enforce either maint mode or drain mode. It is not allowed to set more than
905 * one flag at once. The equivalent "inherited" flag is propagated to all
906 * tracking servers. Maintenance mode disables health checks (but not agent
907 * checks). When either the flag is already set or no flag is passed, nothing
Willy Tarreau8b428482016-11-07 15:53:43 +0100908 * is done. If <cause> is non-null, it will be displayed at the end of the log
909 * lines to justify the state change.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200910 */
Willy Tarreau8b428482016-11-07 15:53:43 +0100911void srv_set_admin_flag(struct server *s, enum srv_admin mode, const char *cause)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200912{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200913 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200914
915 if (!mode)
916 return;
917
918 /* stop going down as soon as we meet a server already in the same state */
Emeric Brun52a91d32017-08-31 14:41:55 +0200919 if (s->next_admin & mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200920 return;
921
Emeric Brun52a91d32017-08-31 14:41:55 +0200922 s->next_admin |= mode;
Emeric Brun64cc49c2017-10-03 14:46:45 +0200923 if (cause)
924 strlcpy2(s->adm_st_chg_cause, cause, sizeof(s->adm_st_chg_cause));
925
926 /* Register changes to be applied asynchronously */
927 if (LIST_ISEMPTY(&s->update_status))
928 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200929
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200930 /* stop going down if the equivalent flag was already present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200931 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & ~mode & SRV_ADMF_MAINT)) ||
932 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & ~mode & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200933 return;
934
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200935 /* compute the inherited flag to propagate */
936 if (mode & SRV_ADMF_MAINT)
937 mode = SRV_ADMF_IMAINT;
938 else if (mode & SRV_ADMF_DRAIN)
939 mode = SRV_ADMF_IDRAIN;
940
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200941 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreau8b428482016-11-07 15:53:43 +0100942 srv_set_admin_flag(srv, mode, cause);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200943}
944
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200945/* Disables admin flag <mode> (among SRV_ADMF_*) on server <s>. This is used to
946 * stop enforcing either maint mode or drain mode. It is not allowed to set more
947 * than one flag at once. The equivalent "inherited" flag is propagated to all
948 * tracking servers. Leaving maintenance mode re-enables health checks. When
949 * either the flag is already cleared or no flag is passed, nothing is done.
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200950 */
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200951void srv_clr_admin_flag(struct server *s, enum srv_admin mode)
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200952{
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200953 struct server *srv;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200954
955 if (!mode)
956 return;
957
958 /* stop going down as soon as we see the flag is not there anymore */
Emeric Brun52a91d32017-08-31 14:41:55 +0200959 if (!(s->next_admin & mode))
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200960 return;
961
Emeric Brun52a91d32017-08-31 14:41:55 +0200962 s->next_admin &= ~mode;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200963
Emeric Brun64cc49c2017-10-03 14:46:45 +0200964 /* Register changes to be applied asynchronously */
965 if (LIST_ISEMPTY(&s->update_status))
966 LIST_ADDQ(&updated_servers, &s->update_status);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200967
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200968 /* stop going down if the equivalent flag is still present (forced or inherited) */
Emeric Brun52a91d32017-08-31 14:41:55 +0200969 if (((mode & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) ||
970 ((mode & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)))
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200971 return;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200972
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200973 if (mode & SRV_ADMF_MAINT)
974 mode = SRV_ADMF_IMAINT;
975 else if (mode & SRV_ADMF_DRAIN)
976 mode = SRV_ADMF_IDRAIN;
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200977
978 for (srv = s->trackers; srv; srv = srv->tracknext)
Willy Tarreaubfc7b7a2014-05-22 16:14:34 +0200979 srv_clr_admin_flag(srv, mode);
Willy Tarreaua0066dd2014-05-16 11:25:16 +0200980}
981
Willy Tarreau757478e2016-11-03 19:22:19 +0100982/* principle: propagate maint and drain to tracking servers. This is useful
983 * upon startup so that inherited states are correct.
984 */
985static void srv_propagate_admin_state(struct server *srv)
986{
987 struct server *srv2;
988
989 if (!srv->trackers)
990 return;
991
992 for (srv2 = srv->trackers; srv2; srv2 = srv2->tracknext) {
Emeric Brun52a91d32017-08-31 14:41:55 +0200993 if (srv->next_admin & (SRV_ADMF_MAINT | SRV_ADMF_CMAINT))
Willy Tarreau8b428482016-11-07 15:53:43 +0100994 srv_set_admin_flag(srv2, SRV_ADMF_IMAINT, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100995
Emeric Brun52a91d32017-08-31 14:41:55 +0200996 if (srv->next_admin & SRV_ADMF_DRAIN)
Willy Tarreau8b428482016-11-07 15:53:43 +0100997 srv_set_admin_flag(srv2, SRV_ADMF_IDRAIN, NULL);
Willy Tarreau757478e2016-11-03 19:22:19 +0100998 }
999}
1000
1001/* Compute and propagate the admin states for all servers in proxy <px>.
1002 * Only servers *not* tracking another one are considered, because other
1003 * ones will be handled when the server they track is visited.
1004 */
1005void srv_compute_all_admin_states(struct proxy *px)
1006{
1007 struct server *srv;
1008
1009 for (srv = px->srv; srv; srv = srv->next) {
1010 if (srv->track)
1011 continue;
1012 srv_propagate_admin_state(srv);
1013 }
1014}
1015
Willy Tarreaudff55432012-10-10 17:51:05 +02001016/* Note: must not be declared <const> as its list will be overwritten.
1017 * Please take care of keeping this list alphabetically sorted, doing so helps
1018 * all code contributors.
1019 * Optional keywords are also declared with a NULL ->parse() function so that
1020 * the config parser can report an appropriate error when a known keyword was
1021 * not enabled.
Frédéric Lécailledfacd692017-04-16 17:14:14 +02001022 * Note: -1 as ->skip value means that the number of arguments are variable.
Willy Tarreaudff55432012-10-10 17:51:05 +02001023 */
1024static struct srv_kw_list srv_kws = { "ALL", { }, {
Frédéric Lécaille6e5e0d82017-03-20 16:30:18 +01001025 { "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 +01001026 { "agent-check", srv_parse_agent_check, 0, 1 }, /* Enable an auxiliary agent check */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001027 { "backup", srv_parse_backup, 0, 1 }, /* Flag as backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001028 { "check", srv_parse_check, 0, 1 }, /* enable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001029 { "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 +01001030 { "cookie", srv_parse_cookie, 1, 1 }, /* Assign a cookie to the server */
Frédéric Lécaille2a0d0612017-03-21 11:53:54 +01001031 { "disabled", srv_parse_disabled, 0, 1 }, /* Start the server in 'disabled' state */
1032 { "enabled", srv_parse_enabled, 0, 1 }, /* Start the server in 'enabled' state */
Frédéric Lécaille1502cfd2017-03-10 15:36:14 +01001033 { "id", srv_parse_id, 1, 0 }, /* set id# of server */
Frédéric Lécaille22f41a22017-03-16 17:17:36 +01001034 { "namespace", srv_parse_namespace, 1, 1 }, /* Namespace the server socket belongs to (if supported) */
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001035 { "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 +01001036 { "no-backup", srv_parse_no_backup, 0, 1 }, /* Flag as non-backup server */
Frédéric Lécaille65aa3562017-03-14 11:20:13 +01001037 { "no-check", srv_parse_no_check, 0, 1 }, /* disable health checks */
Frédéric Lécaille25df8902017-03-10 14:04:31 +01001038 { "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 +01001039 { "no-send-proxy", srv_parse_no_send_proxy, 0, 1 }, /* Disable use of PROXY V1 protocol */
1040 { "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 +01001041 { "non-stick", srv_parse_non_stick, 0, 1 }, /* Disable stick-table persistence */
Frédéric Lécaille547356e2017-03-15 08:55:39 +01001042 { "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 +01001043 { "redir", srv_parse_redir, 1, 1 }, /* Enable redirection mode */
Frédéric Lécaille31045e42017-03-10 16:40:00 +01001044 { "send-proxy", srv_parse_send_proxy, 0, 1 }, /* Enforce use of PROXY V1 protocol */
1045 { "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 +02001046 { "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 +01001047 { "stick", srv_parse_stick, 0, 1 }, /* Enable stick-table persistence */
Frédéric Lécaille67e0e612017-03-14 15:21:31 +01001048 { "track", srv_parse_track, 1, 1 }, /* Set the current state of the server, tracking another one */
Willy Tarreaudff55432012-10-10 17:51:05 +02001049 { NULL, NULL, 0 },
1050}};
1051
1052__attribute__((constructor))
1053static void __listener_init(void)
1054{
1055 srv_register_keywords(&srv_kws);
1056}
1057
Willy Tarreau004e0452013-11-21 11:22:01 +01001058/* Recomputes the server's eweight based on its state, uweight, the current time,
1059 * and the proxy's algorihtm. To be used after updating sv->uweight. The warmup
1060 * state is automatically disabled if the time is elapsed.
1061 */
1062void server_recalc_eweight(struct server *sv)
1063{
1064 struct proxy *px = sv->proxy;
1065 unsigned w;
1066
1067 if (now.tv_sec < sv->last_change || now.tv_sec >= sv->last_change + sv->slowstart) {
1068 /* go to full throttle if the slowstart interval is reached */
Emeric Brun52a91d32017-08-31 14:41:55 +02001069 if (sv->next_state == SRV_ST_STARTING)
1070 sv->next_state = SRV_ST_RUNNING;
Willy Tarreau004e0452013-11-21 11:22:01 +01001071 }
1072
1073 /* We must take care of not pushing the server to full throttle during slow starts.
1074 * It must also start immediately, at least at the minimal step when leaving maintenance.
1075 */
Emeric Brun52a91d32017-08-31 14:41:55 +02001076 if ((sv->next_state == SRV_ST_STARTING) && (px->lbprm.algo & BE_LB_PROP_DYN))
Willy Tarreau004e0452013-11-21 11:22:01 +01001077 w = (px->lbprm.wdiv * (now.tv_sec - sv->last_change) + sv->slowstart) / sv->slowstart;
1078 else
1079 w = px->lbprm.wdiv;
1080
Emeric Brun52a91d32017-08-31 14:41:55 +02001081 sv->next_eweight = (sv->uweight * w + px->lbprm.wmult - 1) / px->lbprm.wmult;
Willy Tarreau004e0452013-11-21 11:22:01 +01001082
Emeric Brun64cc49c2017-10-03 14:46:45 +02001083 /* Register changes to be applied asynchronously */
1084 if (LIST_ISEMPTY(&sv->update_status))
1085 LIST_ADDQ(&updated_servers, &sv->update_status);
Willy Tarreau004e0452013-11-21 11:22:01 +01001086}
1087
Willy Tarreaubaaee002006-06-26 02:48:02 +02001088/*
Simon Horman7d09b9a2013-02-12 10:45:51 +09001089 * Parses weight_str and configures sv accordingly.
1090 * Returns NULL on success, error message string otherwise.
1091 */
1092const char *server_parse_weight_change_request(struct server *sv,
1093 const char *weight_str)
1094{
1095 struct proxy *px;
Simon Hormanb796afa2013-02-12 10:45:53 +09001096 long int w;
1097 char *end;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001098
1099 px = sv->proxy;
1100
1101 /* if the weight is terminated with '%', it is set relative to
1102 * the initial weight, otherwise it is absolute.
1103 */
1104 if (!*weight_str)
1105 return "Require <weight> or <weight%>.\n";
1106
Simon Hormanb796afa2013-02-12 10:45:53 +09001107 w = strtol(weight_str, &end, 10);
1108 if (end == weight_str)
1109 return "Empty weight string empty or preceded by garbage";
1110 else if (end[0] == '%' && end[1] == '\0') {
Simon Horman58b5d292013-02-12 10:45:52 +09001111 if (w < 0)
Simon Horman7d09b9a2013-02-12 10:45:51 +09001112 return "Relative weight must be positive.\n";
Simon Horman58b5d292013-02-12 10:45:52 +09001113 /* Avoid integer overflow */
1114 if (w > 25600)
1115 w = 25600;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001116 w = sv->iweight * w / 100;
Simon Horman58b5d292013-02-12 10:45:52 +09001117 if (w > 256)
1118 w = 256;
Simon Horman7d09b9a2013-02-12 10:45:51 +09001119 }
1120 else if (w < 0 || w > 256)
1121 return "Absolute weight can only be between 0 and 256 inclusive.\n";
Simon Hormanb796afa2013-02-12 10:45:53 +09001122 else if (end[0] != '\0')
1123 return "Trailing garbage in weight string";
Simon Horman7d09b9a2013-02-12 10:45:51 +09001124
1125 if (w && w != sv->iweight && !(px->lbprm.algo & BE_LB_PROP_DYN))
1126 return "Backend is using a static LB algorithm and only accepts weights '0%' and '100%'.\n";
1127
1128 sv->uweight = w;
Willy Tarreau004e0452013-11-21 11:22:01 +01001129 server_recalc_eweight(sv);
Simon Horman7d09b9a2013-02-12 10:45:51 +09001130
1131 return NULL;
1132}
1133
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001134/*
Thierry Fournier09a91782016-02-24 08:25:39 +01001135 * Parses <addr_str> and configures <sv> accordingly. <from> precise
1136 * the source of the change in the associated message log.
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001137 * Returns:
1138 * - error string on error
1139 * - NULL on success
1140 */
1141const char *server_parse_addr_change_request(struct server *sv,
Thierry Fournier09a91782016-02-24 08:25:39 +01001142 const char *addr_str, const char *updater)
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001143{
1144 unsigned char ip[INET6_ADDRSTRLEN];
1145
1146 if (inet_pton(AF_INET6, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001147 update_server_addr(sv, ip, AF_INET6, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001148 return NULL;
1149 }
1150 if (inet_pton(AF_INET, addr_str, ip)) {
Thierry Fournier09a91782016-02-24 08:25:39 +01001151 update_server_addr(sv, ip, AF_INET, updater);
Baptiste Assmann3d8f8312015-04-13 22:54:33 +02001152 return NULL;
1153 }
1154
1155 return "Could not understand IP address format.\n";
1156}
1157
Nenad Merdanovic174dd372016-04-24 23:10:06 +02001158const char *server_parse_maxconn_change_request(struct server *sv,
1159 const char *maxconn_str)
1160{
1161 long int v;
1162 char *end;
1163
1164 if (!*maxconn_str)
1165 return "Require <maxconn>.\n";
1166
1167 v = strtol(maxconn_str, &end, 10);
1168 if (end == maxconn_str)
1169 return "maxconn string empty or preceded by garbage";
1170 else if (end[0] != '\0')
1171 return "Trailing garbage in maxconn string";
1172
1173 if (sv->maxconn == sv->minconn) { // static maxconn
1174 sv->maxconn = sv->minconn = v;
1175 } else { // dynamic maxconn
1176 sv->maxconn = v;
1177 }
1178
1179 if (may_dequeue_tasks(sv, sv->proxy))
1180 process_srv_queue(sv);
1181
1182 return NULL;
1183}
1184
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001185#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001186static struct sample_expr *srv_sni_sample_parse_expr(struct server *srv, struct proxy *px,
1187 const char *file, int linenum, char **err)
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001188{
1189 int idx;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001190 const char *args[] = {
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001191 srv->sni_expr,
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001192 NULL,
1193 };
1194
1195 idx = 0;
Olivier Houchard7d8e6882017-04-20 18:21:17 +02001196 px->conf.args.ctx = ARGC_SRV;
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001197
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001198 return sample_parse_expr((char **)args, &idx, file, linenum, err, &px->conf.args);
1199}
1200
1201static int server_parse_sni_expr(struct server *newsrv, struct proxy *px, char **err)
1202{
1203 struct sample_expr *expr;
1204
1205 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 +01001206 if (!expr) {
1207 memprintf(err, "error detected while parsing sni expression : %s", *err);
1208 return ERR_ALERT | ERR_FATAL;
1209 }
1210
1211 if (!(expr->fetch->val & SMP_VAL_BE_SRV_CON)) {
1212 memprintf(err, "error detected while parsing sni expression : "
1213 " fetch method '%s' extracts information from '%s', "
1214 "none of which is available here.\n",
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001215 newsrv->sni_expr, sample_src_names(expr->fetch->use));
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01001216 return ERR_ALERT | ERR_FATAL;
1217 }
1218
1219 px->http_needed |= !!(expr->fetch->use & SMP_USE_HTTP_ANY);
1220 release_sample_expr(newsrv->ssl_ctx.sni);
1221 newsrv->ssl_ctx.sni = expr;
1222
1223 return 0;
1224}
1225#endif
1226
1227static void display_parser_err(const char *file, int linenum, char **args, int cur_arg, char **err)
1228{
1229 if (err && *err) {
1230 indent_msg(err, 2);
1231 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], *err);
1232 }
1233 else
1234 Alert("parsing [%s:%d] : '%s %s' : error encountered while processing '%s'.\n",
1235 file, linenum, args[0], args[1], args[cur_arg]);
1236}
1237
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001238static void srv_conn_src_sport_range_cpy(struct server *srv,
1239 struct server *src)
1240{
1241 int range_sz;
1242
1243 range_sz = src->conn_src.sport_range->size;
1244 if (range_sz > 0) {
1245 srv->conn_src.sport_range = port_range_alloc_range(range_sz);
1246 if (srv->conn_src.sport_range != NULL) {
1247 int i;
1248
1249 for (i = 0; i < range_sz; i++) {
1250 srv->conn_src.sport_range->ports[i] =
1251 src->conn_src.sport_range->ports[i];
1252 }
1253 }
1254 }
1255}
1256
1257/*
1258 * Copy <src> server connection source settings to <srv> server everything needed.
1259 */
1260static void srv_conn_src_cpy(struct server *srv, struct server *src)
1261{
1262 srv->conn_src.opts = src->conn_src.opts;
1263 srv->conn_src.source_addr = src->conn_src.source_addr;
1264
1265 /* Source port range copy. */
1266 if (src->conn_src.sport_range != NULL)
1267 srv_conn_src_sport_range_cpy(srv, src);
1268
1269#ifdef CONFIG_HAP_TRANSPARENT
1270 if (src->conn_src.bind_hdr_name != NULL) {
1271 srv->conn_src.bind_hdr_name = strdup(src->conn_src.bind_hdr_name);
1272 srv->conn_src.bind_hdr_len = strlen(src->conn_src.bind_hdr_name);
1273 }
1274 srv->conn_src.bind_hdr_occ = src->conn_src.bind_hdr_occ;
1275 srv->conn_src.tproxy_addr = src->conn_src.tproxy_addr;
1276#endif
1277 if (src->conn_src.iface_name != NULL)
1278 srv->conn_src.iface_name = strdup(src->conn_src.iface_name);
1279}
1280
1281/*
1282 * Copy <src> server SSL settings to <srv> server allocating
1283 * everything needed.
1284 */
1285#if defined(USE_OPENSSL)
1286static void srv_ssl_settings_cpy(struct server *srv, struct server *src)
1287{
1288 if (src->ssl_ctx.ca_file != NULL)
1289 srv->ssl_ctx.ca_file = strdup(src->ssl_ctx.ca_file);
1290 if (src->ssl_ctx.crl_file != NULL)
1291 srv->ssl_ctx.crl_file = strdup(src->ssl_ctx.crl_file);
1292 if (src->ssl_ctx.client_crt != NULL)
1293 srv->ssl_ctx.client_crt = strdup(src->ssl_ctx.client_crt);
1294
1295 srv->ssl_ctx.verify = src->ssl_ctx.verify;
1296
1297 if (src->ssl_ctx.verify_host != NULL)
1298 srv->ssl_ctx.verify_host = strdup(src->ssl_ctx.verify_host);
1299 if (src->ssl_ctx.ciphers != NULL)
1300 srv->ssl_ctx.ciphers = strdup(src->ssl_ctx.ciphers);
1301 if (src->sni_expr != NULL)
1302 srv->sni_expr = strdup(src->sni_expr);
1303}
1304#endif
1305
1306/*
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001307 * Prepare <srv> for hostname resolution.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001308 * May be safely called with a default server as <src> argument (without hostname).
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001309 * Returns -1 in case of any allocation failure, 0 if not.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001310 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001311static int srv_prepare_for_resolution(struct server *srv, const char *hostname)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001312{
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001313 if (!hostname)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001314 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001315
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001316 free(srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001317 srv->hostname = strdup(hostname);
Baptiste Assmann81ed1a02017-05-03 10:11:44 +02001318
Baptiste Assmann42746372017-05-03 12:12:02 +02001319 srv->hostname_dn_len = dns_str_to_dn_label_len(hostname);
1320 srv->hostname_dn = calloc(srv->hostname_dn_len + 1, sizeof(char));
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001321
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001322 if (!srv->hostname || !srv->hostname_dn)
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001323 goto err;
1324
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001325 if (!dns_str_to_dn_label(srv->hostname,
Baptiste Assmann42746372017-05-03 12:12:02 +02001326 srv->hostname_dn,
1327 srv->hostname_dn_len + 1))
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001328 goto err;
1329
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001330 return 0;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001331
1332 err:
1333 free(srv->hostname);
1334 srv->hostname = NULL;
Baptiste Assmann42746372017-05-03 12:12:02 +02001335 free(srv->hostname_dn);
1336 srv->hostname_dn = NULL;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001337 return -1;
1338}
1339
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001340/*
1341 * Free the link between a server and its resolution.
1342 * It also performs the following tasks:
1343 * - check if resolution can be moved back in the resolvers' pool
1344 * (and do it)
1345 * - move resolution's hostname_dn and hostname_dn_len to the next requester
1346 * available (when applied)
1347 */
Baptiste Assmann747359e2017-08-14 10:37:46 +02001348void srv_free_from_resolution(struct server *srv)
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001349{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001350 struct dns_requester *requester;
1351 int count;
1352
1353 /* check if we can move the resolution back to the pool.
1354 * if <count> is greater than 1, then we can't */
1355 count = 0;
1356 list_for_each_entry(requester, &srv->resolution->requester.wait, list) {
1357 ++count;
1358 if (count > 1)
1359 break;
1360 }
1361 list_for_each_entry(requester, &srv->resolution->requester.curr, list) {
1362 ++count;
1363 if (count > 1)
1364 break;
1365 }
1366 if (count <= 1) {
1367 /* move the resolution back to the pool */
1368 dns_resolution_free(srv->resolvers, srv->resolution);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001369 return;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001370 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02001371
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001372 dns_rm_requester_from_resolution(srv->dns_requester, srv->resolution);
1373
1374 return;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001375}
1376
1377/*
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001378 * Copy <src> server settings to <srv> server allocating
1379 * everything needed.
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001380 * This function is not supposed to be called at any time, but only
1381 * during server settings parsing or during server allocations from
1382 * a server template, and just after having calloc()'ed a new server.
1383 * So, <src> may only be a default server (when parsing server settings)
1384 * or a server template (during server allocations from a server template).
1385 * <srv_tmpl> distinguishes these two cases (must be 1 if <srv> is a template,
1386 * 0 if not).
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001387 */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001388static void srv_settings_cpy(struct server *srv, struct server *src, int srv_tmpl)
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001389{
1390 /* Connection source settings copy */
1391 srv_conn_src_cpy(srv, src);
1392
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001393 if (srv_tmpl) {
1394 srv->addr = src->addr;
1395 srv->svc_port = src->svc_port;
1396 }
1397
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001398 srv->pp_opts = src->pp_opts;
1399 if (src->rdr_pfx != NULL) {
1400 srv->rdr_pfx = strdup(src->rdr_pfx);
1401 srv->rdr_len = src->rdr_len;
1402 }
1403 if (src->cookie != NULL) {
1404 srv->cookie = strdup(src->cookie);
1405 srv->cklen = src->cklen;
1406 }
1407 srv->use_ssl = src->use_ssl;
1408 srv->check.addr = srv->agent.addr = src->check.addr;
1409 srv->check.use_ssl = src->check.use_ssl;
1410 srv->check.port = src->check.port;
1411 /* Note: 'flags' field has potentially been already initialized. */
1412 srv->flags |= src->flags;
1413 srv->do_check = src->do_check;
1414 srv->do_agent = src->do_agent;
1415 if (srv->check.port)
1416 srv->flags |= SRV_F_CHECKPORT;
1417 srv->check.inter = src->check.inter;
1418 srv->check.fastinter = src->check.fastinter;
1419 srv->check.downinter = src->check.downinter;
1420 srv->agent.use_ssl = src->agent.use_ssl;
1421 srv->agent.port = src->agent.port;
1422 if (src->agent.send_string != NULL)
1423 srv->agent.send_string = strdup(src->agent.send_string);
1424 srv->agent.send_string_len = src->agent.send_string_len;
1425 srv->agent.inter = src->agent.inter;
1426 srv->agent.fastinter = src->agent.fastinter;
1427 srv->agent.downinter = src->agent.downinter;
1428 srv->maxqueue = src->maxqueue;
1429 srv->minconn = src->minconn;
1430 srv->maxconn = src->maxconn;
1431 srv->slowstart = src->slowstart;
1432 srv->observe = src->observe;
1433 srv->onerror = src->onerror;
1434 srv->onmarkeddown = src->onmarkeddown;
1435 srv->onmarkedup = src->onmarkedup;
1436 if (src->trackit != NULL)
1437 srv->trackit = strdup(src->trackit);
1438 srv->consecutive_errors_limit = src->consecutive_errors_limit;
1439 srv->uweight = srv->iweight = src->iweight;
1440
1441 srv->check.send_proxy = src->check.send_proxy;
1442 /* health: up, but will fall down at first failure */
1443 srv->check.rise = srv->check.health = src->check.rise;
1444 srv->check.fall = src->check.fall;
1445
1446 /* Here we check if 'disabled' is the default server state */
Emeric Brun52a91d32017-08-31 14:41:55 +02001447 if (src->next_admin & (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT)) {
1448 srv->next_admin |= SRV_ADMF_CMAINT | SRV_ADMF_FMAINT;
1449 srv->next_state = SRV_ST_STOPPED;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001450 srv->check.state |= CHK_ST_PAUSED;
1451 srv->check.health = 0;
1452 }
1453
1454 /* health: up but will fall down at first failure */
1455 srv->agent.rise = srv->agent.health = src->agent.rise;
1456 srv->agent.fall = src->agent.fall;
1457
1458 if (src->resolvers_id != NULL)
1459 srv->resolvers_id = strdup(src->resolvers_id);
1460 srv->dns_opts.family_prio = src->dns_opts.family_prio;
1461 if (srv->dns_opts.family_prio == AF_UNSPEC)
1462 srv->dns_opts.family_prio = AF_INET6;
1463 memcpy(srv->dns_opts.pref_net,
1464 src->dns_opts.pref_net,
1465 sizeof srv->dns_opts.pref_net);
1466 srv->dns_opts.pref_net_nb = src->dns_opts.pref_net_nb;
1467
1468 srv->init_addr_methods = src->init_addr_methods;
1469 srv->init_addr = src->init_addr;
1470#if defined(USE_OPENSSL)
1471 srv_ssl_settings_cpy(srv, src);
1472#endif
1473#ifdef TCP_USER_TIMEOUT
1474 srv->tcp_ut = src->tcp_ut;
1475#endif
Olivier Houchard8da5f982017-08-04 18:35:36 +02001476 if (srv_tmpl)
1477 srv->srvrq = src->srvrq;
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001478}
1479
1480static struct server *new_server(struct proxy *proxy)
1481{
1482 struct server *srv;
1483
1484 srv = calloc(1, sizeof *srv);
1485 if (!srv)
1486 return NULL;
1487
1488 srv->obj_type = OBJ_TYPE_SERVER;
1489 srv->proxy = proxy;
1490 LIST_INIT(&srv->actconns);
1491 LIST_INIT(&srv->pendconns);
1492 LIST_INIT(&srv->priv_conns);
1493 LIST_INIT(&srv->idle_conns);
1494 LIST_INIT(&srv->safe_conns);
Emeric Brun64cc49c2017-10-03 14:46:45 +02001495 LIST_INIT(&srv->update_status);
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001496
Emeric Brun52a91d32017-08-31 14:41:55 +02001497 srv->next_state = SRV_ST_RUNNING; /* early server setup */
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001498 srv->last_change = now.tv_sec;
1499
1500 srv->check.status = HCHK_STATUS_INI;
1501 srv->check.server = srv;
1502 srv->check.tcpcheck_rules = &proxy->tcpcheck_rules;
1503
1504 srv->agent.status = HCHK_STATUS_INI;
1505 srv->agent.server = srv;
1506 srv->xprt = srv->check.xprt = srv->agent.xprt = xprt_get(XPRT_RAW);
1507
1508 return srv;
1509}
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001510
1511/*
1512 * Validate <srv> server health-check settings.
1513 * Returns 0 if everything is OK, -1 if not.
1514 */
1515static int server_healthcheck_validate(const char *file, int linenum, struct server *srv)
1516{
1517 struct tcpcheck_rule *r = NULL;
1518 struct list *l;
1519
1520 /*
1521 * We need at least a service port, a check port or the first tcp-check rule must
1522 * be a 'connect' one when checking an IPv4/IPv6 server.
1523 */
1524 if ((srv_check_healthcheck_port(&srv->check) != 0) ||
1525 (!is_inet_addr(&srv->check.addr) && (is_addr(&srv->check.addr) || !is_inet_addr(&srv->addr))))
1526 return 0;
1527
1528 r = (struct tcpcheck_rule *)srv->proxy->tcpcheck_rules.n;
1529 if (!r) {
1530 Alert("parsing [%s:%d] : server %s has neither service port nor check port. "
1531 "Check has been disabled.\n",
1532 file, linenum, srv->id);
1533 return -1;
1534 }
1535
1536 /* search the first action (connect / send / expect) in the list */
1537 l = &srv->proxy->tcpcheck_rules;
1538 list_for_each_entry(r, l, list) {
1539 if (r->action != TCPCHK_ACT_COMMENT)
1540 break;
1541 }
1542
1543 if ((r->action != TCPCHK_ACT_CONNECT) || !r->port) {
1544 Alert("parsing [%s:%d] : server %s has neither service port nor check port "
1545 "nor tcp_check rule 'connect' with port information. Check has been disabled.\n",
1546 file, linenum, srv->id);
1547 return -1;
1548 }
1549
1550 /* scan the tcp-check ruleset to ensure a port has been configured */
1551 l = &srv->proxy->tcpcheck_rules;
1552 list_for_each_entry(r, l, list) {
1553 if ((r->action == TCPCHK_ACT_CONNECT) && (!r->port)) {
1554 Alert("parsing [%s:%d] : server %s has neither service port nor check port, "
1555 "and a tcp_check rule 'connect' with no port information. Check has been disabled.\n",
1556 file, linenum, srv->id);
1557 return -1;
1558 }
1559 }
1560
1561 return 0;
1562}
1563
1564/*
1565 * Initialize <srv> health-check structure.
1566 * Returns the error string in case of memory allocation failure, NULL if not.
1567 */
1568static const char *do_health_check_init(struct server *srv, int check_type, int state)
1569{
1570 const char *ret;
1571
1572 if (!srv->do_check)
1573 return NULL;
1574
1575 ret = init_check(&srv->check, check_type);
1576 if (ret)
1577 return ret;
1578
Frédéric Lécaille759ea982017-03-30 17:32:36 +02001579 srv->check.state |= state;
1580 global.maxsock++;
1581
1582 return NULL;
1583}
1584
1585static int server_health_check_init(const char *file, int linenum,
1586 struct server *srv, struct proxy *curproxy)
1587{
1588 const char *ret;
1589
1590 if (!srv->do_check)
1591 return 0;
1592
1593 if (srv->trackit) {
1594 Alert("parsing [%s:%d]: unable to enable checks and tracking at the same time!\n",
1595 file, linenum);
1596 return ERR_ALERT | ERR_FATAL;
1597 }
1598
1599 if (server_healthcheck_validate(file, linenum, srv) < 0)
1600 return ERR_ALERT | ERR_ABORT;
1601
1602 /* note: check type will be set during the config review phase */
1603 ret = do_health_check_init(srv, 0, CHK_ST_CONFIGURED | CHK_ST_ENABLED);
1604 if (ret) {
1605 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1606 return ERR_ALERT | ERR_ABORT;
1607 }
1608
1609 return 0;
1610}
1611
1612/*
1613 * Initialize <srv> agent check structure.
1614 * Returns the error string in case of memory allocation failure, NULL if not.
1615 */
1616static const char *do_server_agent_check_init(struct server *srv, int state)
1617{
1618 const char *ret;
1619
1620 if (!srv->do_agent)
1621 return NULL;
1622
1623 ret = init_check(&srv->agent, PR_O2_LB_AGENT_CHK);
1624 if (ret)
1625 return ret;
1626
1627 if (!srv->agent.inter)
1628 srv->agent.inter = srv->check.inter;
1629
1630 srv->agent.state |= state;
1631 global.maxsock++;
1632
1633 return NULL;
1634}
1635
1636static int server_agent_check_init(const char *file, int linenum,
1637 struct server *srv, struct proxy *curproxy)
1638{
1639 const char *ret;
1640
1641 if (!srv->do_agent)
1642 return 0;
1643
1644 if (!srv->agent.port) {
1645 Alert("parsing [%s:%d] : server %s does not have agent port. Agent check has been disabled.\n",
1646 file, linenum, srv->id);
1647 return ERR_ALERT | ERR_FATAL;
1648 }
1649
1650 ret = do_server_agent_check_init(srv, CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT);
1651 if (ret) {
1652 Alert("parsing [%s:%d] : %s.\n", file, linenum, ret);
1653 return ERR_ALERT | ERR_ABORT;
1654 }
1655
1656 return 0;
1657}
1658
1659#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1660static int server_sni_expr_init(const char *file, int linenum, char **args, int cur_arg,
1661 struct server *srv, struct proxy *proxy)
1662{
1663 int ret;
1664 char *err = NULL;
1665
1666 if (!srv->sni_expr)
1667 return 0;
1668
1669 ret = server_parse_sni_expr(srv, proxy, &err);
1670 if (!ret)
1671 return 0;
1672
1673 display_parser_err(file, linenum, args, cur_arg, &err);
1674 free(err);
1675
1676 return ret;
1677}
1678#endif
1679
1680/*
1681 * Server initializations finalization.
1682 * Initialize health check, agent check and SNI expression if enabled.
1683 * Must not be called for a default server instance.
1684 */
1685static int server_finalize_init(const char *file, int linenum, char **args, int cur_arg,
1686 struct server *srv, struct proxy *px)
1687{
1688 int ret;
1689
1690 if ((ret = server_health_check_init(file, linenum, srv, px)) != 0 ||
1691 (ret = server_agent_check_init(file, linenum, srv, px)) != 0) {
1692 return ret;
1693 }
1694
1695#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1696 if ((ret = server_sni_expr_init(file, linenum, args, cur_arg, srv, px)) != 0)
1697 return ret;
1698#endif
1699
1700 if (srv->flags & SRV_F_BACKUP)
1701 px->srv_bck++;
1702 else
1703 px->srv_act++;
1704 srv_lb_commit_status(srv);
1705
1706 return 0;
1707}
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001708
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001709/*
1710 * Parse as much as possible such a range string argument: low[-high]
1711 * Set <nb_low> and <nb_high> values so that they may be reused by this loop
1712 * for(int i = nb_low; i <= nb_high; i++)... with nb_low >= 1.
1713 * Fails if 'low' < 0 or 'high' is present and not higher than 'low'.
1714 * Returns 0 if succeeded, -1 if not.
1715 */
1716static int srv_tmpl_parse_range(struct server *srv, const char *arg, int *nb_low, int *nb_high)
1717{
1718 char *nb_high_arg;
1719
1720 *nb_high = 0;
1721 chunk_printf(&trash, "%s", arg);
1722 *nb_low = atoi(trash.str);
1723
1724 if ((nb_high_arg = strchr(trash.str, '-'))) {
1725 *nb_high_arg++ = '\0';
1726 *nb_high = atoi(nb_high_arg);
1727 }
1728 else {
1729 *nb_high += *nb_low;
1730 *nb_low = 1;
1731 }
1732
1733 if (*nb_low < 0 || *nb_high < *nb_low)
1734 return -1;
1735
1736 return 0;
1737}
1738
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001739static inline void srv_set_id_from_prefix(struct server *srv, const char *prefix, int nb)
1740{
1741 chunk_printf(&trash, "%s%d", prefix, nb);
1742 free(srv->id);
1743 srv->id = strdup(trash.str);
1744}
1745
1746/*
1747 * Initialize as much as possible servers from <srv> server template.
1748 * Note that a server template is a special server with
1749 * a few different parameters than a server which has
1750 * been parsed mostly the same way as a server.
1751 * Returns the number of servers succesfully allocated,
1752 * 'srv' template included.
1753 */
1754static int server_template_init(struct server *srv, struct proxy *px)
1755{
1756 int i;
1757 struct server *newsrv;
1758
1759 for (i = srv->tmpl_info.nb_low + 1; i <= srv->tmpl_info.nb_high; i++) {
1760 int check_init_state;
1761 int agent_init_state;
1762
1763 newsrv = new_server(px);
1764 if (!newsrv)
1765 goto err;
1766
1767 srv_settings_cpy(newsrv, srv, 1);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02001768 srv_prepare_for_resolution(newsrv, srv->hostname);
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02001769#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1770 if (newsrv->sni_expr) {
1771 newsrv->ssl_ctx.sni = srv_sni_sample_parse_expr(newsrv, px, NULL, 0, NULL);
1772 if (!newsrv->ssl_ctx.sni)
1773 goto err;
1774 }
1775#endif
1776 /* Set this new server ID. */
1777 srv_set_id_from_prefix(newsrv, srv->tmpl_info.prefix, i);
1778
1779 /* Initial checks states. */
1780 check_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED;
1781 agent_init_state = CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_AGENT;
1782
1783 if (do_health_check_init(newsrv, px->options2 & PR_O2_CHK_ANY, check_init_state) ||
1784 do_server_agent_check_init(newsrv, agent_init_state))
1785 goto err;
1786
1787 /* Linked backwards first. This will be restablished after parsing. */
1788 newsrv->next = px->srv;
1789 px->srv = newsrv;
1790 }
1791 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1792
1793 return i - srv->tmpl_info.nb_low;
1794
1795 err:
1796 srv_set_id_from_prefix(srv, srv->tmpl_info.prefix, srv->tmpl_info.nb_low);
1797 if (newsrv) {
1798#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
1799 release_sample_expr(newsrv->ssl_ctx.sni);
1800#endif
1801 free_check(&newsrv->agent);
1802 free_check(&newsrv->check);
1803 }
1804 free(newsrv);
1805 return i - srv->tmpl_info.nb_low;
1806}
1807
Willy Tarreau272adea2014-03-31 10:39:59 +02001808int parse_server(const char *file, int linenum, char **args, struct proxy *curproxy, struct proxy *defproxy)
1809{
1810 struct server *newsrv = NULL;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001811 const char *err = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001812 char *errmsg = NULL;
1813 int err_code = 0;
1814 unsigned val;
Willy Tarreau07101d52015-09-08 16:16:35 +02001815 char *fqdn = NULL;
Willy Tarreau272adea2014-03-31 10:39:59 +02001816
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001817 if (!strcmp(args[0], "server") ||
1818 !strcmp(args[0], "default-server") ||
1819 !strcmp(args[0], "server-template")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001820 int cur_arg;
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01001821 int defsrv = (*args[0] == 'd');
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001822 int srv = !defsrv && !strcmp(args[0], "server");
1823 int srv_tmpl = !defsrv && !srv;
1824 int tmpl_range_low = 0, tmpl_range_high = 0;
Willy Tarreau272adea2014-03-31 10:39:59 +02001825
1826 if (!defsrv && curproxy == defproxy) {
1827 Alert("parsing [%s:%d] : '%s' not allowed in 'defaults' section.\n", file, linenum, args[0]);
1828 err_code |= ERR_ALERT | ERR_FATAL;
1829 goto out;
1830 }
1831 else if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
1832 err_code |= ERR_ALERT | ERR_FATAL;
1833
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001834 /* There is no mandatory first arguments for default server. */
1835 if (srv) {
1836 if (!*args[2]) {
1837 /* 'server' line number of argument check. */
1838 Alert("parsing [%s:%d] : '%s' expects <name> and <addr>[:<port>] as arguments.\n",
1839 file, linenum, args[0]);
1840 err_code |= ERR_ALERT | ERR_FATAL;
1841 goto out;
1842 }
1843
1844 err = invalid_char(args[1]);
1845 }
1846 else if (srv_tmpl) {
1847 if (!*args[3]) {
1848 /* 'server-template' line number of argument check. */
1849 Alert("parsing [%s:%d] : '%s' expects <prefix> <nb | range> <addr>[:<port>] as arguments.\n",
1850 file, linenum, args[0]);
1851 err_code |= ERR_ALERT | ERR_FATAL;
1852 goto out;
1853 }
1854
1855 err = invalid_prefix_char(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001856 }
1857
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001858 if (err) {
1859 Alert("parsing [%s:%d] : character '%c' is not permitted in %s %s '%s'.\n",
1860 file, linenum, *err, args[0], srv ? "name" : "prefix", args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001861 err_code |= ERR_ALERT | ERR_FATAL;
1862 goto out;
1863 }
1864
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001865 cur_arg = 2;
1866 if (srv_tmpl) {
1867 /* Parse server-template <nb | range> arg. */
1868 if (srv_tmpl_parse_range(newsrv, args[cur_arg], &tmpl_range_low, &tmpl_range_high) < 0) {
1869 Alert("parsing [%s:%d] : Wrong %s number or range arg '%s'.\n",
1870 file, linenum, args[0], args[cur_arg]);
1871 err_code |= ERR_ALERT | ERR_FATAL;
1872 goto out;
1873 }
1874 cur_arg++;
1875 }
1876
Willy Tarreau272adea2014-03-31 10:39:59 +02001877 if (!defsrv) {
1878 struct sockaddr_storage *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01001879 int port1, port2, port;
Willy Tarreau272adea2014-03-31 10:39:59 +02001880 struct protocol *proto;
1881
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02001882 newsrv = new_server(curproxy);
1883 if (!newsrv) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001884 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
1885 err_code |= ERR_ALERT | ERR_ABORT;
1886 goto out;
1887 }
1888
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001889 if (srv_tmpl) {
1890 newsrv->tmpl_info.nb_low = tmpl_range_low;
1891 newsrv->tmpl_info.nb_high = tmpl_range_high;
1892 }
1893
Willy Tarreau272adea2014-03-31 10:39:59 +02001894 /* the servers are linked backwards first */
1895 newsrv->next = curproxy->srv;
1896 curproxy->srv = newsrv;
Willy Tarreau272adea2014-03-31 10:39:59 +02001897 newsrv->conf.file = strdup(file);
1898 newsrv->conf.line = linenum;
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001899 /* Note: for a server template, its id is its prefix.
1900 * This is a temporary id which will be used for server allocations to come
1901 * after parsing.
1902 */
1903 if (srv)
1904 newsrv->id = strdup(args[1]);
1905 else
1906 newsrv->tmpl_info.prefix = strdup(args[1]);
Willy Tarreau272adea2014-03-31 10:39:59 +02001907
1908 /* several ways to check the port component :
1909 * - IP => port=+0, relative (IPv4 only)
1910 * - IP: => port=+0, relative
1911 * - IP:N => port=N, absolute
1912 * - IP:+N => port=+N, relative
1913 * - IP:-N => port=-N, relative
1914 */
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02001915 sk = str2sa_range(args[cur_arg], &port, &port1, &port2, &errmsg, NULL, &fqdn, 0);
Willy Tarreau272adea2014-03-31 10:39:59 +02001916 if (!sk) {
1917 Alert("parsing [%s:%d] : '%s %s' : %s\n", file, linenum, args[0], args[1], errmsg);
1918 err_code |= ERR_ALERT | ERR_FATAL;
1919 goto out;
1920 }
1921
1922 proto = protocol_by_family(sk->ss_family);
Willy Tarreau9698f4b2017-01-06 18:42:57 +01001923 if (!fqdn && (!proto || !proto->connect)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02001924 Alert("parsing [%s:%d] : '%s %s' : connect() not supported for this address family.\n",
1925 file, linenum, args[0], args[1]);
1926 err_code |= ERR_ALERT | ERR_FATAL;
1927 goto out;
1928 }
1929
1930 if (!port1 || !port2) {
1931 /* no port specified, +offset, -offset */
Willy Tarreauc93cd162014-05-13 15:54:22 +02001932 newsrv->flags |= SRV_F_MAPPORTS;
Willy Tarreau272adea2014-03-31 10:39:59 +02001933 }
1934 else if (port1 != port2) {
1935 /* port range */
1936 Alert("parsing [%s:%d] : '%s %s' : port ranges are not allowed in '%s'\n",
1937 file, linenum, args[0], args[1], args[2]);
1938 err_code |= ERR_ALERT | ERR_FATAL;
1939 goto out;
1940 }
Willy Tarreau272adea2014-03-31 10:39:59 +02001941
Baptiste Assmanna68ca962015-04-14 01:15:08 +02001942 /* save hostname and create associated name resolution */
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001943 if (fqdn) {
Olivier Houchard8da5f982017-08-04 18:35:36 +02001944 if (fqdn[0] == '_') {
1945 struct dns_srvrq *srvrq = NULL;
1946 int found = 0;
1947 /* SRV record */
1948 /* Check if a SRV request already exists, and if not, create it */
1949 list_for_each_entry(srvrq, &curproxy->srvrq_list, list) {
1950 if (!strcmp(srvrq->name, fqdn)) {
1951 found = 1;
1952 break;
1953 }
1954 }
1955 if (found == 0) {
1956 int hostname_dn_len;
1957
1958 srvrq = calloc(1, sizeof(*srvrq));
1959 if (!srvrq) {
1960 Alert("Failed to allocate memory");
1961 err_code = ERR_ALERT | ERR_FATAL;
1962 goto out;
1963 }
1964 srvrq->obj_type = OBJ_TYPE_SRVRQ;
1965 srvrq->proxy = proxy;
1966 srvrq->name = strdup(fqdn);
1967 srvrq->inter = 2000;
1968 hostname_dn_len = dns_str_to_dn_label_len(fqdn);
1969 if (hostname_dn_len == -1) {
1970 Alert("Failed to parse domaine name '%s'", fqdn);
1971 err_code = ERR_ALERT | ERR_FATAL;
1972 goto out;
1973 }
1974 srvrq->hostname_dn = malloc(hostname_dn_len + 1);
1975 srvrq->hostname_dn_len = hostname_dn_len;
1976 if (!srvrq->hostname_dn) {
1977 Alert("Failed to alloc memory");
1978 err_code = ERR_ALERT | ERR_FATAL;
1979 goto out;
1980 }
1981 if (!dns_str_to_dn_label(fqdn,
1982 srvrq->hostname_dn,
1983 hostname_dn_len + 1)) {
1984 Alert("Failed to parse domain name '%s'", fqdn);
1985 err_code = ERR_ALERT | ERR_FATAL;
1986 goto out;
1987 }
1988 LIST_ADDQ(&proxy->srvrq_list, &srvrq->list);
1989
1990 }
1991 newsrv->srvrq = srvrq;
1992
1993
1994 } else if (srv_prepare_for_resolution(newsrv, fqdn) == -1) {
1995 Alert("parsing [%s:%d] : Can't create DNS resolution for server '%s'\n",
1996 file, linenum, newsrv->id);
1997 err_code |= ERR_ALERT | ERR_FATAL;
1998 goto out;
Baptiste Assmann4f91f7e2017-05-03 12:09:54 +02001999 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002000 }
2001
Willy Tarreau272adea2014-03-31 10:39:59 +02002002 newsrv->addr = *sk;
Willy Tarreau6ecb10a2017-01-06 18:36:06 +01002003 newsrv->svc_port = port;
Willy Tarreau272adea2014-03-31 10:39:59 +02002004
Olivier Houchard8da5f982017-08-04 18:35:36 +02002005 if (!newsrv->srvrq && !newsrv->hostname && !protocol_by_family(newsrv->addr.ss_family)) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002006 Alert("parsing [%s:%d] : Unknown protocol family %d '%s'\n",
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002007 file, linenum, newsrv->addr.ss_family, args[cur_arg]);
Willy Tarreau272adea2014-03-31 10:39:59 +02002008 err_code |= ERR_ALERT | ERR_FATAL;
2009 goto out;
2010 }
Frédéric Lécaille5c3cd972017-03-15 16:36:09 +01002011
Frédéric Lécaille58b207c2017-03-30 14:18:30 +02002012 /* Copy default server settings to new server settings. */
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002013 srv_settings_cpy(newsrv, &curproxy->defsrv, 0);
Frédéric Lécailleb82f7422017-04-13 18:24:23 +02002014 cur_arg++;
Willy Tarreau272adea2014-03-31 10:39:59 +02002015 } else {
2016 newsrv = &curproxy->defsrv;
2017 cur_arg = 1;
Thierry Fournierada34842016-02-17 21:25:09 +01002018 newsrv->dns_opts.family_prio = AF_INET6;
Willy Tarreau272adea2014-03-31 10:39:59 +02002019 }
2020
2021 while (*args[cur_arg]) {
Frédéric Lécaille6e0843c2017-03-21 16:39:15 +01002022 if (!strcmp(args[cur_arg], "agent-inter")) {
Willy Tarreau272adea2014-03-31 10:39:59 +02002023 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2024 if (err) {
2025 Alert("parsing [%s:%d] : unexpected character '%c' in 'agent-inter' argument of server %s.\n",
2026 file, linenum, *err, newsrv->id);
2027 err_code |= ERR_ALERT | ERR_FATAL;
2028 goto out;
2029 }
2030 if (val <= 0) {
2031 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2032 file, linenum, val, args[cur_arg], newsrv->id);
2033 err_code |= ERR_ALERT | ERR_FATAL;
2034 goto out;
2035 }
2036 newsrv->agent.inter = val;
2037 cur_arg += 2;
2038 }
Misiekea849332017-01-09 09:39:51 +01002039 else if (!strcmp(args[cur_arg], "agent-addr")) {
2040 if(str2ip(args[cur_arg + 1], &newsrv->agent.addr) == NULL) {
2041 Alert("parsing agent-addr failed. Check if %s is correct address.\n", args[cur_arg + 1]);
2042 goto out;
2043 }
2044
2045 cur_arg += 2;
2046 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002047 else if (!strcmp(args[cur_arg], "agent-port")) {
2048 global.maxsock++;
2049 newsrv->agent.port = atol(args[cur_arg + 1]);
2050 cur_arg += 2;
2051 }
James Brown55f9ff12015-10-21 18:19:05 -07002052 else if (!strcmp(args[cur_arg], "agent-send")) {
2053 global.maxsock++;
2054 free(newsrv->agent.send_string);
2055 newsrv->agent.send_string_len = strlen(args[cur_arg + 1]);
2056 newsrv->agent.send_string = calloc(1, newsrv->agent.send_string_len + 1);
2057 memcpy(newsrv->agent.send_string, args[cur_arg + 1], newsrv->agent.send_string_len);
2058 cur_arg += 2;
2059 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002060 else if (!strcmp(args[cur_arg], "init-addr")) {
2061 char *p, *end;
2062 int done;
Willy Tarreau4310d362016-11-02 15:05:56 +01002063 struct sockaddr_storage sa;
Baptiste Assmann25938272016-09-21 20:26:16 +02002064
2065 newsrv->init_addr_methods = 0;
2066 memset(&newsrv->init_addr, 0, sizeof(newsrv->init_addr));
2067
2068 for (p = args[cur_arg + 1]; *p; p = end) {
2069 /* cut on next comma */
2070 for (end = p; *end && *end != ','; end++);
2071 if (*end)
2072 *(end++) = 0;
2073
Willy Tarreau4310d362016-11-02 15:05:56 +01002074 memset(&sa, 0, sizeof(sa));
Baptiste Assmann25938272016-09-21 20:26:16 +02002075 if (!strcmp(p, "libc")) {
2076 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LIBC);
2077 }
2078 else if (!strcmp(p, "last")) {
2079 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_LAST);
2080 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01002081 else if (!strcmp(p, "none")) {
2082 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_NONE);
2083 }
Willy Tarreau4310d362016-11-02 15:05:56 +01002084 else if (str2ip2(p, &sa, 0)) {
2085 if (is_addr(&newsrv->init_addr)) {
2086 Alert("parsing [%s:%d]: '%s' : initial address already specified, cannot add '%s'.\n",
2087 file, linenum, args[cur_arg], p);
2088 err_code |= ERR_ALERT | ERR_FATAL;
2089 goto out;
2090 }
2091 newsrv->init_addr = sa;
2092 done = srv_append_initaddr(&newsrv->init_addr_methods, SRV_IADDR_IP);
2093 }
Baptiste Assmann25938272016-09-21 20:26:16 +02002094 else {
Willy Tarreau37ebe122016-11-04 15:17:58 +01002095 Alert("parsing [%s:%d]: '%s' : unknown init-addr method '%s', supported methods are 'libc', 'last', 'none'.\n",
Baptiste Assmann25938272016-09-21 20:26:16 +02002096 file, linenum, args[cur_arg], p);
2097 err_code |= ERR_ALERT | ERR_FATAL;
2098 goto out;
2099 }
2100 if (!done) {
2101 Alert("parsing [%s:%d]: '%s' : too many init-addr methods when trying to add '%s'\n",
2102 file, linenum, args[cur_arg], p);
2103 err_code |= ERR_ALERT | ERR_FATAL;
2104 goto out;
2105 }
2106 }
2107 cur_arg += 2;
2108 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002109 else if (!strcmp(args[cur_arg], "resolvers")) {
Frédéric Lécailledaa2fe62017-04-20 12:17:50 +02002110 free(newsrv->resolvers_id);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002111 newsrv->resolvers_id = strdup(args[cur_arg + 1]);
2112 cur_arg += 2;
2113 }
2114 else if (!strcmp(args[cur_arg], "resolve-prefer")) {
2115 if (!strcmp(args[cur_arg + 1], "ipv4"))
Thierry Fournierada34842016-02-17 21:25:09 +01002116 newsrv->dns_opts.family_prio = AF_INET;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002117 else if (!strcmp(args[cur_arg + 1], "ipv6"))
Thierry Fournierada34842016-02-17 21:25:09 +01002118 newsrv->dns_opts.family_prio = AF_INET6;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02002119 else {
2120 Alert("parsing [%s:%d]: '%s' expects either ipv4 or ipv6 as argument.\n",
2121 file, linenum, args[cur_arg]);
2122 err_code |= ERR_ALERT | ERR_FATAL;
2123 goto out;
2124 }
2125 cur_arg += 2;
2126 }
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002127 else if (!strcmp(args[cur_arg], "resolve-net")) {
2128 char *p, *e;
2129 unsigned char mask;
2130 struct dns_options *opt;
2131
2132 if (!args[cur_arg + 1] || args[cur_arg + 1][0] == '\0') {
2133 Alert("parsing [%s:%d]: '%s' expects a list of networks.\n",
2134 file, linenum, args[cur_arg]);
2135 err_code |= ERR_ALERT | ERR_FATAL;
2136 goto out;
2137 }
2138
2139 opt = &newsrv->dns_opts;
2140
2141 /* Split arguments by comma, and convert it from ipv4 or ipv6
2142 * string network in in_addr or in6_addr.
2143 */
2144 p = args[cur_arg + 1];
2145 e = p;
2146 while (*p != '\0') {
2147 /* If no room avalaible, return error. */
David Carlierd10025c2016-04-08 10:26:44 +01002148 if (opt->pref_net_nb >= SRV_MAX_PREF_NET) {
Thierry Fournierac88cfe2016-02-17 22:05:30 +01002149 Alert("parsing [%s:%d]: '%s' exceed %d networks.\n",
2150 file, linenum, args[cur_arg], SRV_MAX_PREF_NET);
2151 err_code |= ERR_ALERT | ERR_FATAL;
2152 goto out;
2153 }
2154 /* look for end or comma. */
2155 while (*e != ',' && *e != '\0')
2156 e++;
2157 if (*e == ',') {
2158 *e = '\0';
2159 e++;
2160 }
2161 if (str2net(p, 0, &opt->pref_net[opt->pref_net_nb].addr.in4,
2162 &opt->pref_net[opt->pref_net_nb].mask.in4)) {
2163 /* Try to convert input string from ipv4 or ipv6 network. */
2164 opt->pref_net[opt->pref_net_nb].family = AF_INET;
2165 } else if (str62net(p, &opt->pref_net[opt->pref_net_nb].addr.in6,
2166 &mask)) {
2167 /* Try to convert input string from ipv6 network. */
2168 len2mask6(mask, &opt->pref_net[opt->pref_net_nb].mask.in6);
2169 opt->pref_net[opt->pref_net_nb].family = AF_INET6;
2170 } else {
2171 /* All network conversions fail, retrun error. */
2172 Alert("parsing [%s:%d]: '%s': invalid network '%s'.\n",
2173 file, linenum, args[cur_arg], p);
2174 err_code |= ERR_ALERT | ERR_FATAL;
2175 goto out;
2176 }
2177 opt->pref_net_nb++;
2178 p = e;
2179 }
2180
2181 cur_arg += 2;
2182 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002183 else if (!strcmp(args[cur_arg], "rise")) {
2184 if (!*args[cur_arg + 1]) {
2185 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2186 file, linenum, args[cur_arg]);
2187 err_code |= ERR_ALERT | ERR_FATAL;
2188 goto out;
2189 }
2190
2191 newsrv->check.rise = atol(args[cur_arg + 1]);
2192 if (newsrv->check.rise <= 0) {
2193 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2194 file, linenum, args[cur_arg]);
2195 err_code |= ERR_ALERT | ERR_FATAL;
2196 goto out;
2197 }
2198
2199 if (newsrv->check.health)
2200 newsrv->check.health = newsrv->check.rise;
2201 cur_arg += 2;
2202 }
2203 else if (!strcmp(args[cur_arg], "fall")) {
2204 newsrv->check.fall = atol(args[cur_arg + 1]);
2205
2206 if (!*args[cur_arg + 1]) {
2207 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2208 file, linenum, args[cur_arg]);
2209 err_code |= ERR_ALERT | ERR_FATAL;
2210 goto out;
2211 }
2212
2213 if (newsrv->check.fall <= 0) {
2214 Alert("parsing [%s:%d]: '%s' has to be > 0.\n",
2215 file, linenum, args[cur_arg]);
2216 err_code |= ERR_ALERT | ERR_FATAL;
2217 goto out;
2218 }
2219
2220 cur_arg += 2;
2221 }
2222 else if (!strcmp(args[cur_arg], "inter")) {
2223 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2224 if (err) {
2225 Alert("parsing [%s:%d] : unexpected character '%c' in 'inter' argument of server %s.\n",
2226 file, linenum, *err, newsrv->id);
2227 err_code |= ERR_ALERT | ERR_FATAL;
2228 goto out;
2229 }
2230 if (val <= 0) {
2231 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2232 file, linenum, val, args[cur_arg], newsrv->id);
2233 err_code |= ERR_ALERT | ERR_FATAL;
2234 goto out;
2235 }
2236 newsrv->check.inter = val;
Olivier Houchard8da5f982017-08-04 18:35:36 +02002237 if (newsrv->srvrq)
2238 newsrv->srvrq->inter = val;
Willy Tarreau272adea2014-03-31 10:39:59 +02002239 cur_arg += 2;
2240 }
2241 else if (!strcmp(args[cur_arg], "fastinter")) {
2242 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2243 if (err) {
2244 Alert("parsing [%s:%d]: unexpected character '%c' in 'fastinter' argument of server %s.\n",
2245 file, linenum, *err, newsrv->id);
2246 err_code |= ERR_ALERT | ERR_FATAL;
2247 goto out;
2248 }
2249 if (val <= 0) {
2250 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2251 file, linenum, val, args[cur_arg], newsrv->id);
2252 err_code |= ERR_ALERT | ERR_FATAL;
2253 goto out;
2254 }
2255 newsrv->check.fastinter = val;
2256 cur_arg += 2;
2257 }
2258 else if (!strcmp(args[cur_arg], "downinter")) {
2259 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2260 if (err) {
2261 Alert("parsing [%s:%d]: unexpected character '%c' in 'downinter' argument of server %s.\n",
2262 file, linenum, *err, newsrv->id);
2263 err_code |= ERR_ALERT | ERR_FATAL;
2264 goto out;
2265 }
2266 if (val <= 0) {
2267 Alert("parsing [%s:%d]: invalid value %d for argument '%s' of server %s.\n",
2268 file, linenum, val, args[cur_arg], newsrv->id);
2269 err_code |= ERR_ALERT | ERR_FATAL;
2270 goto out;
2271 }
2272 newsrv->check.downinter = val;
2273 cur_arg += 2;
2274 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002275 else if (!strcmp(args[cur_arg], "port")) {
2276 newsrv->check.port = atol(args[cur_arg + 1]);
Baptiste Assmann6b453f12016-08-11 23:12:18 +02002277 newsrv->flags |= SRV_F_CHECKPORT;
Willy Tarreau272adea2014-03-31 10:39:59 +02002278 cur_arg += 2;
2279 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002280 else if (!strcmp(args[cur_arg], "weight")) {
2281 int w;
2282 w = atol(args[cur_arg + 1]);
2283 if (w < 0 || w > SRV_UWGHT_MAX) {
2284 Alert("parsing [%s:%d] : weight of server %s is not within 0 and %d (%d).\n",
2285 file, linenum, newsrv->id, SRV_UWGHT_MAX, w);
2286 err_code |= ERR_ALERT | ERR_FATAL;
2287 goto out;
2288 }
2289 newsrv->uweight = newsrv->iweight = w;
2290 cur_arg += 2;
2291 }
2292 else if (!strcmp(args[cur_arg], "minconn")) {
2293 newsrv->minconn = atol(args[cur_arg + 1]);
2294 cur_arg += 2;
2295 }
2296 else if (!strcmp(args[cur_arg], "maxconn")) {
2297 newsrv->maxconn = atol(args[cur_arg + 1]);
2298 cur_arg += 2;
2299 }
2300 else if (!strcmp(args[cur_arg], "maxqueue")) {
2301 newsrv->maxqueue = atol(args[cur_arg + 1]);
2302 cur_arg += 2;
2303 }
2304 else if (!strcmp(args[cur_arg], "slowstart")) {
2305 /* slowstart is stored in seconds */
2306 const char *err = parse_time_err(args[cur_arg + 1], &val, TIME_UNIT_MS);
2307 if (err) {
2308 Alert("parsing [%s:%d] : unexpected character '%c' in 'slowstart' argument of server %s.\n",
2309 file, linenum, *err, newsrv->id);
2310 err_code |= ERR_ALERT | ERR_FATAL;
2311 goto out;
2312 }
2313 newsrv->slowstart = (val + 999) / 1000;
2314 cur_arg += 2;
2315 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002316 else if (!strcmp(args[cur_arg], "on-error")) {
2317 if (!strcmp(args[cur_arg + 1], "fastinter"))
2318 newsrv->onerror = HANA_ONERR_FASTINTER;
2319 else if (!strcmp(args[cur_arg + 1], "fail-check"))
2320 newsrv->onerror = HANA_ONERR_FAILCHK;
2321 else if (!strcmp(args[cur_arg + 1], "sudden-death"))
2322 newsrv->onerror = HANA_ONERR_SUDDTH;
2323 else if (!strcmp(args[cur_arg + 1], "mark-down"))
2324 newsrv->onerror = HANA_ONERR_MARKDWN;
2325 else {
2326 Alert("parsing [%s:%d]: '%s' expects one of 'fastinter', "
2327 "'fail-check', 'sudden-death' or 'mark-down' but got '%s'\n",
2328 file, linenum, args[cur_arg], args[cur_arg + 1]);
2329 err_code |= ERR_ALERT | ERR_FATAL;
2330 goto out;
2331 }
2332
2333 cur_arg += 2;
2334 }
2335 else if (!strcmp(args[cur_arg], "on-marked-down")) {
2336 if (!strcmp(args[cur_arg + 1], "shutdown-sessions"))
2337 newsrv->onmarkeddown = HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS;
2338 else {
2339 Alert("parsing [%s:%d]: '%s' expects 'shutdown-sessions' but got '%s'\n",
2340 file, linenum, args[cur_arg], args[cur_arg + 1]);
2341 err_code |= ERR_ALERT | ERR_FATAL;
2342 goto out;
2343 }
2344
2345 cur_arg += 2;
2346 }
2347 else if (!strcmp(args[cur_arg], "on-marked-up")) {
2348 if (!strcmp(args[cur_arg + 1], "shutdown-backup-sessions"))
2349 newsrv->onmarkedup = HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS;
2350 else {
2351 Alert("parsing [%s:%d]: '%s' expects 'shutdown-backup-sessions' but got '%s'\n",
2352 file, linenum, args[cur_arg], args[cur_arg + 1]);
2353 err_code |= ERR_ALERT | ERR_FATAL;
2354 goto out;
2355 }
2356
2357 cur_arg += 2;
2358 }
2359 else if (!strcmp(args[cur_arg], "error-limit")) {
2360 if (!*args[cur_arg + 1]) {
2361 Alert("parsing [%s:%d]: '%s' expects an integer argument.\n",
2362 file, linenum, args[cur_arg]);
2363 err_code |= ERR_ALERT | ERR_FATAL;
2364 goto out;
2365 }
2366
2367 newsrv->consecutive_errors_limit = atoi(args[cur_arg + 1]);
2368
2369 if (newsrv->consecutive_errors_limit <= 0) {
2370 Alert("parsing [%s:%d]: %s has to be > 0.\n",
2371 file, linenum, args[cur_arg]);
2372 err_code |= ERR_ALERT | ERR_FATAL;
2373 goto out;
2374 }
2375 cur_arg += 2;
2376 }
Frédéric Lécaille8d083ed2017-04-14 15:19:56 +02002377 else if (!strcmp(args[cur_arg], "usesrc")) { /* address to use outside: needs "source" first */
Willy Tarreau272adea2014-03-31 10:39:59 +02002378 Alert("parsing [%s:%d] : '%s' only allowed after a '%s' statement.\n",
2379 file, linenum, "usesrc", "source");
2380 err_code |= ERR_ALERT | ERR_FATAL;
2381 goto out;
KOVACS Krisztianb3e54fe2014-11-17 15:11:45 +01002382 }
Willy Tarreau272adea2014-03-31 10:39:59 +02002383 else {
2384 static int srv_dumped;
2385 struct srv_kw *kw;
2386 char *err;
2387
2388 kw = srv_find_kw(args[cur_arg]);
2389 if (kw) {
2390 char *err = NULL;
2391 int code;
2392
2393 if (!kw->parse) {
2394 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not implemented in this version (check build options).\n",
2395 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002396 if (kw->skip != -1)
2397 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002398 err_code |= ERR_ALERT | ERR_FATAL;
2399 goto out;
2400 }
2401
2402 if (defsrv && !kw->default_ok) {
2403 Alert("parsing [%s:%d] : '%s %s' : '%s' option is not accepted in default-server sections.\n",
2404 file, linenum, args[0], args[1], args[cur_arg]);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002405 if (kw->skip != -1)
2406 cur_arg += 1 + kw->skip ;
Willy Tarreau272adea2014-03-31 10:39:59 +02002407 err_code |= ERR_ALERT;
2408 continue;
2409 }
2410
2411 code = kw->parse(args, &cur_arg, curproxy, newsrv, &err);
2412 err_code |= code;
2413
2414 if (code) {
Frédéric Lécaille9a146de2017-03-20 14:54:41 +01002415 display_parser_err(file, linenum, args, cur_arg, &err);
Willy Tarreau272adea2014-03-31 10:39:59 +02002416 if (code & ERR_FATAL) {
2417 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002418 if (kw->skip != -1)
2419 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002420 goto out;
2421 }
2422 }
2423 free(err);
Frédéric Lécailledfacd692017-04-16 17:14:14 +02002424 if (kw->skip != -1)
2425 cur_arg += 1 + kw->skip;
Willy Tarreau272adea2014-03-31 10:39:59 +02002426 continue;
2427 }
2428
2429 err = NULL;
2430 if (!srv_dumped) {
2431 srv_dump_kws(&err);
2432 indent_msg(&err, 4);
2433 srv_dumped = 1;
2434 }
2435
2436 Alert("parsing [%s:%d] : '%s %s' unknown keyword '%s'.%s%s\n",
2437 file, linenum, args[0], args[1], args[cur_arg],
2438 err ? " Registered keywords :" : "", err ? err : "");
2439 free(err);
2440
2441 err_code |= ERR_ALERT | ERR_FATAL;
2442 goto out;
2443 }
2444 }
2445
Frédéric Lécaille759ea982017-03-30 17:32:36 +02002446 if (!defsrv)
2447 err_code |= server_finalize_init(file, linenum, args, cur_arg, newsrv, curproxy);
2448 if (err_code & ERR_FATAL)
2449 goto out;
Frédéric Lécaille72ed4752017-04-14 13:28:00 +02002450 if (srv_tmpl)
2451 server_template_init(newsrv, curproxy);
Willy Tarreau272adea2014-03-31 10:39:59 +02002452 }
Willy Tarreau07101d52015-09-08 16:16:35 +02002453 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002454 return 0;
2455
2456 out:
Willy Tarreau07101d52015-09-08 16:16:35 +02002457 free(fqdn);
Willy Tarreau272adea2014-03-31 10:39:59 +02002458 free(errmsg);
2459 return err_code;
2460}
2461
Baptiste Assmann19a106d2015-07-08 22:03:56 +02002462/* Returns a pointer to the first server matching either id <id>.
2463 * NULL is returned if no match is found.
2464 * the lookup is performed in the backend <bk>
2465 */
2466struct server *server_find_by_id(struct proxy *bk, int id)
2467{
2468 struct eb32_node *eb32;
2469 struct server *curserver;
2470
2471 if (!bk || (id ==0))
2472 return NULL;
2473
2474 /* <bk> has no backend capabilities, so it can't have a server */
2475 if (!(bk->cap & PR_CAP_BE))
2476 return NULL;
2477
2478 curserver = NULL;
2479
2480 eb32 = eb32_lookup(&bk->conf.used_server_id, id);
2481 if (eb32)
2482 curserver = container_of(eb32, struct server, conf.id);
2483
2484 return curserver;
2485}
2486
2487/* Returns a pointer to the first server matching either name <name>, or id
2488 * if <name> starts with a '#'. NULL is returned if no match is found.
2489 * the lookup is performed in the backend <bk>
2490 */
2491struct server *server_find_by_name(struct proxy *bk, const char *name)
2492{
2493 struct server *curserver;
2494
2495 if (!bk || !name)
2496 return NULL;
2497
2498 /* <bk> has no backend capabilities, so it can't have a server */
2499 if (!(bk->cap & PR_CAP_BE))
2500 return NULL;
2501
2502 curserver = NULL;
2503 if (*name == '#') {
2504 curserver = server_find_by_id(bk, atoi(name + 1));
2505 if (curserver)
2506 return curserver;
2507 }
2508 else {
2509 curserver = bk->srv;
2510
2511 while (curserver && (strcmp(curserver->id, name) != 0))
2512 curserver = curserver->next;
2513
2514 if (curserver)
2515 return curserver;
2516 }
2517
2518 return NULL;
2519}
2520
2521struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff)
2522{
2523 struct server *byname;
2524 struct server *byid;
2525
2526 if (!name && !id)
2527 return NULL;
2528
2529 if (diff)
2530 *diff = 0;
2531
2532 byname = byid = NULL;
2533
2534 if (name) {
2535 byname = server_find_by_name(bk, name);
2536 if (byname && (!id || byname->puid == id))
2537 return byname;
2538 }
2539
2540 /* remaining possibilities :
2541 * - name not set
2542 * - name set but not found
2543 * - name found but ID doesn't match
2544 */
2545 if (id) {
2546 byid = server_find_by_id(bk, id);
2547 if (byid) {
2548 if (byname) {
2549 /* use id only if forced by configuration */
2550 if (byid->flags & SRV_F_FORCED_ID) {
2551 if (diff)
2552 *diff |= 2;
2553 return byid;
2554 }
2555 else {
2556 if (diff)
2557 *diff |= 1;
2558 return byname;
2559 }
2560 }
2561
2562 /* remaining possibilities:
2563 * - name not set
2564 * - name set but not found
2565 */
2566 if (name && diff)
2567 *diff |= 2;
2568 return byid;
2569 }
2570
2571 /* id bot found */
2572 if (byname) {
2573 if (diff)
2574 *diff |= 1;
2575 return byname;
2576 }
2577 }
2578
2579 return NULL;
2580}
2581
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002582/* Update a server state using the parameters available in the params list */
2583static void srv_update_state(struct server *srv, int version, char **params)
2584{
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002585 char *p;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002586 struct chunk *msg;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002587
2588 /* fields since version 1
2589 * and common to all other upcoming versions
2590 */
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002591 enum srv_state srv_op_state;
2592 enum srv_admin srv_admin_state;
2593 unsigned srv_uweight, srv_iweight;
2594 unsigned long srv_last_time_change;
2595 short srv_check_status;
2596 enum chk_result srv_check_result;
2597 int srv_check_health;
2598 int srv_check_state, srv_agent_state;
2599 int bk_f_forced_id;
2600 int srv_f_forced_id;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002601 int fqdn_set_by_cli;
2602 const char *fqdn;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002603 const char *port_str;
2604 unsigned int port;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002605
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002606 fqdn = NULL;
Frédéric Lécaille31694712017-08-01 08:47:19 +02002607 port = 0;
Willy Tarreau31138fa2015-09-29 18:38:47 +02002608 msg = get_trash_chunk();
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002609 switch (version) {
2610 case 1:
2611 /*
2612 * now we can proceed with server's state update:
2613 * srv_addr: params[0]
2614 * srv_op_state: params[1]
2615 * srv_admin_state: params[2]
2616 * srv_uweight: params[3]
2617 * srv_iweight: params[4]
2618 * srv_last_time_change: params[5]
2619 * srv_check_status: params[6]
2620 * srv_check_result: params[7]
2621 * srv_check_health: params[8]
2622 * srv_check_state: params[9]
2623 * srv_agent_state: params[10]
2624 * bk_f_forced_id: params[11]
2625 * srv_f_forced_id: params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002626 * srv_fqdn: params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02002627 * srv_port: params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002628 */
2629
2630 /* validating srv_op_state */
2631 p = NULL;
2632 errno = 0;
2633 srv_op_state = strtol(params[1], &p, 10);
2634 if ((p == params[1]) || errno == EINVAL || errno == ERANGE ||
2635 (srv_op_state != SRV_ST_STOPPED &&
2636 srv_op_state != SRV_ST_STARTING &&
2637 srv_op_state != SRV_ST_RUNNING &&
2638 srv_op_state != SRV_ST_STOPPING)) {
2639 chunk_appendf(msg, ", invalid srv_op_state value '%s'", params[1]);
2640 }
2641
2642 /* validating srv_admin_state */
2643 p = NULL;
2644 errno = 0;
2645 srv_admin_state = strtol(params[2], &p, 10);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002646 fqdn_set_by_cli = !!(srv_admin_state & SRV_ADMF_HMAINT);
Willy Tarreau757478e2016-11-03 19:22:19 +01002647
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002648 /* inherited statuses will be recomputed later.
2649 * Also disable SRV_ADMF_HMAINT flag (set from stats socket fqdn).
2650 */
2651 srv_admin_state &= ~SRV_ADMF_IDRAIN & ~SRV_ADMF_IMAINT & ~SRV_ADMF_HMAINT;
Willy Tarreau757478e2016-11-03 19:22:19 +01002652
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002653 if ((p == params[2]) || errno == EINVAL || errno == ERANGE ||
2654 (srv_admin_state != 0 &&
2655 srv_admin_state != SRV_ADMF_FMAINT &&
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002656 srv_admin_state != SRV_ADMF_CMAINT &&
2657 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FMAINT) &&
Willy Tarreaue1bde142016-11-03 18:33:25 +01002658 srv_admin_state != (SRV_ADMF_CMAINT | SRV_ADMF_FDRAIN) &&
Willy Tarreau757478e2016-11-03 19:22:19 +01002659 srv_admin_state != SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002660 chunk_appendf(msg, ", invalid srv_admin_state value '%s'", params[2]);
2661 }
2662
2663 /* validating srv_uweight */
2664 p = NULL;
2665 errno = 0;
2666 srv_uweight = strtol(params[3], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002667 if ((p == params[3]) || errno == EINVAL || errno == ERANGE || (srv_uweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002668 chunk_appendf(msg, ", invalid srv_uweight value '%s'", params[3]);
2669
2670 /* validating srv_iweight */
2671 p = NULL;
2672 errno = 0;
2673 srv_iweight = strtol(params[4], &p, 10);
Willy Tarreaue1aebb22015-09-29 18:32:57 +02002674 if ((p == params[4]) || errno == EINVAL || errno == ERANGE || (srv_iweight > SRV_UWGHT_MAX))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002675 chunk_appendf(msg, ", invalid srv_iweight value '%s'", params[4]);
2676
2677 /* validating srv_last_time_change */
2678 p = NULL;
2679 errno = 0;
2680 srv_last_time_change = strtol(params[5], &p, 10);
2681 if ((p == params[5]) || errno == EINVAL || errno == ERANGE)
2682 chunk_appendf(msg, ", invalid srv_last_time_change value '%s'", params[5]);
2683
2684 /* validating srv_check_status */
2685 p = NULL;
2686 errno = 0;
2687 srv_check_status = strtol(params[6], &p, 10);
2688 if (p == params[6] || errno == EINVAL || errno == ERANGE ||
2689 (srv_check_status >= HCHK_STATUS_SIZE))
2690 chunk_appendf(msg, ", invalid srv_check_status value '%s'", params[6]);
2691
2692 /* validating srv_check_result */
2693 p = NULL;
2694 errno = 0;
2695 srv_check_result = strtol(params[7], &p, 10);
2696 if ((p == params[7]) || errno == EINVAL || errno == ERANGE ||
2697 (srv_check_result != CHK_RES_UNKNOWN &&
2698 srv_check_result != CHK_RES_NEUTRAL &&
2699 srv_check_result != CHK_RES_FAILED &&
2700 srv_check_result != CHK_RES_PASSED &&
2701 srv_check_result != CHK_RES_CONDPASS)) {
2702 chunk_appendf(msg, ", invalid srv_check_result value '%s'", params[7]);
2703 }
2704
2705 /* validating srv_check_health */
2706 p = NULL;
2707 errno = 0;
2708 srv_check_health = strtol(params[8], &p, 10);
2709 if (p == params[8] || errno == EINVAL || errno == ERANGE)
2710 chunk_appendf(msg, ", invalid srv_check_health value '%s'", params[8]);
2711
2712 /* validating srv_check_state */
2713 p = NULL;
2714 errno = 0;
2715 srv_check_state = strtol(params[9], &p, 10);
2716 if (p == params[9] || errno == EINVAL || errno == ERANGE ||
2717 (srv_check_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2718 chunk_appendf(msg, ", invalid srv_check_state value '%s'", params[9]);
2719
2720 /* validating srv_agent_state */
2721 p = NULL;
2722 errno = 0;
2723 srv_agent_state = strtol(params[10], &p, 10);
2724 if (p == params[10] || errno == EINVAL || errno == ERANGE ||
2725 (srv_agent_state & ~(CHK_ST_INPROGRESS | CHK_ST_CONFIGURED | CHK_ST_ENABLED | CHK_ST_PAUSED | CHK_ST_AGENT)))
2726 chunk_appendf(msg, ", invalid srv_agent_state value '%s'", params[10]);
2727
2728 /* validating bk_f_forced_id */
2729 p = NULL;
2730 errno = 0;
2731 bk_f_forced_id = strtol(params[11], &p, 10);
2732 if (p == params[11] || errno == EINVAL || errno == ERANGE || !((bk_f_forced_id == 0) || (bk_f_forced_id == 1)))
2733 chunk_appendf(msg, ", invalid bk_f_forced_id value '%s'", params[11]);
2734
2735 /* validating srv_f_forced_id */
2736 p = NULL;
2737 errno = 0;
2738 srv_f_forced_id = strtol(params[12], &p, 10);
2739 if (p == params[12] || errno == EINVAL || errno == ERANGE || !((srv_f_forced_id == 0) || (srv_f_forced_id == 1)))
2740 chunk_appendf(msg, ", invalid srv_f_forced_id value '%s'", params[12]);
2741
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002742 /* validating srv_fqdn */
2743 fqdn = params[13];
2744 if (fqdn && *fqdn == '-')
2745 fqdn = NULL;
2746 if (fqdn && (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn))) {
2747 chunk_appendf(msg, ", invalid srv_fqdn value '%s'", params[13]);
2748 fqdn = NULL;
2749 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002750
Frédéric Lécaille31694712017-08-01 08:47:19 +02002751 port_str = params[14];
2752 if (port_str) {
2753 port = strl2uic(port_str, strlen(port_str));
2754 if (port > USHRT_MAX) {
2755 chunk_appendf(msg, ", invalid srv_port value '%s'", port_str);
2756 port_str = NULL;
2757 }
2758 }
2759
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002760 /* don't apply anything if one error has been detected */
Willy Tarreau31138fa2015-09-29 18:38:47 +02002761 if (msg->len)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002762 goto out;
2763
2764 /* recover operational state and apply it to this server
2765 * and all servers tracking this one */
2766 switch (srv_op_state) {
2767 case SRV_ST_STOPPED:
2768 srv->check.health = 0;
2769 srv_set_stopped(srv, "changed from server-state after a reload");
2770 break;
2771 case SRV_ST_STARTING:
Emeric Brun52a91d32017-08-31 14:41:55 +02002772 srv->next_state = srv_op_state;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002773 break;
2774 case SRV_ST_STOPPING:
2775 srv->check.health = srv->check.rise + srv->check.fall - 1;
2776 srv_set_stopping(srv, "changed from server-state after a reload");
2777 break;
2778 case SRV_ST_RUNNING:
2779 srv->check.health = srv->check.rise + srv->check.fall - 1;
2780 srv_set_running(srv, "");
2781 break;
2782 }
2783
2784 /* When applying server state, the following rules apply:
2785 * - in case of a configuration change, we apply the setting from the new
2786 * configuration, regardless of old running state
2787 * - if no configuration change, we apply old running state only if old running
2788 * state is different from new configuration state
2789 */
2790 /* configuration has changed */
Emeric Brun52a91d32017-08-31 14:41:55 +02002791 if ((srv_admin_state & SRV_ADMF_CMAINT) != (srv->next_admin & SRV_ADMF_CMAINT)) {
2792 if (srv->next_admin & SRV_ADMF_CMAINT)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002793 srv_adm_set_maint(srv);
2794 else
2795 srv_adm_set_ready(srv);
2796 }
2797 /* configuration is the same, let's compate old running state and new conf state */
2798 else {
Emeric Brun52a91d32017-08-31 14:41:55 +02002799 if (srv_admin_state & SRV_ADMF_FMAINT && !(srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002800 srv_adm_set_maint(srv);
Emeric Brun52a91d32017-08-31 14:41:55 +02002801 else if (!(srv_admin_state & SRV_ADMF_FMAINT) && (srv->next_admin & SRV_ADMF_CMAINT))
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002802 srv_adm_set_ready(srv);
2803 }
2804 /* apply drain mode if server is currently enabled */
Emeric Brun52a91d32017-08-31 14:41:55 +02002805 if (!(srv->next_admin & SRV_ADMF_FMAINT) && (srv_admin_state & SRV_ADMF_FDRAIN)) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002806 /* The SRV_ADMF_FDRAIN flag is inherited when srv->iweight is 0
Willy Tarreau22cace22016-11-03 18:19:49 +01002807 * (srv->iweight is the weight set up in configuration).
2808 * There are two possible reasons for FDRAIN to have been present :
2809 * - previous config weight was zero
2810 * - "set server b/s drain" was sent to the CLI
2811 *
2812 * In the first case, we simply want to drop this drain state
2813 * if the new weight is not zero anymore, meaning the administrator
2814 * has intentionally turned the weight back to a positive value to
2815 * enable the server again after an operation. In the second case,
2816 * the drain state was forced on the CLI regardless of the config's
2817 * weight so we don't want a change to the config weight to lose this
2818 * status. What this means is :
2819 * - if previous weight was 0 and new one is >0, drop the DRAIN state.
2820 * - if the previous weight was >0, keep it.
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002821 */
Willy Tarreau22cace22016-11-03 18:19:49 +01002822 if (srv_iweight > 0 || srv->iweight == 0)
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002823 srv_adm_set_drain(srv);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002824 }
2825
2826 srv->last_change = date.tv_sec - srv_last_time_change;
2827 srv->check.status = srv_check_status;
2828 srv->check.result = srv_check_result;
2829 srv->check.health = srv_check_health;
2830
2831 /* Only case we want to apply is removing ENABLED flag which could have been
2832 * done by the "disable health" command over the stats socket
2833 */
2834 if ((srv->check.state & CHK_ST_CONFIGURED) &&
2835 (srv_check_state & CHK_ST_CONFIGURED) &&
2836 !(srv_check_state & CHK_ST_ENABLED))
2837 srv->check.state &= ~CHK_ST_ENABLED;
2838
2839 /* Only case we want to apply is removing ENABLED flag which could have been
2840 * done by the "disable agent" command over the stats socket
2841 */
2842 if ((srv->agent.state & CHK_ST_CONFIGURED) &&
2843 (srv_agent_state & CHK_ST_CONFIGURED) &&
2844 !(srv_agent_state & CHK_ST_ENABLED))
2845 srv->agent.state &= ~CHK_ST_ENABLED;
2846
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002847 /* We want to apply the previous 'running' weight (srv_uweight) only if there
2848 * was no change in the configuration: both previous and new iweight are equals
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002849 *
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002850 * It means that a configuration file change has precedence over a unix socket change
2851 * for server's weight
2852 *
2853 * by default, HAProxy applies the following weight when parsing the configuration
2854 * srv->uweight = srv->iweight
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002855 */
Baptiste Assmann6076d1c2015-09-17 22:53:59 +02002856 if (srv_iweight == srv->iweight) {
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002857 srv->uweight = srv_uweight;
2858 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002859 server_recalc_eweight(srv);
2860
Willy Tarreaue5a60682016-11-09 14:54:53 +01002861 /* load server IP address */
2862 srv->lastaddr = strdup(params[0]);
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002863
2864 if (fqdn && srv->hostname) {
2865 if (!strcmp(srv->hostname, fqdn)) {
2866 /* Here we reset the 'set from stats socket FQDN' flag
2867 * to support such transitions:
2868 * Let's say initial FQDN value is foo1 (in configuration file).
2869 * - FQDN changed from stats socket, from foo1 to foo2 value,
2870 * - FQDN changed again from file configuration (with the same previous value
2871 set from stats socket, from foo1 to foo2 value),
2872 * - reload for any other reason than a FQDN modification,
2873 * the configuration file FQDN matches the fqdn server state file value.
2874 * So we must reset the 'set from stats socket FQDN' flag to be consistent with
2875 * any futher FQDN modification.
2876 */
Emeric Brun52a91d32017-08-31 14:41:55 +02002877 srv->next_admin &= ~SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002878 }
2879 else {
2880 /* If the FDQN has been changed from stats socket,
2881 * apply fqdn state file value (which is the value set
2882 * from stats socket).
2883 */
2884 if (fqdn_set_by_cli) {
2885 srv_set_fqdn(srv, fqdn);
Emeric Brun52a91d32017-08-31 14:41:55 +02002886 srv->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002887 }
2888 }
2889 }
2890
Frédéric Lécaille31694712017-08-01 08:47:19 +02002891 if (port_str)
2892 srv->svc_port = port;
2893
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002894 break;
2895 default:
2896 chunk_appendf(msg, ", version '%d' not supported", version);
2897 }
2898
2899 out:
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002900 if (msg->len) {
2901 chunk_appendf(msg, "\n");
Willy Tarreau31138fa2015-09-29 18:38:47 +02002902 Warning("server-state application failed for server '%s/%s'%s",
2903 srv->proxy->id, srv->id, msg->str);
Baptiste Assmann0821bb92016-01-21 00:20:50 +01002904 }
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002905}
2906
2907/* This function parses all the proxies and only take care of the backends (since we're looking for server)
2908 * For each proxy, it does the following:
2909 * - opens its server state file (either one or local one)
2910 * - read whole file, line by line
2911 * - analyse each line to check if it matches our current backend:
2912 * - backend name matches
2913 * - backend id matches if id is forced and name doesn't match
2914 * - if the server pointed by the line is found, then state is applied
2915 *
2916 * If the running backend uuid or id differs from the state file, then HAProxy reports
2917 * a warning.
2918 */
2919void apply_server_state(void)
2920{
2921 char *cur, *end;
2922 char mybuf[SRV_STATE_LINE_MAXLEN];
2923 int mybuflen;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02002924 char *params[SRV_STATE_FILE_MAX_FIELDS] = {0};
2925 char *srv_params[SRV_STATE_FILE_MAX_FIELDS] = {0};
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02002926 int arg, srv_arg, version, diff;
2927 FILE *f;
2928 char *filepath;
2929 char globalfilepath[MAXPATHLEN + 1];
2930 char localfilepath[MAXPATHLEN + 1];
2931 int len, fileopenerr, globalfilepathlen, localfilepathlen;
2932 extern struct proxy *proxy;
2933 struct proxy *curproxy, *bk;
2934 struct server *srv;
2935
2936 globalfilepathlen = 0;
2937 /* create the globalfilepath variable */
2938 if (global.server_state_file) {
2939 /* absolute path or no base directory provided */
2940 if ((global.server_state_file[0] == '/') || (!global.server_state_base)) {
2941 len = strlen(global.server_state_file);
2942 if (len > MAXPATHLEN) {
2943 globalfilepathlen = 0;
2944 goto globalfileerror;
2945 }
2946 memcpy(globalfilepath, global.server_state_file, len);
2947 globalfilepath[len] = '\0';
2948 globalfilepathlen = len;
2949 }
2950 else if (global.server_state_base) {
2951 len = strlen(global.server_state_base);
2952 globalfilepathlen += len;
2953
2954 if (globalfilepathlen > MAXPATHLEN) {
2955 globalfilepathlen = 0;
2956 goto globalfileerror;
2957 }
2958 strncpy(globalfilepath, global.server_state_base, len);
2959 globalfilepath[globalfilepathlen] = 0;
2960
2961 /* append a slash if needed */
2962 if (!globalfilepathlen || globalfilepath[globalfilepathlen - 1] != '/') {
2963 if (globalfilepathlen + 1 > MAXPATHLEN) {
2964 globalfilepathlen = 0;
2965 goto globalfileerror;
2966 }
2967 globalfilepath[globalfilepathlen++] = '/';
2968 }
2969
2970 len = strlen(global.server_state_file);
2971 if (globalfilepathlen + len > MAXPATHLEN) {
2972 globalfilepathlen = 0;
2973 goto globalfileerror;
2974 }
2975 memcpy(globalfilepath + globalfilepathlen, global.server_state_file, len);
2976 globalfilepathlen += len;
2977 globalfilepath[globalfilepathlen++] = 0;
2978 }
2979 }
2980 globalfileerror:
2981 if (globalfilepathlen == 0)
2982 globalfilepath[0] = '\0';
2983
2984 /* read servers state from local file */
2985 for (curproxy = proxy; curproxy != NULL; curproxy = curproxy->next) {
2986 /* servers are only in backends */
2987 if (!(curproxy->cap & PR_CAP_BE))
2988 continue;
2989 fileopenerr = 0;
2990 filepath = NULL;
2991
2992 /* search server state file path and name */
2993 switch (curproxy->load_server_state_from_file) {
2994 /* read servers state from global file */
2995 case PR_SRV_STATE_FILE_GLOBAL:
2996 /* there was an error while generating global server state file path */
2997 if (globalfilepathlen == 0)
2998 continue;
2999 filepath = globalfilepath;
3000 fileopenerr = 1;
3001 break;
3002 /* this backend has its own file */
3003 case PR_SRV_STATE_FILE_LOCAL:
3004 localfilepathlen = 0;
3005 localfilepath[0] = '\0';
3006 len = 0;
3007 /* create the localfilepath variable */
3008 /* absolute path or no base directory provided */
3009 if ((curproxy->server_state_file_name[0] == '/') || (!global.server_state_base)) {
3010 len = strlen(curproxy->server_state_file_name);
3011 if (len > MAXPATHLEN) {
3012 localfilepathlen = 0;
3013 goto localfileerror;
3014 }
3015 memcpy(localfilepath, curproxy->server_state_file_name, len);
3016 localfilepath[len] = '\0';
3017 localfilepathlen = len;
3018 }
3019 else if (global.server_state_base) {
3020 len = strlen(global.server_state_base);
3021 localfilepathlen += len;
3022
3023 if (localfilepathlen > MAXPATHLEN) {
3024 localfilepathlen = 0;
3025 goto localfileerror;
3026 }
3027 strncpy(localfilepath, global.server_state_base, len);
3028 localfilepath[localfilepathlen] = 0;
3029
3030 /* append a slash if needed */
3031 if (!localfilepathlen || localfilepath[localfilepathlen - 1] != '/') {
3032 if (localfilepathlen + 1 > MAXPATHLEN) {
3033 localfilepathlen = 0;
3034 goto localfileerror;
3035 }
3036 localfilepath[localfilepathlen++] = '/';
3037 }
3038
3039 len = strlen(curproxy->server_state_file_name);
3040 if (localfilepathlen + len > MAXPATHLEN) {
3041 localfilepathlen = 0;
3042 goto localfileerror;
3043 }
3044 memcpy(localfilepath + localfilepathlen, curproxy->server_state_file_name, len);
3045 localfilepathlen += len;
3046 localfilepath[localfilepathlen++] = 0;
3047 }
3048 filepath = localfilepath;
3049 localfileerror:
3050 if (localfilepathlen == 0)
3051 localfilepath[0] = '\0';
3052
3053 break;
3054 case PR_SRV_STATE_FILE_NONE:
3055 default:
3056 continue;
3057 }
3058
3059 /* preload global state file */
3060 errno = 0;
3061 f = fopen(filepath, "r");
3062 if (errno && fileopenerr)
3063 Warning("Can't open server state file '%s': %s\n", filepath, strerror(errno));
3064 if (!f)
3065 continue;
3066
3067 mybuf[0] = '\0';
3068 mybuflen = 0;
3069 version = 0;
3070
3071 /* first character of first line of the file must contain the version of the export */
Dragan Dosencf4fb032015-11-04 23:03:26 +01003072 if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
3073 Warning("Can't read first line of the server state file '%s'\n", filepath);
3074 goto fileclose;
3075 }
3076
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003077 cur = mybuf;
3078 version = atoi(cur);
3079 if ((version < SRV_STATE_FILE_VERSION_MIN) ||
3080 (version > SRV_STATE_FILE_VERSION_MAX))
Dragan Dosencf4fb032015-11-04 23:03:26 +01003081 goto fileclose;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003082
3083 while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
3084 int bk_f_forced_id = 0;
3085 int check_id = 0;
3086 int check_name = 0;
3087
3088 mybuflen = strlen(mybuf);
3089 cur = mybuf;
3090 end = cur + mybuflen;
3091
3092 bk = NULL;
3093 srv = NULL;
3094
3095 /* we need at least one character */
3096 if (mybuflen == 0)
3097 continue;
3098
3099 /* ignore blank characters at the beginning of the line */
3100 while (isspace(*cur))
3101 ++cur;
3102
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003103 /* Ignore empty or commented lines */
3104 if (cur == end || *cur == '#')
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003105 continue;
3106
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003107 /* truncated lines */
3108 if (mybuf[mybuflen - 1] != '\n') {
3109 Warning("server-state file '%s': truncated line\n", filepath);
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003110 continue;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003111 }
3112
3113 /* Removes trailing '\n' */
3114 mybuf[mybuflen - 1] = '\0';
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003115
3116 /* we're now ready to move the line into *srv_params[] */
3117 params[0] = cur;
3118 arg = 1;
3119 srv_arg = 0;
3120 while (*cur && arg < SRV_STATE_FILE_MAX_FIELDS) {
3121 if (isspace(*cur)) {
3122 *cur = '\0';
3123 ++cur;
3124 while (isspace(*cur))
3125 ++cur;
3126 switch (version) {
3127 case 1:
3128 /*
3129 * srv_addr: params[4] => srv_params[0]
3130 * srv_op_state: params[5] => srv_params[1]
3131 * srv_admin_state: params[6] => srv_params[2]
3132 * srv_uweight: params[7] => srv_params[3]
3133 * srv_iweight: params[8] => srv_params[4]
3134 * srv_last_time_change: params[9] => srv_params[5]
3135 * srv_check_status: params[10] => srv_params[6]
3136 * srv_check_result: params[11] => srv_params[7]
3137 * srv_check_health: params[12] => srv_params[8]
3138 * srv_check_state: params[13] => srv_params[9]
3139 * srv_agent_state: params[14] => srv_params[10]
3140 * bk_f_forced_id: params[15] => srv_params[11]
3141 * srv_f_forced_id: params[16] => srv_params[12]
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003142 * srv_fqdn: params[17] => srv_params[13]
Frédéric Lécaille31694712017-08-01 08:47:19 +02003143 * srv_port: params[18] => srv_params[14]
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003144 */
3145 if (arg >= 4) {
3146 srv_params[srv_arg] = cur;
3147 ++srv_arg;
3148 }
3149 break;
3150 }
3151
3152 params[arg] = cur;
3153 ++arg;
3154 }
3155 else {
3156 ++cur;
3157 }
3158 }
3159
3160 /* if line is incomplete line, then ignore it.
3161 * otherwise, update useful flags */
3162 switch (version) {
3163 case 1:
3164 if (arg < SRV_STATE_FILE_NB_FIELDS_VERSION_1)
3165 continue;
3166 bk_f_forced_id = (atoi(params[15]) & PR_O_FORCED_ID);
3167 check_id = (atoi(params[0]) == curproxy->uuid);
3168 check_name = (strcmp(curproxy->id, params[1]) == 0);
3169 break;
3170 }
3171
3172 diff = 0;
3173 bk = curproxy;
3174
3175 /* if backend can't be found, let's continue */
3176 if (!check_id && !check_name)
3177 continue;
3178 else if (!check_id && check_name) {
3179 Warning("backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3180 send_log(bk, LOG_NOTICE, "backend ID mismatch: from server state file: '%s', from running config '%d'\n", params[0], bk->uuid);
3181 }
3182 else if (check_id && !check_name) {
3183 Warning("backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3184 send_log(bk, LOG_NOTICE, "backend name mismatch: from server state file: '%s', from running config '%s'\n", params[1], bk->id);
3185 /* if name doesn't match, we still want to update curproxy if the backend id
3186 * was forced in previous the previous configuration */
3187 if (!bk_f_forced_id)
3188 continue;
3189 }
3190
3191 /* look for the server by its id: param[2] */
3192 /* else look for the server by its name: param[3] */
3193 diff = 0;
3194 srv = server_find_best_match(bk, params[3], atoi(params[2]), &diff);
3195
3196 if (!srv) {
3197 /* if no server found, then warning and continue with next line */
3198 Warning("can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3199 params[3], params[2], params[0], params[1]);
3200 send_log(bk, LOG_NOTICE, "can't find server '%s' with id '%s' in backend with id '%s' or name '%s'\n",
3201 params[3], params[2], params[0], params[1]);
3202 continue;
3203 }
3204 else if (diff & PR_FBM_MISMATCH_ID) {
3205 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);
3206 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 +02003207 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003208 }
3209 else if (diff & PR_FBM_MISMATCH_NAME) {
3210 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);
3211 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 +02003212 continue;
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003213 }
3214
3215 /* now we can proceed with server's state update */
3216 srv_update_state(srv, version, srv_params);
3217 }
Dragan Dosencf4fb032015-11-04 23:03:26 +01003218fileclose:
Baptiste Assmanne11cfcd2015-08-19 16:44:03 +02003219 fclose(f);
3220 }
3221}
3222
Simon Horman7d09b9a2013-02-12 10:45:51 +09003223/*
Baptiste Assmann14e40142015-04-14 01:13:07 +02003224 * update a server's current IP address.
3225 * ip is a pointer to the new IP address, whose address family is ip_sin_family.
3226 * ip is in network format.
3227 * updater is a string which contains an information about the requester of the update.
3228 * updater is used if not NULL.
3229 *
3230 * A log line and a stderr warning message is generated based on server's backend options.
3231 */
Thierry Fournierd35b7a62016-02-24 08:23:22 +01003232int update_server_addr(struct server *s, void *ip, int ip_sin_family, const char *updater)
Baptiste Assmann14e40142015-04-14 01:13:07 +02003233{
3234 /* generates a log line and a warning on stderr */
3235 if (1) {
3236 /* book enough space for both IPv4 and IPv6 */
3237 char oldip[INET6_ADDRSTRLEN];
3238 char newip[INET6_ADDRSTRLEN];
3239
3240 memset(oldip, '\0', INET6_ADDRSTRLEN);
3241 memset(newip, '\0', INET6_ADDRSTRLEN);
3242
3243 /* copy old IP address in a string */
3244 switch (s->addr.ss_family) {
3245 case AF_INET:
3246 inet_ntop(s->addr.ss_family, &((struct sockaddr_in *)&s->addr)->sin_addr, oldip, INET_ADDRSTRLEN);
3247 break;
3248 case AF_INET6:
3249 inet_ntop(s->addr.ss_family, &((struct sockaddr_in6 *)&s->addr)->sin6_addr, oldip, INET6_ADDRSTRLEN);
3250 break;
3251 };
3252
3253 /* copy new IP address in a string */
3254 switch (ip_sin_family) {
3255 case AF_INET:
3256 inet_ntop(ip_sin_family, ip, newip, INET_ADDRSTRLEN);
3257 break;
3258 case AF_INET6:
3259 inet_ntop(ip_sin_family, ip, newip, INET6_ADDRSTRLEN);
3260 break;
3261 };
3262
3263 /* save log line into a buffer */
3264 chunk_printf(&trash, "%s/%s changed its IP from %s to %s by %s",
3265 s->proxy->id, s->id, oldip, newip, updater);
3266
3267 /* write the buffer on stderr */
3268 Warning("%s.\n", trash.str);
3269
3270 /* send a log */
3271 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3272 }
3273
3274 /* save the new IP family */
3275 s->addr.ss_family = ip_sin_family;
3276 /* save the new IP address */
3277 switch (ip_sin_family) {
3278 case AF_INET:
Willy Tarreaueec1d382016-07-13 11:59:39 +02003279 memcpy(&((struct sockaddr_in *)&s->addr)->sin_addr.s_addr, ip, 4);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003280 break;
3281 case AF_INET6:
3282 memcpy(((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr, ip, 16);
3283 break;
3284 };
Olivier Houchard4e694042017-03-14 20:01:29 +01003285 srv_set_dyncookie(s);
Baptiste Assmann14e40142015-04-14 01:13:07 +02003286
3287 return 0;
3288}
3289
3290/*
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003291 * This function update a server's addr and port only for AF_INET and AF_INET6 families.
3292 *
3293 * Caller can pass its name through <updater> to get it integrated in the response message
3294 * returned by the function.
3295 *
3296 * The function first does the following, in that order:
3297 * - validates the new addr and/or port
3298 * - checks if an update is required (new IP or port is different than current ones)
3299 * - checks the update is allowed:
3300 * - don't switch from/to a family other than AF_INET4 and AF_INET6
3301 * - allow all changes if no CHECKS are configured
3302 * - if CHECK is configured:
3303 * - if switch to port map (SRV_F_MAPPORTS), ensure health check have their own ports
3304 * - applies required changes to both ADDR and PORT if both 'required' and 'allowed'
3305 * conditions are met
3306 */
3307const char *update_server_addr_port(struct server *s, const char *addr, const char *port, char *updater)
3308{
3309 struct sockaddr_storage sa;
3310 int ret, port_change_required;
3311 char current_addr[INET6_ADDRSTRLEN];
David Carlier327298c2016-11-20 10:42:38 +00003312 uint16_t current_port, new_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003313 struct chunk *msg;
Olivier Houchard4e694042017-03-14 20:01:29 +01003314 int changed = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003315
3316 msg = get_trash_chunk();
3317 chunk_reset(msg);
3318
3319 if (addr) {
3320 memset(&sa, 0, sizeof(struct sockaddr_storage));
3321 if (str2ip2(addr, &sa, 0) == NULL) {
3322 chunk_printf(msg, "Invalid addr '%s'", addr);
3323 goto out;
3324 }
3325
3326 /* changes are allowed on AF_INET* families only */
3327 if ((sa.ss_family != AF_INET) && (sa.ss_family != AF_INET6)) {
3328 chunk_printf(msg, "Update to families other than AF_INET and AF_INET6 supported only through configuration file");
3329 goto out;
3330 }
3331
3332 /* collecting data currently setup */
3333 memset(current_addr, '\0', sizeof(current_addr));
3334 ret = addr_to_str(&s->addr, current_addr, sizeof(current_addr));
3335 /* changes are allowed on AF_INET* families only */
3336 if ((ret != AF_INET) && (ret != AF_INET6)) {
3337 chunk_printf(msg, "Update for the current server address family is only supported through configuration file");
3338 goto out;
3339 }
3340
3341 /* applying ADDR changes if required and allowed
3342 * ipcmp returns 0 when both ADDR are the same
3343 */
3344 if (ipcmp(&s->addr, &sa) == 0) {
3345 chunk_appendf(msg, "no need to change the addr");
3346 goto port;
3347 }
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003348 ipcpy(&sa, &s->addr);
Olivier Houchard4e694042017-03-14 20:01:29 +01003349 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003350
3351 /* we also need to update check's ADDR only if it uses the server's one */
3352 if ((s->check.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_CHECKADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003353 ipcpy(&sa, &s->check.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003354 }
3355
3356 /* we also need to update agent ADDR only if it use the server's one */
3357 if ((s->agent.state & CHK_ST_CONFIGURED) && (s->flags & SRV_F_AGENTADDR)) {
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003358 ipcpy(&sa, &s->agent.addr);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003359 }
3360
3361 /* update report for caller */
3362 chunk_printf(msg, "IP changed from '%s' to '%s'", current_addr, addr);
3363 }
3364
3365 port:
3366 if (port) {
3367 char sign = '\0';
3368 char *endptr;
3369
3370 if (addr)
3371 chunk_appendf(msg, ", ");
3372
3373 /* collecting data currently setup */
Willy Tarreau04276f32017-01-06 17:41:29 +01003374 current_port = s->svc_port;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003375
3376 /* check if PORT change is required */
3377 port_change_required = 0;
3378
3379 sign = *port;
Ryabin Sergey77ee7522017-01-11 19:39:55 +04003380 errno = 0;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003381 new_port = strtol(port, &endptr, 10);
3382 if ((errno != 0) || (port == endptr)) {
3383 chunk_appendf(msg, "problem converting port '%s' to an int", port);
3384 goto out;
3385 }
3386
3387 /* check if caller triggers a port mapped or offset */
3388 if (sign == '-' || (sign == '+')) {
3389 /* check if server currently uses port map */
3390 if (!(s->flags & SRV_F_MAPPORTS)) {
3391 /* switch from fixed port to port map mandatorily triggers
3392 * a port change */
3393 port_change_required = 1;
3394 /* check is configured
3395 * we're switching from a fixed port to a SRV_F_MAPPORTS (mapped) port
3396 * prevent PORT change if check doesn't have it's dedicated port while switching
3397 * to port mapping */
3398 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3399 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.");
3400 goto out;
3401 }
3402 }
3403 /* we're already using port maps */
3404 else {
3405 port_change_required = current_port != new_port;
3406 }
3407 }
3408 /* fixed port */
3409 else {
3410 port_change_required = current_port != new_port;
3411 }
3412
3413 /* applying PORT changes if required and update response message */
3414 if (port_change_required) {
3415 /* apply new port */
Willy Tarreau04276f32017-01-06 17:41:29 +01003416 s->svc_port = new_port;
Olivier Houchard4e694042017-03-14 20:01:29 +01003417 changed = 1;
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003418
3419 /* prepare message */
3420 chunk_appendf(msg, "port changed from '");
3421 if (s->flags & SRV_F_MAPPORTS)
3422 chunk_appendf(msg, "+");
3423 chunk_appendf(msg, "%d' to '", current_port);
3424
3425 if (sign == '-') {
3426 s->flags |= SRV_F_MAPPORTS;
3427 chunk_appendf(msg, "%c", sign);
3428 /* just use for result output */
3429 new_port = -new_port;
3430 }
3431 else if (sign == '+') {
3432 s->flags |= SRV_F_MAPPORTS;
3433 chunk_appendf(msg, "%c", sign);
3434 }
3435 else {
3436 s->flags &= ~SRV_F_MAPPORTS;
3437 }
3438
3439 chunk_appendf(msg, "%d'", new_port);
3440
3441 /* we also need to update health checks port only if it uses server's realport */
3442 if ((s->check.state & CHK_ST_CONFIGURED) && !(s->flags & SRV_F_CHECKPORT)) {
3443 s->check.port = new_port;
3444 }
3445 }
3446 else {
3447 chunk_appendf(msg, "no need to change the port");
3448 }
3449 }
3450
3451out:
Olivier Houchard4e694042017-03-14 20:01:29 +01003452 if (changed)
3453 srv_set_dyncookie(s);
Baptiste Assmannd458adc2016-08-02 08:18:55 +02003454 if (updater)
3455 chunk_appendf(msg, " by '%s'", updater);
3456 chunk_appendf(msg, "\n");
3457 return msg->str;
3458}
3459
3460
3461/*
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003462 * update server status based on result of name resolution
3463 * returns:
3464 * 0 if server status is updated
3465 * 1 if server status has not changed
3466 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003467int snr_update_srv_status(struct server *s, int has_no_ip)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003468{
3469 struct dns_resolution *resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003470 struct dns_resolvers *resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003471
3472 switch (resolution->status) {
3473 case RSLV_STATUS_NONE:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003474 /* status when HAProxy has just (re)started.
3475 * Nothing to do, since the task is already automatically started */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003476 break;
3477
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003478 case RSLV_STATUS_VALID:
3479 /*
3480 * resume health checks
3481 * server will be turned back on if health check is safe
3482 */
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003483 if (has_no_ip) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003484 if (s->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003485 return 1;
3486 srv_set_admin_flag(s, SRV_ADMF_RMAINT,
3487 "No IP for server ");
3488 return (0);
3489 }
Emeric Brun52a91d32017-08-31 14:41:55 +02003490 if (!(s->next_admin & SRV_ADMF_RMAINT))
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003491 return 1;
3492 srv_clr_admin_flag(s, SRV_ADMF_RMAINT);
3493 chunk_printf(&trash, "Server %s/%s administratively READY thanks to valid DNS answer",
3494 s->proxy->id, s->id);
3495
3496 Warning("%s.\n", trash.str);
3497 send_log(s->proxy, LOG_NOTICE, "%s.\n", trash.str);
3498 return 0;
3499
3500 case RSLV_STATUS_NX:
3501 /* stop server if resolution is NX for a long enough period */
3502 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.nx), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003503 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003504 return 1;
3505 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS NX status");
3506 return 0;
3507 }
3508 break;
3509
3510 case RSLV_STATUS_TIMEOUT:
3511 /* stop server if resolution is TIMEOUT for a long enough period */
3512 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.timeout), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003513 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003514 return 1;
3515 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS timeout status");
3516 return 0;
3517 }
3518 break;
3519
3520 case RSLV_STATUS_REFUSED:
3521 /* stop server if resolution is REFUSED for a long enough period */
3522 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.refused), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003523 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003524 return 1;
3525 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "DNS refused status");
3526 return 0;
3527 }
3528 break;
3529
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003530 default:
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003531 /* stop server if resolution is in unmatched error for a long enough period */
3532 if (tick_is_expired(tick_add(resolution->last_status_change, resolvers->hold.other), now_ms)) {
Emeric Brun52a91d32017-08-31 14:41:55 +02003533 if (s->next_admin & SRV_ADMF_RMAINT)
Baptiste Assmann3b9fe9f2016-11-02 22:58:18 +01003534 return 1;
3535 srv_set_admin_flag(s, SRV_ADMF_RMAINT, "unspecified DNS error");
3536 return 0;
3537 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003538 break;
3539 }
3540
3541 return 1;
3542}
3543
3544/*
3545 * Server Name Resolution valid response callback
3546 * It expects:
3547 * - <nameserver>: the name server which answered the valid response
3548 * - <response>: buffer containing a valid DNS response
3549 * - <response_len>: size of <response>
3550 * It performs the following actions:
3551 * - ignore response if current ip found and server family not met
3552 * - update with first new ip found if family is met and current IP is not found
3553 * returns:
3554 * 0 on error
3555 * 1 when no error or safe ignore
3556 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003557int snr_resolution_cb(struct dns_requester *requester, struct dns_nameserver *nameserver)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003558{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003559 struct server *s = NULL;
3560 struct dns_resolution *resolution = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003561 void *serverip, *firstip;
3562 short server_sin_family, firstip_sin_family;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003563 int ret;
3564 struct chunk *chk = get_trash_chunk();
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003565 int has_no_ip = 0;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003566
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003567 s = objt_server(requester->requester);
3568 if (!s)
3569 return 1;
3570
3571 resolution = s->resolution;
3572
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003573 /* initializing variables */
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003574 firstip = NULL; /* pointer to the first valid response found */
3575 /* it will be used as the new IP if a change is required */
3576 firstip_sin_family = AF_UNSPEC;
3577 serverip = NULL; /* current server IP address */
3578
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003579 /* initializing server IP pointer */
3580 server_sin_family = s->addr.ss_family;
3581 switch (server_sin_family) {
3582 case AF_INET:
3583 serverip = &((struct sockaddr_in *)&s->addr)->sin_addr.s_addr;
3584 break;
3585
3586 case AF_INET6:
3587 serverip = &((struct sockaddr_in6 *)&s->addr)->sin6_addr.s6_addr;
3588 break;
3589
Willy Tarreau3acfcd12017-01-06 19:18:32 +01003590 case AF_UNSPEC:
3591 break;
3592
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003593 default:
3594 goto invalid;
3595 }
3596
Baptiste Assmann729c9012017-05-22 15:13:10 +02003597 ret = dns_get_ip_from_response(&resolution->response, &s->dns_opts,
Thierry Fournierada34842016-02-17 21:25:09 +01003598 serverip, server_sin_family, &firstip,
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003599 &firstip_sin_family, s);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003600
3601 switch (ret) {
3602 case DNS_UPD_NO:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003603 goto update_status;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003604
3605 case DNS_UPD_SRVIP_NOT_FOUND:
3606 goto save_ip;
3607
3608 case DNS_UPD_CNAME:
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003609 goto invalid;
3610
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003611 case DNS_UPD_NO_IP_FOUND:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003612 has_no_ip = 1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003613 goto update_status;
Baptiste Assmann0453a1d2015-09-09 00:51:08 +02003614
Baptiste Assmannfad03182015-10-28 02:03:32 +01003615 case DNS_UPD_NAME_ERROR:
3616 /* if this is not the last expected response, we ignore it */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003617 if (nameserver && (resolution->nb_responses < nameserver->resolvers->count_nameservers))
Baptiste Assmannfad03182015-10-28 02:03:32 +01003618 return 0;
3619 /* update resolution status to OTHER error type */
3620 if (resolution->status != RSLV_STATUS_OTHER) {
3621 resolution->status = RSLV_STATUS_OTHER;
3622 resolution->last_status_change = now_ms;
3623 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003624 goto update_status;
Baptiste Assmannfad03182015-10-28 02:03:32 +01003625
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003626 default:
3627 goto invalid;
3628
3629 }
3630
3631 save_ip:
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003632 if (nameserver)
3633 nameserver->counters.update += 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003634
3635 /* save the first ip we found */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003636 if (nameserver)
3637 chunk_printf(chk, "%s/%s", nameserver->resolvers->id, nameserver->id);
3638 else
3639 chunk_printf(chk, "DNS cache");
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003640 update_server_addr(s, firstip, firstip_sin_family, (char *)chk->str);
3641
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003642 update_status:
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003643 snr_update_srv_status(s, has_no_ip);
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003644 return 1;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003645
3646 invalid:
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003647 if (nameserver) {
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003648 nameserver->counters.invalid += 1;
Christopher Faulet3bbd65b2017-09-15 11:55:45 +02003649 if (resolution->nb_responses >= nameserver->resolvers->count_nameservers)
3650 goto update_status;
3651 }
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003652
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003653 snr_update_srv_status(s, has_no_ip);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003654 return 0;
3655}
3656
3657/*
3658 * Server Name Resolution error management callback
3659 * returns:
3660 * 0 on error
3661 * 1 when no error or safe ignore
3662 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003663int snr_resolution_error_cb(struct dns_requester *requester, int error_code)
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003664{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003665 struct server *s = NULL;
3666 struct dns_resolution *resolution = NULL;
3667 struct dns_resolvers *resolvers = NULL;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003668
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003669 s = objt_server(requester->requester);
3670 if (!s)
3671 return 1;
3672
3673 resolution = s->resolution;
Baptiste Assmann42746372017-05-03 12:12:02 +02003674 resolvers = s->resolvers;
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003675
3676 /* can be ignored if this is not the last response */
3677 if ((error_code != DNS_RESP_TIMEOUT) && (resolution->nb_responses < resolvers->count_nameservers)) {
3678 return 1;
3679 }
3680
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003681 snr_update_srv_status(s, 0);
Baptiste Assmanna68ca962015-04-14 01:15:08 +02003682 return 1;
3683}
3684
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003685/*
3686 * Function to check if <ip> is already affected to a server in the backend
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003687 * which owns <srv> and is up.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003688 * It returns a pointer to the first server found or NULL if <ip> is not yet
3689 * assigned.
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003690 */
3691struct server *snr_check_ip_callback(struct server *srv, void *ip, unsigned char *ip_family)
3692{
3693 struct server *tmpsrv;
3694 struct proxy *be;
3695
3696 if (!srv)
3697 return NULL;
3698
3699 be = srv->proxy;
3700 for (tmpsrv = be->srv; tmpsrv; tmpsrv = tmpsrv->next) {
3701 /* We want to compare the IP in the record with the IP of the servers in the
3702 * same backend, only if:
3703 * * DNS resolution is enabled on the server
3704 * * the hostname used for the resolution by our server is the same than the
3705 * one used for the server found in the backend
3706 * * the server found in the backend is not our current server
3707 */
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003708 if ((tmpsrv->hostname_dn == NULL) ||
3709 (srv->hostname_dn_len != tmpsrv->hostname_dn_len) ||
3710 (strcmp(srv->hostname_dn, tmpsrv->hostname_dn) != 0) ||
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003711 (srv->puid == tmpsrv->puid))
3712 continue;
3713
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003714 /* If the server has been taken down, don't consider it */
Emeric Brun52a91d32017-08-31 14:41:55 +02003715 if (tmpsrv->next_admin & SRV_ADMF_RMAINT)
Olivier Houcharda8c6db82017-07-06 18:46:47 +02003716 continue;
3717
Baptiste Assmannfb7091e2017-05-03 15:43:12 +02003718 /* At this point, we have 2 different servers using the same DNS hostname
3719 * for their respective resolution.
3720 */
3721 if (*ip_family == tmpsrv->addr.ss_family &&
3722 ((tmpsrv->addr.ss_family == AF_INET &&
3723 memcmp(ip, &((struct sockaddr_in *)&tmpsrv->addr)->sin_addr, 4) == 0) ||
3724 (tmpsrv->addr.ss_family == AF_INET6 &&
3725 memcmp(ip, &((struct sockaddr_in6 *)&tmpsrv->addr)->sin6_addr, 16) == 0))) {
3726 return tmpsrv;
3727 }
3728 }
3729
3730 return NULL;
3731}
3732
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003733/* Sets the server's address (srv->addr) from srv->hostname using the libc's
3734 * resolver. This is suited for initial address configuration. Returns 0 on
3735 * success otherwise a non-zero error code. In case of error, *err_code, if
3736 * not NULL, is filled up.
3737 */
3738int srv_set_addr_via_libc(struct server *srv, int *err_code)
3739{
3740 if (str2ip2(srv->hostname, &srv->addr, 1) == NULL) {
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003741 if (err_code)
Willy Tarreau465b6e52016-11-07 19:19:22 +01003742 *err_code |= ERR_WARN;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003743 return 1;
3744 }
3745 return 0;
3746}
3747
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003748/* Set the server's FDQN (->hostname) from <hostname>.
3749 * Returns -1 if failed, 0 if not.
3750 */
3751int srv_set_fqdn(struct server *srv, const char *hostname)
3752{
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003753 struct dns_resolution *resolution;
3754 int hostname_dn_len;
Olivier Houchard8da5f982017-08-04 18:35:36 +02003755 int did_set_reso = 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003756
3757 /* run time DNS resolution was not active for this server
3758 * and we can't enable it at run time for now.
3759 */
3760 if (!srv->dns_requester)
3761 return -1;
3762
3763 chunk_reset(&trash);
3764
3765 /* check if hostname is really a hostname and if we have enough
3766 * room to save it in its domain name format
3767 */
3768 hostname_dn_len = dns_str_to_dn_label_len(hostname);
3769 if (hostname_dn_len == -1 || hostname_dn_len + 1 > trash.size)
3770 return -1;
3771
3772 if (!dns_str_to_dn_label(hostname,
3773 trash.str,
3774 hostname_dn_len + 1))
3775 return -1;
3776
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003777
Olivier Houchard8da5f982017-08-04 18:35:36 +02003778 if (srv->resolution->hostname_dn) {
3779 /* get a resolution from the curr or wait queues, or a brand new one from the pool */
3780 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
3781 if (!resolution)
3782 return -1;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003783
Olivier Houchard8da5f982017-08-04 18:35:36 +02003784 /* in this case, the new hostanme is the same than the old one */
3785 if (srv->resolution == resolution && srv->hostname)
3786 return 0;
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003787
Olivier Houchard8da5f982017-08-04 18:35:36 +02003788 /* first, we need to unlink our server from its current resolution */
3789 srv_free_from_resolution(srv);
3790 } else {
Baptiste Assmann6fb81922017-08-14 16:35:45 +02003791 /* this server's fqdn has been set by a SRV record */
3792 resolution = dns_resolution_list_get(srv->resolvers, trash.str, srv->dns_requester->prefered_query_type);
3793 srv_free_from_resolution(srv);
3794 srv->resolution = resolution;
3795 if (resolution->hostname_dn == NULL) {
3796 resolution->last_resolution = now_ms;
3797 did_set_reso = 1;
3798 }
Olivier Houchard8da5f982017-08-04 18:35:36 +02003799 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003800
3801 /* now we update server's parameters */
3802 free(srv->hostname);
3803 free(srv->hostname_dn);
3804 srv->hostname = strdup(hostname);
3805 srv->hostname_dn = strdup(trash.str);
3806 srv->hostname_dn_len = hostname_dn_len;
3807 if (!srv->hostname || !srv->hostname_dn)
3808 return -1;
Olivier Houchard8da5f982017-08-04 18:35:36 +02003809 if (did_set_reso) {
3810 resolution->query_type = srv->dns_requester->prefered_query_type;
3811 resolution->hostname_dn = srv->hostname_dn;
3812 resolution->hostname_dn_len = hostname_dn_len;
3813 }
Baptiste Assmann201c07f2017-05-22 15:17:15 +02003814
3815 /* then we can link srv to its new resolution */
3816 dns_link_resolution(srv, OBJ_TYPE_SERVER, resolution);
3817
3818 return 0;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003819}
3820
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003821/* Sets the server's address (srv->addr) from srv->lastaddr which was filled
3822 * from the state file. This is suited for initial address configuration.
3823 * Returns 0 on success otherwise a non-zero error code. In case of error,
3824 * *err_code, if not NULL, is filled up.
3825 */
3826static int srv_apply_lastaddr(struct server *srv, int *err_code)
3827{
3828 if (!str2ip2(srv->lastaddr, &srv->addr, 0)) {
3829 if (err_code)
3830 *err_code |= ERR_WARN;
3831 return 1;
3832 }
3833 return 0;
3834}
3835
Willy Tarreau25e51522016-11-04 15:10:17 +01003836/* returns 0 if no error, otherwise a combination of ERR_* flags */
3837static int srv_iterate_initaddr(struct server *srv)
3838{
3839 int return_code = 0;
3840 int err_code;
3841 unsigned int methods;
3842
3843 methods = srv->init_addr_methods;
3844 if (!methods) { // default to "last,libc"
3845 srv_append_initaddr(&methods, SRV_IADDR_LAST);
3846 srv_append_initaddr(&methods, SRV_IADDR_LIBC);
3847 }
3848
Willy Tarreau3eed10e2016-11-07 21:03:16 +01003849 /* "-dr" : always append "none" so that server addresses resolution
3850 * failures are silently ignored, this is convenient to validate some
3851 * configs out of their environment.
3852 */
3853 if (global.tune.options & GTUNE_RESOLVE_DONTFAIL)
3854 srv_append_initaddr(&methods, SRV_IADDR_NONE);
3855
Willy Tarreau25e51522016-11-04 15:10:17 +01003856 while (methods) {
3857 err_code = 0;
3858 switch (srv_get_next_initaddr(&methods)) {
3859 case SRV_IADDR_LAST:
3860 if (!srv->lastaddr)
3861 continue;
3862 if (srv_apply_lastaddr(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003863 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003864 return_code |= err_code;
3865 break;
3866
3867 case SRV_IADDR_LIBC:
3868 if (!srv->hostname)
3869 continue;
3870 if (srv_set_addr_via_libc(srv, &err_code) == 0)
Olivier Houchard4e694042017-03-14 20:01:29 +01003871 goto out;
Willy Tarreau25e51522016-11-04 15:10:17 +01003872 return_code |= err_code;
3873 break;
3874
Willy Tarreau37ebe122016-11-04 15:17:58 +01003875 case SRV_IADDR_NONE:
3876 srv_set_admin_flag(srv, SRV_ADMF_RMAINT, NULL);
Willy Tarreau465b6e52016-11-07 19:19:22 +01003877 if (return_code) {
3878 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', disabling server.\n",
3879 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3880 }
Willy Tarreau37ebe122016-11-04 15:17:58 +01003881 return return_code;
3882
Willy Tarreau4310d362016-11-02 15:05:56 +01003883 case SRV_IADDR_IP:
3884 ipcpy(&srv->init_addr, &srv->addr);
3885 if (return_code) {
3886 Warning("parsing [%s:%d] : 'server %s' : could not resolve address '%s', falling back to configured address.\n",
3887 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3888 }
Olivier Houchard4e694042017-03-14 20:01:29 +01003889 goto out;
Willy Tarreau4310d362016-11-02 15:05:56 +01003890
Willy Tarreau25e51522016-11-04 15:10:17 +01003891 default: /* unhandled method */
3892 break;
3893 }
3894 }
3895
3896 if (!return_code) {
3897 Alert("parsing [%s:%d] : 'server %s' : no method found to resolve address '%s'\n",
3898 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3899 }
Willy Tarreau465b6e52016-11-07 19:19:22 +01003900 else {
3901 Alert("parsing [%s:%d] : 'server %s' : could not resolve address '%s'.\n",
3902 srv->conf.file, srv->conf.line, srv->id, srv->hostname);
3903 }
Willy Tarreau25e51522016-11-04 15:10:17 +01003904
3905 return_code |= ERR_ALERT | ERR_FATAL;
3906 return return_code;
Olivier Houchard4e694042017-03-14 20:01:29 +01003907out:
3908 srv_set_dyncookie(srv);
3909 return return_code;
Willy Tarreau25e51522016-11-04 15:10:17 +01003910}
3911
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003912/*
3913 * This function parses all backends and all servers within each backend
3914 * and performs servers' addr resolution based on information provided by:
3915 * - configuration file
3916 * - server-state file (states provided by an 'old' haproxy process)
3917 *
3918 * Returns 0 if no error, otherwise, a combination of ERR_ flags.
3919 */
3920int srv_init_addr(void)
3921{
3922 struct proxy *curproxy;
3923 int return_code = 0;
3924
3925 curproxy = proxy;
3926 while (curproxy) {
3927 struct server *srv;
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003928
3929 /* servers are in backend only */
3930 if (!(curproxy->cap & PR_CAP_BE))
3931 goto srv_init_addr_next;
3932
Willy Tarreau25e51522016-11-04 15:10:17 +01003933 for (srv = curproxy->srv; srv; srv = srv->next)
Willy Tarreau3d609a72017-09-06 14:22:45 +02003934 if (srv->hostname)
3935 return_code |= srv_iterate_initaddr(srv);
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01003936
3937 srv_init_addr_next:
3938 curproxy = curproxy->next;
3939 }
3940
3941 return return_code;
3942}
3943
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003944const char *update_server_fqdn(struct server *server, const char *fqdn, const char *updater)
3945{
3946
3947 struct chunk *msg;
3948
3949 msg = get_trash_chunk();
3950 chunk_reset(msg);
3951
Olivier Houchard8da5f982017-08-04 18:35:36 +02003952 if (server->hostname && !strcmp(fqdn, server->hostname)) {
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003953 chunk_appendf(msg, "no need to change the FDQN");
3954 goto out;
3955 }
3956
3957 if (strlen(fqdn) > DNS_MAX_NAME_SIZE || invalid_domainchar(fqdn)) {
3958 chunk_appendf(msg, "invalid fqdn '%s'", fqdn);
3959 goto out;
3960 }
3961
3962 chunk_appendf(msg, "%s/%s changed its FQDN from %s to %s",
3963 server->proxy->id, server->id, server->hostname, fqdn);
3964
3965 if (srv_set_fqdn(server, fqdn) < 0) {
3966 chunk_reset(msg);
3967 chunk_appendf(msg, "could not update %s/%s FQDN",
3968 server->proxy->id, server->id);
3969 goto out;
3970 }
3971
3972 /* Flag as FQDN set from stats socket. */
Emeric Brun52a91d32017-08-31 14:41:55 +02003973 server->next_admin |= SRV_ADMF_HMAINT;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02003974
3975 out:
3976 if (updater)
3977 chunk_appendf(msg, " by '%s'", updater);
3978 chunk_appendf(msg, "\n");
3979
3980 return msg->str;
3981}
3982
3983
Willy Tarreau21b069d2016-11-23 17:15:08 +01003984/* Expects to find a backend and a server in <arg> under the form <backend>/<server>,
3985 * and returns the pointer to the server. Otherwise, display adequate error messages
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003986 * 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 +01003987 * used for CLI commands requiring a server name.
3988 * Important: the <arg> is modified to remove the '/'.
3989 */
3990struct server *cli_find_server(struct appctx *appctx, char *arg)
3991{
3992 struct proxy *px;
3993 struct server *sv;
3994 char *line;
3995
3996 /* split "backend/server" and make <line> point to server */
3997 for (line = arg; *line; line++)
3998 if (*line == '/') {
3999 *line++ = '\0';
4000 break;
4001 }
4002
4003 if (!*line || !*arg) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004004 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004005 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004006 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004007 return NULL;
4008 }
4009
4010 if (!get_backend_server(arg, line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004011 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004012 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004013 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004014 return NULL;
4015 }
4016
4017 if (px->state == PR_STSTOPPED) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004018 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004019 appctx->ctx.cli.msg = "Proxy is disabled.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004020 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau21b069d2016-11-23 17:15:08 +01004021 return NULL;
4022 }
4023
4024 return sv;
4025}
4026
William Lallemand222baf22016-11-19 02:00:33 +01004027
4028static int cli_parse_set_server(char **args, struct appctx *appctx, void *private)
4029{
4030 struct server *sv;
4031 const char *warning;
4032
4033 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4034 return 1;
4035
4036 sv = cli_find_server(appctx, args[2]);
4037 if (!sv)
4038 return 1;
4039
4040 if (strcmp(args[3], "weight") == 0) {
4041 warning = server_parse_weight_change_request(sv, args[4]);
4042 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004043 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004044 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004045 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004046 }
4047 }
4048 else if (strcmp(args[3], "state") == 0) {
4049 if (strcmp(args[4], "ready") == 0)
4050 srv_adm_set_ready(sv);
4051 else if (strcmp(args[4], "drain") == 0)
4052 srv_adm_set_drain(sv);
4053 else if (strcmp(args[4], "maint") == 0)
4054 srv_adm_set_maint(sv);
4055 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004056 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004057 appctx->ctx.cli.msg = "'set server <srv> state' expects 'ready', 'drain' and 'maint'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004058 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004059 }
4060 }
4061 else if (strcmp(args[3], "health") == 0) {
4062 if (sv->track) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004063 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004064 appctx->ctx.cli.msg = "cannot change health on a tracking server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004065 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004066 }
4067 else if (strcmp(args[4], "up") == 0) {
4068 sv->check.health = sv->check.rise + sv->check.fall - 1;
4069 srv_set_running(sv, "changed from CLI");
4070 }
4071 else if (strcmp(args[4], "stopping") == 0) {
4072 sv->check.health = sv->check.rise + sv->check.fall - 1;
4073 srv_set_stopping(sv, "changed from CLI");
4074 }
4075 else if (strcmp(args[4], "down") == 0) {
4076 sv->check.health = 0;
4077 srv_set_stopped(sv, "changed from CLI");
4078 }
4079 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004080 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004081 appctx->ctx.cli.msg = "'set server <srv> health' expects 'up', 'stopping', or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004082 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004083 }
4084 }
4085 else if (strcmp(args[3], "agent") == 0) {
4086 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004087 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004088 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004089 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004090 }
4091 else if (strcmp(args[4], "up") == 0) {
4092 sv->agent.health = sv->agent.rise + sv->agent.fall - 1;
4093 srv_set_running(sv, "changed from CLI");
4094 }
4095 else if (strcmp(args[4], "down") == 0) {
4096 sv->agent.health = 0;
4097 srv_set_stopped(sv, "changed from CLI");
4098 }
4099 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004100 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004101 appctx->ctx.cli.msg = "'set server <srv> agent' expects 'up' or 'down'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004102 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004103 }
4104 }
Misiek2da082d2017-01-09 09:40:42 +01004105 else if (strcmp(args[3], "agent-addr") == 0) {
4106 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004107 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004108 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4109 appctx->st0 = CLI_ST_PRINT;
4110 } else {
4111 if (str2ip(args[4], &sv->agent.addr) == NULL) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004112 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004113 appctx->ctx.cli.msg = "incorrect addr address given for agent.\n";
4114 appctx->st0 = CLI_ST_PRINT;
4115 }
4116 }
4117 }
4118 else if (strcmp(args[3], "agent-send") == 0) {
4119 if (!(sv->agent.state & CHK_ST_ENABLED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004120 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004121 appctx->ctx.cli.msg = "agent checks are not enabled on this server.\n";
4122 appctx->st0 = CLI_ST_PRINT;
4123 } else {
4124 char *nss = strdup(args[4]);
4125 if (!nss) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004126 appctx->ctx.cli.severity = LOG_ERR;
Misiek2da082d2017-01-09 09:40:42 +01004127 appctx->ctx.cli.msg = "cannot allocate memory for new string.\n";
4128 appctx->st0 = CLI_ST_PRINT;
4129 } else {
4130 free(sv->agent.send_string);
4131 sv->agent.send_string = nss;
4132 sv->agent.send_string_len = strlen(args[4]);
4133 }
4134 }
4135 }
William Lallemand222baf22016-11-19 02:00:33 +01004136 else if (strcmp(args[3], "check-port") == 0) {
4137 int i = 0;
4138 if (strl2irc(args[4], strlen(args[4]), &i) != 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004139 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004140 appctx->ctx.cli.msg = "'set server <srv> check-port' expects an integer as argument.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004141 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004142 }
4143 if ((i < 0) || (i > 65535)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004144 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004145 appctx->ctx.cli.msg = "provided port is not valid.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004146 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004147 }
4148 /* prevent the update of port to 0 if MAPPORTS are in use */
4149 if ((sv->flags & SRV_F_MAPPORTS) && (i == 0)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004150 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004151 appctx->ctx.cli.msg = "can't unset 'port' since MAPPORTS is in use.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004152 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004153 return 1;
4154 }
4155 sv->check.port = i;
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004156 appctx->ctx.cli.severity = LOG_NOTICE;
William Lallemand222baf22016-11-19 02:00:33 +01004157 appctx->ctx.cli.msg = "health check port updated.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004158 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004159 }
4160 else if (strcmp(args[3], "addr") == 0) {
4161 char *addr = NULL;
4162 char *port = NULL;
4163 if (strlen(args[4]) == 0) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004164 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand222baf22016-11-19 02:00:33 +01004165 appctx->ctx.cli.msg = "set server <b>/<s> addr requires an address and optionally a port.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004166 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004167 return 1;
4168 }
4169 else {
4170 addr = args[4];
4171 }
4172 if (strcmp(args[5], "port") == 0) {
4173 port = args[6];
4174 }
4175 warning = update_server_addr_port(sv, addr, port, "stats socket command");
4176 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004177 appctx->ctx.cli.severity = LOG_WARNING;
William Lallemand222baf22016-11-19 02:00:33 +01004178 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004179 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004180 }
4181 srv_clr_admin_flag(sv, SRV_ADMF_RMAINT);
4182 }
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004183 else if (strcmp(args[3], "fqdn") == 0) {
4184 if (!*args[4]) {
4185 appctx->ctx.cli.msg = "set server <b>/<s> fqdn requires a FQDN.\n";
4186 appctx->st0 = CLI_ST_PRINT;
4187 return 1;
4188 }
4189 warning = update_server_fqdn(sv, args[4], "stats socket command");
4190 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004191 appctx->ctx.cli.severity = LOG_WARNING;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004192 appctx->ctx.cli.msg = warning;
4193 appctx->st0 = CLI_ST_PRINT;
4194 }
4195 }
William Lallemand222baf22016-11-19 02:00:33 +01004196 else {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004197 appctx->ctx.cli.severity = LOG_ERR;
Frédéric Lécailleb418c122017-04-26 11:24:02 +02004198 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 +01004199 appctx->st0 = CLI_ST_PRINT;
William Lallemand222baf22016-11-19 02:00:33 +01004200 }
4201 return 1;
4202}
4203
William Lallemand6b160942016-11-22 12:34:35 +01004204static int cli_parse_get_weight(char **args, struct appctx *appctx, void *private)
4205{
4206 struct stream_interface *si = appctx->owner;
4207 struct proxy *px;
4208 struct server *sv;
4209 char *line;
4210
4211
4212 /* split "backend/server" and make <line> point to server */
4213 for (line = args[2]; *line; line++)
4214 if (*line == '/') {
4215 *line++ = '\0';
4216 break;
4217 }
4218
4219 if (!*line) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004220 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004221 appctx->ctx.cli.msg = "Require 'backend/server'.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004222 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004223 return 1;
4224 }
4225
4226 if (!get_backend_server(args[2], line, &px, &sv)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004227 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004228 appctx->ctx.cli.msg = px ? "No such server.\n" : "No such backend.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004229 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004230 return 1;
4231 }
4232
4233 /* return server's effective weight at the moment */
4234 snprintf(trash.str, trash.size, "%d (initial %d)\n", sv->uweight, sv->iweight);
Willy Tarreau06d80a92017-10-19 14:32:15 +02004235 if (ci_putstr(si_ic(si), trash.str) == -1) {
William Lallemand6b160942016-11-22 12:34:35 +01004236 si_applet_cant_put(si);
Christopher Faulet90b5abe2016-12-05 14:25:08 +01004237 return 0;
4238 }
William Lallemand6b160942016-11-22 12:34:35 +01004239 return 1;
4240}
4241
4242static int cli_parse_set_weight(char **args, struct appctx *appctx, void *private)
4243{
4244 struct server *sv;
4245 const char *warning;
4246
4247 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4248 return 1;
4249
4250 sv = cli_find_server(appctx, args[2]);
4251 if (!sv)
4252 return 1;
4253
4254 warning = server_parse_weight_change_request(sv, args[3]);
4255 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004256 appctx->ctx.cli.severity = LOG_ERR;
William Lallemand6b160942016-11-22 12:34:35 +01004257 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004258 appctx->st0 = CLI_ST_PRINT;
William Lallemand6b160942016-11-22 12:34:35 +01004259 }
4260 return 1;
4261}
4262
Willy Tarreaub8026272016-11-23 11:26:56 +01004263/* parse a "set maxconn server" command. It always returns 1. */
4264static int cli_parse_set_maxconn_server(char **args, struct appctx *appctx, void *private)
4265{
4266 struct server *sv;
4267 const char *warning;
4268
4269 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4270 return 1;
4271
4272 sv = cli_find_server(appctx, args[3]);
4273 if (!sv)
4274 return 1;
4275
4276 warning = server_parse_maxconn_change_request(sv, args[4]);
4277 if (warning) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004278 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreaub8026272016-11-23 11:26:56 +01004279 appctx->ctx.cli.msg = warning;
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004280 appctx->st0 = CLI_ST_PRINT;
Willy Tarreaub8026272016-11-23 11:26:56 +01004281 }
4282 return 1;
4283}
William Lallemand6b160942016-11-22 12:34:35 +01004284
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004285/* parse a "disable agent" command. It always returns 1. */
4286static int cli_parse_disable_agent(char **args, struct appctx *appctx, void *private)
4287{
4288 struct server *sv;
4289
4290 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4291 return 1;
4292
4293 sv = cli_find_server(appctx, args[2]);
4294 if (!sv)
4295 return 1;
4296
4297 sv->agent.state &= ~CHK_ST_ENABLED;
4298 return 1;
4299}
4300
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004301/* parse a "disable health" command. It always returns 1. */
4302static int cli_parse_disable_health(char **args, struct appctx *appctx, void *private)
4303{
4304 struct server *sv;
4305
4306 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4307 return 1;
4308
4309 sv = cli_find_server(appctx, args[2]);
4310 if (!sv)
4311 return 1;
4312
4313 sv->check.state &= ~CHK_ST_ENABLED;
4314 return 1;
4315}
4316
Willy Tarreauffb4d582016-11-24 12:47:00 +01004317/* parse a "disable server" command. It always returns 1. */
4318static int cli_parse_disable_server(char **args, struct appctx *appctx, void *private)
4319{
4320 struct server *sv;
4321
4322 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4323 return 1;
4324
4325 sv = cli_find_server(appctx, args[2]);
4326 if (!sv)
4327 return 1;
4328
4329 srv_adm_set_maint(sv);
4330 return 1;
4331}
4332
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004333/* parse a "enable agent" command. It always returns 1. */
4334static int cli_parse_enable_agent(char **args, struct appctx *appctx, void *private)
4335{
4336 struct server *sv;
4337
4338 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4339 return 1;
4340
4341 sv = cli_find_server(appctx, args[2]);
4342 if (!sv)
4343 return 1;
4344
4345 if (!(sv->agent.state & CHK_ST_CONFIGURED)) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02004346 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004347 appctx->ctx.cli.msg = "Agent was not configured on this server, cannot enable.\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01004348 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004349 return 1;
4350 }
4351
4352 sv->agent.state |= CHK_ST_ENABLED;
4353 return 1;
4354}
4355
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004356/* parse a "enable health" command. It always returns 1. */
4357static int cli_parse_enable_health(char **args, struct appctx *appctx, void *private)
4358{
4359 struct server *sv;
4360
4361 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4362 return 1;
4363
4364 sv = cli_find_server(appctx, args[2]);
4365 if (!sv)
4366 return 1;
4367
4368 sv->check.state |= CHK_ST_ENABLED;
4369 return 1;
4370}
4371
Willy Tarreauffb4d582016-11-24 12:47:00 +01004372/* parse a "enable server" command. It always returns 1. */
4373static int cli_parse_enable_server(char **args, struct appctx *appctx, void *private)
4374{
4375 struct server *sv;
4376
4377 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
4378 return 1;
4379
4380 sv = cli_find_server(appctx, args[2]);
4381 if (!sv)
4382 return 1;
4383
4384 srv_adm_set_ready(sv);
4385 return 1;
4386}
4387
William Lallemand222baf22016-11-19 02:00:33 +01004388/* register cli keywords */
4389static struct cli_kw_list cli_kws = {{ },{
Willy Tarreau58d9cb72016-11-24 12:56:01 +01004390 { { "disable", "agent", NULL }, "disable agent : disable agent checks (use 'set server' instead)", cli_parse_disable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004391 { { "disable", "health", NULL }, "disable health : disable health checks (use 'set server' instead)", cli_parse_disable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004392 { { "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 +01004393 { { "enable", "agent", NULL }, "enable agent : enable agent checks (use 'set server' instead)", cli_parse_enable_agent, NULL },
Willy Tarreau2c04eda2016-11-24 12:51:04 +01004394 { { "enable", "health", NULL }, "enable health : enable health checks (use 'set server' instead)", cli_parse_enable_health, NULL },
Willy Tarreauffb4d582016-11-24 12:47:00 +01004395 { { "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 +01004396 { { "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 +01004397 { { "set", "server", NULL }, "set server : change a server's state, weight or address", cli_parse_set_server },
William Lallemand6b160942016-11-22 12:34:35 +01004398 { { "get", "weight", NULL }, "get weight : report a server's current weight", cli_parse_get_weight },
4399 { { "set", "weight", NULL }, "set weight : change a server's weight (deprecated)", cli_parse_set_weight },
4400
William Lallemand222baf22016-11-19 02:00:33 +01004401 {{},}
4402}};
4403
4404__attribute__((constructor))
4405static void __server_init(void)
4406{
4407 cli_register_kw(&cli_kws);
4408}
Baptiste Assmann83cbaa52016-11-02 15:34:05 +01004409
Emeric Brun64cc49c2017-10-03 14:46:45 +02004410
4411/*
4412 * This function applies server's status changes, it is
4413 * is designed to be called asynchronously.
4414 *
4415 */
4416void srv_update_status(struct server *s)
4417{
4418 struct check *check = &s->check;
4419 int xferred;
4420 struct proxy *px = s->proxy;
4421 int prev_srv_count = s->proxy->srv_bck + s->proxy->srv_act;
4422 int srv_was_stopping = (s->cur_state == SRV_ST_STOPPING) || (s->cur_admin & SRV_ADMF_DRAIN);
4423 int log_level;
4424 struct chunk *tmptrash = NULL;
4425
4426
4427 /* If currently main is not set we try to apply pending state changes */
4428 if (!(s->cur_admin & SRV_ADMF_MAINT)) {
4429 int next_admin;
4430
4431 /* Backup next admin */
4432 next_admin = s->next_admin;
4433
4434 /* restore current admin state */
4435 s->next_admin = s->cur_admin;
4436
4437 if ((s->cur_state != SRV_ST_STOPPED) && (s->next_state == SRV_ST_STOPPED)) {
4438 s->last_change = now.tv_sec;
4439 if (s->proxy->lbprm.set_server_status_down)
4440 s->proxy->lbprm.set_server_status_down(s);
4441
4442 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4443 srv_shutdown_streams(s, SF_ERR_DOWN);
4444
4445 /* we might have streams queued on this server and waiting for
4446 * a connection. Those which are redispatchable will be queued
4447 * to another server or to the proxy itself.
4448 */
4449 xferred = pendconn_redistribute(s);
4450
4451 tmptrash = alloc_trash_chunk();
4452 if (tmptrash) {
4453 chunk_printf(tmptrash,
4454 "%sServer %s/%s is DOWN", s->flags & SRV_F_BACKUP ? "Backup " : "",
4455 s->proxy->id, s->id);
4456
4457 srv_append_status(tmptrash, s, *s->op_st_chg_reason ? s->op_st_chg_reason : NULL, xferred, 0);
4458 Warning("%s.\n", tmptrash->str);
4459
4460 /* we don't send an alert if the server was previously paused */
4461 log_level = srv_was_stopping ? LOG_NOTICE : LOG_ALERT;
4462 send_log(s->proxy, log_level, "%s.\n", tmptrash->str);
4463 send_email_alert(s, log_level, "%s", tmptrash->str);
4464 free_trash_chunk(tmptrash);
4465 tmptrash = NULL;
4466 }
4467 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4468 set_backend_down(s->proxy);
4469
4470 s->counters.down_trans++;
4471 }
4472 else if ((s->cur_state != SRV_ST_STOPPING) && (s->next_state == SRV_ST_STOPPING)) {
4473 s->last_change = now.tv_sec;
4474 if (s->proxy->lbprm.set_server_status_down)
4475 s->proxy->lbprm.set_server_status_down(s);
4476
4477 /* we might have streams queued on this server and waiting for
4478 * a connection. Those which are redispatchable will be queued
4479 * to another server or to the proxy itself.
4480 */
4481 xferred = pendconn_redistribute(s);
4482
4483 tmptrash = alloc_trash_chunk();
4484 if (tmptrash) {
4485 chunk_printf(tmptrash,
4486 "%sServer %s/%s is stopping", s->flags & SRV_F_BACKUP ? "Backup " : "",
4487 s->proxy->id, s->id);
4488
4489 srv_append_status(tmptrash, s, *s->op_st_chg_reason ? s->op_st_chg_reason : NULL, xferred, 0);
4490
4491 Warning("%s.\n", tmptrash->str);
4492 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4493 free_trash_chunk(tmptrash);
4494 tmptrash = NULL;
4495 }
4496
4497 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4498 set_backend_down(s->proxy);
4499 }
4500 else if (((s->cur_state != SRV_ST_RUNNING) && (s->next_state == SRV_ST_RUNNING))
4501 || ((s->cur_state != SRV_ST_STARTING) && (s->next_state == SRV_ST_STARTING))) {
4502 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4503 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4504 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4505 s->proxy->last_change = now.tv_sec;
4506 }
4507
4508 if (s->next_state == SRV_ST_STOPPED && s->last_change < now.tv_sec) // ignore negative times
4509 s->down_time += now.tv_sec - s->last_change;
4510
4511 s->last_change = now.tv_sec;
4512 if (s->next_state == SRV_ST_STARTING)
4513 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4514
4515 server_recalc_eweight(s);
4516 /* now propagate the status change to any LB algorithms */
4517 if (px->lbprm.update_server_eweight)
4518 px->lbprm.update_server_eweight(s);
4519 else if (srv_willbe_usable(s)) {
4520 if (px->lbprm.set_server_status_up)
4521 px->lbprm.set_server_status_up(s);
4522 }
4523 else {
4524 if (px->lbprm.set_server_status_down)
4525 px->lbprm.set_server_status_down(s);
4526 }
4527
4528 /* If the server is set with "on-marked-up shutdown-backup-sessions",
4529 * and it's not a backup server and its effective weight is > 0,
4530 * then it can accept new connections, so we shut down all streams
4531 * on all backup servers.
4532 */
4533 if ((s->onmarkedup & HANA_ONMARKEDUP_SHUTDOWNBACKUPSESSIONS) &&
4534 !(s->flags & SRV_F_BACKUP) && s->next_eweight)
4535 srv_shutdown_backup_streams(s->proxy, SF_ERR_UP);
4536
4537 /* check if we can handle some connections queued at the proxy. We
4538 * will take as many as we can handle.
4539 */
4540 xferred = pendconn_grab_from_px(s);
4541
4542 tmptrash = alloc_trash_chunk();
4543 if (tmptrash) {
4544 chunk_printf(tmptrash,
4545 "%sServer %s/%s is UP", s->flags & SRV_F_BACKUP ? "Backup " : "",
4546 s->proxy->id, s->id);
4547
4548 srv_append_status(tmptrash, s, *s->op_st_chg_reason ? s->op_st_chg_reason : NULL, xferred, 0);
4549 Warning("%s.\n", tmptrash->str);
4550 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4551 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4552 free_trash_chunk(tmptrash);
4553 tmptrash = NULL;
4554 }
4555
4556 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4557 set_backend_down(s->proxy);
4558 }
4559 else if (s->cur_eweight != s->next_eweight) {
4560 /* now propagate the status change to any LB algorithms */
4561 if (px->lbprm.update_server_eweight)
4562 px->lbprm.update_server_eweight(s);
4563 else if (srv_willbe_usable(s)) {
4564 if (px->lbprm.set_server_status_up)
4565 px->lbprm.set_server_status_up(s);
4566 }
4567 else {
4568 if (px->lbprm.set_server_status_down)
4569 px->lbprm.set_server_status_down(s);
4570 }
4571
4572 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4573 set_backend_down(s->proxy);
4574 }
4575
4576 s->next_admin = next_admin;
4577 }
4578
4579
4580 /* Now we try to apply pending admin changes */
4581
4582 /* Maintenance must also disable health checks */
4583 if (!(s->cur_admin & SRV_ADMF_MAINT) && (s->next_admin & SRV_ADMF_MAINT)) {
4584 if (s->check.state & CHK_ST_ENABLED) {
4585 s->check.state |= CHK_ST_PAUSED;
4586 check->health = 0;
4587 }
4588
4589 if (s->cur_state == SRV_ST_STOPPED) { /* server was already down */
4590 chunk_printf(tmptrash,
4591 "%sServer %s/%s was DOWN and now enters maintenance%s%s%s",
4592 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4593 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4594
4595 srv_append_status(tmptrash, s, NULL, -1, (s->next_admin & SRV_ADMF_FMAINT));
4596
4597 if (!(global.mode & MODE_STARTING)) {
4598 Warning("%s.\n", tmptrash->str);
4599 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4600 }
4601 }
4602 else { /* server was still running */
4603 check->health = 0; /* failure */
4604 s->last_change = now.tv_sec;
4605 if (s->proxy->lbprm.set_server_status_down)
4606 s->proxy->lbprm.set_server_status_down(s);
4607
4608 s->next_state = SRV_ST_STOPPED;
4609 if (s->onmarkeddown & HANA_ONMARKEDDOWN_SHUTDOWNSESSIONS)
4610 srv_shutdown_streams(s, SF_ERR_DOWN);
4611
4612 /* we might have streams queued on this server and waiting for
4613 * a connection. Those which are redispatchable will be queued
4614 * to another server or to the proxy itself.
4615 */
4616 xferred = pendconn_redistribute(s);
4617
4618 tmptrash = alloc_trash_chunk();
4619 if (tmptrash) {
4620 chunk_printf(tmptrash,
4621 "%sServer %s/%s is going DOWN for maintenance%s%s%s",
4622 s->flags & SRV_F_BACKUP ? "Backup " : "",
4623 s->proxy->id, s->id,
4624 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4625
4626 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FMAINT));
4627
4628 if (!(global.mode & MODE_STARTING)) {
4629 Warning("%s.\n", tmptrash->str);
4630 send_log(s->proxy, srv_was_stopping ? LOG_NOTICE : LOG_ALERT, "%s.\n", tmptrash->str);
4631 }
4632 free_trash_chunk(tmptrash);
4633 tmptrash = NULL;
4634 }
4635 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4636 set_backend_down(s->proxy);
4637
4638 s->counters.down_trans++;
4639 }
4640 }
4641 else if ((s->cur_admin & SRV_ADMF_MAINT) && !(s->next_admin & SRV_ADMF_MAINT)) {
4642 /* OK here we're leaving maintenance, we have many things to check,
4643 * because the server might possibly be coming back up depending on
4644 * its state. In practice, leaving maintenance means that we should
4645 * immediately turn to UP (more or less the slowstart) under the
4646 * following conditions :
4647 * - server is neither checked nor tracked
4648 * - server tracks another server which is not checked
4649 * - server tracks another server which is already up
4650 * Which sums up as something simpler :
4651 * "either the tracking server is up or the server's checks are disabled
4652 * or up". Otherwise we only re-enable health checks. There's a special
4653 * case associated to the stopping state which can be inherited. Note
4654 * that the server might still be in drain mode, which is naturally dealt
4655 * with by the lower level functions.
4656 */
4657
4658 if (s->check.state & CHK_ST_ENABLED) {
4659 s->check.state &= ~CHK_ST_PAUSED;
4660 check->health = check->rise; /* start OK but check immediately */
4661 }
4662
4663 if ((!s->track || s->track->next_state != SRV_ST_STOPPED) &&
4664 (!(s->agent.state & CHK_ST_ENABLED) || (s->agent.health >= s->agent.rise)) &&
4665 (!(s->check.state & CHK_ST_ENABLED) || (s->check.health >= s->check.rise))) {
4666 if (s->track && s->track->next_state == SRV_ST_STOPPING) {
4667 s->next_state = SRV_ST_STOPPING;
4668 }
4669 else {
4670 s->next_state = SRV_ST_STARTING;
4671 if (s->slowstart > 0)
4672 task_schedule(s->warmup, tick_add(now_ms, MS_TO_TICKS(MAX(1000, s->slowstart / 20))));
4673 else
4674 s->next_state = SRV_ST_RUNNING;
4675 }
4676
4677 }
4678
4679 tmptrash = alloc_trash_chunk();
4680 if (tmptrash) {
4681 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4682 chunk_printf(tmptrash,
4683 "%sServer %s/%s is %s/%s (leaving forced maintenance)",
4684 s->flags & SRV_F_BACKUP ? "Backup " : "",
4685 s->proxy->id, s->id,
4686 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4687 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4688 }
4689 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4690 chunk_printf(tmptrash,
4691 "%sServer %s/%s ('%s') is %s/%s (resolves again)",
4692 s->flags & SRV_F_BACKUP ? "Backup " : "",
4693 s->proxy->id, s->id, s->hostname,
4694 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4695 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4696 }
4697 if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4698 chunk_printf(tmptrash,
4699 "%sServer %s/%s is %s/%s (leaving maintenance)",
4700 s->flags & SRV_F_BACKUP ? "Backup " : "",
4701 s->proxy->id, s->id,
4702 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP",
4703 (s->next_admin & SRV_ADMF_DRAIN) ? "DRAIN" : "READY");
4704 }
4705 Warning("%s.\n", tmptrash->str);
4706 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4707 free_trash_chunk(tmptrash);
4708 tmptrash = NULL;
4709 }
4710
4711 server_recalc_eweight(s);
4712 /* now propagate the status change to any LB algorithms */
4713 if (px->lbprm.update_server_eweight)
4714 px->lbprm.update_server_eweight(s);
4715 else if (srv_willbe_usable(s)) {
4716 if (px->lbprm.set_server_status_up)
4717 px->lbprm.set_server_status_up(s);
4718 }
4719 else {
4720 if (px->lbprm.set_server_status_down)
4721 px->lbprm.set_server_status_down(s);
4722 }
4723
4724 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4725 set_backend_down(s->proxy);
4726
4727 }
4728 else if (s->next_admin & SRV_ADMF_MAINT) {
4729 /* remaining in maintenance mode, let's inform precisely about the
4730 * situation.
4731 */
4732 if (!(s->next_admin & SRV_ADMF_FMAINT) && (s->cur_admin & SRV_ADMF_FMAINT)) {
4733 tmptrash = alloc_trash_chunk();
4734 if (tmptrash) {
4735 chunk_printf(tmptrash,
4736 "%sServer %s/%s is leaving forced maintenance but remains in maintenance",
4737 s->flags & SRV_F_BACKUP ? "Backup " : "",
4738 s->proxy->id, s->id);
4739
4740 if (s->track) /* normally it's mandatory here */
4741 chunk_appendf(tmptrash, " via %s/%s",
4742 s->track->proxy->id, s->track->id);
4743 Warning("%s.\n", tmptrash->str);
4744 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4745 free_trash_chunk(tmptrash);
4746 tmptrash = NULL;
4747 }
4748 }
4749 if (!(s->next_admin & SRV_ADMF_RMAINT) && (s->cur_admin & SRV_ADMF_RMAINT)) {
4750 tmptrash = alloc_trash_chunk();
4751 if (tmptrash) {
4752 chunk_printf(tmptrash,
4753 "%sServer %s/%s ('%s') resolves again but remains in maintenance",
4754 s->flags & SRV_F_BACKUP ? "Backup " : "",
4755 s->proxy->id, s->id, s->hostname);
4756
4757 if (s->track) /* normally it's mandatory here */
4758 chunk_appendf(tmptrash, " via %s/%s",
4759 s->track->proxy->id, s->track->id);
4760 Warning("%s.\n", tmptrash->str);
4761 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4762 free_trash_chunk(tmptrash);
4763 tmptrash = NULL;
4764 }
4765 }
4766 else if (!(s->next_admin & SRV_ADMF_IMAINT) && (s->cur_admin & SRV_ADMF_IMAINT)) {
4767 tmptrash = alloc_trash_chunk();
4768 if (tmptrash) {
4769 chunk_printf(tmptrash,
4770 "%sServer %s/%s remains in forced maintenance",
4771 s->flags & SRV_F_BACKUP ? "Backup " : "",
4772 s->proxy->id, s->id);
4773 Warning("%s.\n", tmptrash->str);
4774 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4775 free_trash_chunk(tmptrash);
4776 tmptrash = NULL;
4777 }
4778 }
4779 /* don't report anything when leaving drain mode and remaining in maintenance */
4780
4781 s->cur_admin = s->next_admin;
4782 }
4783
4784 if (!(s->next_admin & SRV_ADMF_MAINT)) {
4785 if (!(s->cur_admin & SRV_ADMF_DRAIN) && (s->next_admin & SRV_ADMF_DRAIN)) {
4786 /* drain state is applied only if not yet in maint */
4787
4788 s->last_change = now.tv_sec;
4789 if (px->lbprm.set_server_status_down)
4790 px->lbprm.set_server_status_down(s);
4791
4792 /* we might have streams queued on this server and waiting for
4793 * a connection. Those which are redispatchable will be queued
4794 * to another server or to the proxy itself.
4795 */
4796 xferred = pendconn_redistribute(s);
4797
4798 tmptrash = alloc_trash_chunk();
4799 if (tmptrash) {
4800 chunk_printf(tmptrash, "%sServer %s/%s enters drain state%s%s%s",
4801 s->flags & SRV_F_BACKUP ? "Backup " : "", s->proxy->id, s->id,
4802 *(s->adm_st_chg_cause) ? " (" : "", s->adm_st_chg_cause, *(s->adm_st_chg_cause) ? ")" : "");
4803
4804 srv_append_status(tmptrash, s, NULL, xferred, (s->next_admin & SRV_ADMF_FDRAIN));
4805
4806 if (!(global.mode & MODE_STARTING)) {
4807 Warning("%s.\n", tmptrash->str);
4808 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4809 send_email_alert(s, LOG_NOTICE, "%s", tmptrash->str);
4810 }
4811 free_trash_chunk(tmptrash);
4812 tmptrash = NULL;
4813 }
4814
4815 if (prev_srv_count && s->proxy->srv_bck == 0 && s->proxy->srv_act == 0)
4816 set_backend_down(s->proxy);
4817 }
4818 else if ((s->cur_admin & SRV_ADMF_DRAIN) && !(s->next_admin & SRV_ADMF_DRAIN)) {
4819 /* OK completely leaving drain mode */
4820 if (s->proxy->srv_bck == 0 && s->proxy->srv_act == 0) {
4821 if (s->proxy->last_change < now.tv_sec) // ignore negative times
4822 s->proxy->down_time += now.tv_sec - s->proxy->last_change;
4823 s->proxy->last_change = now.tv_sec;
4824 }
4825
4826 if (s->last_change < now.tv_sec) // ignore negative times
4827 s->down_time += now.tv_sec - s->last_change;
4828 s->last_change = now.tv_sec;
4829 server_recalc_eweight(s);
4830
4831 tmptrash = alloc_trash_chunk();
4832 if (tmptrash) {
4833 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4834 chunk_printf(tmptrash,
4835 "%sServer %s/%s is %s (leaving forced drain)",
4836 s->flags & SRV_F_BACKUP ? "Backup " : "",
4837 s->proxy->id, s->id,
4838 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4839 }
4840 else {
4841 chunk_printf(tmptrash,
4842 "%sServer %s/%s is %s (leaving drain)",
4843 s->flags & SRV_F_BACKUP ? "Backup " : "",
4844 s->proxy->id, s->id,
4845 (s->next_state == SRV_ST_STOPPED) ? "DOWN" : "UP");
4846 if (s->track) /* normally it's mandatory here */
4847 chunk_appendf(tmptrash, " via %s/%s",
4848 s->track->proxy->id, s->track->id);
4849 }
4850
4851 Warning("%s.\n", tmptrash->str);
4852 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4853 free_trash_chunk(tmptrash);
4854 tmptrash = NULL;
4855 }
4856
4857 /* now propagate the status change to any LB algorithms */
4858 if (px->lbprm.update_server_eweight)
4859 px->lbprm.update_server_eweight(s);
4860 else if (srv_willbe_usable(s)) {
4861 if (px->lbprm.set_server_status_up)
4862 px->lbprm.set_server_status_up(s);
4863 }
4864 else {
4865 if (px->lbprm.set_server_status_down)
4866 px->lbprm.set_server_status_down(s);
4867 }
4868 }
4869 else if ((s->next_admin & SRV_ADMF_DRAIN)) {
4870 /* remaining in drain mode after removing one of its flags */
4871
4872 tmptrash = alloc_trash_chunk();
4873 if (tmptrash) {
4874 if (!(s->next_admin & SRV_ADMF_FDRAIN)) {
4875 chunk_printf(tmptrash,
4876 "%sServer %s/%s is leaving forced drain but remains in drain mode",
4877 s->flags & SRV_F_BACKUP ? "Backup " : "",
4878 s->proxy->id, s->id);
4879
4880 if (s->track) /* normally it's mandatory here */
4881 chunk_appendf(tmptrash, " via %s/%s",
4882 s->track->proxy->id, s->track->id);
4883 }
4884 else {
4885 chunk_printf(tmptrash,
4886 "%sServer %s/%s remains in forced drain mode",
4887 s->flags & SRV_F_BACKUP ? "Backup " : "",
4888 s->proxy->id, s->id);
4889 }
4890 Warning("%s.\n", tmptrash->str);
4891 send_log(s->proxy, LOG_NOTICE, "%s.\n", tmptrash->str);
4892 free_trash_chunk(tmptrash);
4893 tmptrash = NULL;
4894 }
4895
4896 /* commit new admin status */
4897
4898 s->cur_admin = s->next_admin;
4899 }
4900 }
4901
4902 /* Re-set log strings to empty */
4903 *s->op_st_chg_reason = 0;
4904 *s->adm_st_chg_cause = 0;
4905}
4906/*
4907 * This function loops on servers registered for asynchronous
4908 * status changes
4909 */
4910void servers_update_status(void) {
4911 struct server *s, *stmp;
4912
4913 list_for_each_entry_safe(s, stmp, &updated_servers, update_status) {
4914 srv_update_status(s);
4915 LIST_DEL(&s->update_status);
4916 LIST_INIT(&s->update_status);
4917 }
4918}
4919
Baptiste Assmanna68ca962015-04-14 01:15:08 +02004920/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02004921 * Local variables:
4922 * c-indent-level: 8
4923 * c-basic-offset: 8
4924 * End:
4925 */